1 /*-------------------------------------------------------------------------
4 * The Postgres function manager.
6 * Portions Copyright (c) 1996-2008, 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.120 2008/06/06 22:35:22 alvherre 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 "parser/parse_expr.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)
329 prosrcattr = SysCacheGetAttr(PROCOID, procedureTuple,
330 Anum_pg_proc_prosrc, &isnull);
332 elog(ERROR, "null prosrc for function %u", functionId);
333 prosrcstring = TextDatumGetCString(prosrcattr);
335 probinattr = SysCacheGetAttr(PROCOID, procedureTuple,
336 Anum_pg_proc_probin, &isnull);
338 elog(ERROR, "null probin for function %u", functionId);
339 probinstring = TextDatumGetCString(probinattr);
341 /* Look up the function itself */
342 user_fn = load_external_function(probinstring, prosrcstring, true,
345 /* Get the function information record (real or default) */
346 inforec = fetch_finfo_record(libraryhandle, prosrcstring);
348 /* Cache the addresses for later calls */
349 record_C_func(procedureTuple, user_fn, inforec);
355 switch (inforec->api_version)
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++)
367 fnextra->arg_toastable[i] =
368 TypeIsToastable(procedureStruct->proargtypes.values[i]);
372 /* New style: call directly */
373 finfo->fn_addr = user_fn;
376 /* Shouldn't get here if fetch_finfo_record did its job */
377 elog(ERROR, "unrecognized function API version: %d",
378 inforec->api_version);
384 * Special fmgr_info processing for other-language functions. Note
385 * that finfo->fn_oid is not valid yet.
388 fmgr_info_other_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
390 Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
391 Oid language = procedureStruct->prolang;
392 HeapTuple languageTuple;
393 Form_pg_language languageStruct;
396 languageTuple = SearchSysCache(LANGOID,
397 ObjectIdGetDatum(language),
399 if (!HeapTupleIsValid(languageTuple))
400 elog(ERROR, "cache lookup failed for language %u", language);
401 languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
403 fmgr_info(languageStruct->lanplcallfoid, &plfinfo);
404 finfo->fn_addr = plfinfo.fn_addr;
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.
411 if (plfinfo.fn_extra != NULL)
412 elog(ERROR, "language %u has old-style handler", language);
414 ReleaseSysCache(languageTuple);
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.
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
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
431 const Pg_finfo_record *
432 fetch_finfo_record(void *filehandle, char *funcname)
435 PGFInfoFunction infofunc;
436 const Pg_finfo_record *inforec;
437 static Pg_finfo_record default_inforec = {0};
439 /* Compute name of info func */
440 infofuncname = (char *) palloc(strlen(funcname) + 10);
441 strcpy(infofuncname, "pg_finfo_");
442 strcat(infofuncname, funcname);
444 /* Try to look up the info function */
445 infofunc = (PGFInfoFunction) lookup_external_function(filehandle,
447 if (infofunc == NULL)
449 /* Not found --- assume version 0 */
451 return &default_inforec;
454 /* Found, so call it */
455 inforec = (*infofunc) ();
457 /* Validate result as best we can */
459 elog(ERROR, "null result from info function \"%s\"", infofuncname);
460 switch (inforec->api_version)
464 /* OK, no additional fields to validate */
468 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
469 errmsg("unrecognized API version %d reported by info function \"%s\"",
470 inforec->api_version, infofuncname)));
479 /*-------------------------------------------------------------------------
480 * Routines for caching lookup information for external C functions.
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 *-------------------------------------------------------------------------
489 * lookup_C_func: try to find a C function in the hash table
491 * If an entry exists and is up to date, return it; else return NULL
493 static CFuncHashTabEntry *
494 lookup_C_func(HeapTuple procedureTuple)
496 Oid fn_oid = HeapTupleGetOid(procedureTuple);
497 CFuncHashTabEntry *entry;
499 if (CFuncHash == NULL)
500 return NULL; /* no table yet */
501 entry = (CFuncHashTabEntry *)
502 hash_search(CFuncHash,
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 */
515 * record_C_func: enter (or update) info about a C function in the hash table
518 record_C_func(HeapTuple procedureTuple,
519 PGFunction user_fn, const Pg_finfo_record *inforec)
521 Oid fn_oid = HeapTupleGetOid(procedureTuple);
522 CFuncHashTabEntry *entry;
525 /* Create the hash table if it doesn't exist yet */
526 if (CFuncHash == NULL)
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",
537 HASH_ELEM | HASH_FUNCTION);
540 entry = (CFuncHashTabEntry *)
541 hash_search(CFuncHash,
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;
553 * clear_external_function_hash: remove entries for a library being closed
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.
559 clear_external_function_hash(void *filehandle)
562 hash_destroy(CFuncHash);
568 * Copy an FmgrInfo struct
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.
575 fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo,
576 MemoryContext destcxt)
578 memcpy(dstinfo, srcinfo, sizeof(FmgrInfo));
579 dstinfo->fn_mcxt = destcxt;
580 if (dstinfo->fn_addr == fmgr_oldstyle)
582 /* For oldstyle functions we must copy fn_extra */
583 Oldstyle_fnextra *fnextra;
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;
591 dstinfo->fn_extra = NULL;
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.
601 fmgr_internal_function(const char *proname)
603 const FmgrBuiltin *fbp = fmgr_lookupByName(proname);
612 * Handler for old-style "C" language functions
615 fmgr_oldstyle(PG_FUNCTION_ARGS)
617 Oldstyle_fnextra *fnextra;
618 int n_arguments = fcinfo->nargs;
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;
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.
634 * We also need to detoast any TOAST-ed inputs, since it's unlikely that
635 * an old-style function knows about TOASTing.
638 for (i = 0; i < n_arguments; i++)
642 else if (fnextra->arg_toastable[i])
643 fcinfo->arg[i] = PointerGetDatum(PG_DETOAST_DATUM(fcinfo->arg[i]));
645 fcinfo->isnull = isnull;
647 user_fn = fnextra->func;
652 returnValue = (char *) (*user_fn) ();
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?
661 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
665 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
669 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
674 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
680 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
687 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
695 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
704 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
714 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
725 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
737 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
750 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
764 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
779 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
795 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
812 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
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.
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 */
846 return PointerGetDatum(returnValue);
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.
856 struct fmgr_security_definer_cache
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 */
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.
874 fmgr_security_definer(PG_FUNCTION_ARGS)
877 struct fmgr_security_definer_cache *volatile fcache;
878 FmgrInfo *save_flinfo;
881 volatile int save_nestlevel;
882 PgStat_FunctionCallUsage fcusage;
884 if (!fcinfo->flinfo->fn_extra)
887 Form_pg_proc procedureStruct;
890 MemoryContext oldcxt;
892 fcache = MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt,
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;
899 tuple = SearchSysCache(PROCOID,
900 ObjectIdGetDatum(fcinfo->flinfo->fn_oid),
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);
907 if (procedureStruct->prosecdef)
908 fcache->userid = procedureStruct->proowner;
910 datum = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_proconfig,
914 oldcxt = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
915 fcache->proconfig = DatumGetArrayTypePCopy(datum);
916 MemoryContextSwitchTo(oldcxt);
919 ReleaseSysCache(tuple);
921 fcinfo->flinfo->fn_extra = fcache;
924 fcache = fcinfo->flinfo->fn_extra;
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();
931 save_nestlevel = 0; /* keep compiler quiet */
933 if (OidIsValid(fcache->userid))
934 SetUserIdAndContext(fcache->userid, true);
936 if (fcache->proconfig)
938 ProcessGUCArray(fcache->proconfig,
939 (superuser() ? PGC_SUSET : PGC_USERSET),
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.
949 save_flinfo = fcinfo->flinfo;
953 fcinfo->flinfo = &fcache->flinfo;
955 /* See notes in fmgr_info_cxt_security */
956 pgstat_init_function_usage(fcinfo, &fcusage);
958 result = FunctionCallInvoke(fcinfo);
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.
964 pgstat_end_function_usage(&fcusage,
965 (fcinfo->resultinfo == NULL ||
966 !IsA(fcinfo->resultinfo, ReturnSetInfo) ||
967 ((ReturnSetInfo *) fcinfo->resultinfo)->isDone != ExprMultipleResult));
971 fcinfo->flinfo = save_flinfo;
976 fcinfo->flinfo = save_flinfo;
978 if (fcache->proconfig)
979 AtEOXact_GUC(true, save_nestlevel);
980 if (OidIsValid(fcache->userid))
981 SetUserIdAndContext(save_userid, save_secdefcxt);
987 /*-------------------------------------------------------------------------
988 * Support routines for callers of fmgr-compatible functions
989 *-------------------------------------------------------------------------
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.
999 DirectFunctionCall1(PGFunction func, Datum arg1)
1001 FunctionCallInfoData fcinfo;
1004 InitFunctionCallInfoData(fcinfo, NULL, 1, NULL, NULL);
1006 fcinfo.arg[0] = arg1;
1007 fcinfo.argnull[0] = false;
1009 result = (*func) (&fcinfo);
1011 /* Check for null result, since caller is clearly not expecting one */
1013 elog(ERROR, "function %p returned NULL", (void *) func);
1019 DirectFunctionCall2(PGFunction func, Datum arg1, Datum arg2)
1021 FunctionCallInfoData fcinfo;
1024 InitFunctionCallInfoData(fcinfo, NULL, 2, NULL, NULL);
1026 fcinfo.arg[0] = arg1;
1027 fcinfo.arg[1] = arg2;
1028 fcinfo.argnull[0] = false;
1029 fcinfo.argnull[1] = false;
1031 result = (*func) (&fcinfo);
1033 /* Check for null result, since caller is clearly not expecting one */
1035 elog(ERROR, "function %p returned NULL", (void *) func);
1041 DirectFunctionCall3(PGFunction func, Datum arg1, Datum arg2,
1044 FunctionCallInfoData fcinfo;
1047 InitFunctionCallInfoData(fcinfo, NULL, 3, NULL, NULL);
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;
1056 result = (*func) (&fcinfo);
1058 /* Check for null result, since caller is clearly not expecting one */
1060 elog(ERROR, "function %p returned NULL", (void *) func);
1066 DirectFunctionCall4(PGFunction func, Datum arg1, Datum arg2,
1067 Datum arg3, Datum arg4)
1069 FunctionCallInfoData fcinfo;
1072 InitFunctionCallInfoData(fcinfo, NULL, 4, NULL, NULL);
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;
1083 result = (*func) (&fcinfo);
1085 /* Check for null result, since caller is clearly not expecting one */
1087 elog(ERROR, "function %p returned NULL", (void *) func);
1093 DirectFunctionCall5(PGFunction func, Datum arg1, Datum arg2,
1094 Datum arg3, Datum arg4, Datum arg5)
1096 FunctionCallInfoData fcinfo;
1099 InitFunctionCallInfoData(fcinfo, NULL, 5, NULL, NULL);
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;
1112 result = (*func) (&fcinfo);
1114 /* Check for null result, since caller is clearly not expecting one */
1116 elog(ERROR, "function %p returned NULL", (void *) func);
1122 DirectFunctionCall6(PGFunction func, Datum arg1, Datum arg2,
1123 Datum arg3, Datum arg4, Datum arg5,
1126 FunctionCallInfoData fcinfo;
1129 InitFunctionCallInfoData(fcinfo, NULL, 6, NULL, NULL);
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;
1144 result = (*func) (&fcinfo);
1146 /* Check for null result, since caller is clearly not expecting one */
1148 elog(ERROR, "function %p returned NULL", (void *) func);
1154 DirectFunctionCall7(PGFunction func, Datum arg1, Datum arg2,
1155 Datum arg3, Datum arg4, Datum arg5,
1156 Datum arg6, Datum arg7)
1158 FunctionCallInfoData fcinfo;
1161 InitFunctionCallInfoData(fcinfo, NULL, 7, NULL, NULL);
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;
1178 result = (*func) (&fcinfo);
1180 /* Check for null result, since caller is clearly not expecting one */
1182 elog(ERROR, "function %p returned NULL", (void *) func);
1188 DirectFunctionCall8(PGFunction func, Datum arg1, Datum arg2,
1189 Datum arg3, Datum arg4, Datum arg5,
1190 Datum arg6, Datum arg7, Datum arg8)
1192 FunctionCallInfoData fcinfo;
1195 InitFunctionCallInfoData(fcinfo, NULL, 8, NULL, NULL);
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;
1214 result = (*func) (&fcinfo);
1216 /* Check for null result, since caller is clearly not expecting one */
1218 elog(ERROR, "function %p returned NULL", (void *) func);
1224 DirectFunctionCall9(PGFunction func, Datum arg1, Datum arg2,
1225 Datum arg3, Datum arg4, Datum arg5,
1226 Datum arg6, Datum arg7, Datum arg8,
1229 FunctionCallInfoData fcinfo;
1232 InitFunctionCallInfoData(fcinfo, NULL, 9, NULL, NULL);
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;
1253 result = (*func) (&fcinfo);
1255 /* Check for null result, since caller is clearly not expecting one */
1257 elog(ERROR, "function %p returned NULL", (void *) func);
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.
1269 FunctionCall1(FmgrInfo *flinfo, Datum arg1)
1271 FunctionCallInfoData fcinfo;
1274 InitFunctionCallInfoData(fcinfo, flinfo, 1, NULL, NULL);
1276 fcinfo.arg[0] = arg1;
1277 fcinfo.argnull[0] = false;
1279 result = FunctionCallInvoke(&fcinfo);
1281 /* Check for null result, since caller is clearly not expecting one */
1283 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1289 FunctionCall2(FmgrInfo *flinfo, Datum arg1, Datum arg2)
1292 * XXX if you change this routine, see also the inlined version in
1293 * utils/sort/tuplesort.c!
1295 FunctionCallInfoData fcinfo;
1298 InitFunctionCallInfoData(fcinfo, flinfo, 2, NULL, NULL);
1300 fcinfo.arg[0] = arg1;
1301 fcinfo.arg[1] = arg2;
1302 fcinfo.argnull[0] = false;
1303 fcinfo.argnull[1] = false;
1305 result = FunctionCallInvoke(&fcinfo);
1307 /* Check for null result, since caller is clearly not expecting one */
1309 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1315 FunctionCall3(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1318 FunctionCallInfoData fcinfo;
1321 InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);
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;
1330 result = FunctionCallInvoke(&fcinfo);
1332 /* Check for null result, since caller is clearly not expecting one */
1334 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1340 FunctionCall4(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1341 Datum arg3, Datum arg4)
1343 FunctionCallInfoData fcinfo;
1346 InitFunctionCallInfoData(fcinfo, flinfo, 4, NULL, NULL);
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;
1357 result = FunctionCallInvoke(&fcinfo);
1359 /* Check for null result, since caller is clearly not expecting one */
1361 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1367 FunctionCall5(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1368 Datum arg3, Datum arg4, Datum arg5)
1370 FunctionCallInfoData fcinfo;
1373 InitFunctionCallInfoData(fcinfo, flinfo, 5, NULL, NULL);
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;
1386 result = FunctionCallInvoke(&fcinfo);
1388 /* Check for null result, since caller is clearly not expecting one */
1390 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1396 FunctionCall6(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1397 Datum arg3, Datum arg4, Datum arg5,
1400 FunctionCallInfoData fcinfo;
1403 InitFunctionCallInfoData(fcinfo, flinfo, 6, NULL, NULL);
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;
1418 result = FunctionCallInvoke(&fcinfo);
1420 /* Check for null result, since caller is clearly not expecting one */
1422 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1428 FunctionCall7(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1429 Datum arg3, Datum arg4, Datum arg5,
1430 Datum arg6, Datum arg7)
1432 FunctionCallInfoData fcinfo;
1435 InitFunctionCallInfoData(fcinfo, flinfo, 7, NULL, NULL);
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;
1452 result = FunctionCallInvoke(&fcinfo);
1454 /* Check for null result, since caller is clearly not expecting one */
1456 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1462 FunctionCall8(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1463 Datum arg3, Datum arg4, Datum arg5,
1464 Datum arg6, Datum arg7, Datum arg8)
1466 FunctionCallInfoData fcinfo;
1469 InitFunctionCallInfoData(fcinfo, flinfo, 8, NULL, NULL);
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;
1488 result = FunctionCallInvoke(&fcinfo);
1490 /* Check for null result, since caller is clearly not expecting one */
1492 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1498 FunctionCall9(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1499 Datum arg3, Datum arg4, Datum arg5,
1500 Datum arg6, Datum arg7, Datum arg8,
1503 FunctionCallInfoData fcinfo;
1506 InitFunctionCallInfoData(fcinfo, flinfo, 9, NULL, NULL);
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;
1527 result = FunctionCallInvoke(&fcinfo);
1529 /* Check for null result, since caller is clearly not expecting one */
1531 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
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().
1545 OidFunctionCall1(Oid functionId, Datum arg1)
1548 FunctionCallInfoData fcinfo;
1551 fmgr_info(functionId, &flinfo);
1553 InitFunctionCallInfoData(fcinfo, &flinfo, 1, NULL, NULL);
1555 fcinfo.arg[0] = arg1;
1556 fcinfo.argnull[0] = false;
1558 result = FunctionCallInvoke(&fcinfo);
1560 /* Check for null result, since caller is clearly not expecting one */
1562 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1568 OidFunctionCall2(Oid functionId, Datum arg1, Datum arg2)
1571 FunctionCallInfoData fcinfo;
1574 fmgr_info(functionId, &flinfo);
1576 InitFunctionCallInfoData(fcinfo, &flinfo, 2, NULL, NULL);
1578 fcinfo.arg[0] = arg1;
1579 fcinfo.arg[1] = arg2;
1580 fcinfo.argnull[0] = false;
1581 fcinfo.argnull[1] = false;
1583 result = FunctionCallInvoke(&fcinfo);
1585 /* Check for null result, since caller is clearly not expecting one */
1587 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1593 OidFunctionCall3(Oid functionId, Datum arg1, Datum arg2,
1597 FunctionCallInfoData fcinfo;
1600 fmgr_info(functionId, &flinfo);
1602 InitFunctionCallInfoData(fcinfo, &flinfo, 3, NULL, NULL);
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;
1611 result = FunctionCallInvoke(&fcinfo);
1613 /* Check for null result, since caller is clearly not expecting one */
1615 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1621 OidFunctionCall4(Oid functionId, Datum arg1, Datum arg2,
1622 Datum arg3, Datum arg4)
1625 FunctionCallInfoData fcinfo;
1628 fmgr_info(functionId, &flinfo);
1630 InitFunctionCallInfoData(fcinfo, &flinfo, 4, NULL, NULL);
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;
1641 result = FunctionCallInvoke(&fcinfo);
1643 /* Check for null result, since caller is clearly not expecting one */
1645 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1651 OidFunctionCall5(Oid functionId, Datum arg1, Datum arg2,
1652 Datum arg3, Datum arg4, Datum arg5)
1655 FunctionCallInfoData fcinfo;
1658 fmgr_info(functionId, &flinfo);
1660 InitFunctionCallInfoData(fcinfo, &flinfo, 5, NULL, NULL);
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;
1673 result = FunctionCallInvoke(&fcinfo);
1675 /* Check for null result, since caller is clearly not expecting one */
1677 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1683 OidFunctionCall6(Oid functionId, Datum arg1, Datum arg2,
1684 Datum arg3, Datum arg4, Datum arg5,
1688 FunctionCallInfoData fcinfo;
1691 fmgr_info(functionId, &flinfo);
1693 InitFunctionCallInfoData(fcinfo, &flinfo, 6, NULL, NULL);
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;
1708 result = FunctionCallInvoke(&fcinfo);
1710 /* Check for null result, since caller is clearly not expecting one */
1712 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1718 OidFunctionCall7(Oid functionId, Datum arg1, Datum arg2,
1719 Datum arg3, Datum arg4, Datum arg5,
1720 Datum arg6, Datum arg7)
1723 FunctionCallInfoData fcinfo;
1726 fmgr_info(functionId, &flinfo);
1728 InitFunctionCallInfoData(fcinfo, &flinfo, 7, NULL, NULL);
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;
1745 result = FunctionCallInvoke(&fcinfo);
1747 /* Check for null result, since caller is clearly not expecting one */
1749 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1755 OidFunctionCall8(Oid functionId, Datum arg1, Datum arg2,
1756 Datum arg3, Datum arg4, Datum arg5,
1757 Datum arg6, Datum arg7, Datum arg8)
1760 FunctionCallInfoData fcinfo;
1763 fmgr_info(functionId, &flinfo);
1765 InitFunctionCallInfoData(fcinfo, &flinfo, 8, NULL, NULL);
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;
1784 result = FunctionCallInvoke(&fcinfo);
1786 /* Check for null result, since caller is clearly not expecting one */
1788 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1794 OidFunctionCall9(Oid functionId, Datum arg1, Datum arg2,
1795 Datum arg3, Datum arg4, Datum arg5,
1796 Datum arg6, Datum arg7, Datum arg8,
1800 FunctionCallInfoData fcinfo;
1803 fmgr_info(functionId, &flinfo);
1805 InitFunctionCallInfoData(fcinfo, &flinfo, 9, NULL, NULL);
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;
1826 result = FunctionCallInvoke(&fcinfo);
1828 /* Check for null result, since caller is clearly not expecting one */
1830 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1837 * Special cases for convenient invocation of datatype I/O functions.
1841 * Call a previously-looked-up datatype input function.
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.
1849 InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
1851 FunctionCallInfoData fcinfo;
1854 if (str == NULL && flinfo->fn_strict)
1855 return (Datum) 0; /* just return null result */
1857 InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);
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;
1866 result = FunctionCallInvoke(&fcinfo);
1868 /* Should get null result if and only if str is NULL */
1872 elog(ERROR, "input function %u returned non-NULL",
1873 fcinfo.flinfo->fn_oid);
1878 elog(ERROR, "input function %u returned NULL",
1879 fcinfo.flinfo->fn_oid);
1886 * Call a previously-looked-up datatype output function.
1888 * Do not call this on NULL datums.
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.
1894 OutputFunctionCall(FmgrInfo *flinfo, Datum val)
1896 return DatumGetCString(FunctionCall1(flinfo, val));
1900 * Call a previously-looked-up datatype binary-input function.
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.
1908 ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf,
1909 Oid typioparam, int32 typmod)
1911 FunctionCallInfoData fcinfo;
1914 if (buf == NULL && flinfo->fn_strict)
1915 return (Datum) 0; /* just return null result */
1917 InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);
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;
1926 result = FunctionCallInvoke(&fcinfo);
1928 /* Should get null result if and only if buf is NULL */
1932 elog(ERROR, "receive function %u returned non-NULL",
1933 fcinfo.flinfo->fn_oid);
1938 elog(ERROR, "receive function %u returned NULL",
1939 fcinfo.flinfo->fn_oid);
1946 * Call a previously-looked-up datatype binary-output function.
1948 * Do not call this on NULL datums.
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.
1955 SendFunctionCall(FmgrInfo *flinfo, Datum val)
1957 return DatumGetByteaP(FunctionCall1(flinfo, val));
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.
1965 OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
1969 fmgr_info(functionId, &flinfo);
1970 return InputFunctionCall(&flinfo, str, typioparam, typmod);
1974 OidOutputFunctionCall(Oid functionId, Datum val)
1978 fmgr_info(functionId, &flinfo);
1979 return OutputFunctionCall(&flinfo, val);
1983 OidReceiveFunctionCall(Oid functionId, StringInfo buf,
1984 Oid typioparam, int32 typmod)
1988 fmgr_info(functionId, &flinfo);
1989 return ReceiveFunctionCall(&flinfo, buf, typioparam, typmod);
1993 OidSendFunctionCall(Oid functionId, Datum val)
1997 fmgr_info(functionId, &flinfo);
1998 return SendFunctionCall(&flinfo, val);
2003 * !!! OLD INTERFACE !!!
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
2011 * DEPRECATED, DO NOT USE IN NEW CODE
2014 fmgr(Oid procedureId,...)
2017 FunctionCallInfoData fcinfo;
2021 fmgr_info(procedureId, &flinfo);
2023 MemSet(&fcinfo, 0, sizeof(fcinfo));
2024 fcinfo.flinfo = &flinfo;
2025 fcinfo.nargs = flinfo.fn_nargs;
2026 n_arguments = fcinfo.nargs;
2028 if (n_arguments > 0)
2033 if (n_arguments > FUNC_MAX_ARGS)
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 *));
2044 result = FunctionCallInvoke(&fcinfo);
2046 /* Check for null result, since caller is clearly not expecting one */
2048 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
2050 return DatumGetPointer(result);
2054 /*-------------------------------------------------------------------------
2055 * Support routines for standard maybe-pass-by-reference datatypes
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.
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 *-------------------------------------------------------------------------
2070 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
2073 Int64GetDatum(int64 X)
2075 #ifndef INT64_IS_BUSTED
2076 int64 *retval = (int64 *) palloc(sizeof(int64));
2079 return PointerGetDatum(retval);
2080 #else /* INT64_IS_BUSTED */
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.
2088 int64 *retval = (int64 *) palloc0(Max(sizeof(int64), 8));
2091 return PointerGetDatum(retval);
2092 #endif /* INT64_IS_BUSTED */
2095 #endif /* USE_FLOAT8_BYVAL */
2098 Float4GetDatum(float4 X)
2100 #ifdef USE_FLOAT4_BYVAL
2107 return SET_4_BYTES(myunion.retval);
2109 float4 *retval = (float4 *) palloc(sizeof(float4));
2112 return PointerGetDatum(retval);
2116 #ifdef USE_FLOAT4_BYVAL
2119 DatumGetFloat4(Datum X)
2126 myunion.value = GET_4_BYTES(X);
2127 return myunion.retval;
2130 #endif /* USE_FLOAT4_BYVAL */
2133 Float8GetDatum(float8 X)
2135 #ifdef USE_FLOAT8_BYVAL
2142 return SET_8_BYTES(myunion.retval);
2144 float8 *retval = (float8 *) palloc(sizeof(float8));
2147 return PointerGetDatum(retval);
2151 #ifdef USE_FLOAT8_BYVAL
2154 DatumGetFloat8(Datum X)
2161 myunion.value = GET_8_BYTES(X);
2162 return myunion.retval;
2165 #endif /* USE_FLOAT8_BYVAL */
2168 /*-------------------------------------------------------------------------
2169 * Support routines for toastable datatypes
2170 *-------------------------------------------------------------------------
2174 pg_detoast_datum(struct varlena * datum)
2176 if (VARATT_IS_EXTENDED(datum))
2177 return heap_tuple_untoast_attr(datum);
2183 pg_detoast_datum_copy(struct varlena * datum)
2185 if (VARATT_IS_EXTENDED(datum))
2186 return heap_tuple_untoast_attr(datum);
2189 /* Make a modifiable copy of the varlena object */
2190 Size len = VARSIZE(datum);
2191 struct varlena *result = (struct varlena *) palloc(len);
2193 memcpy(result, datum, len);
2199 pg_detoast_datum_slice(struct varlena * datum, int32 first, int32 count)
2201 /* Only get the specified portion from the toast rel */
2202 return heap_tuple_untoast_attr_slice(datum, first, count);
2206 pg_detoast_datum_packed(struct varlena * datum)
2208 if (VARATT_IS_COMPRESSED(datum) || VARATT_IS_EXTERNAL(datum))
2209 return heap_tuple_untoast_attr(datum);
2214 /*-------------------------------------------------------------------------
2215 * Support routines for extracting info from fn_expr parse tree
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 *-------------------------------------------------------------------------
2223 * Get the actual type OID of the function return type
2225 * Returns InvalidOid if information is not available
2228 get_fn_expr_rettype(FmgrInfo *flinfo)
2233 * can't return anything useful if we have no FmgrInfo or if its fn_expr
2234 * node has not been initialized
2236 if (!flinfo || !flinfo->fn_expr)
2239 expr = flinfo->fn_expr;
2241 return exprType(expr);
2245 * Get the actual type OID of a specific function argument (counting from 0)
2247 * Returns InvalidOid if information is not available
2250 get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
2253 * can't return anything useful if we have no FmgrInfo or if its fn_expr
2254 * node has not been initialized
2256 if (!flinfo || !flinfo->fn_expr)
2259 return get_call_expr_argtype(flinfo->fn_expr, argnum);
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
2266 * Returns InvalidOid if information is not available
2269 get_call_expr_argtype(Node *expr, int argnum)
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;
2292 if (argnum < 0 || argnum >= list_length(args))
2295 argtype = exprType((Node *) list_nth(args, argnum));
2298 * special hack for ScalarArrayOpExpr and ArrayCoerceExpr: what the
2299 * underlying function will actually get passed is the element type of the
2302 if (IsA(expr, ScalarArrayOpExpr) &&
2304 argtype = get_element_type(argtype);
2305 else if (IsA(expr, ArrayCoerceExpr) &&
2307 argtype = get_element_type(argtype);