1 /*-------------------------------------------------------------------------
4 * The Postgres function manager.
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/utils/fmgr/fmgr.c,v 1.124 2009/01/01 17:23:51 momjian Exp $
13 *-------------------------------------------------------------------------
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 "nodes/nodeFuncs.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"
34 * Declaration for old-style function pointer type. This is now used only
35 * in fmgr_oldstyle() and is no longer exported.
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().
48 #if (defined(__mc68000__) || (defined(__m68k__))) && defined(__ELF__)
49 typedef int32 (*func_ptr) ();
51 typedef char *(*func_ptr) ();
55 * For an oldstyle function, fn_extra points to a record like this:
59 func_ptr func; /* Address of the oldstyle function */
60 bool arg_toastable[FUNC_MAX_ARGS]; /* is n'th arg of a toastable
65 * Hashtable for fast lookup of external C functions
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 */
77 static HTAB *CFuncHash = NULL;
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);
92 * Lookup routines for builtin-function table. We can search by either Oid
93 * or name, but search by Oid is much faster.
96 static const FmgrBuiltin *
97 fmgr_isbuiltin(Oid id)
100 int high = fmgr_nbuiltins - 1;
103 * Loop invariant: low is the first index that could contain target entry,
104 * and high is the last index that could contain it.
108 int i = (high + low) / 2;
109 const FmgrBuiltin *ptr = &fmgr_builtins[i];
113 else if (id > ptr->foid)
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
126 static const FmgrBuiltin *
127 fmgr_lookupByName(const char *name)
131 for (i = 0; i < fmgr_nbuiltins; i++)
133 if (strcmp(name, fmgr_builtins[i].funcName) == 0)
134 return fmgr_builtins + i;
140 * This routine fills a FmgrInfo struct, given the OID
141 * of the function to be called.
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.
153 fmgr_info(Oid functionId, FmgrInfo *finfo)
155 fmgr_info_cxt(functionId, finfo, CurrentMemoryContext);
159 * Fill a FmgrInfo struct, specifying a memory context in which its
160 * subsidiary data should go.
163 fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
165 fmgr_info_cxt_security(functionId, finfo, mcxt, false);
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.
173 fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt,
174 bool ignore_security)
176 const FmgrBuiltin *fbp;
177 HeapTuple procedureTuple;
178 Form_pg_proc procedureStruct;
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
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 */
193 if ((fbp = fmgr_isbuiltin(functionId)) != NULL)
196 * Fast path for builtin functions: don't bother consulting pg_proc
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;
207 /* Otherwise we need the pg_proc entry */
208 procedureTuple = SearchSysCache(PROCOID,
209 ObjectIdGetDatum(functionId),
211 if (!HeapTupleIsValid(procedureTuple))
212 elog(ERROR, "cache lookup failed for function %u", functionId);
213 procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
215 finfo->fn_nargs = procedureStruct->pronargs;
216 finfo->fn_strict = procedureStruct->proisstrict;
217 finfo->fn_retset = procedureStruct->proretset;
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.
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.
232 if (!ignore_security &&
233 (procedureStruct->prosecdef ||
234 !heap_attisnull(procedureTuple, Anum_pg_proc_proconfig)))
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);
243 switch (procedureStruct->prolang)
245 case INTERNALlanguageId:
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!)
256 prosrcdatum = SysCacheGetAttr(PROCOID, procedureTuple,
257 Anum_pg_proc_prosrc, &isnull);
259 elog(ERROR, "null prosrc");
260 prosrc = TextDatumGetCString(prosrcdatum);
261 fbp = fmgr_lookupByName(prosrc);
264 (errcode(ERRCODE_UNDEFINED_FUNCTION),
265 errmsg("internal function \"%s\" is not in internal lookup table",
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 */
275 fmgr_info_C_lang(functionId, finfo, procedureTuple);
276 finfo->fn_stats = TRACK_FUNC_PL; /* ie, track if ALL */
280 finfo->fn_addr = fmgr_sql;
281 finfo->fn_stats = TRACK_FUNC_PL; /* ie, track if ALL */
285 fmgr_info_other_lang(functionId, finfo, procedureTuple);
286 finfo->fn_stats = TRACK_FUNC_OFF; /* ie, track if not OFF */
290 finfo->fn_oid = functionId;
291 ReleaseSysCache(procedureTuple);
295 * Special fmgr_info processing for C-language functions. Note that
296 * finfo->fn_oid is not valid yet.
299 fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
301 Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
302 CFuncHashTabEntry *hashentry;
304 const Pg_finfo_record *inforec;
305 Oldstyle_fnextra *fnextra;
310 * See if we have the function address cached already
312 hashentry = lookup_C_func(procedureTuple);
315 user_fn = hashentry->user_fn;
316 inforec = hashentry->inforec;
327 * Get prosrc and probin strings (link symbol and library filename).
328 * While in general these columns might be null, that's not allowed
329 * for C-language functions.
331 prosrcattr = SysCacheGetAttr(PROCOID, procedureTuple,
332 Anum_pg_proc_prosrc, &isnull);
334 elog(ERROR, "null prosrc for C function %u", functionId);
335 prosrcstring = TextDatumGetCString(prosrcattr);
337 probinattr = SysCacheGetAttr(PROCOID, procedureTuple,
338 Anum_pg_proc_probin, &isnull);
340 elog(ERROR, "null probin for C function %u", functionId);
341 probinstring = TextDatumGetCString(probinattr);
343 /* Look up the function itself */
344 user_fn = load_external_function(probinstring, prosrcstring, true,
347 /* Get the function information record (real or default) */
348 inforec = fetch_finfo_record(libraryhandle, prosrcstring);
350 /* Cache the addresses for later calls */
351 record_C_func(procedureTuple, user_fn, inforec);
357 switch (inforec->api_version)
360 /* Old style: need to use a handler */
361 finfo->fn_addr = fmgr_oldstyle;
362 fnextra = (Oldstyle_fnextra *)
363 MemoryContextAllocZero(finfo->fn_mcxt,
364 sizeof(Oldstyle_fnextra));
365 finfo->fn_extra = (void *) fnextra;
366 fnextra->func = (func_ptr) user_fn;
367 for (i = 0; i < procedureStruct->pronargs; i++)
369 fnextra->arg_toastable[i] =
370 TypeIsToastable(procedureStruct->proargtypes.values[i]);
374 /* New style: call directly */
375 finfo->fn_addr = user_fn;
378 /* Shouldn't get here if fetch_finfo_record did its job */
379 elog(ERROR, "unrecognized function API version: %d",
380 inforec->api_version);
386 * Special fmgr_info processing for other-language functions. Note
387 * that finfo->fn_oid is not valid yet.
390 fmgr_info_other_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
392 Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
393 Oid language = procedureStruct->prolang;
394 HeapTuple languageTuple;
395 Form_pg_language languageStruct;
398 languageTuple = SearchSysCache(LANGOID,
399 ObjectIdGetDatum(language),
401 if (!HeapTupleIsValid(languageTuple))
402 elog(ERROR, "cache lookup failed for language %u", language);
403 languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
405 fmgr_info(languageStruct->lanplcallfoid, &plfinfo);
406 finfo->fn_addr = plfinfo.fn_addr;
409 * If lookup of the PL handler function produced nonnull fn_extra,
410 * complain --- it must be an oldstyle function! We no longer support
411 * oldstyle PL handlers.
413 if (plfinfo.fn_extra != NULL)
414 elog(ERROR, "language %u has old-style handler", language);
416 ReleaseSysCache(languageTuple);
420 * Fetch and validate the information record for the given external function.
421 * The function is specified by a handle for the containing library
422 * (obtained from load_external_function) as well as the function name.
424 * If no info function exists for the given name, it is not an error.
425 * Instead we return a default info record for a version-0 function.
426 * We want to raise an error here only if the info function returns
429 * This function is broken out of fmgr_info_C_lang so that fmgr_c_validator
430 * can validate the information record for a function not yet entered into
433 const Pg_finfo_record *
434 fetch_finfo_record(void *filehandle, char *funcname)
437 PGFInfoFunction infofunc;
438 const Pg_finfo_record *inforec;
439 static Pg_finfo_record default_inforec = {0};
441 /* Compute name of info func */
442 infofuncname = (char *) palloc(strlen(funcname) + 10);
443 strcpy(infofuncname, "pg_finfo_");
444 strcat(infofuncname, funcname);
446 /* Try to look up the info function */
447 infofunc = (PGFInfoFunction) lookup_external_function(filehandle,
449 if (infofunc == NULL)
451 /* Not found --- assume version 0 */
453 return &default_inforec;
456 /* Found, so call it */
457 inforec = (*infofunc) ();
459 /* Validate result as best we can */
461 elog(ERROR, "null result from info function \"%s\"", infofuncname);
462 switch (inforec->api_version)
466 /* OK, no additional fields to validate */
470 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
471 errmsg("unrecognized API version %d reported by info function \"%s\"",
472 inforec->api_version, infofuncname)));
481 /*-------------------------------------------------------------------------
482 * Routines for caching lookup information for external C functions.
484 * The routines in dfmgr.c are relatively slow, so we try to avoid running
485 * them more than once per external function per session. We use a hash table
486 * with the function OID as the lookup key.
487 *-------------------------------------------------------------------------
491 * lookup_C_func: try to find a C function in the hash table
493 * If an entry exists and is up to date, return it; else return NULL
495 static CFuncHashTabEntry *
496 lookup_C_func(HeapTuple procedureTuple)
498 Oid fn_oid = HeapTupleGetOid(procedureTuple);
499 CFuncHashTabEntry *entry;
501 if (CFuncHash == NULL)
502 return NULL; /* no table yet */
503 entry = (CFuncHashTabEntry *)
504 hash_search(CFuncHash,
509 return NULL; /* no such entry */
510 if (entry->fn_xmin == HeapTupleHeaderGetXmin(procedureTuple->t_data) &&
511 ItemPointerEquals(&entry->fn_tid, &procedureTuple->t_self))
512 return entry; /* OK */
513 return NULL; /* entry is out of date */
517 * record_C_func: enter (or update) info about a C function in the hash table
520 record_C_func(HeapTuple procedureTuple,
521 PGFunction user_fn, const Pg_finfo_record *inforec)
523 Oid fn_oid = HeapTupleGetOid(procedureTuple);
524 CFuncHashTabEntry *entry;
527 /* Create the hash table if it doesn't exist yet */
528 if (CFuncHash == NULL)
532 MemSet(&hash_ctl, 0, sizeof(hash_ctl));
533 hash_ctl.keysize = sizeof(Oid);
534 hash_ctl.entrysize = sizeof(CFuncHashTabEntry);
535 hash_ctl.hash = oid_hash;
536 CFuncHash = hash_create("CFuncHash",
539 HASH_ELEM | HASH_FUNCTION);
542 entry = (CFuncHashTabEntry *)
543 hash_search(CFuncHash,
547 /* OID is already filled in */
548 entry->fn_xmin = HeapTupleHeaderGetXmin(procedureTuple->t_data);
549 entry->fn_tid = procedureTuple->t_self;
550 entry->user_fn = user_fn;
551 entry->inforec = inforec;
555 * clear_external_function_hash: remove entries for a library being closed
557 * Presently we just zap the entire hash table, but later it might be worth
558 * the effort to remove only the entries associated with the given handle.
561 clear_external_function_hash(void *filehandle)
564 hash_destroy(CFuncHash);
570 * Copy an FmgrInfo struct
572 * This is inherently somewhat bogus since we can't reliably duplicate
573 * language-dependent subsidiary info. We cheat by zeroing fn_extra,
574 * instead, meaning that subsidiary info will have to be recomputed.
577 fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo,
578 MemoryContext destcxt)
580 memcpy(dstinfo, srcinfo, sizeof(FmgrInfo));
581 dstinfo->fn_mcxt = destcxt;
582 if (dstinfo->fn_addr == fmgr_oldstyle)
584 /* For oldstyle functions we must copy fn_extra */
585 Oldstyle_fnextra *fnextra;
587 fnextra = (Oldstyle_fnextra *)
588 MemoryContextAlloc(destcxt, sizeof(Oldstyle_fnextra));
589 memcpy(fnextra, srcinfo->fn_extra, sizeof(Oldstyle_fnextra));
590 dstinfo->fn_extra = (void *) fnextra;
593 dstinfo->fn_extra = NULL;
598 * Specialized lookup routine for fmgr_internal_validator: given the alleged
599 * name of an internal function, return the OID of the function.
600 * If the name is not recognized, return InvalidOid.
603 fmgr_internal_function(const char *proname)
605 const FmgrBuiltin *fbp = fmgr_lookupByName(proname);
614 * Handler for old-style "C" language functions
617 fmgr_oldstyle(PG_FUNCTION_ARGS)
619 Oldstyle_fnextra *fnextra;
620 int n_arguments = fcinfo->nargs;
626 if (fcinfo->flinfo == NULL || fcinfo->flinfo->fn_extra == NULL)
627 elog(ERROR, "fmgr_oldstyle received NULL pointer");
628 fnextra = (Oldstyle_fnextra *) fcinfo->flinfo->fn_extra;
631 * Result is NULL if any argument is NULL, but we still call the function
632 * (peculiar, but that's the way it worked before, and after all this is a
633 * backwards-compatibility wrapper). Note, however, that we'll never get
634 * here with NULL arguments if the function is marked strict.
636 * We also need to detoast any TOAST-ed inputs, since it's unlikely that
637 * an old-style function knows about TOASTing.
640 for (i = 0; i < n_arguments; i++)
644 else if (fnextra->arg_toastable[i])
645 fcinfo->arg[i] = PointerGetDatum(PG_DETOAST_DATUM(fcinfo->arg[i]));
647 fcinfo->isnull = isnull;
649 user_fn = fnextra->func;
654 returnValue = (char *) (*user_fn) ();
659 * nullvalue() used to use isNull to check if arg is NULL; perhaps
660 * there are other functions still out there that also rely on
661 * this undocumented hack?
663 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
667 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
671 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
676 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
682 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
689 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
697 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
706 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
716 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
727 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
739 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
752 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
766 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
781 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
797 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
814 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
834 * Increasing FUNC_MAX_ARGS doesn't automatically add cases to the
835 * above code, so mention the actual value in this error not
836 * FUNC_MAX_ARGS. You could add cases to the above if you needed
837 * to support old-style functions with many arguments, but making
838 * 'em be new-style is probably a better idea.
841 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
842 errmsg("function %u has too many arguments (%d, maximum is %d)",
843 fcinfo->flinfo->fn_oid, n_arguments, 16)));
844 returnValue = NULL; /* keep compiler quiet */
848 return PointerGetDatum(returnValue);
853 * Support for security-definer and proconfig-using functions. We support
854 * both of these features using the same call handler, because they are
855 * often used together and it would be inefficient (as well as notationally
856 * messy) to have two levels of call handler involved.
858 struct fmgr_security_definer_cache
860 FmgrInfo flinfo; /* lookup info for target function */
861 Oid userid; /* userid to set, or InvalidOid */
862 ArrayType *proconfig; /* GUC values to set, or NULL */
866 * Function handler for security-definer/proconfig functions. We extract the
867 * OID of the actual function and do a fmgr lookup again. Then we fetch the
868 * pg_proc row and copy the owner ID and proconfig fields. (All this info
869 * is cached for the duration of the current query.) To execute a call,
870 * we temporarily replace the flinfo with the cached/looked-up one, while
871 * keeping the outer fcinfo (which contains all the actual arguments, etc.)
872 * intact. This is not re-entrant, but then the fcinfo itself can't be used
873 * re-entrantly anyway.
876 fmgr_security_definer(PG_FUNCTION_ARGS)
879 struct fmgr_security_definer_cache *volatile fcache;
880 FmgrInfo *save_flinfo;
883 volatile int save_nestlevel;
884 PgStat_FunctionCallUsage fcusage;
886 if (!fcinfo->flinfo->fn_extra)
889 Form_pg_proc procedureStruct;
892 MemoryContext oldcxt;
894 fcache = MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt,
897 fmgr_info_cxt_security(fcinfo->flinfo->fn_oid, &fcache->flinfo,
898 fcinfo->flinfo->fn_mcxt, true);
899 fcache->flinfo.fn_expr = fcinfo->flinfo->fn_expr;
901 tuple = SearchSysCache(PROCOID,
902 ObjectIdGetDatum(fcinfo->flinfo->fn_oid),
904 if (!HeapTupleIsValid(tuple))
905 elog(ERROR, "cache lookup failed for function %u",
906 fcinfo->flinfo->fn_oid);
907 procedureStruct = (Form_pg_proc) GETSTRUCT(tuple);
909 if (procedureStruct->prosecdef)
910 fcache->userid = procedureStruct->proowner;
912 datum = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_proconfig,
916 oldcxt = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
917 fcache->proconfig = DatumGetArrayTypePCopy(datum);
918 MemoryContextSwitchTo(oldcxt);
921 ReleaseSysCache(tuple);
923 fcinfo->flinfo->fn_extra = fcache;
926 fcache = fcinfo->flinfo->fn_extra;
928 /* GetUserIdAndContext is cheap enough that no harm in a wasted call */
929 GetUserIdAndContext(&save_userid, &save_secdefcxt);
930 if (fcache->proconfig) /* Need a new GUC nesting level */
931 save_nestlevel = NewGUCNestLevel();
933 save_nestlevel = 0; /* keep compiler quiet */
935 if (OidIsValid(fcache->userid))
936 SetUserIdAndContext(fcache->userid, true);
938 if (fcache->proconfig)
940 ProcessGUCArray(fcache->proconfig,
941 (superuser() ? PGC_SUSET : PGC_USERSET),
947 * We don't need to restore GUC or userid settings on error, because the
948 * ensuing xact or subxact abort will do that. The PG_TRY block is only
949 * needed to clean up the flinfo link.
951 save_flinfo = fcinfo->flinfo;
955 fcinfo->flinfo = &fcache->flinfo;
957 /* See notes in fmgr_info_cxt_security */
958 pgstat_init_function_usage(fcinfo, &fcusage);
960 result = FunctionCallInvoke(fcinfo);
963 * We could be calling either a regular or a set-returning function,
964 * so we have to test to see what finalize flag to use.
966 pgstat_end_function_usage(&fcusage,
967 (fcinfo->resultinfo == NULL ||
968 !IsA(fcinfo->resultinfo, ReturnSetInfo) ||
969 ((ReturnSetInfo *) fcinfo->resultinfo)->isDone != ExprMultipleResult));
973 fcinfo->flinfo = save_flinfo;
978 fcinfo->flinfo = save_flinfo;
980 if (fcache->proconfig)
981 AtEOXact_GUC(true, save_nestlevel);
982 if (OidIsValid(fcache->userid))
983 SetUserIdAndContext(save_userid, save_secdefcxt);
989 /*-------------------------------------------------------------------------
990 * Support routines for callers of fmgr-compatible functions
991 *-------------------------------------------------------------------------
995 * These are for invocation of a specifically named function with a
996 * directly-computed parameter list. Note that neither arguments nor result
997 * are allowed to be NULL. Also, the function cannot be one that needs to
998 * look at FmgrInfo, since there won't be any.
1001 DirectFunctionCall1(PGFunction func, Datum arg1)
1003 FunctionCallInfoData fcinfo;
1006 InitFunctionCallInfoData(fcinfo, NULL, 1, NULL, NULL);
1008 fcinfo.arg[0] = arg1;
1009 fcinfo.argnull[0] = false;
1011 result = (*func) (&fcinfo);
1013 /* Check for null result, since caller is clearly not expecting one */
1015 elog(ERROR, "function %p returned NULL", (void *) func);
1021 DirectFunctionCall2(PGFunction func, Datum arg1, Datum arg2)
1023 FunctionCallInfoData fcinfo;
1026 InitFunctionCallInfoData(fcinfo, NULL, 2, NULL, NULL);
1028 fcinfo.arg[0] = arg1;
1029 fcinfo.arg[1] = arg2;
1030 fcinfo.argnull[0] = false;
1031 fcinfo.argnull[1] = false;
1033 result = (*func) (&fcinfo);
1035 /* Check for null result, since caller is clearly not expecting one */
1037 elog(ERROR, "function %p returned NULL", (void *) func);
1043 DirectFunctionCall3(PGFunction func, Datum arg1, Datum arg2,
1046 FunctionCallInfoData fcinfo;
1049 InitFunctionCallInfoData(fcinfo, NULL, 3, NULL, NULL);
1051 fcinfo.arg[0] = arg1;
1052 fcinfo.arg[1] = arg2;
1053 fcinfo.arg[2] = arg3;
1054 fcinfo.argnull[0] = false;
1055 fcinfo.argnull[1] = false;
1056 fcinfo.argnull[2] = false;
1058 result = (*func) (&fcinfo);
1060 /* Check for null result, since caller is clearly not expecting one */
1062 elog(ERROR, "function %p returned NULL", (void *) func);
1068 DirectFunctionCall4(PGFunction func, Datum arg1, Datum arg2,
1069 Datum arg3, Datum arg4)
1071 FunctionCallInfoData fcinfo;
1074 InitFunctionCallInfoData(fcinfo, NULL, 4, NULL, NULL);
1076 fcinfo.arg[0] = arg1;
1077 fcinfo.arg[1] = arg2;
1078 fcinfo.arg[2] = arg3;
1079 fcinfo.arg[3] = arg4;
1080 fcinfo.argnull[0] = false;
1081 fcinfo.argnull[1] = false;
1082 fcinfo.argnull[2] = false;
1083 fcinfo.argnull[3] = false;
1085 result = (*func) (&fcinfo);
1087 /* Check for null result, since caller is clearly not expecting one */
1089 elog(ERROR, "function %p returned NULL", (void *) func);
1095 DirectFunctionCall5(PGFunction func, Datum arg1, Datum arg2,
1096 Datum arg3, Datum arg4, Datum arg5)
1098 FunctionCallInfoData fcinfo;
1101 InitFunctionCallInfoData(fcinfo, NULL, 5, NULL, NULL);
1103 fcinfo.arg[0] = arg1;
1104 fcinfo.arg[1] = arg2;
1105 fcinfo.arg[2] = arg3;
1106 fcinfo.arg[3] = arg4;
1107 fcinfo.arg[4] = arg5;
1108 fcinfo.argnull[0] = false;
1109 fcinfo.argnull[1] = false;
1110 fcinfo.argnull[2] = false;
1111 fcinfo.argnull[3] = false;
1112 fcinfo.argnull[4] = false;
1114 result = (*func) (&fcinfo);
1116 /* Check for null result, since caller is clearly not expecting one */
1118 elog(ERROR, "function %p returned NULL", (void *) func);
1124 DirectFunctionCall6(PGFunction func, Datum arg1, Datum arg2,
1125 Datum arg3, Datum arg4, Datum arg5,
1128 FunctionCallInfoData fcinfo;
1131 InitFunctionCallInfoData(fcinfo, NULL, 6, NULL, NULL);
1133 fcinfo.arg[0] = arg1;
1134 fcinfo.arg[1] = arg2;
1135 fcinfo.arg[2] = arg3;
1136 fcinfo.arg[3] = arg4;
1137 fcinfo.arg[4] = arg5;
1138 fcinfo.arg[5] = arg6;
1139 fcinfo.argnull[0] = false;
1140 fcinfo.argnull[1] = false;
1141 fcinfo.argnull[2] = false;
1142 fcinfo.argnull[3] = false;
1143 fcinfo.argnull[4] = false;
1144 fcinfo.argnull[5] = false;
1146 result = (*func) (&fcinfo);
1148 /* Check for null result, since caller is clearly not expecting one */
1150 elog(ERROR, "function %p returned NULL", (void *) func);
1156 DirectFunctionCall7(PGFunction func, Datum arg1, Datum arg2,
1157 Datum arg3, Datum arg4, Datum arg5,
1158 Datum arg6, Datum arg7)
1160 FunctionCallInfoData fcinfo;
1163 InitFunctionCallInfoData(fcinfo, NULL, 7, NULL, NULL);
1165 fcinfo.arg[0] = arg1;
1166 fcinfo.arg[1] = arg2;
1167 fcinfo.arg[2] = arg3;
1168 fcinfo.arg[3] = arg4;
1169 fcinfo.arg[4] = arg5;
1170 fcinfo.arg[5] = arg6;
1171 fcinfo.arg[6] = arg7;
1172 fcinfo.argnull[0] = false;
1173 fcinfo.argnull[1] = false;
1174 fcinfo.argnull[2] = false;
1175 fcinfo.argnull[3] = false;
1176 fcinfo.argnull[4] = false;
1177 fcinfo.argnull[5] = false;
1178 fcinfo.argnull[6] = false;
1180 result = (*func) (&fcinfo);
1182 /* Check for null result, since caller is clearly not expecting one */
1184 elog(ERROR, "function %p returned NULL", (void *) func);
1190 DirectFunctionCall8(PGFunction func, Datum arg1, Datum arg2,
1191 Datum arg3, Datum arg4, Datum arg5,
1192 Datum arg6, Datum arg7, Datum arg8)
1194 FunctionCallInfoData fcinfo;
1197 InitFunctionCallInfoData(fcinfo, NULL, 8, NULL, NULL);
1199 fcinfo.arg[0] = arg1;
1200 fcinfo.arg[1] = arg2;
1201 fcinfo.arg[2] = arg3;
1202 fcinfo.arg[3] = arg4;
1203 fcinfo.arg[4] = arg5;
1204 fcinfo.arg[5] = arg6;
1205 fcinfo.arg[6] = arg7;
1206 fcinfo.arg[7] = arg8;
1207 fcinfo.argnull[0] = false;
1208 fcinfo.argnull[1] = false;
1209 fcinfo.argnull[2] = false;
1210 fcinfo.argnull[3] = false;
1211 fcinfo.argnull[4] = false;
1212 fcinfo.argnull[5] = false;
1213 fcinfo.argnull[6] = false;
1214 fcinfo.argnull[7] = false;
1216 result = (*func) (&fcinfo);
1218 /* Check for null result, since caller is clearly not expecting one */
1220 elog(ERROR, "function %p returned NULL", (void *) func);
1226 DirectFunctionCall9(PGFunction func, Datum arg1, Datum arg2,
1227 Datum arg3, Datum arg4, Datum arg5,
1228 Datum arg6, Datum arg7, Datum arg8,
1231 FunctionCallInfoData fcinfo;
1234 InitFunctionCallInfoData(fcinfo, NULL, 9, NULL, NULL);
1236 fcinfo.arg[0] = arg1;
1237 fcinfo.arg[1] = arg2;
1238 fcinfo.arg[2] = arg3;
1239 fcinfo.arg[3] = arg4;
1240 fcinfo.arg[4] = arg5;
1241 fcinfo.arg[5] = arg6;
1242 fcinfo.arg[6] = arg7;
1243 fcinfo.arg[7] = arg8;
1244 fcinfo.arg[8] = arg9;
1245 fcinfo.argnull[0] = false;
1246 fcinfo.argnull[1] = false;
1247 fcinfo.argnull[2] = false;
1248 fcinfo.argnull[3] = false;
1249 fcinfo.argnull[4] = false;
1250 fcinfo.argnull[5] = false;
1251 fcinfo.argnull[6] = false;
1252 fcinfo.argnull[7] = false;
1253 fcinfo.argnull[8] = false;
1255 result = (*func) (&fcinfo);
1257 /* Check for null result, since caller is clearly not expecting one */
1259 elog(ERROR, "function %p returned NULL", (void *) func);
1266 * These are for invocation of a previously-looked-up function with a
1267 * directly-computed parameter list. Note that neither arguments nor result
1268 * are allowed to be NULL.
1271 FunctionCall1(FmgrInfo *flinfo, Datum arg1)
1273 FunctionCallInfoData fcinfo;
1276 InitFunctionCallInfoData(fcinfo, flinfo, 1, NULL, NULL);
1278 fcinfo.arg[0] = arg1;
1279 fcinfo.argnull[0] = false;
1281 result = FunctionCallInvoke(&fcinfo);
1283 /* Check for null result, since caller is clearly not expecting one */
1285 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1291 FunctionCall2(FmgrInfo *flinfo, Datum arg1, Datum arg2)
1294 * XXX if you change this routine, see also the inlined version in
1295 * utils/sort/tuplesort.c!
1297 FunctionCallInfoData fcinfo;
1300 InitFunctionCallInfoData(fcinfo, flinfo, 2, NULL, NULL);
1302 fcinfo.arg[0] = arg1;
1303 fcinfo.arg[1] = arg2;
1304 fcinfo.argnull[0] = false;
1305 fcinfo.argnull[1] = false;
1307 result = FunctionCallInvoke(&fcinfo);
1309 /* Check for null result, since caller is clearly not expecting one */
1311 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1317 FunctionCall3(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1320 FunctionCallInfoData fcinfo;
1323 InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);
1325 fcinfo.arg[0] = arg1;
1326 fcinfo.arg[1] = arg2;
1327 fcinfo.arg[2] = arg3;
1328 fcinfo.argnull[0] = false;
1329 fcinfo.argnull[1] = false;
1330 fcinfo.argnull[2] = false;
1332 result = FunctionCallInvoke(&fcinfo);
1334 /* Check for null result, since caller is clearly not expecting one */
1336 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1342 FunctionCall4(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1343 Datum arg3, Datum arg4)
1345 FunctionCallInfoData fcinfo;
1348 InitFunctionCallInfoData(fcinfo, flinfo, 4, NULL, NULL);
1350 fcinfo.arg[0] = arg1;
1351 fcinfo.arg[1] = arg2;
1352 fcinfo.arg[2] = arg3;
1353 fcinfo.arg[3] = arg4;
1354 fcinfo.argnull[0] = false;
1355 fcinfo.argnull[1] = false;
1356 fcinfo.argnull[2] = false;
1357 fcinfo.argnull[3] = false;
1359 result = FunctionCallInvoke(&fcinfo);
1361 /* Check for null result, since caller is clearly not expecting one */
1363 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1369 FunctionCall5(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1370 Datum arg3, Datum arg4, Datum arg5)
1372 FunctionCallInfoData fcinfo;
1375 InitFunctionCallInfoData(fcinfo, flinfo, 5, NULL, NULL);
1377 fcinfo.arg[0] = arg1;
1378 fcinfo.arg[1] = arg2;
1379 fcinfo.arg[2] = arg3;
1380 fcinfo.arg[3] = arg4;
1381 fcinfo.arg[4] = arg5;
1382 fcinfo.argnull[0] = false;
1383 fcinfo.argnull[1] = false;
1384 fcinfo.argnull[2] = false;
1385 fcinfo.argnull[3] = false;
1386 fcinfo.argnull[4] = false;
1388 result = FunctionCallInvoke(&fcinfo);
1390 /* Check for null result, since caller is clearly not expecting one */
1392 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1398 FunctionCall6(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1399 Datum arg3, Datum arg4, Datum arg5,
1402 FunctionCallInfoData fcinfo;
1405 InitFunctionCallInfoData(fcinfo, flinfo, 6, NULL, NULL);
1407 fcinfo.arg[0] = arg1;
1408 fcinfo.arg[1] = arg2;
1409 fcinfo.arg[2] = arg3;
1410 fcinfo.arg[3] = arg4;
1411 fcinfo.arg[4] = arg5;
1412 fcinfo.arg[5] = arg6;
1413 fcinfo.argnull[0] = false;
1414 fcinfo.argnull[1] = false;
1415 fcinfo.argnull[2] = false;
1416 fcinfo.argnull[3] = false;
1417 fcinfo.argnull[4] = false;
1418 fcinfo.argnull[5] = false;
1420 result = FunctionCallInvoke(&fcinfo);
1422 /* Check for null result, since caller is clearly not expecting one */
1424 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1430 FunctionCall7(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1431 Datum arg3, Datum arg4, Datum arg5,
1432 Datum arg6, Datum arg7)
1434 FunctionCallInfoData fcinfo;
1437 InitFunctionCallInfoData(fcinfo, flinfo, 7, NULL, NULL);
1439 fcinfo.arg[0] = arg1;
1440 fcinfo.arg[1] = arg2;
1441 fcinfo.arg[2] = arg3;
1442 fcinfo.arg[3] = arg4;
1443 fcinfo.arg[4] = arg5;
1444 fcinfo.arg[5] = arg6;
1445 fcinfo.arg[6] = arg7;
1446 fcinfo.argnull[0] = false;
1447 fcinfo.argnull[1] = false;
1448 fcinfo.argnull[2] = false;
1449 fcinfo.argnull[3] = false;
1450 fcinfo.argnull[4] = false;
1451 fcinfo.argnull[5] = false;
1452 fcinfo.argnull[6] = false;
1454 result = FunctionCallInvoke(&fcinfo);
1456 /* Check for null result, since caller is clearly not expecting one */
1458 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1464 FunctionCall8(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1465 Datum arg3, Datum arg4, Datum arg5,
1466 Datum arg6, Datum arg7, Datum arg8)
1468 FunctionCallInfoData fcinfo;
1471 InitFunctionCallInfoData(fcinfo, flinfo, 8, NULL, NULL);
1473 fcinfo.arg[0] = arg1;
1474 fcinfo.arg[1] = arg2;
1475 fcinfo.arg[2] = arg3;
1476 fcinfo.arg[3] = arg4;
1477 fcinfo.arg[4] = arg5;
1478 fcinfo.arg[5] = arg6;
1479 fcinfo.arg[6] = arg7;
1480 fcinfo.arg[7] = arg8;
1481 fcinfo.argnull[0] = false;
1482 fcinfo.argnull[1] = false;
1483 fcinfo.argnull[2] = false;
1484 fcinfo.argnull[3] = false;
1485 fcinfo.argnull[4] = false;
1486 fcinfo.argnull[5] = false;
1487 fcinfo.argnull[6] = false;
1488 fcinfo.argnull[7] = false;
1490 result = FunctionCallInvoke(&fcinfo);
1492 /* Check for null result, since caller is clearly not expecting one */
1494 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1500 FunctionCall9(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1501 Datum arg3, Datum arg4, Datum arg5,
1502 Datum arg6, Datum arg7, Datum arg8,
1505 FunctionCallInfoData fcinfo;
1508 InitFunctionCallInfoData(fcinfo, flinfo, 9, NULL, NULL);
1510 fcinfo.arg[0] = arg1;
1511 fcinfo.arg[1] = arg2;
1512 fcinfo.arg[2] = arg3;
1513 fcinfo.arg[3] = arg4;
1514 fcinfo.arg[4] = arg5;
1515 fcinfo.arg[5] = arg6;
1516 fcinfo.arg[6] = arg7;
1517 fcinfo.arg[7] = arg8;
1518 fcinfo.arg[8] = arg9;
1519 fcinfo.argnull[0] = false;
1520 fcinfo.argnull[1] = false;
1521 fcinfo.argnull[2] = false;
1522 fcinfo.argnull[3] = false;
1523 fcinfo.argnull[4] = false;
1524 fcinfo.argnull[5] = false;
1525 fcinfo.argnull[6] = false;
1526 fcinfo.argnull[7] = false;
1527 fcinfo.argnull[8] = false;
1529 result = FunctionCallInvoke(&fcinfo);
1531 /* Check for null result, since caller is clearly not expecting one */
1533 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1540 * These are for invocation of a function identified by OID with a
1541 * directly-computed parameter list. Note that neither arguments nor result
1542 * are allowed to be NULL. These are essentially fmgr_info() followed
1543 * by FunctionCallN(). If the same function is to be invoked repeatedly,
1544 * do the fmgr_info() once and then use FunctionCallN().
1547 OidFunctionCall1(Oid functionId, Datum arg1)
1550 FunctionCallInfoData fcinfo;
1553 fmgr_info(functionId, &flinfo);
1555 InitFunctionCallInfoData(fcinfo, &flinfo, 1, NULL, NULL);
1557 fcinfo.arg[0] = arg1;
1558 fcinfo.argnull[0] = false;
1560 result = FunctionCallInvoke(&fcinfo);
1562 /* Check for null result, since caller is clearly not expecting one */
1564 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1570 OidFunctionCall2(Oid functionId, Datum arg1, Datum arg2)
1573 FunctionCallInfoData fcinfo;
1576 fmgr_info(functionId, &flinfo);
1578 InitFunctionCallInfoData(fcinfo, &flinfo, 2, NULL, NULL);
1580 fcinfo.arg[0] = arg1;
1581 fcinfo.arg[1] = arg2;
1582 fcinfo.argnull[0] = false;
1583 fcinfo.argnull[1] = false;
1585 result = FunctionCallInvoke(&fcinfo);
1587 /* Check for null result, since caller is clearly not expecting one */
1589 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1595 OidFunctionCall3(Oid functionId, Datum arg1, Datum arg2,
1599 FunctionCallInfoData fcinfo;
1602 fmgr_info(functionId, &flinfo);
1604 InitFunctionCallInfoData(fcinfo, &flinfo, 3, NULL, NULL);
1606 fcinfo.arg[0] = arg1;
1607 fcinfo.arg[1] = arg2;
1608 fcinfo.arg[2] = arg3;
1609 fcinfo.argnull[0] = false;
1610 fcinfo.argnull[1] = false;
1611 fcinfo.argnull[2] = false;
1613 result = FunctionCallInvoke(&fcinfo);
1615 /* Check for null result, since caller is clearly not expecting one */
1617 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1623 OidFunctionCall4(Oid functionId, Datum arg1, Datum arg2,
1624 Datum arg3, Datum arg4)
1627 FunctionCallInfoData fcinfo;
1630 fmgr_info(functionId, &flinfo);
1632 InitFunctionCallInfoData(fcinfo, &flinfo, 4, NULL, NULL);
1634 fcinfo.arg[0] = arg1;
1635 fcinfo.arg[1] = arg2;
1636 fcinfo.arg[2] = arg3;
1637 fcinfo.arg[3] = arg4;
1638 fcinfo.argnull[0] = false;
1639 fcinfo.argnull[1] = false;
1640 fcinfo.argnull[2] = false;
1641 fcinfo.argnull[3] = false;
1643 result = FunctionCallInvoke(&fcinfo);
1645 /* Check for null result, since caller is clearly not expecting one */
1647 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1653 OidFunctionCall5(Oid functionId, Datum arg1, Datum arg2,
1654 Datum arg3, Datum arg4, Datum arg5)
1657 FunctionCallInfoData fcinfo;
1660 fmgr_info(functionId, &flinfo);
1662 InitFunctionCallInfoData(fcinfo, &flinfo, 5, NULL, NULL);
1664 fcinfo.arg[0] = arg1;
1665 fcinfo.arg[1] = arg2;
1666 fcinfo.arg[2] = arg3;
1667 fcinfo.arg[3] = arg4;
1668 fcinfo.arg[4] = arg5;
1669 fcinfo.argnull[0] = false;
1670 fcinfo.argnull[1] = false;
1671 fcinfo.argnull[2] = false;
1672 fcinfo.argnull[3] = false;
1673 fcinfo.argnull[4] = false;
1675 result = FunctionCallInvoke(&fcinfo);
1677 /* Check for null result, since caller is clearly not expecting one */
1679 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1685 OidFunctionCall6(Oid functionId, Datum arg1, Datum arg2,
1686 Datum arg3, Datum arg4, Datum arg5,
1690 FunctionCallInfoData fcinfo;
1693 fmgr_info(functionId, &flinfo);
1695 InitFunctionCallInfoData(fcinfo, &flinfo, 6, NULL, NULL);
1697 fcinfo.arg[0] = arg1;
1698 fcinfo.arg[1] = arg2;
1699 fcinfo.arg[2] = arg3;
1700 fcinfo.arg[3] = arg4;
1701 fcinfo.arg[4] = arg5;
1702 fcinfo.arg[5] = arg6;
1703 fcinfo.argnull[0] = false;
1704 fcinfo.argnull[1] = false;
1705 fcinfo.argnull[2] = false;
1706 fcinfo.argnull[3] = false;
1707 fcinfo.argnull[4] = false;
1708 fcinfo.argnull[5] = false;
1710 result = FunctionCallInvoke(&fcinfo);
1712 /* Check for null result, since caller is clearly not expecting one */
1714 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1720 OidFunctionCall7(Oid functionId, Datum arg1, Datum arg2,
1721 Datum arg3, Datum arg4, Datum arg5,
1722 Datum arg6, Datum arg7)
1725 FunctionCallInfoData fcinfo;
1728 fmgr_info(functionId, &flinfo);
1730 InitFunctionCallInfoData(fcinfo, &flinfo, 7, NULL, NULL);
1732 fcinfo.arg[0] = arg1;
1733 fcinfo.arg[1] = arg2;
1734 fcinfo.arg[2] = arg3;
1735 fcinfo.arg[3] = arg4;
1736 fcinfo.arg[4] = arg5;
1737 fcinfo.arg[5] = arg6;
1738 fcinfo.arg[6] = arg7;
1739 fcinfo.argnull[0] = false;
1740 fcinfo.argnull[1] = false;
1741 fcinfo.argnull[2] = false;
1742 fcinfo.argnull[3] = false;
1743 fcinfo.argnull[4] = false;
1744 fcinfo.argnull[5] = false;
1745 fcinfo.argnull[6] = false;
1747 result = FunctionCallInvoke(&fcinfo);
1749 /* Check for null result, since caller is clearly not expecting one */
1751 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1757 OidFunctionCall8(Oid functionId, Datum arg1, Datum arg2,
1758 Datum arg3, Datum arg4, Datum arg5,
1759 Datum arg6, Datum arg7, Datum arg8)
1762 FunctionCallInfoData fcinfo;
1765 fmgr_info(functionId, &flinfo);
1767 InitFunctionCallInfoData(fcinfo, &flinfo, 8, NULL, NULL);
1769 fcinfo.arg[0] = arg1;
1770 fcinfo.arg[1] = arg2;
1771 fcinfo.arg[2] = arg3;
1772 fcinfo.arg[3] = arg4;
1773 fcinfo.arg[4] = arg5;
1774 fcinfo.arg[5] = arg6;
1775 fcinfo.arg[6] = arg7;
1776 fcinfo.arg[7] = arg8;
1777 fcinfo.argnull[0] = false;
1778 fcinfo.argnull[1] = false;
1779 fcinfo.argnull[2] = false;
1780 fcinfo.argnull[3] = false;
1781 fcinfo.argnull[4] = false;
1782 fcinfo.argnull[5] = false;
1783 fcinfo.argnull[6] = false;
1784 fcinfo.argnull[7] = false;
1786 result = FunctionCallInvoke(&fcinfo);
1788 /* Check for null result, since caller is clearly not expecting one */
1790 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1796 OidFunctionCall9(Oid functionId, Datum arg1, Datum arg2,
1797 Datum arg3, Datum arg4, Datum arg5,
1798 Datum arg6, Datum arg7, Datum arg8,
1802 FunctionCallInfoData fcinfo;
1805 fmgr_info(functionId, &flinfo);
1807 InitFunctionCallInfoData(fcinfo, &flinfo, 9, NULL, NULL);
1809 fcinfo.arg[0] = arg1;
1810 fcinfo.arg[1] = arg2;
1811 fcinfo.arg[2] = arg3;
1812 fcinfo.arg[3] = arg4;
1813 fcinfo.arg[4] = arg5;
1814 fcinfo.arg[5] = arg6;
1815 fcinfo.arg[6] = arg7;
1816 fcinfo.arg[7] = arg8;
1817 fcinfo.arg[8] = arg9;
1818 fcinfo.argnull[0] = false;
1819 fcinfo.argnull[1] = false;
1820 fcinfo.argnull[2] = false;
1821 fcinfo.argnull[3] = false;
1822 fcinfo.argnull[4] = false;
1823 fcinfo.argnull[5] = false;
1824 fcinfo.argnull[6] = false;
1825 fcinfo.argnull[7] = false;
1826 fcinfo.argnull[8] = false;
1828 result = FunctionCallInvoke(&fcinfo);
1830 /* Check for null result, since caller is clearly not expecting one */
1832 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1839 * Special cases for convenient invocation of datatype I/O functions.
1843 * Call a previously-looked-up datatype input function.
1845 * "str" may be NULL to indicate we are reading a NULL. In this case
1846 * the caller should assume the result is NULL, but we'll call the input
1847 * function anyway if it's not strict. So this is almost but not quite
1848 * the same as FunctionCall3.
1851 InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
1853 FunctionCallInfoData fcinfo;
1856 if (str == NULL && flinfo->fn_strict)
1857 return (Datum) 0; /* just return null result */
1859 InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);
1861 fcinfo.arg[0] = CStringGetDatum(str);
1862 fcinfo.arg[1] = ObjectIdGetDatum(typioparam);
1863 fcinfo.arg[2] = Int32GetDatum(typmod);
1864 fcinfo.argnull[0] = (str == NULL);
1865 fcinfo.argnull[1] = false;
1866 fcinfo.argnull[2] = false;
1868 result = FunctionCallInvoke(&fcinfo);
1870 /* Should get null result if and only if str is NULL */
1874 elog(ERROR, "input function %u returned non-NULL",
1875 fcinfo.flinfo->fn_oid);
1880 elog(ERROR, "input function %u returned NULL",
1881 fcinfo.flinfo->fn_oid);
1888 * Call a previously-looked-up datatype output function.
1890 * Do not call this on NULL datums.
1892 * This is mere window dressing for FunctionCall1, but its use is recommended
1893 * anyway so that code invoking output functions can be identified easily.
1896 OutputFunctionCall(FmgrInfo *flinfo, Datum val)
1898 return DatumGetCString(FunctionCall1(flinfo, val));
1902 * Call a previously-looked-up datatype binary-input function.
1904 * "buf" may be NULL to indicate we are reading a NULL. In this case
1905 * the caller should assume the result is NULL, but we'll call the receive
1906 * function anyway if it's not strict. So this is almost but not quite
1907 * the same as FunctionCall3.
1910 ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf,
1911 Oid typioparam, int32 typmod)
1913 FunctionCallInfoData fcinfo;
1916 if (buf == NULL && flinfo->fn_strict)
1917 return (Datum) 0; /* just return null result */
1919 InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);
1921 fcinfo.arg[0] = PointerGetDatum(buf);
1922 fcinfo.arg[1] = ObjectIdGetDatum(typioparam);
1923 fcinfo.arg[2] = Int32GetDatum(typmod);
1924 fcinfo.argnull[0] = (buf == NULL);
1925 fcinfo.argnull[1] = false;
1926 fcinfo.argnull[2] = false;
1928 result = FunctionCallInvoke(&fcinfo);
1930 /* Should get null result if and only if buf is NULL */
1934 elog(ERROR, "receive function %u returned non-NULL",
1935 fcinfo.flinfo->fn_oid);
1940 elog(ERROR, "receive function %u returned NULL",
1941 fcinfo.flinfo->fn_oid);
1948 * Call a previously-looked-up datatype binary-output function.
1950 * Do not call this on NULL datums.
1952 * This is little more than window dressing for FunctionCall1, but its use is
1953 * recommended anyway so that code invoking output functions can be identified
1954 * easily. Note however that it does guarantee a non-toasted result.
1957 SendFunctionCall(FmgrInfo *flinfo, Datum val)
1959 return DatumGetByteaP(FunctionCall1(flinfo, val));
1963 * As above, for I/O functions identified by OID. These are only to be used
1964 * in seldom-executed code paths. They are not only slow but leak memory.
1967 OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
1971 fmgr_info(functionId, &flinfo);
1972 return InputFunctionCall(&flinfo, str, typioparam, typmod);
1976 OidOutputFunctionCall(Oid functionId, Datum val)
1980 fmgr_info(functionId, &flinfo);
1981 return OutputFunctionCall(&flinfo, val);
1985 OidReceiveFunctionCall(Oid functionId, StringInfo buf,
1986 Oid typioparam, int32 typmod)
1990 fmgr_info(functionId, &flinfo);
1991 return ReceiveFunctionCall(&flinfo, buf, typioparam, typmod);
1995 OidSendFunctionCall(Oid functionId, Datum val)
1999 fmgr_info(functionId, &flinfo);
2000 return SendFunctionCall(&flinfo, val);
2005 * !!! OLD INTERFACE !!!
2007 * fmgr() is the only remaining vestige of the old-style caller support
2008 * functions. It's no longer used anywhere in the Postgres distribution,
2009 * but we should leave it around for a release or two to ease the transition
2010 * for user-supplied C functions. OidFunctionCallN() replaces it for new
2013 * DEPRECATED, DO NOT USE IN NEW CODE
2016 fmgr(Oid procedureId,...)
2019 FunctionCallInfoData fcinfo;
2023 fmgr_info(procedureId, &flinfo);
2025 MemSet(&fcinfo, 0, sizeof(fcinfo));
2026 fcinfo.flinfo = &flinfo;
2027 fcinfo.nargs = flinfo.fn_nargs;
2028 n_arguments = fcinfo.nargs;
2030 if (n_arguments > 0)
2035 if (n_arguments > FUNC_MAX_ARGS)
2037 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
2038 errmsg("function %u has too many arguments (%d, maximum is %d)",
2039 flinfo.fn_oid, n_arguments, FUNC_MAX_ARGS)));
2040 va_start(pvar, procedureId);
2041 for (i = 0; i < n_arguments; i++)
2042 fcinfo.arg[i] = PointerGetDatum(va_arg(pvar, char *));
2046 result = FunctionCallInvoke(&fcinfo);
2048 /* Check for null result, since caller is clearly not expecting one */
2050 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
2052 return DatumGetPointer(result);
2056 /*-------------------------------------------------------------------------
2057 * Support routines for standard maybe-pass-by-reference datatypes
2059 * int8, float4, and float8 can be passed by value if Datum is wide enough.
2060 * (For backwards-compatibility reasons, we allow pass-by-ref to be chosen
2061 * at compile time even if pass-by-val is possible.) For the float types,
2062 * we need a support routine even if we are passing by value, because many
2063 * machines pass int and float function parameters/results differently;
2064 * so we need to play weird games with unions.
2066 * Note: there is only one switch controlling the pass-by-value option for
2067 * both int8 and float8; this is to avoid making things unduly complicated
2068 * for the timestamp types, which might have either representation.
2069 *-------------------------------------------------------------------------
2072 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
2075 Int64GetDatum(int64 X)
2077 #ifndef INT64_IS_BUSTED
2078 int64 *retval = (int64 *) palloc(sizeof(int64));
2081 return PointerGetDatum(retval);
2082 #else /* INT64_IS_BUSTED */
2085 * On a machine with no 64-bit-int C datatype, sizeof(int64) will not be
2086 * 8, but we want Int64GetDatum to return an 8-byte object anyway, with
2087 * zeroes in the unused bits. This is needed so that, for example, hash
2088 * join of int8 will behave properly.
2090 int64 *retval = (int64 *) palloc0(Max(sizeof(int64), 8));
2093 return PointerGetDatum(retval);
2094 #endif /* INT64_IS_BUSTED */
2097 #endif /* USE_FLOAT8_BYVAL */
2100 Float4GetDatum(float4 X)
2102 #ifdef USE_FLOAT4_BYVAL
2109 return SET_4_BYTES(myunion.retval);
2111 float4 *retval = (float4 *) palloc(sizeof(float4));
2114 return PointerGetDatum(retval);
2118 #ifdef USE_FLOAT4_BYVAL
2121 DatumGetFloat4(Datum X)
2128 myunion.value = GET_4_BYTES(X);
2129 return myunion.retval;
2132 #endif /* USE_FLOAT4_BYVAL */
2135 Float8GetDatum(float8 X)
2137 #ifdef USE_FLOAT8_BYVAL
2144 return SET_8_BYTES(myunion.retval);
2146 float8 *retval = (float8 *) palloc(sizeof(float8));
2149 return PointerGetDatum(retval);
2153 #ifdef USE_FLOAT8_BYVAL
2156 DatumGetFloat8(Datum X)
2163 myunion.value = GET_8_BYTES(X);
2164 return myunion.retval;
2167 #endif /* USE_FLOAT8_BYVAL */
2170 /*-------------------------------------------------------------------------
2171 * Support routines for toastable datatypes
2172 *-------------------------------------------------------------------------
2176 pg_detoast_datum(struct varlena * datum)
2178 if (VARATT_IS_EXTENDED(datum))
2179 return heap_tuple_untoast_attr(datum);
2185 pg_detoast_datum_copy(struct varlena * datum)
2187 if (VARATT_IS_EXTENDED(datum))
2188 return heap_tuple_untoast_attr(datum);
2191 /* Make a modifiable copy of the varlena object */
2192 Size len = VARSIZE(datum);
2193 struct varlena *result = (struct varlena *) palloc(len);
2195 memcpy(result, datum, len);
2201 pg_detoast_datum_slice(struct varlena * datum, int32 first, int32 count)
2203 /* Only get the specified portion from the toast rel */
2204 return heap_tuple_untoast_attr_slice(datum, first, count);
2208 pg_detoast_datum_packed(struct varlena * datum)
2210 if (VARATT_IS_COMPRESSED(datum) || VARATT_IS_EXTERNAL(datum))
2211 return heap_tuple_untoast_attr(datum);
2216 /*-------------------------------------------------------------------------
2217 * Support routines for extracting info from fn_expr parse tree
2219 * These are needed by polymorphic functions, which accept multiple possible
2220 * input types and need help from the parser to know what they've got.
2221 * Also, some functions might be interested in whether a parameter is constant.
2222 *-------------------------------------------------------------------------
2226 * Get the actual type OID of the function return type
2228 * Returns InvalidOid if information is not available
2231 get_fn_expr_rettype(FmgrInfo *flinfo)
2236 * can't return anything useful if we have no FmgrInfo or if its fn_expr
2237 * node has not been initialized
2239 if (!flinfo || !flinfo->fn_expr)
2242 expr = flinfo->fn_expr;
2244 return exprType(expr);
2248 * Get the actual type OID of a specific function argument (counting from 0)
2250 * Returns InvalidOid if information is not available
2253 get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
2256 * can't return anything useful if we have no FmgrInfo or if its fn_expr
2257 * node has not been initialized
2259 if (!flinfo || !flinfo->fn_expr)
2262 return get_call_expr_argtype(flinfo->fn_expr, argnum);
2266 * Get the actual type OID of a specific function argument (counting from 0),
2267 * but working from the calling expression tree instead of FmgrInfo
2269 * Returns InvalidOid if information is not available
2272 get_call_expr_argtype(Node *expr, int argnum)
2280 if (IsA(expr, FuncExpr))
2281 args = ((FuncExpr *) expr)->args;
2282 else if (IsA(expr, OpExpr))
2283 args = ((OpExpr *) expr)->args;
2284 else if (IsA(expr, DistinctExpr))
2285 args = ((DistinctExpr *) expr)->args;
2286 else if (IsA(expr, ScalarArrayOpExpr))
2287 args = ((ScalarArrayOpExpr *) expr)->args;
2288 else if (IsA(expr, ArrayCoerceExpr))
2289 args = list_make1(((ArrayCoerceExpr *) expr)->arg);
2290 else if (IsA(expr, NullIfExpr))
2291 args = ((NullIfExpr *) expr)->args;
2292 else if (IsA(expr, WindowFunc))
2293 args = ((WindowFunc *) expr)->args;
2297 if (argnum < 0 || argnum >= list_length(args))
2300 argtype = exprType((Node *) list_nth(args, argnum));
2303 * special hack for ScalarArrayOpExpr and ArrayCoerceExpr: what the
2304 * underlying function will actually get passed is the element type of the
2307 if (IsA(expr, ScalarArrayOpExpr) &&
2309 argtype = get_element_type(argtype);
2310 else if (IsA(expr, ArrayCoerceExpr) &&
2312 argtype = get_element_type(argtype);
2318 * Find out whether a specific function argument is constant for the
2319 * duration of a query
2321 * Returns false if information is not available
2324 get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum)
2327 * can't return anything useful if we have no FmgrInfo or if its fn_expr
2328 * node has not been initialized
2330 if (!flinfo || !flinfo->fn_expr)
2333 return get_call_expr_arg_stable(flinfo->fn_expr, argnum);
2337 * Find out whether a specific function argument is constant for the
2338 * duration of a query, but working from the calling expression tree
2340 * Returns false if information is not available
2343 get_call_expr_arg_stable(Node *expr, int argnum)
2351 if (IsA(expr, FuncExpr))
2352 args = ((FuncExpr *) expr)->args;
2353 else if (IsA(expr, OpExpr))
2354 args = ((OpExpr *) expr)->args;
2355 else if (IsA(expr, DistinctExpr))
2356 args = ((DistinctExpr *) expr)->args;
2357 else if (IsA(expr, ScalarArrayOpExpr))
2358 args = ((ScalarArrayOpExpr *) expr)->args;
2359 else if (IsA(expr, ArrayCoerceExpr))
2360 args = list_make1(((ArrayCoerceExpr *) expr)->arg);
2361 else if (IsA(expr, NullIfExpr))
2362 args = ((NullIfExpr *) expr)->args;
2363 else if (IsA(expr, WindowFunc))
2364 args = ((WindowFunc *) expr)->args;
2368 if (argnum < 0 || argnum >= list_length(args))
2371 arg = (Node *) list_nth(args, argnum);
2374 * Either a true Const or an external Param will have a value that
2375 * doesn't change during the execution of the query. In future we
2376 * might want to consider other cases too, e.g. now().
2378 if (IsA(arg, Const))
2380 if (IsA(arg, Param) &&
2381 ((Param *) arg)->paramkind == PARAM_EXTERN)