1 /*-------------------------------------------------------------------------
4 * The Postgres function manager.
6 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/utils/fmgr/fmgr.c
13 *-------------------------------------------------------------------------
18 #include "access/detoast.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/acl.h"
27 #include "utils/builtins.h"
28 #include "utils/fmgrtab.h"
29 #include "utils/guc.h"
30 #include "utils/lsyscache.h"
31 #include "utils/syscache.h"
34 * Hooks for function calls
36 PGDLLIMPORT needs_fmgr_hook_type needs_fmgr_hook = NULL;
37 PGDLLIMPORT fmgr_hook_type fmgr_hook = NULL;
40 * Hashtable for fast lookup of external C functions
44 /* fn_oid is the hash key and so must be first! */
45 Oid fn_oid; /* OID of an external C function */
46 TransactionId fn_xmin; /* for checking up-to-dateness */
47 ItemPointerData fn_tid;
48 PGFunction user_fn; /* the function's address */
49 const Pg_finfo_record *inforec; /* address of its info record */
52 static HTAB *CFuncHash = NULL;
55 static void fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt,
56 bool ignore_security);
57 static void fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple);
58 static void fmgr_info_other_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple);
59 static CFuncHashTabEntry *lookup_C_func(HeapTuple procedureTuple);
60 static void record_C_func(HeapTuple procedureTuple,
61 PGFunction user_fn, const Pg_finfo_record *inforec);
63 /* extern so it's callable via JIT */
64 extern Datum fmgr_security_definer(PG_FUNCTION_ARGS);
68 * Lookup routines for builtin-function table. We can search by either Oid
69 * or name, but search by Oid is much faster.
72 static const FmgrBuiltin *
73 fmgr_isbuiltin(Oid id)
77 /* fast lookup only possible if original oid still assigned */
78 if (id > fmgr_last_builtin_oid)
82 * Lookup function data. If there's a miss in that range it's likely a
83 * nonexistent function, returning NULL here will trigger an ERROR later.
85 index = fmgr_builtin_oid_index[id];
86 if (index == InvalidOidBuiltinMapping)
89 return &fmgr_builtins[index];
93 * Lookup a builtin by name. Note there can be more than one entry in
94 * the array with the same name, but they should all point to the same
97 static const FmgrBuiltin *
98 fmgr_lookupByName(const char *name)
102 for (i = 0; i < fmgr_nbuiltins; i++)
104 if (strcmp(name, fmgr_builtins[i].funcName) == 0)
105 return fmgr_builtins + i;
111 * This routine fills a FmgrInfo struct, given the OID
112 * of the function to be called.
114 * The caller's CurrentMemoryContext is used as the fn_mcxt of the info
115 * struct; this means that any subsidiary data attached to the info struct
116 * (either by fmgr_info itself, or later on by a function call handler)
117 * will be allocated in that context. The caller must ensure that this
118 * context is at least as long-lived as the info struct itself. This is
119 * not a problem in typical cases where the info struct is on the stack or
120 * in freshly-palloc'd space. However, if one intends to store an info
121 * struct in a long-lived table, it's better to use fmgr_info_cxt.
124 fmgr_info(Oid functionId, FmgrInfo *finfo)
126 fmgr_info_cxt_security(functionId, finfo, CurrentMemoryContext, false);
130 * Fill a FmgrInfo struct, specifying a memory context in which its
131 * subsidiary data should go.
134 fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
136 fmgr_info_cxt_security(functionId, finfo, mcxt, false);
140 * This one does the actual work. ignore_security is ordinarily false
141 * but is set to true when we need to avoid recursion.
144 fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt,
145 bool ignore_security)
147 const FmgrBuiltin *fbp;
148 HeapTuple procedureTuple;
149 Form_pg_proc procedureStruct;
155 * fn_oid *must* be filled in last. Some code assumes that if fn_oid is
156 * valid, the whole struct is valid. Some FmgrInfo struct's do survive
159 finfo->fn_oid = InvalidOid;
160 finfo->fn_extra = NULL;
161 finfo->fn_mcxt = mcxt;
162 finfo->fn_expr = NULL; /* caller may set this later */
164 if ((fbp = fmgr_isbuiltin(functionId)) != NULL)
167 * Fast path for builtin functions: don't bother consulting pg_proc
169 finfo->fn_nargs = fbp->nargs;
170 finfo->fn_strict = fbp->strict;
171 finfo->fn_retset = fbp->retset;
172 finfo->fn_stats = TRACK_FUNC_ALL; /* ie, never track */
173 finfo->fn_addr = fbp->func;
174 finfo->fn_oid = functionId;
178 /* Otherwise we need the pg_proc entry */
179 procedureTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
180 if (!HeapTupleIsValid(procedureTuple))
181 elog(ERROR, "cache lookup failed for function %u", functionId);
182 procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
184 finfo->fn_nargs = procedureStruct->pronargs;
185 finfo->fn_strict = procedureStruct->proisstrict;
186 finfo->fn_retset = procedureStruct->proretset;
189 * If it has prosecdef set, non-null proconfig, or if a plugin wants to
190 * hook function entry/exit, use fmgr_security_definer call handler ---
191 * unless we are being called again by fmgr_security_definer or
192 * fmgr_info_other_lang.
194 * When using fmgr_security_definer, function stats tracking is always
195 * disabled at the outer level, and instead we set the flag properly in
196 * fmgr_security_definer's private flinfo and implement the tracking
197 * inside fmgr_security_definer. This loses the ability to charge the
198 * overhead of fmgr_security_definer to the function, but gains the
199 * ability to set the track_functions GUC as a local GUC parameter of an
200 * interesting function and have the right things happen.
202 if (!ignore_security &&
203 (procedureStruct->prosecdef ||
204 !heap_attisnull(procedureTuple, Anum_pg_proc_proconfig, NULL) ||
205 FmgrHookIsNeeded(functionId)))
207 finfo->fn_addr = fmgr_security_definer;
208 finfo->fn_stats = TRACK_FUNC_ALL; /* ie, never track */
209 finfo->fn_oid = functionId;
210 ReleaseSysCache(procedureTuple);
214 switch (procedureStruct->prolang)
216 case INTERNALlanguageId:
219 * For an ordinary builtin function, we should never get here
220 * because the fmgr_isbuiltin() search above will have succeeded.
221 * However, if the user has done a CREATE FUNCTION to create an
222 * alias for a builtin function, we can end up here. In that case
223 * we have to look up the function by name. The name of the
224 * internal function is stored in prosrc (it doesn't have to be
225 * the same as the name of the alias!)
227 prosrcdatum = SysCacheGetAttr(PROCOID, procedureTuple,
228 Anum_pg_proc_prosrc, &isnull);
230 elog(ERROR, "null prosrc");
231 prosrc = TextDatumGetCString(prosrcdatum);
232 fbp = fmgr_lookupByName(prosrc);
235 (errcode(ERRCODE_UNDEFINED_FUNCTION),
236 errmsg("internal function \"%s\" is not in internal lookup table",
239 /* Should we check that nargs, strict, retset match the table? */
240 finfo->fn_addr = fbp->func;
241 /* note this policy is also assumed in fast path above */
242 finfo->fn_stats = TRACK_FUNC_ALL; /* ie, never track */
246 fmgr_info_C_lang(functionId, finfo, procedureTuple);
247 finfo->fn_stats = TRACK_FUNC_PL; /* ie, track if ALL */
251 finfo->fn_addr = fmgr_sql;
252 finfo->fn_stats = TRACK_FUNC_PL; /* ie, track if ALL */
256 fmgr_info_other_lang(functionId, finfo, procedureTuple);
257 finfo->fn_stats = TRACK_FUNC_OFF; /* ie, track if not OFF */
261 finfo->fn_oid = functionId;
262 ReleaseSysCache(procedureTuple);
266 * Return module and C function name providing implementation of functionId.
268 * If *mod == NULL and *fn == NULL, no C symbol is known to implement
271 * If *mod == NULL and *fn != NULL, the function is implemented by a symbol in
274 * If *mod != NULL and *fn !=NULL the function is implemented in an extension
277 * The returned module and function names are pstrdup'ed into the current
281 fmgr_symbol(Oid functionId, char **mod, char **fn)
283 HeapTuple procedureTuple;
284 Form_pg_proc procedureStruct;
289 /* Otherwise we need the pg_proc entry */
290 procedureTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
291 if (!HeapTupleIsValid(procedureTuple))
292 elog(ERROR, "cache lookup failed for function %u", functionId);
293 procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
297 if (procedureStruct->prosecdef ||
298 !heap_attisnull(procedureTuple, Anum_pg_proc_proconfig, NULL) ||
299 FmgrHookIsNeeded(functionId))
301 *mod = NULL; /* core binary */
302 *fn = pstrdup("fmgr_security_definer");
303 ReleaseSysCache(procedureTuple);
307 /* see fmgr_info_cxt_security for the individual cases */
308 switch (procedureStruct->prolang)
310 case INTERNALlanguageId:
311 prosrcattr = SysCacheGetAttr(PROCOID, procedureTuple,
312 Anum_pg_proc_prosrc, &isnull);
314 elog(ERROR, "null prosrc");
316 *mod = NULL; /* core binary */
317 *fn = TextDatumGetCString(prosrcattr);
321 prosrcattr = SysCacheGetAttr(PROCOID, procedureTuple,
322 Anum_pg_proc_prosrc, &isnull);
324 elog(ERROR, "null prosrc for C function %u", functionId);
326 probinattr = SysCacheGetAttr(PROCOID, procedureTuple,
327 Anum_pg_proc_probin, &isnull);
329 elog(ERROR, "null probin for C function %u", functionId);
332 * No need to check symbol presence / API version here, already
333 * checked in fmgr_info_cxt_security.
335 *mod = TextDatumGetCString(probinattr);
336 *fn = TextDatumGetCString(prosrcattr);
340 *mod = NULL; /* core binary */
341 *fn = pstrdup("fmgr_sql");
346 *fn = NULL; /* unknown, pass pointer */
350 ReleaseSysCache(procedureTuple);
355 * Special fmgr_info processing for C-language functions. Note that
356 * finfo->fn_oid is not valid yet.
359 fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
361 CFuncHashTabEntry *hashentry;
363 const Pg_finfo_record *inforec;
367 * See if we have the function address cached already
369 hashentry = lookup_C_func(procedureTuple);
372 user_fn = hashentry->user_fn;
373 inforec = hashentry->inforec;
384 * Get prosrc and probin strings (link symbol and library filename).
385 * While in general these columns might be null, that's not allowed
386 * for C-language functions.
388 prosrcattr = SysCacheGetAttr(PROCOID, procedureTuple,
389 Anum_pg_proc_prosrc, &isnull);
391 elog(ERROR, "null prosrc for C function %u", functionId);
392 prosrcstring = TextDatumGetCString(prosrcattr);
394 probinattr = SysCacheGetAttr(PROCOID, procedureTuple,
395 Anum_pg_proc_probin, &isnull);
397 elog(ERROR, "null probin for C function %u", functionId);
398 probinstring = TextDatumGetCString(probinattr);
400 /* Look up the function itself */
401 user_fn = load_external_function(probinstring, prosrcstring, true,
404 /* Get the function information record (real or default) */
405 inforec = fetch_finfo_record(libraryhandle, prosrcstring);
407 /* Cache the addresses for later calls */
408 record_C_func(procedureTuple, user_fn, inforec);
414 switch (inforec->api_version)
417 /* New style: call directly */
418 finfo->fn_addr = user_fn;
421 /* Shouldn't get here if fetch_finfo_record did its job */
422 elog(ERROR, "unrecognized function API version: %d",
423 inforec->api_version);
429 * Special fmgr_info processing for other-language functions. Note
430 * that finfo->fn_oid is not valid yet.
433 fmgr_info_other_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
435 Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
436 Oid language = procedureStruct->prolang;
437 HeapTuple languageTuple;
438 Form_pg_language languageStruct;
441 languageTuple = SearchSysCache1(LANGOID, ObjectIdGetDatum(language));
442 if (!HeapTupleIsValid(languageTuple))
443 elog(ERROR, "cache lookup failed for language %u", language);
444 languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
447 * Look up the language's call handler function, ignoring any attributes
448 * that would normally cause insertion of fmgr_security_definer. We need
449 * to get back a bare pointer to the actual C-language function.
451 fmgr_info_cxt_security(languageStruct->lanplcallfoid, &plfinfo,
452 CurrentMemoryContext, true);
453 finfo->fn_addr = plfinfo.fn_addr;
455 ReleaseSysCache(languageTuple);
459 * Fetch and validate the information record for the given external function.
460 * The function is specified by a handle for the containing library
461 * (obtained from load_external_function) as well as the function name.
463 * If no info function exists for the given name an error is raised.
465 * This function is broken out of fmgr_info_C_lang so that fmgr_c_validator
466 * can validate the information record for a function not yet entered into
469 const Pg_finfo_record *
470 fetch_finfo_record(void *filehandle, const char *funcname)
473 PGFInfoFunction infofunc;
474 const Pg_finfo_record *inforec;
476 infofuncname = psprintf("pg_finfo_%s", funcname);
478 /* Try to look up the info function */
479 infofunc = (PGFInfoFunction) lookup_external_function(filehandle,
481 if (infofunc == NULL)
484 (errcode(ERRCODE_UNDEFINED_FUNCTION),
485 errmsg("could not find function information for function \"%s\"",
487 errhint("SQL-callable functions need an accompanying PG_FUNCTION_INFO_V1(funcname).")));
488 return NULL; /* silence compiler */
491 /* Found, so call it */
492 inforec = (*infofunc) ();
494 /* Validate result as best we can */
496 elog(ERROR, "null result from info function \"%s\"", infofuncname);
497 switch (inforec->api_version)
500 /* OK, no additional fields to validate */
504 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
505 errmsg("unrecognized API version %d reported by info function \"%s\"",
506 inforec->api_version, infofuncname)));
515 /*-------------------------------------------------------------------------
516 * Routines for caching lookup information for external C functions.
518 * The routines in dfmgr.c are relatively slow, so we try to avoid running
519 * them more than once per external function per session. We use a hash table
520 * with the function OID as the lookup key.
521 *-------------------------------------------------------------------------
525 * lookup_C_func: try to find a C function in the hash table
527 * If an entry exists and is up to date, return it; else return NULL
529 static CFuncHashTabEntry *
530 lookup_C_func(HeapTuple procedureTuple)
532 Oid fn_oid = ((Form_pg_proc) GETSTRUCT(procedureTuple))->oid;
533 CFuncHashTabEntry *entry;
535 if (CFuncHash == NULL)
536 return NULL; /* no table yet */
537 entry = (CFuncHashTabEntry *)
538 hash_search(CFuncHash,
543 return NULL; /* no such entry */
544 if (entry->fn_xmin == HeapTupleHeaderGetRawXmin(procedureTuple->t_data) &&
545 ItemPointerEquals(&entry->fn_tid, &procedureTuple->t_self))
546 return entry; /* OK */
547 return NULL; /* entry is out of date */
551 * record_C_func: enter (or update) info about a C function in the hash table
554 record_C_func(HeapTuple procedureTuple,
555 PGFunction user_fn, const Pg_finfo_record *inforec)
557 Oid fn_oid = ((Form_pg_proc) GETSTRUCT(procedureTuple))->oid;
558 CFuncHashTabEntry *entry;
561 /* Create the hash table if it doesn't exist yet */
562 if (CFuncHash == NULL)
566 MemSet(&hash_ctl, 0, sizeof(hash_ctl));
567 hash_ctl.keysize = sizeof(Oid);
568 hash_ctl.entrysize = sizeof(CFuncHashTabEntry);
569 CFuncHash = hash_create("CFuncHash",
572 HASH_ELEM | HASH_BLOBS);
575 entry = (CFuncHashTabEntry *)
576 hash_search(CFuncHash,
580 /* OID is already filled in */
581 entry->fn_xmin = HeapTupleHeaderGetRawXmin(procedureTuple->t_data);
582 entry->fn_tid = procedureTuple->t_self;
583 entry->user_fn = user_fn;
584 entry->inforec = inforec;
588 * clear_external_function_hash: remove entries for a library being closed
590 * Presently we just zap the entire hash table, but later it might be worth
591 * the effort to remove only the entries associated with the given handle.
594 clear_external_function_hash(void *filehandle)
597 hash_destroy(CFuncHash);
603 * Copy an FmgrInfo struct
605 * This is inherently somewhat bogus since we can't reliably duplicate
606 * language-dependent subsidiary info. We cheat by zeroing fn_extra,
607 * instead, meaning that subsidiary info will have to be recomputed.
610 fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo,
611 MemoryContext destcxt)
613 memcpy(dstinfo, srcinfo, sizeof(FmgrInfo));
614 dstinfo->fn_mcxt = destcxt;
615 dstinfo->fn_extra = NULL;
620 * Specialized lookup routine for fmgr_internal_validator: given the alleged
621 * name of an internal function, return the OID of the function.
622 * If the name is not recognized, return InvalidOid.
625 fmgr_internal_function(const char *proname)
627 const FmgrBuiltin *fbp = fmgr_lookupByName(proname);
636 * Support for security-definer and proconfig-using functions. We support
637 * both of these features using the same call handler, because they are
638 * often used together and it would be inefficient (as well as notationally
639 * messy) to have two levels of call handler involved.
641 struct fmgr_security_definer_cache
643 FmgrInfo flinfo; /* lookup info for target function */
644 Oid userid; /* userid to set, or InvalidOid */
645 ArrayType *proconfig; /* GUC values to set, or NULL */
646 Datum arg; /* passthrough argument for plugin modules */
650 * Function handler for security-definer/proconfig/plugin-hooked functions.
651 * We extract the OID of the actual function and do a fmgr lookup again.
652 * Then we fetch the pg_proc row and copy the owner ID and proconfig fields.
653 * (All this info is cached for the duration of the current query.)
654 * To execute a call, we temporarily replace the flinfo with the cached
655 * and looked-up one, while keeping the outer fcinfo (which contains all
656 * the actual arguments, etc.) intact. This is not re-entrant, but then
657 * the fcinfo itself can't be used reentrantly anyway.
660 fmgr_security_definer(PG_FUNCTION_ARGS)
663 struct fmgr_security_definer_cache *volatile fcache;
664 FmgrInfo *save_flinfo;
666 int save_sec_context;
667 volatile int save_nestlevel;
668 PgStat_FunctionCallUsage fcusage;
670 if (!fcinfo->flinfo->fn_extra)
673 Form_pg_proc procedureStruct;
676 MemoryContext oldcxt;
678 fcache = MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt,
681 fmgr_info_cxt_security(fcinfo->flinfo->fn_oid, &fcache->flinfo,
682 fcinfo->flinfo->fn_mcxt, true);
683 fcache->flinfo.fn_expr = fcinfo->flinfo->fn_expr;
685 tuple = SearchSysCache1(PROCOID,
686 ObjectIdGetDatum(fcinfo->flinfo->fn_oid));
687 if (!HeapTupleIsValid(tuple))
688 elog(ERROR, "cache lookup failed for function %u",
689 fcinfo->flinfo->fn_oid);
690 procedureStruct = (Form_pg_proc) GETSTRUCT(tuple);
692 if (procedureStruct->prosecdef)
693 fcache->userid = procedureStruct->proowner;
695 datum = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_proconfig,
699 oldcxt = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
700 fcache->proconfig = DatumGetArrayTypePCopy(datum);
701 MemoryContextSwitchTo(oldcxt);
704 ReleaseSysCache(tuple);
706 fcinfo->flinfo->fn_extra = fcache;
709 fcache = fcinfo->flinfo->fn_extra;
711 /* GetUserIdAndSecContext is cheap enough that no harm in a wasted call */
712 GetUserIdAndSecContext(&save_userid, &save_sec_context);
713 if (fcache->proconfig) /* Need a new GUC nesting level */
714 save_nestlevel = NewGUCNestLevel();
716 save_nestlevel = 0; /* keep compiler quiet */
718 if (OidIsValid(fcache->userid))
719 SetUserIdAndSecContext(fcache->userid,
720 save_sec_context | SECURITY_LOCAL_USERID_CHANGE);
722 if (fcache->proconfig)
724 ProcessGUCArray(fcache->proconfig,
725 (superuser() ? PGC_SUSET : PGC_USERSET),
730 /* function manager hook */
732 (*fmgr_hook) (FHET_START, &fcache->flinfo, &fcache->arg);
735 * We don't need to restore GUC or userid settings on error, because the
736 * ensuing xact or subxact abort will do that. The PG_TRY block is only
737 * needed to clean up the flinfo link.
739 save_flinfo = fcinfo->flinfo;
743 fcinfo->flinfo = &fcache->flinfo;
745 /* See notes in fmgr_info_cxt_security */
746 pgstat_init_function_usage(fcinfo, &fcusage);
748 result = FunctionCallInvoke(fcinfo);
751 * We could be calling either a regular or a set-returning function,
752 * so we have to test to see what finalize flag to use.
754 pgstat_end_function_usage(&fcusage,
755 (fcinfo->resultinfo == NULL ||
756 !IsA(fcinfo->resultinfo, ReturnSetInfo) ||
757 ((ReturnSetInfo *) fcinfo->resultinfo)->isDone != ExprMultipleResult));
761 fcinfo->flinfo = save_flinfo;
763 (*fmgr_hook) (FHET_ABORT, &fcache->flinfo, &fcache->arg);
768 fcinfo->flinfo = save_flinfo;
770 if (fcache->proconfig)
771 AtEOXact_GUC(true, save_nestlevel);
772 if (OidIsValid(fcache->userid))
773 SetUserIdAndSecContext(save_userid, save_sec_context);
775 (*fmgr_hook) (FHET_END, &fcache->flinfo, &fcache->arg);
781 /*-------------------------------------------------------------------------
782 * Support routines for callers of fmgr-compatible functions
783 *-------------------------------------------------------------------------
787 * These are for invocation of a specifically named function with a
788 * directly-computed parameter list. Note that neither arguments nor result
789 * are allowed to be NULL. Also, the function cannot be one that needs to
790 * look at FmgrInfo, since there won't be any.
793 DirectFunctionCall1Coll(PGFunction func, Oid collation, Datum arg1)
795 LOCAL_FCINFO(fcinfo, 1);
798 InitFunctionCallInfoData(*fcinfo, NULL, 1, collation, NULL, NULL);
800 fcinfo->args[0].value = arg1;
801 fcinfo->args[0].isnull = false;
803 result = (*func) (fcinfo);
805 /* Check for null result, since caller is clearly not expecting one */
807 elog(ERROR, "function %p returned NULL", (void *) func);
813 DirectFunctionCall2Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2)
815 LOCAL_FCINFO(fcinfo, 2);
818 InitFunctionCallInfoData(*fcinfo, NULL, 2, collation, NULL, NULL);
820 fcinfo->args[0].value = arg1;
821 fcinfo->args[0].isnull = false;
822 fcinfo->args[1].value = arg2;
823 fcinfo->args[1].isnull = false;
825 result = (*func) (fcinfo);
827 /* Check for null result, since caller is clearly not expecting one */
829 elog(ERROR, "function %p returned NULL", (void *) func);
835 DirectFunctionCall3Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
838 LOCAL_FCINFO(fcinfo, 3);
841 InitFunctionCallInfoData(*fcinfo, NULL, 3, collation, NULL, NULL);
843 fcinfo->args[0].value = arg1;
844 fcinfo->args[0].isnull = false;
845 fcinfo->args[1].value = arg2;
846 fcinfo->args[1].isnull = false;
847 fcinfo->args[2].value = arg3;
848 fcinfo->args[2].isnull = false;
850 result = (*func) (fcinfo);
852 /* Check for null result, since caller is clearly not expecting one */
854 elog(ERROR, "function %p returned NULL", (void *) func);
860 DirectFunctionCall4Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
861 Datum arg3, Datum arg4)
863 LOCAL_FCINFO(fcinfo, 4);
866 InitFunctionCallInfoData(*fcinfo, NULL, 4, collation, NULL, NULL);
868 fcinfo->args[0].value = arg1;
869 fcinfo->args[0].isnull = false;
870 fcinfo->args[1].value = arg2;
871 fcinfo->args[1].isnull = false;
872 fcinfo->args[2].value = arg3;
873 fcinfo->args[2].isnull = false;
874 fcinfo->args[3].value = arg4;
875 fcinfo->args[3].isnull = false;
877 result = (*func) (fcinfo);
879 /* Check for null result, since caller is clearly not expecting one */
881 elog(ERROR, "function %p returned NULL", (void *) func);
887 DirectFunctionCall5Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
888 Datum arg3, Datum arg4, Datum arg5)
890 LOCAL_FCINFO(fcinfo, 5);
893 InitFunctionCallInfoData(*fcinfo, NULL, 5, collation, NULL, NULL);
895 fcinfo->args[0].value = arg1;
896 fcinfo->args[0].isnull = false;
897 fcinfo->args[1].value = arg2;
898 fcinfo->args[1].isnull = false;
899 fcinfo->args[2].value = arg3;
900 fcinfo->args[2].isnull = false;
901 fcinfo->args[3].value = arg4;
902 fcinfo->args[3].isnull = false;
903 fcinfo->args[4].value = arg5;
904 fcinfo->args[4].isnull = false;
906 result = (*func) (fcinfo);
908 /* Check for null result, since caller is clearly not expecting one */
910 elog(ERROR, "function %p returned NULL", (void *) func);
916 DirectFunctionCall6Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
917 Datum arg3, Datum arg4, Datum arg5,
920 LOCAL_FCINFO(fcinfo, 6);
923 InitFunctionCallInfoData(*fcinfo, NULL, 6, collation, NULL, NULL);
925 fcinfo->args[0].value = arg1;
926 fcinfo->args[0].isnull = false;
927 fcinfo->args[1].value = arg2;
928 fcinfo->args[1].isnull = false;
929 fcinfo->args[2].value = arg3;
930 fcinfo->args[2].isnull = false;
931 fcinfo->args[3].value = arg4;
932 fcinfo->args[3].isnull = false;
933 fcinfo->args[4].value = arg5;
934 fcinfo->args[4].isnull = false;
935 fcinfo->args[5].value = arg6;
936 fcinfo->args[5].isnull = false;
938 result = (*func) (fcinfo);
940 /* Check for null result, since caller is clearly not expecting one */
942 elog(ERROR, "function %p returned NULL", (void *) func);
948 DirectFunctionCall7Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
949 Datum arg3, Datum arg4, Datum arg5,
950 Datum arg6, Datum arg7)
952 LOCAL_FCINFO(fcinfo, 7);
955 InitFunctionCallInfoData(*fcinfo, NULL, 7, collation, NULL, NULL);
957 fcinfo->args[0].value = arg1;
958 fcinfo->args[0].isnull = false;
959 fcinfo->args[1].value = arg2;
960 fcinfo->args[1].isnull = false;
961 fcinfo->args[2].value = arg3;
962 fcinfo->args[2].isnull = false;
963 fcinfo->args[3].value = arg4;
964 fcinfo->args[3].isnull = false;
965 fcinfo->args[4].value = arg5;
966 fcinfo->args[4].isnull = false;
967 fcinfo->args[5].value = arg6;
968 fcinfo->args[5].isnull = false;
969 fcinfo->args[6].value = arg7;
970 fcinfo->args[6].isnull = false;
972 result = (*func) (fcinfo);
974 /* Check for null result, since caller is clearly not expecting one */
976 elog(ERROR, "function %p returned NULL", (void *) func);
982 DirectFunctionCall8Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
983 Datum arg3, Datum arg4, Datum arg5,
984 Datum arg6, Datum arg7, Datum arg8)
986 LOCAL_FCINFO(fcinfo, 8);
989 InitFunctionCallInfoData(*fcinfo, NULL, 8, collation, NULL, NULL);
991 fcinfo->args[0].value = arg1;
992 fcinfo->args[0].isnull = false;
993 fcinfo->args[1].value = arg2;
994 fcinfo->args[1].isnull = false;
995 fcinfo->args[2].value = arg3;
996 fcinfo->args[2].isnull = false;
997 fcinfo->args[3].value = arg4;
998 fcinfo->args[3].isnull = false;
999 fcinfo->args[4].value = arg5;
1000 fcinfo->args[4].isnull = false;
1001 fcinfo->args[5].value = arg6;
1002 fcinfo->args[5].isnull = false;
1003 fcinfo->args[6].value = arg7;
1004 fcinfo->args[6].isnull = false;
1005 fcinfo->args[7].value = arg8;
1006 fcinfo->args[7].isnull = false;
1008 result = (*func) (fcinfo);
1010 /* Check for null result, since caller is clearly not expecting one */
1012 elog(ERROR, "function %p returned NULL", (void *) func);
1018 DirectFunctionCall9Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
1019 Datum arg3, Datum arg4, Datum arg5,
1020 Datum arg6, Datum arg7, Datum arg8,
1023 LOCAL_FCINFO(fcinfo, 9);
1026 InitFunctionCallInfoData(*fcinfo, NULL, 9, collation, NULL, NULL);
1028 fcinfo->args[0].value = arg1;
1029 fcinfo->args[0].isnull = false;
1030 fcinfo->args[1].value = arg2;
1031 fcinfo->args[1].isnull = false;
1032 fcinfo->args[2].value = arg3;
1033 fcinfo->args[2].isnull = false;
1034 fcinfo->args[3].value = arg4;
1035 fcinfo->args[3].isnull = false;
1036 fcinfo->args[4].value = arg5;
1037 fcinfo->args[4].isnull = false;
1038 fcinfo->args[5].value = arg6;
1039 fcinfo->args[5].isnull = false;
1040 fcinfo->args[6].value = arg7;
1041 fcinfo->args[6].isnull = false;
1042 fcinfo->args[7].value = arg8;
1043 fcinfo->args[7].isnull = false;
1044 fcinfo->args[8].value = arg9;
1045 fcinfo->args[8].isnull = false;
1047 result = (*func) (fcinfo);
1049 /* Check for null result, since caller is clearly not expecting one */
1051 elog(ERROR, "function %p returned NULL", (void *) func);
1057 * These functions work like the DirectFunctionCall functions except that
1058 * they use the flinfo parameter to initialise the fcinfo for the call.
1059 * It's recommended that the callee only use the fn_extra and fn_mcxt
1060 * fields, as other fields will typically describe the calling function
1061 * not the callee. Conversely, the calling function should not have
1062 * used fn_extra, unless its use is known to be compatible with the callee's.
1066 CallerFInfoFunctionCall1(PGFunction func, FmgrInfo *flinfo, Oid collation, Datum arg1)
1068 LOCAL_FCINFO(fcinfo, 1);
1071 InitFunctionCallInfoData(*fcinfo, flinfo, 1, collation, NULL, NULL);
1073 fcinfo->args[0].value = arg1;
1074 fcinfo->args[0].isnull = false;
1076 result = (*func) (fcinfo);
1078 /* Check for null result, since caller is clearly not expecting one */
1080 elog(ERROR, "function %p returned NULL", (void *) func);
1086 CallerFInfoFunctionCall2(PGFunction func, FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
1088 LOCAL_FCINFO(fcinfo, 2);
1091 InitFunctionCallInfoData(*fcinfo, flinfo, 2, collation, NULL, NULL);
1093 fcinfo->args[0].value = arg1;
1094 fcinfo->args[0].isnull = false;
1095 fcinfo->args[1].value = arg2;
1096 fcinfo->args[1].isnull = false;
1098 result = (*func) (fcinfo);
1100 /* Check for null result, since caller is clearly not expecting one */
1102 elog(ERROR, "function %p returned NULL", (void *) func);
1108 * These are for invocation of a previously-looked-up function with a
1109 * directly-computed parameter list. Note that neither arguments nor result
1110 * are allowed to be NULL.
1113 FunctionCall0Coll(FmgrInfo *flinfo, Oid collation)
1115 LOCAL_FCINFO(fcinfo, 0);
1118 InitFunctionCallInfoData(*fcinfo, flinfo, 0, collation, NULL, NULL);
1120 result = FunctionCallInvoke(fcinfo);
1122 /* Check for null result, since caller is clearly not expecting one */
1124 elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
1130 FunctionCall1Coll(FmgrInfo *flinfo, Oid collation, Datum arg1)
1132 LOCAL_FCINFO(fcinfo, 1);
1135 InitFunctionCallInfoData(*fcinfo, flinfo, 1, collation, NULL, NULL);
1137 fcinfo->args[0].value = arg1;
1138 fcinfo->args[0].isnull = false;
1140 result = FunctionCallInvoke(fcinfo);
1142 /* Check for null result, since caller is clearly not expecting one */
1144 elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
1150 FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
1152 LOCAL_FCINFO(fcinfo, 2);
1155 InitFunctionCallInfoData(*fcinfo, flinfo, 2, collation, NULL, NULL);
1157 fcinfo->args[0].value = arg1;
1158 fcinfo->args[0].isnull = false;
1159 fcinfo->args[1].value = arg2;
1160 fcinfo->args[1].isnull = false;
1162 result = FunctionCallInvoke(fcinfo);
1164 /* Check for null result, since caller is clearly not expecting one */
1166 elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
1172 FunctionCall3Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
1175 LOCAL_FCINFO(fcinfo, 3);
1178 InitFunctionCallInfoData(*fcinfo, flinfo, 3, collation, NULL, NULL);
1180 fcinfo->args[0].value = arg1;
1181 fcinfo->args[0].isnull = false;
1182 fcinfo->args[1].value = arg2;
1183 fcinfo->args[1].isnull = false;
1184 fcinfo->args[2].value = arg3;
1185 fcinfo->args[2].isnull = false;
1187 result = FunctionCallInvoke(fcinfo);
1189 /* Check for null result, since caller is clearly not expecting one */
1191 elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
1197 FunctionCall4Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
1198 Datum arg3, Datum arg4)
1200 LOCAL_FCINFO(fcinfo, 4);
1203 InitFunctionCallInfoData(*fcinfo, flinfo, 4, collation, NULL, NULL);
1205 fcinfo->args[0].value = arg1;
1206 fcinfo->args[0].isnull = false;
1207 fcinfo->args[1].value = arg2;
1208 fcinfo->args[1].isnull = false;
1209 fcinfo->args[2].value = arg3;
1210 fcinfo->args[2].isnull = false;
1211 fcinfo->args[3].value = arg4;
1212 fcinfo->args[3].isnull = false;
1214 result = FunctionCallInvoke(fcinfo);
1216 /* Check for null result, since caller is clearly not expecting one */
1218 elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
1224 FunctionCall5Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
1225 Datum arg3, Datum arg4, Datum arg5)
1227 LOCAL_FCINFO(fcinfo, 5);
1230 InitFunctionCallInfoData(*fcinfo, flinfo, 5, collation, NULL, NULL);
1232 fcinfo->args[0].value = arg1;
1233 fcinfo->args[0].isnull = false;
1234 fcinfo->args[1].value = arg2;
1235 fcinfo->args[1].isnull = false;
1236 fcinfo->args[2].value = arg3;
1237 fcinfo->args[2].isnull = false;
1238 fcinfo->args[3].value = arg4;
1239 fcinfo->args[3].isnull = false;
1240 fcinfo->args[4].value = arg5;
1241 fcinfo->args[4].isnull = false;
1243 result = FunctionCallInvoke(fcinfo);
1245 /* Check for null result, since caller is clearly not expecting one */
1247 elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
1253 FunctionCall6Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
1254 Datum arg3, Datum arg4, Datum arg5,
1257 LOCAL_FCINFO(fcinfo, 6);
1260 InitFunctionCallInfoData(*fcinfo, flinfo, 6, collation, NULL, NULL);
1262 fcinfo->args[0].value = arg1;
1263 fcinfo->args[0].isnull = false;
1264 fcinfo->args[1].value = arg2;
1265 fcinfo->args[1].isnull = false;
1266 fcinfo->args[2].value = arg3;
1267 fcinfo->args[2].isnull = false;
1268 fcinfo->args[3].value = arg4;
1269 fcinfo->args[3].isnull = false;
1270 fcinfo->args[4].value = arg5;
1271 fcinfo->args[4].isnull = false;
1272 fcinfo->args[5].value = arg6;
1273 fcinfo->args[5].isnull = false;
1275 result = FunctionCallInvoke(fcinfo);
1277 /* Check for null result, since caller is clearly not expecting one */
1279 elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
1285 FunctionCall7Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
1286 Datum arg3, Datum arg4, Datum arg5,
1287 Datum arg6, Datum arg7)
1289 LOCAL_FCINFO(fcinfo, 7);
1292 InitFunctionCallInfoData(*fcinfo, flinfo, 7, collation, NULL, NULL);
1294 fcinfo->args[0].value = arg1;
1295 fcinfo->args[0].isnull = false;
1296 fcinfo->args[1].value = arg2;
1297 fcinfo->args[1].isnull = false;
1298 fcinfo->args[2].value = arg3;
1299 fcinfo->args[2].isnull = false;
1300 fcinfo->args[3].value = arg4;
1301 fcinfo->args[3].isnull = false;
1302 fcinfo->args[4].value = arg5;
1303 fcinfo->args[4].isnull = false;
1304 fcinfo->args[5].value = arg6;
1305 fcinfo->args[5].isnull = false;
1306 fcinfo->args[6].value = arg7;
1307 fcinfo->args[6].isnull = false;
1309 result = FunctionCallInvoke(fcinfo);
1311 /* Check for null result, since caller is clearly not expecting one */
1313 elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
1319 FunctionCall8Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
1320 Datum arg3, Datum arg4, Datum arg5,
1321 Datum arg6, Datum arg7, Datum arg8)
1323 LOCAL_FCINFO(fcinfo, 8);
1326 InitFunctionCallInfoData(*fcinfo, flinfo, 8, collation, NULL, NULL);
1328 fcinfo->args[0].value = arg1;
1329 fcinfo->args[0].isnull = false;
1330 fcinfo->args[1].value = arg2;
1331 fcinfo->args[1].isnull = false;
1332 fcinfo->args[2].value = arg3;
1333 fcinfo->args[2].isnull = false;
1334 fcinfo->args[3].value = arg4;
1335 fcinfo->args[3].isnull = false;
1336 fcinfo->args[4].value = arg5;
1337 fcinfo->args[4].isnull = false;
1338 fcinfo->args[5].value = arg6;
1339 fcinfo->args[5].isnull = false;
1340 fcinfo->args[6].value = arg7;
1341 fcinfo->args[6].isnull = false;
1342 fcinfo->args[7].value = arg8;
1343 fcinfo->args[7].isnull = false;
1345 result = FunctionCallInvoke(fcinfo);
1347 /* Check for null result, since caller is clearly not expecting one */
1349 elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
1355 FunctionCall9Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
1356 Datum arg3, Datum arg4, Datum arg5,
1357 Datum arg6, Datum arg7, Datum arg8,
1360 LOCAL_FCINFO(fcinfo, 9);
1363 InitFunctionCallInfoData(*fcinfo, flinfo, 9, collation, NULL, NULL);
1365 fcinfo->args[0].value = arg1;
1366 fcinfo->args[0].isnull = false;
1367 fcinfo->args[1].value = arg2;
1368 fcinfo->args[1].isnull = false;
1369 fcinfo->args[2].value = arg3;
1370 fcinfo->args[2].isnull = false;
1371 fcinfo->args[3].value = arg4;
1372 fcinfo->args[3].isnull = false;
1373 fcinfo->args[4].value = arg5;
1374 fcinfo->args[4].isnull = false;
1375 fcinfo->args[5].value = arg6;
1376 fcinfo->args[5].isnull = false;
1377 fcinfo->args[6].value = arg7;
1378 fcinfo->args[6].isnull = false;
1379 fcinfo->args[7].value = arg8;
1380 fcinfo->args[7].isnull = false;
1381 fcinfo->args[8].value = arg9;
1382 fcinfo->args[8].isnull = false;
1384 result = FunctionCallInvoke(fcinfo);
1386 /* Check for null result, since caller is clearly not expecting one */
1388 elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
1395 * These are for invocation of a function identified by OID with a
1396 * directly-computed parameter list. Note that neither arguments nor result
1397 * are allowed to be NULL. These are essentially fmgr_info() followed
1398 * by FunctionCallN(). If the same function is to be invoked repeatedly,
1399 * do the fmgr_info() once and then use FunctionCallN().
1402 OidFunctionCall0Coll(Oid functionId, Oid collation)
1406 fmgr_info(functionId, &flinfo);
1408 return FunctionCall0Coll(&flinfo, collation);
1412 OidFunctionCall1Coll(Oid functionId, Oid collation, Datum arg1)
1416 fmgr_info(functionId, &flinfo);
1418 return FunctionCall1Coll(&flinfo, collation, arg1);
1422 OidFunctionCall2Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2)
1426 fmgr_info(functionId, &flinfo);
1428 return FunctionCall2Coll(&flinfo, collation, arg1, arg2);
1432 OidFunctionCall3Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
1437 fmgr_info(functionId, &flinfo);
1439 return FunctionCall3Coll(&flinfo, collation, arg1, arg2, arg3);
1443 OidFunctionCall4Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
1444 Datum arg3, Datum arg4)
1448 fmgr_info(functionId, &flinfo);
1450 return FunctionCall4Coll(&flinfo, collation, arg1, arg2, arg3, arg4);
1454 OidFunctionCall5Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
1455 Datum arg3, Datum arg4, Datum arg5)
1459 fmgr_info(functionId, &flinfo);
1461 return FunctionCall5Coll(&flinfo, collation, arg1, arg2, arg3, arg4, arg5);
1465 OidFunctionCall6Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
1466 Datum arg3, Datum arg4, Datum arg5,
1471 fmgr_info(functionId, &flinfo);
1473 return FunctionCall6Coll(&flinfo, collation, arg1, arg2, arg3, arg4, arg5,
1478 OidFunctionCall7Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
1479 Datum arg3, Datum arg4, Datum arg5,
1480 Datum arg6, Datum arg7)
1484 fmgr_info(functionId, &flinfo);
1486 return FunctionCall7Coll(&flinfo, collation, arg1, arg2, arg3, arg4, arg5,
1491 OidFunctionCall8Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
1492 Datum arg3, Datum arg4, Datum arg5,
1493 Datum arg6, Datum arg7, Datum arg8)
1497 fmgr_info(functionId, &flinfo);
1499 return FunctionCall8Coll(&flinfo, collation, arg1, arg2, arg3, arg4, arg5,
1504 OidFunctionCall9Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
1505 Datum arg3, Datum arg4, Datum arg5,
1506 Datum arg6, Datum arg7, Datum arg8,
1511 fmgr_info(functionId, &flinfo);
1513 return FunctionCall9Coll(&flinfo, collation, arg1, arg2, arg3, arg4, arg5,
1514 arg6, arg7, arg8, arg9);
1519 * Special cases for convenient invocation of datatype I/O functions.
1523 * Call a previously-looked-up datatype input function.
1525 * "str" may be NULL to indicate we are reading a NULL. In this case
1526 * the caller should assume the result is NULL, but we'll call the input
1527 * function anyway if it's not strict. So this is almost but not quite
1528 * the same as FunctionCall3.
1531 InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
1533 LOCAL_FCINFO(fcinfo, 3);
1536 if (str == NULL && flinfo->fn_strict)
1537 return (Datum) 0; /* just return null result */
1539 InitFunctionCallInfoData(*fcinfo, flinfo, 3, InvalidOid, NULL, NULL);
1541 fcinfo->args[0].value = CStringGetDatum(str);
1542 fcinfo->args[0].isnull = false;
1543 fcinfo->args[1].value = ObjectIdGetDatum(typioparam);
1544 fcinfo->args[1].isnull = false;
1545 fcinfo->args[2].value = Int32GetDatum(typmod);
1546 fcinfo->args[2].isnull = false;
1548 result = FunctionCallInvoke(fcinfo);
1550 /* Should get null result if and only if str is NULL */
1553 if (!fcinfo->isnull)
1554 elog(ERROR, "input function %u returned non-NULL",
1560 elog(ERROR, "input function %u returned NULL",
1568 * Call a previously-looked-up datatype output function.
1570 * Do not call this on NULL datums.
1572 * This is currently little more than window dressing for FunctionCall1.
1575 OutputFunctionCall(FmgrInfo *flinfo, Datum val)
1577 return DatumGetCString(FunctionCall1(flinfo, val));
1581 * Call a previously-looked-up datatype binary-input function.
1583 * "buf" may be NULL to indicate we are reading a NULL. In this case
1584 * the caller should assume the result is NULL, but we'll call the receive
1585 * function anyway if it's not strict. So this is almost but not quite
1586 * the same as FunctionCall3.
1589 ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf,
1590 Oid typioparam, int32 typmod)
1592 LOCAL_FCINFO(fcinfo, 3);
1595 if (buf == NULL && flinfo->fn_strict)
1596 return (Datum) 0; /* just return null result */
1598 InitFunctionCallInfoData(*fcinfo, flinfo, 3, InvalidOid, NULL, NULL);
1600 fcinfo->args[0].value = PointerGetDatum(buf);
1601 fcinfo->args[0].isnull = false;
1602 fcinfo->args[1].value = ObjectIdGetDatum(typioparam);
1603 fcinfo->args[1].isnull = false;
1604 fcinfo->args[2].value = Int32GetDatum(typmod);
1605 fcinfo->args[2].isnull = false;
1607 result = FunctionCallInvoke(fcinfo);
1609 /* Should get null result if and only if buf is NULL */
1612 if (!fcinfo->isnull)
1613 elog(ERROR, "receive function %u returned non-NULL",
1619 elog(ERROR, "receive function %u returned NULL",
1627 * Call a previously-looked-up datatype binary-output function.
1629 * Do not call this on NULL datums.
1631 * This is little more than window dressing for FunctionCall1, but it does
1632 * guarantee a non-toasted result, which strictly speaking the underlying
1636 SendFunctionCall(FmgrInfo *flinfo, Datum val)
1638 return DatumGetByteaP(FunctionCall1(flinfo, val));
1642 * As above, for I/O functions identified by OID. These are only to be used
1643 * in seldom-executed code paths. They are not only slow but leak memory.
1646 OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
1650 fmgr_info(functionId, &flinfo);
1651 return InputFunctionCall(&flinfo, str, typioparam, typmod);
1655 OidOutputFunctionCall(Oid functionId, Datum val)
1659 fmgr_info(functionId, &flinfo);
1660 return OutputFunctionCall(&flinfo, val);
1664 OidReceiveFunctionCall(Oid functionId, StringInfo buf,
1665 Oid typioparam, int32 typmod)
1669 fmgr_info(functionId, &flinfo);
1670 return ReceiveFunctionCall(&flinfo, buf, typioparam, typmod);
1674 OidSendFunctionCall(Oid functionId, Datum val)
1678 fmgr_info(functionId, &flinfo);
1679 return SendFunctionCall(&flinfo, val);
1683 /*-------------------------------------------------------------------------
1684 * Support routines for standard maybe-pass-by-reference datatypes
1686 * int8, float4, and float8 can be passed by value if Datum is wide enough.
1687 * (For backwards-compatibility reasons, we allow pass-by-ref to be chosen
1688 * at compile time even if pass-by-val is possible.)
1690 * Note: there is only one switch controlling the pass-by-value option for
1691 * both int8 and float8; this is to avoid making things unduly complicated
1692 * for the timestamp types, which might have either representation.
1693 *-------------------------------------------------------------------------
1696 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
1699 Int64GetDatum(int64 X)
1701 int64 *retval = (int64 *) palloc(sizeof(int64));
1704 return PointerGetDatum(retval);
1706 #endif /* USE_FLOAT8_BYVAL */
1708 #ifndef USE_FLOAT4_BYVAL
1711 Float4GetDatum(float4 X)
1713 float4 *retval = (float4 *) palloc(sizeof(float4));
1716 return PointerGetDatum(retval);
1720 #ifndef USE_FLOAT8_BYVAL
1723 Float8GetDatum(float8 X)
1725 float8 *retval = (float8 *) palloc(sizeof(float8));
1728 return PointerGetDatum(retval);
1733 /*-------------------------------------------------------------------------
1734 * Support routines for toastable datatypes
1735 *-------------------------------------------------------------------------
1739 pg_detoast_datum(struct varlena *datum)
1741 if (VARATT_IS_EXTENDED(datum))
1742 return heap_tuple_untoast_attr(datum);
1748 pg_detoast_datum_copy(struct varlena *datum)
1750 if (VARATT_IS_EXTENDED(datum))
1751 return heap_tuple_untoast_attr(datum);
1754 /* Make a modifiable copy of the varlena object */
1755 Size len = VARSIZE(datum);
1756 struct varlena *result = (struct varlena *) palloc(len);
1758 memcpy(result, datum, len);
1764 pg_detoast_datum_slice(struct varlena *datum, int32 first, int32 count)
1766 /* Only get the specified portion from the toast rel */
1767 return heap_tuple_untoast_attr_slice(datum, first, count);
1771 pg_detoast_datum_packed(struct varlena *datum)
1773 if (VARATT_IS_COMPRESSED(datum) || VARATT_IS_EXTERNAL(datum))
1774 return heap_tuple_untoast_attr(datum);
1779 /*-------------------------------------------------------------------------
1780 * Support routines for extracting info from fn_expr parse tree
1782 * These are needed by polymorphic functions, which accept multiple possible
1783 * input types and need help from the parser to know what they've got.
1784 * Also, some functions might be interested in whether a parameter is constant.
1785 * Functions taking VARIADIC ANY also need to know about the VARIADIC keyword.
1786 *-------------------------------------------------------------------------
1790 * Get the actual type OID of the function return type
1792 * Returns InvalidOid if information is not available
1795 get_fn_expr_rettype(FmgrInfo *flinfo)
1800 * can't return anything useful if we have no FmgrInfo or if its fn_expr
1801 * node has not been initialized
1803 if (!flinfo || !flinfo->fn_expr)
1806 expr = flinfo->fn_expr;
1808 return exprType(expr);
1812 * Get the actual type OID of a specific function argument (counting from 0)
1814 * Returns InvalidOid if information is not available
1817 get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
1820 * can't return anything useful if we have no FmgrInfo or if its fn_expr
1821 * node has not been initialized
1823 if (!flinfo || !flinfo->fn_expr)
1826 return get_call_expr_argtype(flinfo->fn_expr, argnum);
1830 * Get the actual type OID of a specific function argument (counting from 0),
1831 * but working from the calling expression tree instead of FmgrInfo
1833 * Returns InvalidOid if information is not available
1836 get_call_expr_argtype(Node *expr, int argnum)
1844 if (IsA(expr, FuncExpr))
1845 args = ((FuncExpr *) expr)->args;
1846 else if (IsA(expr, OpExpr))
1847 args = ((OpExpr *) expr)->args;
1848 else if (IsA(expr, DistinctExpr))
1849 args = ((DistinctExpr *) expr)->args;
1850 else if (IsA(expr, ScalarArrayOpExpr))
1851 args = ((ScalarArrayOpExpr *) expr)->args;
1852 else if (IsA(expr, NullIfExpr))
1853 args = ((NullIfExpr *) expr)->args;
1854 else if (IsA(expr, WindowFunc))
1855 args = ((WindowFunc *) expr)->args;
1859 if (argnum < 0 || argnum >= list_length(args))
1862 argtype = exprType((Node *) list_nth(args, argnum));
1865 * special hack for ScalarArrayOpExpr: what the underlying function will
1866 * actually get passed is the element type of the array.
1868 if (IsA(expr, ScalarArrayOpExpr) &&
1870 argtype = get_base_element_type(argtype);
1876 * Find out whether a specific function argument is constant for the
1877 * duration of a query
1879 * Returns false if information is not available
1882 get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum)
1885 * can't return anything useful if we have no FmgrInfo or if its fn_expr
1886 * node has not been initialized
1888 if (!flinfo || !flinfo->fn_expr)
1891 return get_call_expr_arg_stable(flinfo->fn_expr, argnum);
1895 * Find out whether a specific function argument is constant for the
1896 * duration of a query, but working from the calling expression tree
1898 * Returns false if information is not available
1901 get_call_expr_arg_stable(Node *expr, int argnum)
1909 if (IsA(expr, FuncExpr))
1910 args = ((FuncExpr *) expr)->args;
1911 else if (IsA(expr, OpExpr))
1912 args = ((OpExpr *) expr)->args;
1913 else if (IsA(expr, DistinctExpr))
1914 args = ((DistinctExpr *) expr)->args;
1915 else if (IsA(expr, ScalarArrayOpExpr))
1916 args = ((ScalarArrayOpExpr *) expr)->args;
1917 else if (IsA(expr, NullIfExpr))
1918 args = ((NullIfExpr *) expr)->args;
1919 else if (IsA(expr, WindowFunc))
1920 args = ((WindowFunc *) expr)->args;
1924 if (argnum < 0 || argnum >= list_length(args))
1927 arg = (Node *) list_nth(args, argnum);
1930 * Either a true Const or an external Param will have a value that doesn't
1931 * change during the execution of the query. In future we might want to
1932 * consider other cases too, e.g. now().
1934 if (IsA(arg, Const))
1936 if (IsA(arg, Param) &&
1937 ((Param *) arg)->paramkind == PARAM_EXTERN)
1944 * Get the VARIADIC flag from the function invocation
1946 * Returns false (the default assumption) if information is not available
1948 * Note this is generally only of interest to VARIADIC ANY functions
1951 get_fn_expr_variadic(FmgrInfo *flinfo)
1956 * can't return anything useful if we have no FmgrInfo or if its fn_expr
1957 * node has not been initialized
1959 if (!flinfo || !flinfo->fn_expr)
1962 expr = flinfo->fn_expr;
1964 if (IsA(expr, FuncExpr))
1965 return ((FuncExpr *) expr)->funcvariadic;
1970 /*-------------------------------------------------------------------------
1971 * Support routines for procedural language implementations
1972 *-------------------------------------------------------------------------
1976 * Verify that a validator is actually associated with the language of a
1977 * particular function and that the user has access to both the language and
1978 * the function. All validators should call this before doing anything
1979 * substantial. Doing so ensures a user cannot achieve anything with explicit
1980 * calls to validators that he could not achieve with CREATE FUNCTION or by
1981 * simply calling an existing function.
1983 * When this function returns false, callers should skip all validation work
1984 * and call PG_RETURN_VOID(). This never happens at present; it is reserved
1985 * for future expansion.
1987 * In particular, checking that the validator corresponds to the function's
1988 * language allows untrusted language validators to assume they process only
1989 * superuser-chosen source code. (Untrusted language call handlers, by
1990 * definition, do assume that.) A user lacking the USAGE language privilege
1991 * would be unable to reach the validator through CREATE FUNCTION, so we check
1992 * that to block explicit calls as well. Checking the EXECUTE privilege on
1993 * the function is often superfluous, because most users can clone the
1994 * function to get an executable copy. It is meaningful against users with no
1995 * database TEMP right and no permanent schema CREATE right, thereby unable to
1996 * create any function. Also, if the function tracks persistent state by
1997 * function OID or name, validating the original function might permit more
1998 * mischief than creating and validating a clone thereof.
2001 CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid)
2005 Form_pg_proc procStruct;
2006 Form_pg_language langStruct;
2007 AclResult aclresult;
2010 * Get the function's pg_proc entry. Throw a user-facing error for bad
2011 * OID, because validators can be called with user-specified OIDs.
2013 procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionOid));
2014 if (!HeapTupleIsValid(procTup))
2016 (errcode(ERRCODE_UNDEFINED_FUNCTION),
2017 errmsg("function with OID %u does not exist", functionOid)));
2018 procStruct = (Form_pg_proc) GETSTRUCT(procTup);
2021 * Fetch pg_language entry to know if this is the correct validation
2022 * function for that pg_proc entry.
2024 langTup = SearchSysCache1(LANGOID, ObjectIdGetDatum(procStruct->prolang));
2025 if (!HeapTupleIsValid(langTup))
2026 elog(ERROR, "cache lookup failed for language %u", procStruct->prolang);
2027 langStruct = (Form_pg_language) GETSTRUCT(langTup);
2029 if (langStruct->lanvalidator != validatorOid)
2031 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2032 errmsg("language validation function %u called for language %u instead of %u",
2033 validatorOid, procStruct->prolang,
2034 langStruct->lanvalidator)));
2036 /* first validate that we have permissions to use the language */
2037 aclresult = pg_language_aclcheck(procStruct->prolang, GetUserId(),
2039 if (aclresult != ACLCHECK_OK)
2040 aclcheck_error(aclresult, OBJECT_LANGUAGE,
2041 NameStr(langStruct->lanname));
2044 * Check whether we are allowed to execute the function itself. If we can
2045 * execute it, there should be no possible side-effect of
2046 * compiling/validation that execution can't have.
2048 aclresult = pg_proc_aclcheck(functionOid, GetUserId(), ACL_EXECUTE);
2049 if (aclresult != ACLCHECK_OK)
2050 aclcheck_error(aclresult, OBJECT_FUNCTION, NameStr(procStruct->proname));
2052 ReleaseSysCache(procTup);
2053 ReleaseSysCache(langTup);