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