]> granicus.if.org Git - postgresql/blob - src/backend/utils/fmgr/fmgr.c
Add a "provariadic" column to pg_proc to eliminate the remarkably expensive
[postgresql] / src / backend / utils / fmgr / fmgr.c
1 /*-------------------------------------------------------------------------
2  *
3  * fmgr.c
4  *        The Postgres function manager.
5  *
6  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/utils/fmgr/fmgr.c,v 1.121 2008/07/16 16:55:23 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #include "postgres.h"
17
18 #include "access/tuptoaster.h"
19 #include "catalog/pg_language.h"
20 #include "catalog/pg_proc.h"
21 #include "executor/functions.h"
22 #include "lib/stringinfo.h"
23 #include "miscadmin.h"
24 #include "parser/parse_expr.h"
25 #include "pgstat.h"
26 #include "utils/builtins.h"
27 #include "utils/fmgrtab.h"
28 #include "utils/guc.h"
29 #include "utils/lsyscache.h"
30 #include "utils/syscache.h"
31
32
33 /*
34  * Declaration for old-style function pointer type.  This is now used only
35  * in fmgr_oldstyle() and is no longer exported.
36  *
37  * The m68k SVR4 ABI defines that pointers are returned in %a0 instead of
38  * %d0. So if a function pointer is declared to return a pointer, the
39  * compiler may look only into %a0, but if the called function was declared
40  * to return an integer type, it puts its value only into %d0. So the
41  * caller doesn't pick up the correct return value. The solution is to
42  * declare the function pointer to return int, so the compiler picks up the
43  * return value from %d0. (Functions returning pointers put their value
44  * *additionally* into %d0 for compatibility.) The price is that there are
45  * some warnings about int->pointer conversions ... which we can suppress
46  * with suitably ugly casts in fmgr_oldstyle().
47  */
48 #if (defined(__mc68000__) || (defined(__m68k__))) && defined(__ELF__)
49 typedef int32 (*func_ptr) ();
50 #else
51 typedef char *(*func_ptr) ();
52 #endif
53
54 /*
55  * For an oldstyle function, fn_extra points to a record like this:
56  */
57 typedef struct
58 {
59         func_ptr        func;                   /* Address of the oldstyle function */
60         bool            arg_toastable[FUNC_MAX_ARGS];   /* is n'th arg of a toastable
61                                                                                                  * datatype? */
62 } Oldstyle_fnextra;
63
64 /*
65  * Hashtable for fast lookup of external C functions
66  */
67 typedef struct
68 {
69         /* fn_oid is the hash key and so must be first! */
70         Oid                     fn_oid;                 /* OID of an external C function */
71         TransactionId fn_xmin;          /* for checking up-to-dateness */
72         ItemPointerData fn_tid;
73         PGFunction      user_fn;                /* the function's address */
74         const Pg_finfo_record *inforec;         /* address of its info record */
75 } CFuncHashTabEntry;
76
77 static HTAB *CFuncHash = NULL;
78
79
80 static void fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt,
81                                            bool ignore_security);
82 static void fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple);
83 static void fmgr_info_other_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple);
84 static CFuncHashTabEntry *lookup_C_func(HeapTuple procedureTuple);
85 static void record_C_func(HeapTuple procedureTuple,
86                           PGFunction user_fn, const Pg_finfo_record *inforec);
87 static Datum fmgr_oldstyle(PG_FUNCTION_ARGS);
88 static Datum fmgr_security_definer(PG_FUNCTION_ARGS);
89
90
91 /*
92  * Lookup routines for builtin-function table.  We can search by either Oid
93  * or name, but search by Oid is much faster.
94  */
95
96 static const FmgrBuiltin *
97 fmgr_isbuiltin(Oid id)
98 {
99         int                     low = 0;
100         int                     high = fmgr_nbuiltins - 1;
101
102         /*
103          * Loop invariant: low is the first index that could contain target entry,
104          * and high is the last index that could contain it.
105          */
106         while (low <= high)
107         {
108                 int                     i = (high + low) / 2;
109                 const FmgrBuiltin *ptr = &fmgr_builtins[i];
110
111                 if (id == ptr->foid)
112                         return ptr;
113                 else if (id > ptr->foid)
114                         low = i + 1;
115                 else
116                         high = i - 1;
117         }
118         return NULL;
119 }
120
121 /*
122  * Lookup a builtin by name.  Note there can be more than one entry in
123  * the array with the same name, but they should all point to the same
124  * routine.
125  */
126 static const FmgrBuiltin *
127 fmgr_lookupByName(const char *name)
128 {
129         int                     i;
130
131         for (i = 0; i < fmgr_nbuiltins; i++)
132         {
133                 if (strcmp(name, fmgr_builtins[i].funcName) == 0)
134                         return fmgr_builtins + i;
135         }
136         return NULL;
137 }
138
139 /*
140  * This routine fills a FmgrInfo struct, given the OID
141  * of the function to be called.
142  *
143  * The caller's CurrentMemoryContext is used as the fn_mcxt of the info
144  * struct; this means that any subsidiary data attached to the info struct
145  * (either by fmgr_info itself, or later on by a function call handler)
146  * will be allocated in that context.  The caller must ensure that this
147  * context is at least as long-lived as the info struct itself.  This is
148  * not a problem in typical cases where the info struct is on the stack or
149  * in freshly-palloc'd space.  However, if one intends to store an info
150  * struct in a long-lived table, it's better to use fmgr_info_cxt.
151  */
152 void
153 fmgr_info(Oid functionId, FmgrInfo *finfo)
154 {
155         fmgr_info_cxt(functionId, finfo, CurrentMemoryContext);
156 }
157
158 /*
159  * Fill a FmgrInfo struct, specifying a memory context in which its
160  * subsidiary data should go.
161  */
162 void
163 fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
164 {
165         fmgr_info_cxt_security(functionId, finfo, mcxt, false);
166 }
167
168 /*
169  * This one does the actual work.  ignore_security is ordinarily false
170  * but is set to true by fmgr_security_definer to avoid recursion.
171  */
172 static void
173 fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt,
174                                            bool ignore_security)
175 {
176         const FmgrBuiltin *fbp;
177         HeapTuple       procedureTuple;
178         Form_pg_proc procedureStruct;
179         Datum           prosrcdatum;
180         bool            isnull;
181         char       *prosrc;
182
183         /*
184          * fn_oid *must* be filled in last.  Some code assumes that if fn_oid is
185          * valid, the whole struct is valid.  Some FmgrInfo struct's do survive
186          * elogs.
187          */
188         finfo->fn_oid = InvalidOid;
189         finfo->fn_extra = NULL;
190         finfo->fn_mcxt = mcxt;
191         finfo->fn_expr = NULL;          /* caller may set this later */
192
193         if ((fbp = fmgr_isbuiltin(functionId)) != NULL)
194         {
195                 /*
196                  * Fast path for builtin functions: don't bother consulting pg_proc
197                  */
198                 finfo->fn_nargs = fbp->nargs;
199                 finfo->fn_strict = fbp->strict;
200                 finfo->fn_retset = fbp->retset;
201                 finfo->fn_stats = TRACK_FUNC_ALL;       /* ie, never track */
202                 finfo->fn_addr = fbp->func;
203                 finfo->fn_oid = functionId;
204                 return;
205         }
206
207         /* Otherwise we need the pg_proc entry */
208         procedureTuple = SearchSysCache(PROCOID,
209                                                                         ObjectIdGetDatum(functionId),
210                                                                         0, 0, 0);
211         if (!HeapTupleIsValid(procedureTuple))
212                 elog(ERROR, "cache lookup failed for function %u", functionId);
213         procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
214
215         finfo->fn_nargs = procedureStruct->pronargs;
216         finfo->fn_strict = procedureStruct->proisstrict;
217         finfo->fn_retset = procedureStruct->proretset;
218
219         /*
220          * If it has prosecdef set, or non-null proconfig, use
221          * fmgr_security_definer call handler --- unless we are being called
222          * again by fmgr_security_definer.
223          *
224          * When using fmgr_security_definer, function stats tracking is always
225          * disabled at the outer level, and instead we set the flag properly
226          * in fmgr_security_definer's private flinfo and implement the tracking
227          * inside fmgr_security_definer.  This loses the ability to charge the
228          * overhead of fmgr_security_definer to the function, but gains the
229          * ability to set the track_functions GUC as a local GUC parameter of
230          * an interesting function and have the right things happen.
231          */
232         if (!ignore_security &&
233                 (procedureStruct->prosecdef ||
234                  !heap_attisnull(procedureTuple, Anum_pg_proc_proconfig)))
235         {
236                 finfo->fn_addr = fmgr_security_definer;
237                 finfo->fn_stats = TRACK_FUNC_ALL;       /* ie, never track */
238                 finfo->fn_oid = functionId;
239                 ReleaseSysCache(procedureTuple);
240                 return;
241         }
242
243         switch (procedureStruct->prolang)
244         {
245                 case INTERNALlanguageId:
246
247                         /*
248                          * For an ordinary builtin function, we should never get here
249                          * because the isbuiltin() search above will have succeeded.
250                          * However, if the user has done a CREATE FUNCTION to create an
251                          * alias for a builtin function, we can end up here.  In that case
252                          * we have to look up the function by name.  The name of the
253                          * internal function is stored in prosrc (it doesn't have to be
254                          * the same as the name of the alias!)
255                          */
256                         prosrcdatum = SysCacheGetAttr(PROCOID, procedureTuple,
257                                                                                   Anum_pg_proc_prosrc, &isnull);
258                         if (isnull)
259                                 elog(ERROR, "null prosrc");
260                         prosrc = TextDatumGetCString(prosrcdatum);
261                         fbp = fmgr_lookupByName(prosrc);
262                         if (fbp == NULL)
263                                 ereport(ERROR,
264                                                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
265                                                  errmsg("internal function \"%s\" is not in internal lookup table",
266                                                                 prosrc)));
267                         pfree(prosrc);
268                         /* Should we check that nargs, strict, retset match the table? */
269                         finfo->fn_addr = fbp->func;
270                         /* note this policy is also assumed in fast path above */
271                         finfo->fn_stats = TRACK_FUNC_ALL;       /* ie, never track */
272                         break;
273
274                 case ClanguageId:
275                         fmgr_info_C_lang(functionId, finfo, procedureTuple);
276                         finfo->fn_stats = TRACK_FUNC_PL;        /* ie, track if ALL */
277                         break;
278
279                 case SQLlanguageId:
280                         finfo->fn_addr = fmgr_sql;
281                         finfo->fn_stats = TRACK_FUNC_PL;        /* ie, track if ALL */
282                         break;
283
284                 default:
285                         fmgr_info_other_lang(functionId, finfo, procedureTuple);
286                         finfo->fn_stats = TRACK_FUNC_OFF;       /* ie, track if not OFF */
287                         break;
288         }
289
290         finfo->fn_oid = functionId;
291         ReleaseSysCache(procedureTuple);
292 }
293
294 /*
295  * Special fmgr_info processing for C-language functions.  Note that
296  * finfo->fn_oid is not valid yet.
297  */
298 static void
299 fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
300 {
301         Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
302         CFuncHashTabEntry *hashentry;
303         PGFunction      user_fn;
304         const Pg_finfo_record *inforec;
305         Oldstyle_fnextra *fnextra;
306         bool            isnull;
307         int                     i;
308
309         /*
310          * See if we have the function address cached already
311          */
312         hashentry = lookup_C_func(procedureTuple);
313         if (hashentry)
314         {
315                 user_fn = hashentry->user_fn;
316                 inforec = hashentry->inforec;
317         }
318         else
319         {
320                 Datum           prosrcattr,
321                                         probinattr;
322                 char       *prosrcstring,
323                                    *probinstring;
324                 void       *libraryhandle;
325
326                 /*
327                  * Get prosrc and probin strings (link symbol and library filename).
328                  * While in general these columns might be null, that's not allowed
329                  * for C-language functions.
330                  */
331                 prosrcattr = SysCacheGetAttr(PROCOID, procedureTuple,
332                                                                          Anum_pg_proc_prosrc, &isnull);
333                 if (isnull)
334                         elog(ERROR, "null prosrc for C function %u", functionId);
335                 prosrcstring = TextDatumGetCString(prosrcattr);
336
337                 probinattr = SysCacheGetAttr(PROCOID, procedureTuple,
338                                                                          Anum_pg_proc_probin, &isnull);
339                 if (isnull)
340                         elog(ERROR, "null probin for C function %u", functionId);
341                 probinstring = TextDatumGetCString(probinattr);
342
343                 /* Look up the function itself */
344                 user_fn = load_external_function(probinstring, prosrcstring, true,
345                                                                                  &libraryhandle);
346
347                 /* Get the function information record (real or default) */
348                 inforec = fetch_finfo_record(libraryhandle, prosrcstring);
349
350                 /* Cache the addresses for later calls */
351                 record_C_func(procedureTuple, user_fn, inforec);
352
353                 pfree(prosrcstring);
354                 pfree(probinstring);
355         }
356
357         switch (inforec->api_version)
358         {
359                 case 0:
360                         /* Old style: need to use a handler */
361                         finfo->fn_addr = fmgr_oldstyle;
362                         fnextra = (Oldstyle_fnextra *)
363                                 MemoryContextAllocZero(finfo->fn_mcxt,
364                                                                            sizeof(Oldstyle_fnextra));
365                         finfo->fn_extra = (void *) fnextra;
366                         fnextra->func = (func_ptr) user_fn;
367                         for (i = 0; i < procedureStruct->pronargs; i++)
368                         {
369                                 fnextra->arg_toastable[i] =
370                                         TypeIsToastable(procedureStruct->proargtypes.values[i]);
371                         }
372                         break;
373                 case 1:
374                         /* New style: call directly */
375                         finfo->fn_addr = user_fn;
376                         break;
377                 default:
378                         /* Shouldn't get here if fetch_finfo_record did its job */
379                         elog(ERROR, "unrecognized function API version: %d",
380                                  inforec->api_version);
381                         break;
382         }
383 }
384
385 /*
386  * Special fmgr_info processing for other-language functions.  Note
387  * that finfo->fn_oid is not valid yet.
388  */
389 static void
390 fmgr_info_other_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
391 {
392         Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
393         Oid                     language = procedureStruct->prolang;
394         HeapTuple       languageTuple;
395         Form_pg_language languageStruct;
396         FmgrInfo        plfinfo;
397
398         languageTuple = SearchSysCache(LANGOID,
399                                                                    ObjectIdGetDatum(language),
400                                                                    0, 0, 0);
401         if (!HeapTupleIsValid(languageTuple))
402                 elog(ERROR, "cache lookup failed for language %u", language);
403         languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
404
405         fmgr_info(languageStruct->lanplcallfoid, &plfinfo);
406         finfo->fn_addr = plfinfo.fn_addr;
407
408         /*
409          * If lookup of the PL handler function produced nonnull fn_extra,
410          * complain --- it must be an oldstyle function! We no longer support
411          * oldstyle PL handlers.
412          */
413         if (plfinfo.fn_extra != NULL)
414                 elog(ERROR, "language %u has old-style handler", language);
415
416         ReleaseSysCache(languageTuple);
417 }
418
419 /*
420  * Fetch and validate the information record for the given external function.
421  * The function is specified by a handle for the containing library
422  * (obtained from load_external_function) as well as the function name.
423  *
424  * If no info function exists for the given name, it is not an error.
425  * Instead we return a default info record for a version-0 function.
426  * We want to raise an error here only if the info function returns
427  * something bogus.
428  *
429  * This function is broken out of fmgr_info_C_lang so that fmgr_c_validator
430  * can validate the information record for a function not yet entered into
431  * pg_proc.
432  */
433 const Pg_finfo_record *
434 fetch_finfo_record(void *filehandle, char *funcname)
435 {
436         char       *infofuncname;
437         PGFInfoFunction infofunc;
438         const Pg_finfo_record *inforec;
439         static Pg_finfo_record default_inforec = {0};
440
441         /* Compute name of info func */
442         infofuncname = (char *) palloc(strlen(funcname) + 10);
443         strcpy(infofuncname, "pg_finfo_");
444         strcat(infofuncname, funcname);
445
446         /* Try to look up the info function */
447         infofunc = (PGFInfoFunction) lookup_external_function(filehandle,
448                                                                                                                   infofuncname);
449         if (infofunc == NULL)
450         {
451                 /* Not found --- assume version 0 */
452                 pfree(infofuncname);
453                 return &default_inforec;
454         }
455
456         /* Found, so call it */
457         inforec = (*infofunc) ();
458
459         /* Validate result as best we can */
460         if (inforec == NULL)
461                 elog(ERROR, "null result from info function \"%s\"", infofuncname);
462         switch (inforec->api_version)
463         {
464                 case 0:
465                 case 1:
466                         /* OK, no additional fields to validate */
467                         break;
468                 default:
469                         ereport(ERROR,
470                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
471                                          errmsg("unrecognized API version %d reported by info function \"%s\"",
472                                                         inforec->api_version, infofuncname)));
473                         break;
474         }
475
476         pfree(infofuncname);
477         return inforec;
478 }
479
480
481 /*-------------------------------------------------------------------------
482  *              Routines for caching lookup information for external C functions.
483  *
484  * The routines in dfmgr.c are relatively slow, so we try to avoid running
485  * them more than once per external function per session.  We use a hash table
486  * with the function OID as the lookup key.
487  *-------------------------------------------------------------------------
488  */
489
490 /*
491  * lookup_C_func: try to find a C function in the hash table
492  *
493  * If an entry exists and is up to date, return it; else return NULL
494  */
495 static CFuncHashTabEntry *
496 lookup_C_func(HeapTuple procedureTuple)
497 {
498         Oid                     fn_oid = HeapTupleGetOid(procedureTuple);
499         CFuncHashTabEntry *entry;
500
501         if (CFuncHash == NULL)
502                 return NULL;                    /* no table yet */
503         entry = (CFuncHashTabEntry *)
504                 hash_search(CFuncHash,
505                                         &fn_oid,
506                                         HASH_FIND,
507                                         NULL);
508         if (entry == NULL)
509                 return NULL;                    /* no such entry */
510         if (entry->fn_xmin == HeapTupleHeaderGetXmin(procedureTuple->t_data) &&
511                 ItemPointerEquals(&entry->fn_tid, &procedureTuple->t_self))
512                 return entry;                   /* OK */
513         return NULL;                            /* entry is out of date */
514 }
515
516 /*
517  * record_C_func: enter (or update) info about a C function in the hash table
518  */
519 static void
520 record_C_func(HeapTuple procedureTuple,
521                           PGFunction user_fn, const Pg_finfo_record *inforec)
522 {
523         Oid                     fn_oid = HeapTupleGetOid(procedureTuple);
524         CFuncHashTabEntry *entry;
525         bool            found;
526
527         /* Create the hash table if it doesn't exist yet */
528         if (CFuncHash == NULL)
529         {
530                 HASHCTL         hash_ctl;
531
532                 MemSet(&hash_ctl, 0, sizeof(hash_ctl));
533                 hash_ctl.keysize = sizeof(Oid);
534                 hash_ctl.entrysize = sizeof(CFuncHashTabEntry);
535                 hash_ctl.hash = oid_hash;
536                 CFuncHash = hash_create("CFuncHash",
537                                                                 100,
538                                                                 &hash_ctl,
539                                                                 HASH_ELEM | HASH_FUNCTION);
540         }
541
542         entry = (CFuncHashTabEntry *)
543                 hash_search(CFuncHash,
544                                         &fn_oid,
545                                         HASH_ENTER,
546                                         &found);
547         /* OID is already filled in */
548         entry->fn_xmin = HeapTupleHeaderGetXmin(procedureTuple->t_data);
549         entry->fn_tid = procedureTuple->t_self;
550         entry->user_fn = user_fn;
551         entry->inforec = inforec;
552 }
553
554 /*
555  * clear_external_function_hash: remove entries for a library being closed
556  *
557  * Presently we just zap the entire hash table, but later it might be worth
558  * the effort to remove only the entries associated with the given handle.
559  */
560 void
561 clear_external_function_hash(void *filehandle)
562 {
563         if (CFuncHash)
564                 hash_destroy(CFuncHash);
565         CFuncHash = NULL;
566 }
567
568
569 /*
570  * Copy an FmgrInfo struct
571  *
572  * This is inherently somewhat bogus since we can't reliably duplicate
573  * language-dependent subsidiary info.  We cheat by zeroing fn_extra,
574  * instead, meaning that subsidiary info will have to be recomputed.
575  */
576 void
577 fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo,
578                            MemoryContext destcxt)
579 {
580         memcpy(dstinfo, srcinfo, sizeof(FmgrInfo));
581         dstinfo->fn_mcxt = destcxt;
582         if (dstinfo->fn_addr == fmgr_oldstyle)
583         {
584                 /* For oldstyle functions we must copy fn_extra */
585                 Oldstyle_fnextra *fnextra;
586
587                 fnextra = (Oldstyle_fnextra *)
588                         MemoryContextAlloc(destcxt, sizeof(Oldstyle_fnextra));
589                 memcpy(fnextra, srcinfo->fn_extra, sizeof(Oldstyle_fnextra));
590                 dstinfo->fn_extra = (void *) fnextra;
591         }
592         else
593                 dstinfo->fn_extra = NULL;
594 }
595
596
597 /*
598  * Specialized lookup routine for fmgr_internal_validator: given the alleged
599  * name of an internal function, return the OID of the function.
600  * If the name is not recognized, return InvalidOid.
601  */
602 Oid
603 fmgr_internal_function(const char *proname)
604 {
605         const FmgrBuiltin *fbp = fmgr_lookupByName(proname);
606
607         if (fbp == NULL)
608                 return InvalidOid;
609         return fbp->foid;
610 }
611
612
613 /*
614  * Handler for old-style "C" language functions
615  */
616 static Datum
617 fmgr_oldstyle(PG_FUNCTION_ARGS)
618 {
619         Oldstyle_fnextra *fnextra;
620         int                     n_arguments = fcinfo->nargs;
621         int                     i;
622         bool            isnull;
623         func_ptr        user_fn;
624         char       *returnValue;
625
626         if (fcinfo->flinfo == NULL || fcinfo->flinfo->fn_extra == NULL)
627                 elog(ERROR, "fmgr_oldstyle received NULL pointer");
628         fnextra = (Oldstyle_fnextra *) fcinfo->flinfo->fn_extra;
629
630         /*
631          * Result is NULL if any argument is NULL, but we still call the function
632          * (peculiar, but that's the way it worked before, and after all this is a
633          * backwards-compatibility wrapper).  Note, however, that we'll never get
634          * here with NULL arguments if the function is marked strict.
635          *
636          * We also need to detoast any TOAST-ed inputs, since it's unlikely that
637          * an old-style function knows about TOASTing.
638          */
639         isnull = false;
640         for (i = 0; i < n_arguments; i++)
641         {
642                 if (PG_ARGISNULL(i))
643                         isnull = true;
644                 else if (fnextra->arg_toastable[i])
645                         fcinfo->arg[i] = PointerGetDatum(PG_DETOAST_DATUM(fcinfo->arg[i]));
646         }
647         fcinfo->isnull = isnull;
648
649         user_fn = fnextra->func;
650
651         switch (n_arguments)
652         {
653                 case 0:
654                         returnValue = (char *) (*user_fn) ();
655                         break;
656                 case 1:
657
658                         /*
659                          * nullvalue() used to use isNull to check if arg is NULL; perhaps
660                          * there are other functions still out there that also rely on
661                          * this undocumented hack?
662                          */
663                         returnValue = (char *) (*user_fn) (fcinfo->arg[0],
664                                                                                            &fcinfo->isnull);
665                         break;
666                 case 2:
667                         returnValue = (char *) (*user_fn) (fcinfo->arg[0],
668                                                                                            fcinfo->arg[1]);
669                         break;
670                 case 3:
671                         returnValue = (char *) (*user_fn) (fcinfo->arg[0],
672                                                                                            fcinfo->arg[1],
673                                                                                            fcinfo->arg[2]);
674                         break;
675                 case 4:
676                         returnValue = (char *) (*user_fn) (fcinfo->arg[0],
677                                                                                            fcinfo->arg[1],
678                                                                                            fcinfo->arg[2],
679                                                                                            fcinfo->arg[3]);
680                         break;
681                 case 5:
682                         returnValue = (char *) (*user_fn) (fcinfo->arg[0],
683                                                                                            fcinfo->arg[1],
684                                                                                            fcinfo->arg[2],
685                                                                                            fcinfo->arg[3],
686                                                                                            fcinfo->arg[4]);
687                         break;
688                 case 6:
689                         returnValue = (char *) (*user_fn) (fcinfo->arg[0],
690                                                                                            fcinfo->arg[1],
691                                                                                            fcinfo->arg[2],
692                                                                                            fcinfo->arg[3],
693                                                                                            fcinfo->arg[4],
694                                                                                            fcinfo->arg[5]);
695                         break;
696                 case 7:
697                         returnValue = (char *) (*user_fn) (fcinfo->arg[0],
698                                                                                            fcinfo->arg[1],
699                                                                                            fcinfo->arg[2],
700                                                                                            fcinfo->arg[3],
701                                                                                            fcinfo->arg[4],
702                                                                                            fcinfo->arg[5],
703                                                                                            fcinfo->arg[6]);
704                         break;
705                 case 8:
706                         returnValue = (char *) (*user_fn) (fcinfo->arg[0],
707                                                                                            fcinfo->arg[1],
708                                                                                            fcinfo->arg[2],
709                                                                                            fcinfo->arg[3],
710                                                                                            fcinfo->arg[4],
711                                                                                            fcinfo->arg[5],
712                                                                                            fcinfo->arg[6],
713                                                                                            fcinfo->arg[7]);
714                         break;
715                 case 9:
716                         returnValue = (char *) (*user_fn) (fcinfo->arg[0],
717                                                                                            fcinfo->arg[1],
718                                                                                            fcinfo->arg[2],
719                                                                                            fcinfo->arg[3],
720                                                                                            fcinfo->arg[4],
721                                                                                            fcinfo->arg[5],
722                                                                                            fcinfo->arg[6],
723                                                                                            fcinfo->arg[7],
724                                                                                            fcinfo->arg[8]);
725                         break;
726                 case 10:
727                         returnValue = (char *) (*user_fn) (fcinfo->arg[0],
728                                                                                            fcinfo->arg[1],
729                                                                                            fcinfo->arg[2],
730                                                                                            fcinfo->arg[3],
731                                                                                            fcinfo->arg[4],
732                                                                                            fcinfo->arg[5],
733                                                                                            fcinfo->arg[6],
734                                                                                            fcinfo->arg[7],
735                                                                                            fcinfo->arg[8],
736                                                                                            fcinfo->arg[9]);
737                         break;
738                 case 11:
739                         returnValue = (char *) (*user_fn) (fcinfo->arg[0],
740                                                                                            fcinfo->arg[1],
741                                                                                            fcinfo->arg[2],
742                                                                                            fcinfo->arg[3],
743                                                                                            fcinfo->arg[4],
744                                                                                            fcinfo->arg[5],
745                                                                                            fcinfo->arg[6],
746                                                                                            fcinfo->arg[7],
747                                                                                            fcinfo->arg[8],
748                                                                                            fcinfo->arg[9],
749                                                                                            fcinfo->arg[10]);
750                         break;
751                 case 12:
752                         returnValue = (char *) (*user_fn) (fcinfo->arg[0],
753                                                                                            fcinfo->arg[1],
754                                                                                            fcinfo->arg[2],
755                                                                                            fcinfo->arg[3],
756                                                                                            fcinfo->arg[4],
757                                                                                            fcinfo->arg[5],
758                                                                                            fcinfo->arg[6],
759                                                                                            fcinfo->arg[7],
760                                                                                            fcinfo->arg[8],
761                                                                                            fcinfo->arg[9],
762                                                                                            fcinfo->arg[10],
763                                                                                            fcinfo->arg[11]);
764                         break;
765                 case 13:
766                         returnValue = (char *) (*user_fn) (fcinfo->arg[0],
767                                                                                            fcinfo->arg[1],
768                                                                                            fcinfo->arg[2],
769                                                                                            fcinfo->arg[3],
770                                                                                            fcinfo->arg[4],
771                                                                                            fcinfo->arg[5],
772                                                                                            fcinfo->arg[6],
773                                                                                            fcinfo->arg[7],
774                                                                                            fcinfo->arg[8],
775                                                                                            fcinfo->arg[9],
776                                                                                            fcinfo->arg[10],
777                                                                                            fcinfo->arg[11],
778                                                                                            fcinfo->arg[12]);
779                         break;
780                 case 14:
781                         returnValue = (char *) (*user_fn) (fcinfo->arg[0],
782                                                                                            fcinfo->arg[1],
783                                                                                            fcinfo->arg[2],
784                                                                                            fcinfo->arg[3],
785                                                                                            fcinfo->arg[4],
786                                                                                            fcinfo->arg[5],
787                                                                                            fcinfo->arg[6],
788                                                                                            fcinfo->arg[7],
789                                                                                            fcinfo->arg[8],
790                                                                                            fcinfo->arg[9],
791                                                                                            fcinfo->arg[10],
792                                                                                            fcinfo->arg[11],
793                                                                                            fcinfo->arg[12],
794                                                                                            fcinfo->arg[13]);
795                         break;
796                 case 15:
797                         returnValue = (char *) (*user_fn) (fcinfo->arg[0],
798                                                                                            fcinfo->arg[1],
799                                                                                            fcinfo->arg[2],
800                                                                                            fcinfo->arg[3],
801                                                                                            fcinfo->arg[4],
802                                                                                            fcinfo->arg[5],
803                                                                                            fcinfo->arg[6],
804                                                                                            fcinfo->arg[7],
805                                                                                            fcinfo->arg[8],
806                                                                                            fcinfo->arg[9],
807                                                                                            fcinfo->arg[10],
808                                                                                            fcinfo->arg[11],
809                                                                                            fcinfo->arg[12],
810                                                                                            fcinfo->arg[13],
811                                                                                            fcinfo->arg[14]);
812                         break;
813                 case 16:
814                         returnValue = (char *) (*user_fn) (fcinfo->arg[0],
815                                                                                            fcinfo->arg[1],
816                                                                                            fcinfo->arg[2],
817                                                                                            fcinfo->arg[3],
818                                                                                            fcinfo->arg[4],
819                                                                                            fcinfo->arg[5],
820                                                                                            fcinfo->arg[6],
821                                                                                            fcinfo->arg[7],
822                                                                                            fcinfo->arg[8],
823                                                                                            fcinfo->arg[9],
824                                                                                            fcinfo->arg[10],
825                                                                                            fcinfo->arg[11],
826                                                                                            fcinfo->arg[12],
827                                                                                            fcinfo->arg[13],
828                                                                                            fcinfo->arg[14],
829                                                                                            fcinfo->arg[15]);
830                         break;
831                 default:
832
833                         /*
834                          * Increasing FUNC_MAX_ARGS doesn't automatically add cases to the
835                          * above code, so mention the actual value in this error not
836                          * FUNC_MAX_ARGS.  You could add cases to the above if you needed
837                          * to support old-style functions with many arguments, but making
838                          * 'em be new-style is probably a better idea.
839                          */
840                         ereport(ERROR,
841                                         (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
842                          errmsg("function %u has too many arguments (%d, maximum is %d)",
843                                         fcinfo->flinfo->fn_oid, n_arguments, 16)));
844                         returnValue = NULL; /* keep compiler quiet */
845                         break;
846         }
847
848         return PointerGetDatum(returnValue);
849 }
850
851
852 /*
853  * Support for security-definer and proconfig-using functions.  We support
854  * both of these features using the same call handler, because they are
855  * often used together and it would be inefficient (as well as notationally
856  * messy) to have two levels of call handler involved.
857  */
858 struct fmgr_security_definer_cache
859 {
860         FmgrInfo        flinfo;                 /* lookup info for target function */
861         Oid                     userid;                 /* userid to set, or InvalidOid */
862         ArrayType  *proconfig;          /* GUC values to set, or NULL */
863 };
864
865 /*
866  * Function handler for security-definer/proconfig functions.  We extract the
867  * OID of the actual function and do a fmgr lookup again.  Then we fetch the
868  * pg_proc row and copy the owner ID and proconfig fields.      (All this info
869  * is cached for the duration of the current query.)  To execute a call,
870  * we temporarily replace the flinfo with the cached/looked-up one, while
871  * keeping the outer fcinfo (which contains all the actual arguments, etc.)
872  * intact.      This is not re-entrant, but then the fcinfo itself can't be used
873  * re-entrantly anyway.
874  */
875 static Datum
876 fmgr_security_definer(PG_FUNCTION_ARGS)
877 {
878         Datum           result;
879         struct fmgr_security_definer_cache *volatile fcache;
880         FmgrInfo   *save_flinfo;
881         Oid                     save_userid;
882         bool            save_secdefcxt;
883         volatile int save_nestlevel;
884         PgStat_FunctionCallUsage fcusage;
885
886         if (!fcinfo->flinfo->fn_extra)
887         {
888                 HeapTuple       tuple;
889                 Form_pg_proc procedureStruct;
890                 Datum           datum;
891                 bool            isnull;
892                 MemoryContext oldcxt;
893
894                 fcache = MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt,
895                                                                                 sizeof(*fcache));
896
897                 fmgr_info_cxt_security(fcinfo->flinfo->fn_oid, &fcache->flinfo,
898                                                            fcinfo->flinfo->fn_mcxt, true);
899                 fcache->flinfo.fn_expr = fcinfo->flinfo->fn_expr;
900
901                 tuple = SearchSysCache(PROCOID,
902                                                            ObjectIdGetDatum(fcinfo->flinfo->fn_oid),
903                                                            0, 0, 0);
904                 if (!HeapTupleIsValid(tuple))
905                         elog(ERROR, "cache lookup failed for function %u",
906                                  fcinfo->flinfo->fn_oid);
907                 procedureStruct = (Form_pg_proc) GETSTRUCT(tuple);
908
909                 if (procedureStruct->prosecdef)
910                         fcache->userid = procedureStruct->proowner;
911
912                 datum = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_proconfig,
913                                                                 &isnull);
914                 if (!isnull)
915                 {
916                         oldcxt = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
917                         fcache->proconfig = DatumGetArrayTypePCopy(datum);
918                         MemoryContextSwitchTo(oldcxt);
919                 }
920
921                 ReleaseSysCache(tuple);
922
923                 fcinfo->flinfo->fn_extra = fcache;
924         }
925         else
926                 fcache = fcinfo->flinfo->fn_extra;
927
928         /* GetUserIdAndContext is cheap enough that no harm in a wasted call */
929         GetUserIdAndContext(&save_userid, &save_secdefcxt);
930         if (fcache->proconfig)          /* Need a new GUC nesting level */
931                 save_nestlevel = NewGUCNestLevel();
932         else
933                 save_nestlevel = 0;             /* keep compiler quiet */
934
935         if (OidIsValid(fcache->userid))
936                 SetUserIdAndContext(fcache->userid, true);
937
938         if (fcache->proconfig)
939         {
940                 ProcessGUCArray(fcache->proconfig,
941                                                 (superuser() ? PGC_SUSET : PGC_USERSET),
942                                                 PGC_S_SESSION,
943                                                 GUC_ACTION_SAVE);
944         }
945
946         /*
947          * We don't need to restore GUC or userid settings on error, because the
948          * ensuing xact or subxact abort will do that.  The PG_TRY block is only
949          * needed to clean up the flinfo link.
950          */
951         save_flinfo = fcinfo->flinfo;
952
953         PG_TRY();
954         {
955                 fcinfo->flinfo = &fcache->flinfo;
956
957                 /* See notes in fmgr_info_cxt_security */
958                 pgstat_init_function_usage(fcinfo, &fcusage);
959
960                 result = FunctionCallInvoke(fcinfo);
961
962                 /*
963                  * We could be calling either a regular or a set-returning function,
964                  * so we have to test to see what finalize flag to use.
965                  */
966                 pgstat_end_function_usage(&fcusage,
967                                                                   (fcinfo->resultinfo == NULL ||
968                                                                    !IsA(fcinfo->resultinfo, ReturnSetInfo) ||
969                                                                    ((ReturnSetInfo *) fcinfo->resultinfo)->isDone != ExprMultipleResult));
970         }
971         PG_CATCH();
972         {
973                 fcinfo->flinfo = save_flinfo;
974                 PG_RE_THROW();
975         }
976         PG_END_TRY();
977
978         fcinfo->flinfo = save_flinfo;
979
980         if (fcache->proconfig)
981                 AtEOXact_GUC(true, save_nestlevel);
982         if (OidIsValid(fcache->userid))
983                 SetUserIdAndContext(save_userid, save_secdefcxt);
984
985         return result;
986 }
987
988
989 /*-------------------------------------------------------------------------
990  *              Support routines for callers of fmgr-compatible functions
991  *-------------------------------------------------------------------------
992  */
993
994 /*
995  * These are for invocation of a specifically named function with a
996  * directly-computed parameter list.  Note that neither arguments nor result
997  * are allowed to be NULL.      Also, the function cannot be one that needs to
998  * look at FmgrInfo, since there won't be any.
999  */
1000 Datum
1001 DirectFunctionCall1(PGFunction func, Datum arg1)
1002 {
1003         FunctionCallInfoData fcinfo;
1004         Datum           result;
1005
1006         InitFunctionCallInfoData(fcinfo, NULL, 1, NULL, NULL);
1007
1008         fcinfo.arg[0] = arg1;
1009         fcinfo.argnull[0] = false;
1010
1011         result = (*func) (&fcinfo);
1012
1013         /* Check for null result, since caller is clearly not expecting one */
1014         if (fcinfo.isnull)
1015                 elog(ERROR, "function %p returned NULL", (void *) func);
1016
1017         return result;
1018 }
1019
1020 Datum
1021 DirectFunctionCall2(PGFunction func, Datum arg1, Datum arg2)
1022 {
1023         FunctionCallInfoData fcinfo;
1024         Datum           result;
1025
1026         InitFunctionCallInfoData(fcinfo, NULL, 2, NULL, NULL);
1027
1028         fcinfo.arg[0] = arg1;
1029         fcinfo.arg[1] = arg2;
1030         fcinfo.argnull[0] = false;
1031         fcinfo.argnull[1] = false;
1032
1033         result = (*func) (&fcinfo);
1034
1035         /* Check for null result, since caller is clearly not expecting one */
1036         if (fcinfo.isnull)
1037                 elog(ERROR, "function %p returned NULL", (void *) func);
1038
1039         return result;
1040 }
1041
1042 Datum
1043 DirectFunctionCall3(PGFunction func, Datum arg1, Datum arg2,
1044                                         Datum arg3)
1045 {
1046         FunctionCallInfoData fcinfo;
1047         Datum           result;
1048
1049         InitFunctionCallInfoData(fcinfo, NULL, 3, NULL, NULL);
1050
1051         fcinfo.arg[0] = arg1;
1052         fcinfo.arg[1] = arg2;
1053         fcinfo.arg[2] = arg3;
1054         fcinfo.argnull[0] = false;
1055         fcinfo.argnull[1] = false;
1056         fcinfo.argnull[2] = false;
1057
1058         result = (*func) (&fcinfo);
1059
1060         /* Check for null result, since caller is clearly not expecting one */
1061         if (fcinfo.isnull)
1062                 elog(ERROR, "function %p returned NULL", (void *) func);
1063
1064         return result;
1065 }
1066
1067 Datum
1068 DirectFunctionCall4(PGFunction func, Datum arg1, Datum arg2,
1069                                         Datum arg3, Datum arg4)
1070 {
1071         FunctionCallInfoData fcinfo;
1072         Datum           result;
1073
1074         InitFunctionCallInfoData(fcinfo, NULL, 4, NULL, NULL);
1075
1076         fcinfo.arg[0] = arg1;
1077         fcinfo.arg[1] = arg2;
1078         fcinfo.arg[2] = arg3;
1079         fcinfo.arg[3] = arg4;
1080         fcinfo.argnull[0] = false;
1081         fcinfo.argnull[1] = false;
1082         fcinfo.argnull[2] = false;
1083         fcinfo.argnull[3] = false;
1084
1085         result = (*func) (&fcinfo);
1086
1087         /* Check for null result, since caller is clearly not expecting one */
1088         if (fcinfo.isnull)
1089                 elog(ERROR, "function %p returned NULL", (void *) func);
1090
1091         return result;
1092 }
1093
1094 Datum
1095 DirectFunctionCall5(PGFunction func, Datum arg1, Datum arg2,
1096                                         Datum arg3, Datum arg4, Datum arg5)
1097 {
1098         FunctionCallInfoData fcinfo;
1099         Datum           result;
1100
1101         InitFunctionCallInfoData(fcinfo, NULL, 5, NULL, NULL);
1102
1103         fcinfo.arg[0] = arg1;
1104         fcinfo.arg[1] = arg2;
1105         fcinfo.arg[2] = arg3;
1106         fcinfo.arg[3] = arg4;
1107         fcinfo.arg[4] = arg5;
1108         fcinfo.argnull[0] = false;
1109         fcinfo.argnull[1] = false;
1110         fcinfo.argnull[2] = false;
1111         fcinfo.argnull[3] = false;
1112         fcinfo.argnull[4] = false;
1113
1114         result = (*func) (&fcinfo);
1115
1116         /* Check for null result, since caller is clearly not expecting one */
1117         if (fcinfo.isnull)
1118                 elog(ERROR, "function %p returned NULL", (void *) func);
1119
1120         return result;
1121 }
1122
1123 Datum
1124 DirectFunctionCall6(PGFunction func, Datum arg1, Datum arg2,
1125                                         Datum arg3, Datum arg4, Datum arg5,
1126                                         Datum arg6)
1127 {
1128         FunctionCallInfoData fcinfo;
1129         Datum           result;
1130
1131         InitFunctionCallInfoData(fcinfo, NULL, 6, NULL, NULL);
1132
1133         fcinfo.arg[0] = arg1;
1134         fcinfo.arg[1] = arg2;
1135         fcinfo.arg[2] = arg3;
1136         fcinfo.arg[3] = arg4;
1137         fcinfo.arg[4] = arg5;
1138         fcinfo.arg[5] = arg6;
1139         fcinfo.argnull[0] = false;
1140         fcinfo.argnull[1] = false;
1141         fcinfo.argnull[2] = false;
1142         fcinfo.argnull[3] = false;
1143         fcinfo.argnull[4] = false;
1144         fcinfo.argnull[5] = false;
1145
1146         result = (*func) (&fcinfo);
1147
1148         /* Check for null result, since caller is clearly not expecting one */
1149         if (fcinfo.isnull)
1150                 elog(ERROR, "function %p returned NULL", (void *) func);
1151
1152         return result;
1153 }
1154
1155 Datum
1156 DirectFunctionCall7(PGFunction func, Datum arg1, Datum arg2,
1157                                         Datum arg3, Datum arg4, Datum arg5,
1158                                         Datum arg6, Datum arg7)
1159 {
1160         FunctionCallInfoData fcinfo;
1161         Datum           result;
1162
1163         InitFunctionCallInfoData(fcinfo, NULL, 7, NULL, NULL);
1164
1165         fcinfo.arg[0] = arg1;
1166         fcinfo.arg[1] = arg2;
1167         fcinfo.arg[2] = arg3;
1168         fcinfo.arg[3] = arg4;
1169         fcinfo.arg[4] = arg5;
1170         fcinfo.arg[5] = arg6;
1171         fcinfo.arg[6] = arg7;
1172         fcinfo.argnull[0] = false;
1173         fcinfo.argnull[1] = false;
1174         fcinfo.argnull[2] = false;
1175         fcinfo.argnull[3] = false;
1176         fcinfo.argnull[4] = false;
1177         fcinfo.argnull[5] = false;
1178         fcinfo.argnull[6] = false;
1179
1180         result = (*func) (&fcinfo);
1181
1182         /* Check for null result, since caller is clearly not expecting one */
1183         if (fcinfo.isnull)
1184                 elog(ERROR, "function %p returned NULL", (void *) func);
1185
1186         return result;
1187 }
1188
1189 Datum
1190 DirectFunctionCall8(PGFunction func, Datum arg1, Datum arg2,
1191                                         Datum arg3, Datum arg4, Datum arg5,
1192                                         Datum arg6, Datum arg7, Datum arg8)
1193 {
1194         FunctionCallInfoData fcinfo;
1195         Datum           result;
1196
1197         InitFunctionCallInfoData(fcinfo, NULL, 8, NULL, NULL);
1198
1199         fcinfo.arg[0] = arg1;
1200         fcinfo.arg[1] = arg2;
1201         fcinfo.arg[2] = arg3;
1202         fcinfo.arg[3] = arg4;
1203         fcinfo.arg[4] = arg5;
1204         fcinfo.arg[5] = arg6;
1205         fcinfo.arg[6] = arg7;
1206         fcinfo.arg[7] = arg8;
1207         fcinfo.argnull[0] = false;
1208         fcinfo.argnull[1] = false;
1209         fcinfo.argnull[2] = false;
1210         fcinfo.argnull[3] = false;
1211         fcinfo.argnull[4] = false;
1212         fcinfo.argnull[5] = false;
1213         fcinfo.argnull[6] = false;
1214         fcinfo.argnull[7] = false;
1215
1216         result = (*func) (&fcinfo);
1217
1218         /* Check for null result, since caller is clearly not expecting one */
1219         if (fcinfo.isnull)
1220                 elog(ERROR, "function %p returned NULL", (void *) func);
1221
1222         return result;
1223 }
1224
1225 Datum
1226 DirectFunctionCall9(PGFunction func, Datum arg1, Datum arg2,
1227                                         Datum arg3, Datum arg4, Datum arg5,
1228                                         Datum arg6, Datum arg7, Datum arg8,
1229                                         Datum arg9)
1230 {
1231         FunctionCallInfoData fcinfo;
1232         Datum           result;
1233
1234         InitFunctionCallInfoData(fcinfo, NULL, 9, NULL, NULL);
1235
1236         fcinfo.arg[0] = arg1;
1237         fcinfo.arg[1] = arg2;
1238         fcinfo.arg[2] = arg3;
1239         fcinfo.arg[3] = arg4;
1240         fcinfo.arg[4] = arg5;
1241         fcinfo.arg[5] = arg6;
1242         fcinfo.arg[6] = arg7;
1243         fcinfo.arg[7] = arg8;
1244         fcinfo.arg[8] = arg9;
1245         fcinfo.argnull[0] = false;
1246         fcinfo.argnull[1] = false;
1247         fcinfo.argnull[2] = false;
1248         fcinfo.argnull[3] = false;
1249         fcinfo.argnull[4] = false;
1250         fcinfo.argnull[5] = false;
1251         fcinfo.argnull[6] = false;
1252         fcinfo.argnull[7] = false;
1253         fcinfo.argnull[8] = false;
1254
1255         result = (*func) (&fcinfo);
1256
1257         /* Check for null result, since caller is clearly not expecting one */
1258         if (fcinfo.isnull)
1259                 elog(ERROR, "function %p returned NULL", (void *) func);
1260
1261         return result;
1262 }
1263
1264
1265 /*
1266  * These are for invocation of a previously-looked-up function with a
1267  * directly-computed parameter list.  Note that neither arguments nor result
1268  * are allowed to be NULL.
1269  */
1270 Datum
1271 FunctionCall1(FmgrInfo *flinfo, Datum arg1)
1272 {
1273         FunctionCallInfoData fcinfo;
1274         Datum           result;
1275
1276         InitFunctionCallInfoData(fcinfo, flinfo, 1, NULL, NULL);
1277
1278         fcinfo.arg[0] = arg1;
1279         fcinfo.argnull[0] = false;
1280
1281         result = FunctionCallInvoke(&fcinfo);
1282
1283         /* Check for null result, since caller is clearly not expecting one */
1284         if (fcinfo.isnull)
1285                 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1286
1287         return result;
1288 }
1289
1290 Datum
1291 FunctionCall2(FmgrInfo *flinfo, Datum arg1, Datum arg2)
1292 {
1293         /*
1294          * XXX if you change this routine, see also the inlined version in
1295          * utils/sort/tuplesort.c!
1296          */
1297         FunctionCallInfoData fcinfo;
1298         Datum           result;
1299
1300         InitFunctionCallInfoData(fcinfo, flinfo, 2, NULL, NULL);
1301
1302         fcinfo.arg[0] = arg1;
1303         fcinfo.arg[1] = arg2;
1304         fcinfo.argnull[0] = false;
1305         fcinfo.argnull[1] = false;
1306
1307         result = FunctionCallInvoke(&fcinfo);
1308
1309         /* Check for null result, since caller is clearly not expecting one */
1310         if (fcinfo.isnull)
1311                 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1312
1313         return result;
1314 }
1315
1316 Datum
1317 FunctionCall3(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1318                           Datum arg3)
1319 {
1320         FunctionCallInfoData fcinfo;
1321         Datum           result;
1322
1323         InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);
1324
1325         fcinfo.arg[0] = arg1;
1326         fcinfo.arg[1] = arg2;
1327         fcinfo.arg[2] = arg3;
1328         fcinfo.argnull[0] = false;
1329         fcinfo.argnull[1] = false;
1330         fcinfo.argnull[2] = false;
1331
1332         result = FunctionCallInvoke(&fcinfo);
1333
1334         /* Check for null result, since caller is clearly not expecting one */
1335         if (fcinfo.isnull)
1336                 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1337
1338         return result;
1339 }
1340
1341 Datum
1342 FunctionCall4(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1343                           Datum arg3, Datum arg4)
1344 {
1345         FunctionCallInfoData fcinfo;
1346         Datum           result;
1347
1348         InitFunctionCallInfoData(fcinfo, flinfo, 4, NULL, NULL);
1349
1350         fcinfo.arg[0] = arg1;
1351         fcinfo.arg[1] = arg2;
1352         fcinfo.arg[2] = arg3;
1353         fcinfo.arg[3] = arg4;
1354         fcinfo.argnull[0] = false;
1355         fcinfo.argnull[1] = false;
1356         fcinfo.argnull[2] = false;
1357         fcinfo.argnull[3] = false;
1358
1359         result = FunctionCallInvoke(&fcinfo);
1360
1361         /* Check for null result, since caller is clearly not expecting one */
1362         if (fcinfo.isnull)
1363                 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1364
1365         return result;
1366 }
1367
1368 Datum
1369 FunctionCall5(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1370                           Datum arg3, Datum arg4, Datum arg5)
1371 {
1372         FunctionCallInfoData fcinfo;
1373         Datum           result;
1374
1375         InitFunctionCallInfoData(fcinfo, flinfo, 5, NULL, NULL);
1376
1377         fcinfo.arg[0] = arg1;
1378         fcinfo.arg[1] = arg2;
1379         fcinfo.arg[2] = arg3;
1380         fcinfo.arg[3] = arg4;
1381         fcinfo.arg[4] = arg5;
1382         fcinfo.argnull[0] = false;
1383         fcinfo.argnull[1] = false;
1384         fcinfo.argnull[2] = false;
1385         fcinfo.argnull[3] = false;
1386         fcinfo.argnull[4] = false;
1387
1388         result = FunctionCallInvoke(&fcinfo);
1389
1390         /* Check for null result, since caller is clearly not expecting one */
1391         if (fcinfo.isnull)
1392                 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1393
1394         return result;
1395 }
1396
1397 Datum
1398 FunctionCall6(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1399                           Datum arg3, Datum arg4, Datum arg5,
1400                           Datum arg6)
1401 {
1402         FunctionCallInfoData fcinfo;
1403         Datum           result;
1404
1405         InitFunctionCallInfoData(fcinfo, flinfo, 6, NULL, NULL);
1406
1407         fcinfo.arg[0] = arg1;
1408         fcinfo.arg[1] = arg2;
1409         fcinfo.arg[2] = arg3;
1410         fcinfo.arg[3] = arg4;
1411         fcinfo.arg[4] = arg5;
1412         fcinfo.arg[5] = arg6;
1413         fcinfo.argnull[0] = false;
1414         fcinfo.argnull[1] = false;
1415         fcinfo.argnull[2] = false;
1416         fcinfo.argnull[3] = false;
1417         fcinfo.argnull[4] = false;
1418         fcinfo.argnull[5] = false;
1419
1420         result = FunctionCallInvoke(&fcinfo);
1421
1422         /* Check for null result, since caller is clearly not expecting one */
1423         if (fcinfo.isnull)
1424                 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1425
1426         return result;
1427 }
1428
1429 Datum
1430 FunctionCall7(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1431                           Datum arg3, Datum arg4, Datum arg5,
1432                           Datum arg6, Datum arg7)
1433 {
1434         FunctionCallInfoData fcinfo;
1435         Datum           result;
1436
1437         InitFunctionCallInfoData(fcinfo, flinfo, 7, NULL, NULL);
1438
1439         fcinfo.arg[0] = arg1;
1440         fcinfo.arg[1] = arg2;
1441         fcinfo.arg[2] = arg3;
1442         fcinfo.arg[3] = arg4;
1443         fcinfo.arg[4] = arg5;
1444         fcinfo.arg[5] = arg6;
1445         fcinfo.arg[6] = arg7;
1446         fcinfo.argnull[0] = false;
1447         fcinfo.argnull[1] = false;
1448         fcinfo.argnull[2] = false;
1449         fcinfo.argnull[3] = false;
1450         fcinfo.argnull[4] = false;
1451         fcinfo.argnull[5] = false;
1452         fcinfo.argnull[6] = false;
1453
1454         result = FunctionCallInvoke(&fcinfo);
1455
1456         /* Check for null result, since caller is clearly not expecting one */
1457         if (fcinfo.isnull)
1458                 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1459
1460         return result;
1461 }
1462
1463 Datum
1464 FunctionCall8(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1465                           Datum arg3, Datum arg4, Datum arg5,
1466                           Datum arg6, Datum arg7, Datum arg8)
1467 {
1468         FunctionCallInfoData fcinfo;
1469         Datum           result;
1470
1471         InitFunctionCallInfoData(fcinfo, flinfo, 8, NULL, NULL);
1472
1473         fcinfo.arg[0] = arg1;
1474         fcinfo.arg[1] = arg2;
1475         fcinfo.arg[2] = arg3;
1476         fcinfo.arg[3] = arg4;
1477         fcinfo.arg[4] = arg5;
1478         fcinfo.arg[5] = arg6;
1479         fcinfo.arg[6] = arg7;
1480         fcinfo.arg[7] = arg8;
1481         fcinfo.argnull[0] = false;
1482         fcinfo.argnull[1] = false;
1483         fcinfo.argnull[2] = false;
1484         fcinfo.argnull[3] = false;
1485         fcinfo.argnull[4] = false;
1486         fcinfo.argnull[5] = false;
1487         fcinfo.argnull[6] = false;
1488         fcinfo.argnull[7] = false;
1489
1490         result = FunctionCallInvoke(&fcinfo);
1491
1492         /* Check for null result, since caller is clearly not expecting one */
1493         if (fcinfo.isnull)
1494                 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1495
1496         return result;
1497 }
1498
1499 Datum
1500 FunctionCall9(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1501                           Datum arg3, Datum arg4, Datum arg5,
1502                           Datum arg6, Datum arg7, Datum arg8,
1503                           Datum arg9)
1504 {
1505         FunctionCallInfoData fcinfo;
1506         Datum           result;
1507
1508         InitFunctionCallInfoData(fcinfo, flinfo, 9, NULL, NULL);
1509
1510         fcinfo.arg[0] = arg1;
1511         fcinfo.arg[1] = arg2;
1512         fcinfo.arg[2] = arg3;
1513         fcinfo.arg[3] = arg4;
1514         fcinfo.arg[4] = arg5;
1515         fcinfo.arg[5] = arg6;
1516         fcinfo.arg[6] = arg7;
1517         fcinfo.arg[7] = arg8;
1518         fcinfo.arg[8] = arg9;
1519         fcinfo.argnull[0] = false;
1520         fcinfo.argnull[1] = false;
1521         fcinfo.argnull[2] = false;
1522         fcinfo.argnull[3] = false;
1523         fcinfo.argnull[4] = false;
1524         fcinfo.argnull[5] = false;
1525         fcinfo.argnull[6] = false;
1526         fcinfo.argnull[7] = false;
1527         fcinfo.argnull[8] = false;
1528
1529         result = FunctionCallInvoke(&fcinfo);
1530
1531         /* Check for null result, since caller is clearly not expecting one */
1532         if (fcinfo.isnull)
1533                 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1534
1535         return result;
1536 }
1537
1538
1539 /*
1540  * These are for invocation of a function identified by OID with a
1541  * directly-computed parameter list.  Note that neither arguments nor result
1542  * are allowed to be NULL.      These are essentially fmgr_info() followed
1543  * by FunctionCallN().  If the same function is to be invoked repeatedly,
1544  * do the fmgr_info() once and then use FunctionCallN().
1545  */
1546 Datum
1547 OidFunctionCall1(Oid functionId, Datum arg1)
1548 {
1549         FmgrInfo        flinfo;
1550         FunctionCallInfoData fcinfo;
1551         Datum           result;
1552
1553         fmgr_info(functionId, &flinfo);
1554
1555         InitFunctionCallInfoData(fcinfo, &flinfo, 1, NULL, NULL);
1556
1557         fcinfo.arg[0] = arg1;
1558         fcinfo.argnull[0] = false;
1559
1560         result = FunctionCallInvoke(&fcinfo);
1561
1562         /* Check for null result, since caller is clearly not expecting one */
1563         if (fcinfo.isnull)
1564                 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1565
1566         return result;
1567 }
1568
1569 Datum
1570 OidFunctionCall2(Oid functionId, Datum arg1, Datum arg2)
1571 {
1572         FmgrInfo        flinfo;
1573         FunctionCallInfoData fcinfo;
1574         Datum           result;
1575
1576         fmgr_info(functionId, &flinfo);
1577
1578         InitFunctionCallInfoData(fcinfo, &flinfo, 2, NULL, NULL);
1579
1580         fcinfo.arg[0] = arg1;
1581         fcinfo.arg[1] = arg2;
1582         fcinfo.argnull[0] = false;
1583         fcinfo.argnull[1] = false;
1584
1585         result = FunctionCallInvoke(&fcinfo);
1586
1587         /* Check for null result, since caller is clearly not expecting one */
1588         if (fcinfo.isnull)
1589                 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1590
1591         return result;
1592 }
1593
1594 Datum
1595 OidFunctionCall3(Oid functionId, Datum arg1, Datum arg2,
1596                                  Datum arg3)
1597 {
1598         FmgrInfo        flinfo;
1599         FunctionCallInfoData fcinfo;
1600         Datum           result;
1601
1602         fmgr_info(functionId, &flinfo);
1603
1604         InitFunctionCallInfoData(fcinfo, &flinfo, 3, NULL, NULL);
1605
1606         fcinfo.arg[0] = arg1;
1607         fcinfo.arg[1] = arg2;
1608         fcinfo.arg[2] = arg3;
1609         fcinfo.argnull[0] = false;
1610         fcinfo.argnull[1] = false;
1611         fcinfo.argnull[2] = false;
1612
1613         result = FunctionCallInvoke(&fcinfo);
1614
1615         /* Check for null result, since caller is clearly not expecting one */
1616         if (fcinfo.isnull)
1617                 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1618
1619         return result;
1620 }
1621
1622 Datum
1623 OidFunctionCall4(Oid functionId, Datum arg1, Datum arg2,
1624                                  Datum arg3, Datum arg4)
1625 {
1626         FmgrInfo        flinfo;
1627         FunctionCallInfoData fcinfo;
1628         Datum           result;
1629
1630         fmgr_info(functionId, &flinfo);
1631
1632         InitFunctionCallInfoData(fcinfo, &flinfo, 4, NULL, NULL);
1633
1634         fcinfo.arg[0] = arg1;
1635         fcinfo.arg[1] = arg2;
1636         fcinfo.arg[2] = arg3;
1637         fcinfo.arg[3] = arg4;
1638         fcinfo.argnull[0] = false;
1639         fcinfo.argnull[1] = false;
1640         fcinfo.argnull[2] = false;
1641         fcinfo.argnull[3] = false;
1642
1643         result = FunctionCallInvoke(&fcinfo);
1644
1645         /* Check for null result, since caller is clearly not expecting one */
1646         if (fcinfo.isnull)
1647                 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1648
1649         return result;
1650 }
1651
1652 Datum
1653 OidFunctionCall5(Oid functionId, Datum arg1, Datum arg2,
1654                                  Datum arg3, Datum arg4, Datum arg5)
1655 {
1656         FmgrInfo        flinfo;
1657         FunctionCallInfoData fcinfo;
1658         Datum           result;
1659
1660         fmgr_info(functionId, &flinfo);
1661
1662         InitFunctionCallInfoData(fcinfo, &flinfo, 5, NULL, NULL);
1663
1664         fcinfo.arg[0] = arg1;
1665         fcinfo.arg[1] = arg2;
1666         fcinfo.arg[2] = arg3;
1667         fcinfo.arg[3] = arg4;
1668         fcinfo.arg[4] = arg5;
1669         fcinfo.argnull[0] = false;
1670         fcinfo.argnull[1] = false;
1671         fcinfo.argnull[2] = false;
1672         fcinfo.argnull[3] = false;
1673         fcinfo.argnull[4] = false;
1674
1675         result = FunctionCallInvoke(&fcinfo);
1676
1677         /* Check for null result, since caller is clearly not expecting one */
1678         if (fcinfo.isnull)
1679                 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1680
1681         return result;
1682 }
1683
1684 Datum
1685 OidFunctionCall6(Oid functionId, Datum arg1, Datum arg2,
1686                                  Datum arg3, Datum arg4, Datum arg5,
1687                                  Datum arg6)
1688 {
1689         FmgrInfo        flinfo;
1690         FunctionCallInfoData fcinfo;
1691         Datum           result;
1692
1693         fmgr_info(functionId, &flinfo);
1694
1695         InitFunctionCallInfoData(fcinfo, &flinfo, 6, NULL, NULL);
1696
1697         fcinfo.arg[0] = arg1;
1698         fcinfo.arg[1] = arg2;
1699         fcinfo.arg[2] = arg3;
1700         fcinfo.arg[3] = arg4;
1701         fcinfo.arg[4] = arg5;
1702         fcinfo.arg[5] = arg6;
1703         fcinfo.argnull[0] = false;
1704         fcinfo.argnull[1] = false;
1705         fcinfo.argnull[2] = false;
1706         fcinfo.argnull[3] = false;
1707         fcinfo.argnull[4] = false;
1708         fcinfo.argnull[5] = false;
1709
1710         result = FunctionCallInvoke(&fcinfo);
1711
1712         /* Check for null result, since caller is clearly not expecting one */
1713         if (fcinfo.isnull)
1714                 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1715
1716         return result;
1717 }
1718
1719 Datum
1720 OidFunctionCall7(Oid functionId, Datum arg1, Datum arg2,
1721                                  Datum arg3, Datum arg4, Datum arg5,
1722                                  Datum arg6, Datum arg7)
1723 {
1724         FmgrInfo        flinfo;
1725         FunctionCallInfoData fcinfo;
1726         Datum           result;
1727
1728         fmgr_info(functionId, &flinfo);
1729
1730         InitFunctionCallInfoData(fcinfo, &flinfo, 7, NULL, NULL);
1731
1732         fcinfo.arg[0] = arg1;
1733         fcinfo.arg[1] = arg2;
1734         fcinfo.arg[2] = arg3;
1735         fcinfo.arg[3] = arg4;
1736         fcinfo.arg[4] = arg5;
1737         fcinfo.arg[5] = arg6;
1738         fcinfo.arg[6] = arg7;
1739         fcinfo.argnull[0] = false;
1740         fcinfo.argnull[1] = false;
1741         fcinfo.argnull[2] = false;
1742         fcinfo.argnull[3] = false;
1743         fcinfo.argnull[4] = false;
1744         fcinfo.argnull[5] = false;
1745         fcinfo.argnull[6] = false;
1746
1747         result = FunctionCallInvoke(&fcinfo);
1748
1749         /* Check for null result, since caller is clearly not expecting one */
1750         if (fcinfo.isnull)
1751                 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1752
1753         return result;
1754 }
1755
1756 Datum
1757 OidFunctionCall8(Oid functionId, Datum arg1, Datum arg2,
1758                                  Datum arg3, Datum arg4, Datum arg5,
1759                                  Datum arg6, Datum arg7, Datum arg8)
1760 {
1761         FmgrInfo        flinfo;
1762         FunctionCallInfoData fcinfo;
1763         Datum           result;
1764
1765         fmgr_info(functionId, &flinfo);
1766
1767         InitFunctionCallInfoData(fcinfo, &flinfo, 8, NULL, NULL);
1768
1769         fcinfo.arg[0] = arg1;
1770         fcinfo.arg[1] = arg2;
1771         fcinfo.arg[2] = arg3;
1772         fcinfo.arg[3] = arg4;
1773         fcinfo.arg[4] = arg5;
1774         fcinfo.arg[5] = arg6;
1775         fcinfo.arg[6] = arg7;
1776         fcinfo.arg[7] = arg8;
1777         fcinfo.argnull[0] = false;
1778         fcinfo.argnull[1] = false;
1779         fcinfo.argnull[2] = false;
1780         fcinfo.argnull[3] = false;
1781         fcinfo.argnull[4] = false;
1782         fcinfo.argnull[5] = false;
1783         fcinfo.argnull[6] = false;
1784         fcinfo.argnull[7] = false;
1785
1786         result = FunctionCallInvoke(&fcinfo);
1787
1788         /* Check for null result, since caller is clearly not expecting one */
1789         if (fcinfo.isnull)
1790                 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1791
1792         return result;
1793 }
1794
1795 Datum
1796 OidFunctionCall9(Oid functionId, Datum arg1, Datum arg2,
1797                                  Datum arg3, Datum arg4, Datum arg5,
1798                                  Datum arg6, Datum arg7, Datum arg8,
1799                                  Datum arg9)
1800 {
1801         FmgrInfo        flinfo;
1802         FunctionCallInfoData fcinfo;
1803         Datum           result;
1804
1805         fmgr_info(functionId, &flinfo);
1806
1807         InitFunctionCallInfoData(fcinfo, &flinfo, 9, NULL, NULL);
1808
1809         fcinfo.arg[0] = arg1;
1810         fcinfo.arg[1] = arg2;
1811         fcinfo.arg[2] = arg3;
1812         fcinfo.arg[3] = arg4;
1813         fcinfo.arg[4] = arg5;
1814         fcinfo.arg[5] = arg6;
1815         fcinfo.arg[6] = arg7;
1816         fcinfo.arg[7] = arg8;
1817         fcinfo.arg[8] = arg9;
1818         fcinfo.argnull[0] = false;
1819         fcinfo.argnull[1] = false;
1820         fcinfo.argnull[2] = false;
1821         fcinfo.argnull[3] = false;
1822         fcinfo.argnull[4] = false;
1823         fcinfo.argnull[5] = false;
1824         fcinfo.argnull[6] = false;
1825         fcinfo.argnull[7] = false;
1826         fcinfo.argnull[8] = false;
1827
1828         result = FunctionCallInvoke(&fcinfo);
1829
1830         /* Check for null result, since caller is clearly not expecting one */
1831         if (fcinfo.isnull)
1832                 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1833
1834         return result;
1835 }
1836
1837
1838 /*
1839  * Special cases for convenient invocation of datatype I/O functions.
1840  */
1841
1842 /*
1843  * Call a previously-looked-up datatype input function.
1844  *
1845  * "str" may be NULL to indicate we are reading a NULL.  In this case
1846  * the caller should assume the result is NULL, but we'll call the input
1847  * function anyway if it's not strict.  So this is almost but not quite
1848  * the same as FunctionCall3.
1849  */
1850 Datum
1851 InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
1852 {
1853         FunctionCallInfoData fcinfo;
1854         Datum           result;
1855
1856         if (str == NULL && flinfo->fn_strict)
1857                 return (Datum) 0;               /* just return null result */
1858
1859         InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);
1860
1861         fcinfo.arg[0] = CStringGetDatum(str);
1862         fcinfo.arg[1] = ObjectIdGetDatum(typioparam);
1863         fcinfo.arg[2] = Int32GetDatum(typmod);
1864         fcinfo.argnull[0] = (str == NULL);
1865         fcinfo.argnull[1] = false;
1866         fcinfo.argnull[2] = false;
1867
1868         result = FunctionCallInvoke(&fcinfo);
1869
1870         /* Should get null result if and only if str is NULL */
1871         if (str == NULL)
1872         {
1873                 if (!fcinfo.isnull)
1874                         elog(ERROR, "input function %u returned non-NULL",
1875                                  fcinfo.flinfo->fn_oid);
1876         }
1877         else
1878         {
1879                 if (fcinfo.isnull)
1880                         elog(ERROR, "input function %u returned NULL",
1881                                  fcinfo.flinfo->fn_oid);
1882         }
1883
1884         return result;
1885 }
1886
1887 /*
1888  * Call a previously-looked-up datatype output function.
1889  *
1890  * Do not call this on NULL datums.
1891  *
1892  * This is mere window dressing for FunctionCall1, but its use is recommended
1893  * anyway so that code invoking output functions can be identified easily.
1894  */
1895 char *
1896 OutputFunctionCall(FmgrInfo *flinfo, Datum val)
1897 {
1898         return DatumGetCString(FunctionCall1(flinfo, val));
1899 }
1900
1901 /*
1902  * Call a previously-looked-up datatype binary-input function.
1903  *
1904  * "buf" may be NULL to indicate we are reading a NULL.  In this case
1905  * the caller should assume the result is NULL, but we'll call the receive
1906  * function anyway if it's not strict.  So this is almost but not quite
1907  * the same as FunctionCall3.
1908  */
1909 Datum
1910 ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf,
1911                                         Oid typioparam, int32 typmod)
1912 {
1913         FunctionCallInfoData fcinfo;
1914         Datum           result;
1915
1916         if (buf == NULL && flinfo->fn_strict)
1917                 return (Datum) 0;               /* just return null result */
1918
1919         InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);
1920
1921         fcinfo.arg[0] = PointerGetDatum(buf);
1922         fcinfo.arg[1] = ObjectIdGetDatum(typioparam);
1923         fcinfo.arg[2] = Int32GetDatum(typmod);
1924         fcinfo.argnull[0] = (buf == NULL);
1925         fcinfo.argnull[1] = false;
1926         fcinfo.argnull[2] = false;
1927
1928         result = FunctionCallInvoke(&fcinfo);
1929
1930         /* Should get null result if and only if buf is NULL */
1931         if (buf == NULL)
1932         {
1933                 if (!fcinfo.isnull)
1934                         elog(ERROR, "receive function %u returned non-NULL",
1935                                  fcinfo.flinfo->fn_oid);
1936         }
1937         else
1938         {
1939                 if (fcinfo.isnull)
1940                         elog(ERROR, "receive function %u returned NULL",
1941                                  fcinfo.flinfo->fn_oid);
1942         }
1943
1944         return result;
1945 }
1946
1947 /*
1948  * Call a previously-looked-up datatype binary-output function.
1949  *
1950  * Do not call this on NULL datums.
1951  *
1952  * This is little more than window dressing for FunctionCall1, but its use is
1953  * recommended anyway so that code invoking output functions can be identified
1954  * easily.      Note however that it does guarantee a non-toasted result.
1955  */
1956 bytea *
1957 SendFunctionCall(FmgrInfo *flinfo, Datum val)
1958 {
1959         return DatumGetByteaP(FunctionCall1(flinfo, val));
1960 }
1961
1962 /*
1963  * As above, for I/O functions identified by OID.  These are only to be used
1964  * in seldom-executed code paths.  They are not only slow but leak memory.
1965  */
1966 Datum
1967 OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
1968 {
1969         FmgrInfo        flinfo;
1970
1971         fmgr_info(functionId, &flinfo);
1972         return InputFunctionCall(&flinfo, str, typioparam, typmod);
1973 }
1974
1975 char *
1976 OidOutputFunctionCall(Oid functionId, Datum val)
1977 {
1978         FmgrInfo        flinfo;
1979
1980         fmgr_info(functionId, &flinfo);
1981         return OutputFunctionCall(&flinfo, val);
1982 }
1983
1984 Datum
1985 OidReceiveFunctionCall(Oid functionId, StringInfo buf,
1986                                            Oid typioparam, int32 typmod)
1987 {
1988         FmgrInfo        flinfo;
1989
1990         fmgr_info(functionId, &flinfo);
1991         return ReceiveFunctionCall(&flinfo, buf, typioparam, typmod);
1992 }
1993
1994 bytea *
1995 OidSendFunctionCall(Oid functionId, Datum val)
1996 {
1997         FmgrInfo        flinfo;
1998
1999         fmgr_info(functionId, &flinfo);
2000         return SendFunctionCall(&flinfo, val);
2001 }
2002
2003
2004 /*
2005  * !!! OLD INTERFACE !!!
2006  *
2007  * fmgr() is the only remaining vestige of the old-style caller support
2008  * functions.  It's no longer used anywhere in the Postgres distribution,
2009  * but we should leave it around for a release or two to ease the transition
2010  * for user-supplied C functions.  OidFunctionCallN() replaces it for new
2011  * code.
2012  *
2013  * DEPRECATED, DO NOT USE IN NEW CODE
2014  */
2015 char *
2016 fmgr(Oid procedureId,...)
2017 {
2018         FmgrInfo        flinfo;
2019         FunctionCallInfoData fcinfo;
2020         int                     n_arguments;
2021         Datum           result;
2022
2023         fmgr_info(procedureId, &flinfo);
2024
2025         MemSet(&fcinfo, 0, sizeof(fcinfo));
2026         fcinfo.flinfo = &flinfo;
2027         fcinfo.nargs = flinfo.fn_nargs;
2028         n_arguments = fcinfo.nargs;
2029
2030         if (n_arguments > 0)
2031         {
2032                 va_list         pvar;
2033                 int                     i;
2034
2035                 if (n_arguments > FUNC_MAX_ARGS)
2036                         ereport(ERROR,
2037                                         (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
2038                          errmsg("function %u has too many arguments (%d, maximum is %d)",
2039                                         flinfo.fn_oid, n_arguments, FUNC_MAX_ARGS)));
2040                 va_start(pvar, procedureId);
2041                 for (i = 0; i < n_arguments; i++)
2042                         fcinfo.arg[i] = PointerGetDatum(va_arg(pvar, char *));
2043                 va_end(pvar);
2044         }
2045
2046         result = FunctionCallInvoke(&fcinfo);
2047
2048         /* Check for null result, since caller is clearly not expecting one */
2049         if (fcinfo.isnull)
2050                 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
2051
2052         return DatumGetPointer(result);
2053 }
2054
2055
2056 /*-------------------------------------------------------------------------
2057  *              Support routines for standard maybe-pass-by-reference datatypes
2058  *
2059  * int8, float4, and float8 can be passed by value if Datum is wide enough.
2060  * (For backwards-compatibility reasons, we allow pass-by-ref to be chosen
2061  * at compile time even if pass-by-val is possible.)  For the float types,
2062  * we need a support routine even if we are passing by value, because many
2063  * machines pass int and float function parameters/results differently;
2064  * so we need to play weird games with unions.
2065  *
2066  * Note: there is only one switch controlling the pass-by-value option for
2067  * both int8 and float8; this is to avoid making things unduly complicated
2068  * for the timestamp types, which might have either representation.
2069  *-------------------------------------------------------------------------
2070  */
2071
2072 #ifndef USE_FLOAT8_BYVAL                /* controls int8 too */
2073
2074 Datum
2075 Int64GetDatum(int64 X)
2076 {
2077 #ifndef INT64_IS_BUSTED
2078         int64      *retval = (int64 *) palloc(sizeof(int64));
2079
2080         *retval = X;
2081         return PointerGetDatum(retval);
2082 #else                                                   /* INT64_IS_BUSTED */
2083
2084         /*
2085          * On a machine with no 64-bit-int C datatype, sizeof(int64) will not be
2086          * 8, but we want Int64GetDatum to return an 8-byte object anyway, with
2087          * zeroes in the unused bits.  This is needed so that, for example, hash
2088          * join of int8 will behave properly.
2089          */
2090         int64      *retval = (int64 *) palloc0(Max(sizeof(int64), 8));
2091
2092         *retval = X;
2093         return PointerGetDatum(retval);
2094 #endif   /* INT64_IS_BUSTED */
2095 }
2096
2097 #endif /* USE_FLOAT8_BYVAL */
2098
2099 Datum
2100 Float4GetDatum(float4 X)
2101 {
2102 #ifdef USE_FLOAT4_BYVAL
2103         union {
2104                 float4  value;
2105                 int32   retval;
2106         } myunion;
2107
2108         myunion.value = X;
2109         return SET_4_BYTES(myunion.retval);
2110 #else
2111         float4     *retval = (float4 *) palloc(sizeof(float4));
2112
2113         *retval = X;
2114         return PointerGetDatum(retval);
2115 #endif
2116 }
2117
2118 #ifdef USE_FLOAT4_BYVAL
2119
2120 float4
2121 DatumGetFloat4(Datum X)
2122 {
2123         union {
2124                 int32   value;
2125                 float4  retval;
2126         } myunion;
2127
2128         myunion.value = GET_4_BYTES(X);
2129         return myunion.retval;
2130 }
2131
2132 #endif /* USE_FLOAT4_BYVAL */
2133
2134 Datum
2135 Float8GetDatum(float8 X)
2136 {
2137 #ifdef USE_FLOAT8_BYVAL
2138         union {
2139                 float8  value;
2140                 int64   retval;
2141         } myunion;
2142
2143         myunion.value = X;
2144         return SET_8_BYTES(myunion.retval);
2145 #else
2146         float8     *retval = (float8 *) palloc(sizeof(float8));
2147
2148         *retval = X;
2149         return PointerGetDatum(retval);
2150 #endif
2151 }
2152
2153 #ifdef USE_FLOAT8_BYVAL
2154
2155 float8
2156 DatumGetFloat8(Datum X)
2157 {
2158         union {
2159                 int64   value;
2160                 float8  retval;
2161         } myunion;
2162
2163         myunion.value = GET_8_BYTES(X);
2164         return myunion.retval;
2165 }
2166
2167 #endif /* USE_FLOAT8_BYVAL */
2168
2169
2170 /*-------------------------------------------------------------------------
2171  *              Support routines for toastable datatypes
2172  *-------------------------------------------------------------------------
2173  */
2174
2175 struct varlena *
2176 pg_detoast_datum(struct varlena * datum)
2177 {
2178         if (VARATT_IS_EXTENDED(datum))
2179                 return heap_tuple_untoast_attr(datum);
2180         else
2181                 return datum;
2182 }
2183
2184 struct varlena *
2185 pg_detoast_datum_copy(struct varlena * datum)
2186 {
2187         if (VARATT_IS_EXTENDED(datum))
2188                 return heap_tuple_untoast_attr(datum);
2189         else
2190         {
2191                 /* Make a modifiable copy of the varlena object */
2192                 Size            len = VARSIZE(datum);
2193                 struct varlena *result = (struct varlena *) palloc(len);
2194
2195                 memcpy(result, datum, len);
2196                 return result;
2197         }
2198 }
2199
2200 struct varlena *
2201 pg_detoast_datum_slice(struct varlena * datum, int32 first, int32 count)
2202 {
2203         /* Only get the specified portion from the toast rel */
2204         return heap_tuple_untoast_attr_slice(datum, first, count);
2205 }
2206
2207 struct varlena *
2208 pg_detoast_datum_packed(struct varlena * datum)
2209 {
2210         if (VARATT_IS_COMPRESSED(datum) || VARATT_IS_EXTERNAL(datum))
2211                 return heap_tuple_untoast_attr(datum);
2212         else
2213                 return datum;
2214 }
2215
2216 /*-------------------------------------------------------------------------
2217  *              Support routines for extracting info from fn_expr parse tree
2218  *
2219  * These are needed by polymorphic functions, which accept multiple possible
2220  * input types and need help from the parser to know what they've got.
2221  *-------------------------------------------------------------------------
2222  */
2223
2224 /*
2225  * Get the actual type OID of the function return type
2226  *
2227  * Returns InvalidOid if information is not available
2228  */
2229 Oid
2230 get_fn_expr_rettype(FmgrInfo *flinfo)
2231 {
2232         Node       *expr;
2233
2234         /*
2235          * can't return anything useful if we have no FmgrInfo or if its fn_expr
2236          * node has not been initialized
2237          */
2238         if (!flinfo || !flinfo->fn_expr)
2239                 return InvalidOid;
2240
2241         expr = flinfo->fn_expr;
2242
2243         return exprType(expr);
2244 }
2245
2246 /*
2247  * Get the actual type OID of a specific function argument (counting from 0)
2248  *
2249  * Returns InvalidOid if information is not available
2250  */
2251 Oid
2252 get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
2253 {
2254         /*
2255          * can't return anything useful if we have no FmgrInfo or if its fn_expr
2256          * node has not been initialized
2257          */
2258         if (!flinfo || !flinfo->fn_expr)
2259                 return InvalidOid;
2260
2261         return get_call_expr_argtype(flinfo->fn_expr, argnum);
2262 }
2263
2264 /*
2265  * Get the actual type OID of a specific function argument (counting from 0),
2266  * but working from the calling expression tree instead of FmgrInfo
2267  *
2268  * Returns InvalidOid if information is not available
2269  */
2270 Oid
2271 get_call_expr_argtype(Node *expr, int argnum)
2272 {
2273         List       *args;
2274         Oid                     argtype;
2275
2276         if (expr == NULL)
2277                 return InvalidOid;
2278
2279         if (IsA(expr, FuncExpr))
2280                 args = ((FuncExpr *) expr)->args;
2281         else if (IsA(expr, OpExpr))
2282                 args = ((OpExpr *) expr)->args;
2283         else if (IsA(expr, DistinctExpr))
2284                 args = ((DistinctExpr *) expr)->args;
2285         else if (IsA(expr, ScalarArrayOpExpr))
2286                 args = ((ScalarArrayOpExpr *) expr)->args;
2287         else if (IsA(expr, ArrayCoerceExpr))
2288                 args = list_make1(((ArrayCoerceExpr *) expr)->arg);
2289         else if (IsA(expr, NullIfExpr))
2290                 args = ((NullIfExpr *) expr)->args;
2291         else
2292                 return InvalidOid;
2293
2294         if (argnum < 0 || argnum >= list_length(args))
2295                 return InvalidOid;
2296
2297         argtype = exprType((Node *) list_nth(args, argnum));
2298
2299         /*
2300          * special hack for ScalarArrayOpExpr and ArrayCoerceExpr: what the
2301          * underlying function will actually get passed is the element type of the
2302          * array.
2303          */
2304         if (IsA(expr, ScalarArrayOpExpr) &&
2305                 argnum == 1)
2306                 argtype = get_element_type(argtype);
2307         else if (IsA(expr, ArrayCoerceExpr) &&
2308                          argnum == 0)
2309                 argtype = get_element_type(argtype);
2310
2311         return argtype;
2312 }