1 /*-------------------------------------------------------------------------
4 * The Postgres function manager.
6 * Portions Copyright (c) 1996-2017, 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/tuptoaster.h"
19 #include "catalog/pg_language.h"
20 #include "catalog/pg_proc.h"
21 #include "executor/functions.h"
22 #include "lib/stringinfo.h"
23 #include "miscadmin.h"
24 #include "nodes/nodeFuncs.h"
26 #include "utils/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);
62 static Datum fmgr_security_definer(PG_FUNCTION_ARGS);
66 * Lookup routines for builtin-function table. We can search by either Oid
67 * or name, but search by Oid is much faster.
70 static const FmgrBuiltin *
71 fmgr_isbuiltin(Oid id)
74 int high = fmgr_nbuiltins - 1;
77 * Loop invariant: low is the first index that could contain target entry,
78 * and high is the last index that could contain it.
82 int i = (high + low) / 2;
83 const FmgrBuiltin *ptr = &fmgr_builtins[i];
87 else if (id > ptr->foid)
96 * Lookup a builtin by name. Note there can be more than one entry in
97 * the array with the same name, but they should all point to the same
100 static const FmgrBuiltin *
101 fmgr_lookupByName(const char *name)
105 for (i = 0; i < fmgr_nbuiltins; i++)
107 if (strcmp(name, fmgr_builtins[i].funcName) == 0)
108 return fmgr_builtins + i;
114 * This routine fills a FmgrInfo struct, given the OID
115 * of the function to be called.
117 * The caller's CurrentMemoryContext is used as the fn_mcxt of the info
118 * struct; this means that any subsidiary data attached to the info struct
119 * (either by fmgr_info itself, or later on by a function call handler)
120 * will be allocated in that context. The caller must ensure that this
121 * context is at least as long-lived as the info struct itself. This is
122 * not a problem in typical cases where the info struct is on the stack or
123 * in freshly-palloc'd space. However, if one intends to store an info
124 * struct in a long-lived table, it's better to use fmgr_info_cxt.
127 fmgr_info(Oid functionId, FmgrInfo *finfo)
129 fmgr_info_cxt_security(functionId, finfo, CurrentMemoryContext, false);
133 * Fill a FmgrInfo struct, specifying a memory context in which its
134 * subsidiary data should go.
137 fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
139 fmgr_info_cxt_security(functionId, finfo, mcxt, false);
143 * This one does the actual work. ignore_security is ordinarily false
144 * but is set to true when we need to avoid recursion.
147 fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt,
148 bool ignore_security)
150 const FmgrBuiltin *fbp;
151 HeapTuple procedureTuple;
152 Form_pg_proc procedureStruct;
158 * fn_oid *must* be filled in last. Some code assumes that if fn_oid is
159 * valid, the whole struct is valid. Some FmgrInfo struct's do survive
162 finfo->fn_oid = InvalidOid;
163 finfo->fn_extra = NULL;
164 finfo->fn_mcxt = mcxt;
165 finfo->fn_expr = NULL; /* caller may set this later */
167 if ((fbp = fmgr_isbuiltin(functionId)) != NULL)
170 * Fast path for builtin functions: don't bother consulting pg_proc
172 finfo->fn_nargs = fbp->nargs;
173 finfo->fn_strict = fbp->strict;
174 finfo->fn_retset = fbp->retset;
175 finfo->fn_stats = TRACK_FUNC_ALL; /* ie, never track */
176 finfo->fn_addr = fbp->func;
177 finfo->fn_oid = functionId;
181 /* Otherwise we need the pg_proc entry */
182 procedureTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
183 if (!HeapTupleIsValid(procedureTuple))
184 elog(ERROR, "cache lookup failed for function %u", functionId);
185 procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
187 finfo->fn_nargs = procedureStruct->pronargs;
188 finfo->fn_strict = procedureStruct->proisstrict;
189 finfo->fn_retset = procedureStruct->proretset;
192 * If it has prosecdef set, non-null proconfig, or if a plugin wants to
193 * hook function entry/exit, use fmgr_security_definer call handler ---
194 * unless we are being called again by fmgr_security_definer or
195 * fmgr_info_other_lang.
197 * When using fmgr_security_definer, function stats tracking is always
198 * disabled at the outer level, and instead we set the flag properly in
199 * fmgr_security_definer's private flinfo and implement the tracking
200 * inside fmgr_security_definer. This loses the ability to charge the
201 * overhead of fmgr_security_definer to the function, but gains the
202 * ability to set the track_functions GUC as a local GUC parameter of an
203 * interesting function and have the right things happen.
205 if (!ignore_security &&
206 (procedureStruct->prosecdef ||
207 !heap_attisnull(procedureTuple, Anum_pg_proc_proconfig) ||
208 FmgrHookIsNeeded(functionId)))
210 finfo->fn_addr = fmgr_security_definer;
211 finfo->fn_stats = TRACK_FUNC_ALL; /* ie, never track */
212 finfo->fn_oid = functionId;
213 ReleaseSysCache(procedureTuple);
217 switch (procedureStruct->prolang)
219 case INTERNALlanguageId:
222 * For an ordinary builtin function, we should never get here
223 * because the isbuiltin() search above will have succeeded.
224 * However, if the user has done a CREATE FUNCTION to create an
225 * alias for a builtin function, we can end up here. In that case
226 * we have to look up the function by name. The name of the
227 * internal function is stored in prosrc (it doesn't have to be
228 * the same as the name of the alias!)
230 prosrcdatum = SysCacheGetAttr(PROCOID, procedureTuple,
231 Anum_pg_proc_prosrc, &isnull);
233 elog(ERROR, "null prosrc");
234 prosrc = TextDatumGetCString(prosrcdatum);
235 fbp = fmgr_lookupByName(prosrc);
238 (errcode(ERRCODE_UNDEFINED_FUNCTION),
239 errmsg("internal function \"%s\" is not in internal lookup table",
242 /* Should we check that nargs, strict, retset match the table? */
243 finfo->fn_addr = fbp->func;
244 /* note this policy is also assumed in fast path above */
245 finfo->fn_stats = TRACK_FUNC_ALL; /* ie, never track */
249 fmgr_info_C_lang(functionId, finfo, procedureTuple);
250 finfo->fn_stats = TRACK_FUNC_PL; /* ie, track if ALL */
254 finfo->fn_addr = fmgr_sql;
255 finfo->fn_stats = TRACK_FUNC_PL; /* ie, track if ALL */
259 fmgr_info_other_lang(functionId, finfo, procedureTuple);
260 finfo->fn_stats = TRACK_FUNC_OFF; /* ie, track if not OFF */
264 finfo->fn_oid = functionId;
265 ReleaseSysCache(procedureTuple);
269 * Special fmgr_info processing for C-language functions. Note that
270 * finfo->fn_oid is not valid yet.
273 fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
275 CFuncHashTabEntry *hashentry;
277 const Pg_finfo_record *inforec;
281 * See if we have the function address cached already
283 hashentry = lookup_C_func(procedureTuple);
286 user_fn = hashentry->user_fn;
287 inforec = hashentry->inforec;
298 * Get prosrc and probin strings (link symbol and library filename).
299 * While in general these columns might be null, that's not allowed
300 * for C-language functions.
302 prosrcattr = SysCacheGetAttr(PROCOID, procedureTuple,
303 Anum_pg_proc_prosrc, &isnull);
305 elog(ERROR, "null prosrc for C function %u", functionId);
306 prosrcstring = TextDatumGetCString(prosrcattr);
308 probinattr = SysCacheGetAttr(PROCOID, procedureTuple,
309 Anum_pg_proc_probin, &isnull);
311 elog(ERROR, "null probin for C function %u", functionId);
312 probinstring = TextDatumGetCString(probinattr);
314 /* Look up the function itself */
315 user_fn = load_external_function(probinstring, prosrcstring, true,
318 /* Get the function information record (real or default) */
319 inforec = fetch_finfo_record(libraryhandle, prosrcstring);
321 /* Cache the addresses for later calls */
322 record_C_func(procedureTuple, user_fn, inforec);
328 switch (inforec->api_version)
331 /* New style: call directly */
332 finfo->fn_addr = user_fn;
335 /* Shouldn't get here if fetch_finfo_record did its job */
336 elog(ERROR, "unrecognized function API version: %d",
337 inforec->api_version);
343 * Special fmgr_info processing for other-language functions. Note
344 * that finfo->fn_oid is not valid yet.
347 fmgr_info_other_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
349 Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
350 Oid language = procedureStruct->prolang;
351 HeapTuple languageTuple;
352 Form_pg_language languageStruct;
355 languageTuple = SearchSysCache1(LANGOID, ObjectIdGetDatum(language));
356 if (!HeapTupleIsValid(languageTuple))
357 elog(ERROR, "cache lookup failed for language %u", language);
358 languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
361 * Look up the language's call handler function, ignoring any attributes
362 * that would normally cause insertion of fmgr_security_definer. We need
363 * to get back a bare pointer to the actual C-language function.
365 fmgr_info_cxt_security(languageStruct->lanplcallfoid, &plfinfo,
366 CurrentMemoryContext, true);
367 finfo->fn_addr = plfinfo.fn_addr;
369 ReleaseSysCache(languageTuple);
373 * Fetch and validate the information record for the given external function.
374 * The function is specified by a handle for the containing library
375 * (obtained from load_external_function) as well as the function name.
377 * If no info function exists for the given name an error is raised.
379 * This function is broken out of fmgr_info_C_lang so that fmgr_c_validator
380 * can validate the information record for a function not yet entered into
383 const Pg_finfo_record *
384 fetch_finfo_record(void *filehandle, const char *funcname)
387 PGFInfoFunction infofunc;
388 const Pg_finfo_record *inforec;
390 infofuncname = psprintf("pg_finfo_%s", funcname);
392 /* Try to look up the info function */
393 infofunc = (PGFInfoFunction) lookup_external_function(filehandle,
395 if (infofunc == NULL)
398 (errcode(ERRCODE_UNDEFINED_FUNCTION),
399 errmsg("could not find function information for function \"%s\"",
401 errhint("SQL-callable functions need an accompanying PG_FUNCTION_INFO_V1(funcname).")));
402 return NULL; /* silence compiler */
405 /* Found, so call it */
406 inforec = (*infofunc) ();
408 /* Validate result as best we can */
410 elog(ERROR, "null result from info function \"%s\"", infofuncname);
411 switch (inforec->api_version)
414 /* OK, no additional fields to validate */
418 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
419 errmsg("unrecognized API version %d reported by info function \"%s\"",
420 inforec->api_version, infofuncname)));
429 /*-------------------------------------------------------------------------
430 * Routines for caching lookup information for external C functions.
432 * The routines in dfmgr.c are relatively slow, so we try to avoid running
433 * them more than once per external function per session. We use a hash table
434 * with the function OID as the lookup key.
435 *-------------------------------------------------------------------------
439 * lookup_C_func: try to find a C function in the hash table
441 * If an entry exists and is up to date, return it; else return NULL
443 static CFuncHashTabEntry *
444 lookup_C_func(HeapTuple procedureTuple)
446 Oid fn_oid = HeapTupleGetOid(procedureTuple);
447 CFuncHashTabEntry *entry;
449 if (CFuncHash == NULL)
450 return NULL; /* no table yet */
451 entry = (CFuncHashTabEntry *)
452 hash_search(CFuncHash,
457 return NULL; /* no such entry */
458 if (entry->fn_xmin == HeapTupleHeaderGetRawXmin(procedureTuple->t_data) &&
459 ItemPointerEquals(&entry->fn_tid, &procedureTuple->t_self))
460 return entry; /* OK */
461 return NULL; /* entry is out of date */
465 * record_C_func: enter (or update) info about a C function in the hash table
468 record_C_func(HeapTuple procedureTuple,
469 PGFunction user_fn, const Pg_finfo_record *inforec)
471 Oid fn_oid = HeapTupleGetOid(procedureTuple);
472 CFuncHashTabEntry *entry;
475 /* Create the hash table if it doesn't exist yet */
476 if (CFuncHash == NULL)
480 MemSet(&hash_ctl, 0, sizeof(hash_ctl));
481 hash_ctl.keysize = sizeof(Oid);
482 hash_ctl.entrysize = sizeof(CFuncHashTabEntry);
483 CFuncHash = hash_create("CFuncHash",
486 HASH_ELEM | HASH_BLOBS);
489 entry = (CFuncHashTabEntry *)
490 hash_search(CFuncHash,
494 /* OID is already filled in */
495 entry->fn_xmin = HeapTupleHeaderGetRawXmin(procedureTuple->t_data);
496 entry->fn_tid = procedureTuple->t_self;
497 entry->user_fn = user_fn;
498 entry->inforec = inforec;
502 * clear_external_function_hash: remove entries for a library being closed
504 * Presently we just zap the entire hash table, but later it might be worth
505 * the effort to remove only the entries associated with the given handle.
508 clear_external_function_hash(void *filehandle)
511 hash_destroy(CFuncHash);
517 * Copy an FmgrInfo struct
519 * This is inherently somewhat bogus since we can't reliably duplicate
520 * language-dependent subsidiary info. We cheat by zeroing fn_extra,
521 * instead, meaning that subsidiary info will have to be recomputed.
524 fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo,
525 MemoryContext destcxt)
527 memcpy(dstinfo, srcinfo, sizeof(FmgrInfo));
528 dstinfo->fn_mcxt = destcxt;
529 dstinfo->fn_extra = NULL;
534 * Specialized lookup routine for fmgr_internal_validator: given the alleged
535 * name of an internal function, return the OID of the function.
536 * If the name is not recognized, return InvalidOid.
539 fmgr_internal_function(const char *proname)
541 const FmgrBuiltin *fbp = fmgr_lookupByName(proname);
550 * Support for security-definer and proconfig-using functions. We support
551 * both of these features using the same call handler, because they are
552 * often used together and it would be inefficient (as well as notationally
553 * messy) to have two levels of call handler involved.
555 struct fmgr_security_definer_cache
557 FmgrInfo flinfo; /* lookup info for target function */
558 Oid userid; /* userid to set, or InvalidOid */
559 ArrayType *proconfig; /* GUC values to set, or NULL */
560 Datum arg; /* passthrough argument for plugin modules */
564 * Function handler for security-definer/proconfig/plugin-hooked functions.
565 * We extract the OID of the actual function and do a fmgr lookup again.
566 * Then we fetch the pg_proc row and copy the owner ID and proconfig fields.
567 * (All this info is cached for the duration of the current query.)
568 * To execute a call, we temporarily replace the flinfo with the cached
569 * and looked-up one, while keeping the outer fcinfo (which contains all
570 * the actual arguments, etc.) intact. This is not re-entrant, but then
571 * the fcinfo itself can't be used reentrantly anyway.
574 fmgr_security_definer(PG_FUNCTION_ARGS)
577 struct fmgr_security_definer_cache *volatile fcache;
578 FmgrInfo *save_flinfo;
580 int save_sec_context;
581 volatile int save_nestlevel;
582 PgStat_FunctionCallUsage fcusage;
584 if (!fcinfo->flinfo->fn_extra)
587 Form_pg_proc procedureStruct;
590 MemoryContext oldcxt;
592 fcache = MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt,
595 fmgr_info_cxt_security(fcinfo->flinfo->fn_oid, &fcache->flinfo,
596 fcinfo->flinfo->fn_mcxt, true);
597 fcache->flinfo.fn_expr = fcinfo->flinfo->fn_expr;
599 tuple = SearchSysCache1(PROCOID,
600 ObjectIdGetDatum(fcinfo->flinfo->fn_oid));
601 if (!HeapTupleIsValid(tuple))
602 elog(ERROR, "cache lookup failed for function %u",
603 fcinfo->flinfo->fn_oid);
604 procedureStruct = (Form_pg_proc) GETSTRUCT(tuple);
606 if (procedureStruct->prosecdef)
607 fcache->userid = procedureStruct->proowner;
609 datum = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_proconfig,
613 oldcxt = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
614 fcache->proconfig = DatumGetArrayTypePCopy(datum);
615 MemoryContextSwitchTo(oldcxt);
618 ReleaseSysCache(tuple);
620 fcinfo->flinfo->fn_extra = fcache;
623 fcache = fcinfo->flinfo->fn_extra;
625 /* GetUserIdAndSecContext is cheap enough that no harm in a wasted call */
626 GetUserIdAndSecContext(&save_userid, &save_sec_context);
627 if (fcache->proconfig) /* Need a new GUC nesting level */
628 save_nestlevel = NewGUCNestLevel();
630 save_nestlevel = 0; /* keep compiler quiet */
632 if (OidIsValid(fcache->userid))
633 SetUserIdAndSecContext(fcache->userid,
634 save_sec_context | SECURITY_LOCAL_USERID_CHANGE);
636 if (fcache->proconfig)
638 ProcessGUCArray(fcache->proconfig,
639 (superuser() ? PGC_SUSET : PGC_USERSET),
644 /* function manager hook */
646 (*fmgr_hook) (FHET_START, &fcache->flinfo, &fcache->arg);
649 * We don't need to restore GUC or userid settings on error, because the
650 * ensuing xact or subxact abort will do that. The PG_TRY block is only
651 * needed to clean up the flinfo link.
653 save_flinfo = fcinfo->flinfo;
657 fcinfo->flinfo = &fcache->flinfo;
659 /* See notes in fmgr_info_cxt_security */
660 pgstat_init_function_usage(fcinfo, &fcusage);
662 result = FunctionCallInvoke(fcinfo);
665 * We could be calling either a regular or a set-returning function,
666 * so we have to test to see what finalize flag to use.
668 pgstat_end_function_usage(&fcusage,
669 (fcinfo->resultinfo == NULL ||
670 !IsA(fcinfo->resultinfo, ReturnSetInfo) ||
671 ((ReturnSetInfo *) fcinfo->resultinfo)->isDone != ExprMultipleResult));
675 fcinfo->flinfo = save_flinfo;
677 (*fmgr_hook) (FHET_ABORT, &fcache->flinfo, &fcache->arg);
682 fcinfo->flinfo = save_flinfo;
684 if (fcache->proconfig)
685 AtEOXact_GUC(true, save_nestlevel);
686 if (OidIsValid(fcache->userid))
687 SetUserIdAndSecContext(save_userid, save_sec_context);
689 (*fmgr_hook) (FHET_END, &fcache->flinfo, &fcache->arg);
695 /*-------------------------------------------------------------------------
696 * Support routines for callers of fmgr-compatible functions
697 *-------------------------------------------------------------------------
701 * These are for invocation of a specifically named function with a
702 * directly-computed parameter list. Note that neither arguments nor result
703 * are allowed to be NULL. Also, the function cannot be one that needs to
704 * look at FmgrInfo, since there won't be any.
707 DirectFunctionCall1Coll(PGFunction func, Oid collation, Datum arg1)
709 FunctionCallInfoData fcinfo;
712 InitFunctionCallInfoData(fcinfo, NULL, 1, collation, NULL, NULL);
714 fcinfo.arg[0] = arg1;
715 fcinfo.argnull[0] = false;
717 result = (*func) (&fcinfo);
719 /* Check for null result, since caller is clearly not expecting one */
721 elog(ERROR, "function %p returned NULL", (void *) func);
727 DirectFunctionCall2Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2)
729 FunctionCallInfoData fcinfo;
732 InitFunctionCallInfoData(fcinfo, NULL, 2, collation, NULL, NULL);
734 fcinfo.arg[0] = arg1;
735 fcinfo.arg[1] = arg2;
736 fcinfo.argnull[0] = false;
737 fcinfo.argnull[1] = false;
739 result = (*func) (&fcinfo);
741 /* Check for null result, since caller is clearly not expecting one */
743 elog(ERROR, "function %p returned NULL", (void *) func);
749 DirectFunctionCall3Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
752 FunctionCallInfoData fcinfo;
755 InitFunctionCallInfoData(fcinfo, NULL, 3, collation, NULL, NULL);
757 fcinfo.arg[0] = arg1;
758 fcinfo.arg[1] = arg2;
759 fcinfo.arg[2] = arg3;
760 fcinfo.argnull[0] = false;
761 fcinfo.argnull[1] = false;
762 fcinfo.argnull[2] = false;
764 result = (*func) (&fcinfo);
766 /* Check for null result, since caller is clearly not expecting one */
768 elog(ERROR, "function %p returned NULL", (void *) func);
774 DirectFunctionCall4Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
775 Datum arg3, Datum arg4)
777 FunctionCallInfoData fcinfo;
780 InitFunctionCallInfoData(fcinfo, NULL, 4, collation, NULL, NULL);
782 fcinfo.arg[0] = arg1;
783 fcinfo.arg[1] = arg2;
784 fcinfo.arg[2] = arg3;
785 fcinfo.arg[3] = arg4;
786 fcinfo.argnull[0] = false;
787 fcinfo.argnull[1] = false;
788 fcinfo.argnull[2] = false;
789 fcinfo.argnull[3] = false;
791 result = (*func) (&fcinfo);
793 /* Check for null result, since caller is clearly not expecting one */
795 elog(ERROR, "function %p returned NULL", (void *) func);
801 DirectFunctionCall5Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
802 Datum arg3, Datum arg4, Datum arg5)
804 FunctionCallInfoData fcinfo;
807 InitFunctionCallInfoData(fcinfo, NULL, 5, collation, NULL, NULL);
809 fcinfo.arg[0] = arg1;
810 fcinfo.arg[1] = arg2;
811 fcinfo.arg[2] = arg3;
812 fcinfo.arg[3] = arg4;
813 fcinfo.arg[4] = arg5;
814 fcinfo.argnull[0] = false;
815 fcinfo.argnull[1] = false;
816 fcinfo.argnull[2] = false;
817 fcinfo.argnull[3] = false;
818 fcinfo.argnull[4] = false;
820 result = (*func) (&fcinfo);
822 /* Check for null result, since caller is clearly not expecting one */
824 elog(ERROR, "function %p returned NULL", (void *) func);
830 DirectFunctionCall6Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
831 Datum arg3, Datum arg4, Datum arg5,
834 FunctionCallInfoData fcinfo;
837 InitFunctionCallInfoData(fcinfo, NULL, 6, collation, NULL, NULL);
839 fcinfo.arg[0] = arg1;
840 fcinfo.arg[1] = arg2;
841 fcinfo.arg[2] = arg3;
842 fcinfo.arg[3] = arg4;
843 fcinfo.arg[4] = arg5;
844 fcinfo.arg[5] = arg6;
845 fcinfo.argnull[0] = false;
846 fcinfo.argnull[1] = false;
847 fcinfo.argnull[2] = false;
848 fcinfo.argnull[3] = false;
849 fcinfo.argnull[4] = false;
850 fcinfo.argnull[5] = false;
852 result = (*func) (&fcinfo);
854 /* Check for null result, since caller is clearly not expecting one */
856 elog(ERROR, "function %p returned NULL", (void *) func);
862 DirectFunctionCall7Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
863 Datum arg3, Datum arg4, Datum arg5,
864 Datum arg6, Datum arg7)
866 FunctionCallInfoData fcinfo;
869 InitFunctionCallInfoData(fcinfo, NULL, 7, collation, NULL, NULL);
871 fcinfo.arg[0] = arg1;
872 fcinfo.arg[1] = arg2;
873 fcinfo.arg[2] = arg3;
874 fcinfo.arg[3] = arg4;
875 fcinfo.arg[4] = arg5;
876 fcinfo.arg[5] = arg6;
877 fcinfo.arg[6] = arg7;
878 fcinfo.argnull[0] = false;
879 fcinfo.argnull[1] = false;
880 fcinfo.argnull[2] = false;
881 fcinfo.argnull[3] = false;
882 fcinfo.argnull[4] = false;
883 fcinfo.argnull[5] = false;
884 fcinfo.argnull[6] = false;
886 result = (*func) (&fcinfo);
888 /* Check for null result, since caller is clearly not expecting one */
890 elog(ERROR, "function %p returned NULL", (void *) func);
896 DirectFunctionCall8Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
897 Datum arg3, Datum arg4, Datum arg5,
898 Datum arg6, Datum arg7, Datum arg8)
900 FunctionCallInfoData fcinfo;
903 InitFunctionCallInfoData(fcinfo, NULL, 8, collation, NULL, NULL);
905 fcinfo.arg[0] = arg1;
906 fcinfo.arg[1] = arg2;
907 fcinfo.arg[2] = arg3;
908 fcinfo.arg[3] = arg4;
909 fcinfo.arg[4] = arg5;
910 fcinfo.arg[5] = arg6;
911 fcinfo.arg[6] = arg7;
912 fcinfo.arg[7] = arg8;
913 fcinfo.argnull[0] = false;
914 fcinfo.argnull[1] = false;
915 fcinfo.argnull[2] = false;
916 fcinfo.argnull[3] = false;
917 fcinfo.argnull[4] = false;
918 fcinfo.argnull[5] = false;
919 fcinfo.argnull[6] = false;
920 fcinfo.argnull[7] = false;
922 result = (*func) (&fcinfo);
924 /* Check for null result, since caller is clearly not expecting one */
926 elog(ERROR, "function %p returned NULL", (void *) func);
932 DirectFunctionCall9Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
933 Datum arg3, Datum arg4, Datum arg5,
934 Datum arg6, Datum arg7, Datum arg8,
937 FunctionCallInfoData fcinfo;
940 InitFunctionCallInfoData(fcinfo, NULL, 9, collation, NULL, NULL);
942 fcinfo.arg[0] = arg1;
943 fcinfo.arg[1] = arg2;
944 fcinfo.arg[2] = arg3;
945 fcinfo.arg[3] = arg4;
946 fcinfo.arg[4] = arg5;
947 fcinfo.arg[5] = arg6;
948 fcinfo.arg[6] = arg7;
949 fcinfo.arg[7] = arg8;
950 fcinfo.arg[8] = arg9;
951 fcinfo.argnull[0] = false;
952 fcinfo.argnull[1] = false;
953 fcinfo.argnull[2] = false;
954 fcinfo.argnull[3] = false;
955 fcinfo.argnull[4] = false;
956 fcinfo.argnull[5] = false;
957 fcinfo.argnull[6] = false;
958 fcinfo.argnull[7] = false;
959 fcinfo.argnull[8] = false;
961 result = (*func) (&fcinfo);
963 /* Check for null result, since caller is clearly not expecting one */
965 elog(ERROR, "function %p returned NULL", (void *) func);
971 * These functions work like the DirectFunctionCall functions except that
972 * they use the flinfo parameter to initialise the fcinfo for the call.
973 * It's recommended that the callee only use the fn_extra and fn_mcxt
974 * fields, as other fields will typically describe the calling function
975 * not the callee. Conversely, the calling function should not have
976 * used fn_extra, unless its use is known to be compatible with the callee's.
980 CallerFInfoFunctionCall1(PGFunction func, FmgrInfo *flinfo, Oid collation, Datum arg1)
982 FunctionCallInfoData fcinfo;
985 InitFunctionCallInfoData(fcinfo, flinfo, 1, collation, NULL, NULL);
987 fcinfo.arg[0] = arg1;
988 fcinfo.argnull[0] = false;
990 result = (*func) (&fcinfo);
992 /* Check for null result, since caller is clearly not expecting one */
994 elog(ERROR, "function %p returned NULL", (void *) func);
1000 CallerFInfoFunctionCall2(PGFunction func, FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
1002 FunctionCallInfoData fcinfo;
1005 InitFunctionCallInfoData(fcinfo, flinfo, 2, collation, NULL, NULL);
1007 fcinfo.arg[0] = arg1;
1008 fcinfo.arg[1] = arg2;
1009 fcinfo.argnull[0] = false;
1010 fcinfo.argnull[1] = false;
1012 result = (*func) (&fcinfo);
1014 /* Check for null result, since caller is clearly not expecting one */
1016 elog(ERROR, "function %p returned NULL", (void *) func);
1022 * These are for invocation of a previously-looked-up function with a
1023 * directly-computed parameter list. Note that neither arguments nor result
1024 * are allowed to be NULL.
1027 FunctionCall1Coll(FmgrInfo *flinfo, Oid collation, Datum arg1)
1029 FunctionCallInfoData fcinfo;
1032 InitFunctionCallInfoData(fcinfo, flinfo, 1, collation, NULL, NULL);
1034 fcinfo.arg[0] = arg1;
1035 fcinfo.argnull[0] = false;
1037 result = FunctionCallInvoke(&fcinfo);
1039 /* Check for null result, since caller is clearly not expecting one */
1041 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1047 FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
1049 FunctionCallInfoData fcinfo;
1052 InitFunctionCallInfoData(fcinfo, flinfo, 2, collation, NULL, NULL);
1054 fcinfo.arg[0] = arg1;
1055 fcinfo.arg[1] = arg2;
1056 fcinfo.argnull[0] = false;
1057 fcinfo.argnull[1] = false;
1059 result = FunctionCallInvoke(&fcinfo);
1061 /* Check for null result, since caller is clearly not expecting one */
1063 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1069 FunctionCall3Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
1072 FunctionCallInfoData fcinfo;
1075 InitFunctionCallInfoData(fcinfo, flinfo, 3, collation, NULL, NULL);
1077 fcinfo.arg[0] = arg1;
1078 fcinfo.arg[1] = arg2;
1079 fcinfo.arg[2] = arg3;
1080 fcinfo.argnull[0] = false;
1081 fcinfo.argnull[1] = false;
1082 fcinfo.argnull[2] = false;
1084 result = FunctionCallInvoke(&fcinfo);
1086 /* Check for null result, since caller is clearly not expecting one */
1088 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1094 FunctionCall4Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
1095 Datum arg3, Datum arg4)
1097 FunctionCallInfoData fcinfo;
1100 InitFunctionCallInfoData(fcinfo, flinfo, 4, collation, NULL, NULL);
1102 fcinfo.arg[0] = arg1;
1103 fcinfo.arg[1] = arg2;
1104 fcinfo.arg[2] = arg3;
1105 fcinfo.arg[3] = arg4;
1106 fcinfo.argnull[0] = false;
1107 fcinfo.argnull[1] = false;
1108 fcinfo.argnull[2] = false;
1109 fcinfo.argnull[3] = false;
1111 result = FunctionCallInvoke(&fcinfo);
1113 /* Check for null result, since caller is clearly not expecting one */
1115 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1121 FunctionCall5Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
1122 Datum arg3, Datum arg4, Datum arg5)
1124 FunctionCallInfoData fcinfo;
1127 InitFunctionCallInfoData(fcinfo, flinfo, 5, collation, NULL, NULL);
1129 fcinfo.arg[0] = arg1;
1130 fcinfo.arg[1] = arg2;
1131 fcinfo.arg[2] = arg3;
1132 fcinfo.arg[3] = arg4;
1133 fcinfo.arg[4] = arg5;
1134 fcinfo.argnull[0] = false;
1135 fcinfo.argnull[1] = false;
1136 fcinfo.argnull[2] = false;
1137 fcinfo.argnull[3] = false;
1138 fcinfo.argnull[4] = false;
1140 result = FunctionCallInvoke(&fcinfo);
1142 /* Check for null result, since caller is clearly not expecting one */
1144 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1150 FunctionCall6Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
1151 Datum arg3, Datum arg4, Datum arg5,
1154 FunctionCallInfoData fcinfo;
1157 InitFunctionCallInfoData(fcinfo, flinfo, 6, collation, NULL, NULL);
1159 fcinfo.arg[0] = arg1;
1160 fcinfo.arg[1] = arg2;
1161 fcinfo.arg[2] = arg3;
1162 fcinfo.arg[3] = arg4;
1163 fcinfo.arg[4] = arg5;
1164 fcinfo.arg[5] = arg6;
1165 fcinfo.argnull[0] = false;
1166 fcinfo.argnull[1] = false;
1167 fcinfo.argnull[2] = false;
1168 fcinfo.argnull[3] = false;
1169 fcinfo.argnull[4] = false;
1170 fcinfo.argnull[5] = false;
1172 result = FunctionCallInvoke(&fcinfo);
1174 /* Check for null result, since caller is clearly not expecting one */
1176 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1182 FunctionCall7Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
1183 Datum arg3, Datum arg4, Datum arg5,
1184 Datum arg6, Datum arg7)
1186 FunctionCallInfoData fcinfo;
1189 InitFunctionCallInfoData(fcinfo, flinfo, 7, collation, NULL, NULL);
1191 fcinfo.arg[0] = arg1;
1192 fcinfo.arg[1] = arg2;
1193 fcinfo.arg[2] = arg3;
1194 fcinfo.arg[3] = arg4;
1195 fcinfo.arg[4] = arg5;
1196 fcinfo.arg[5] = arg6;
1197 fcinfo.arg[6] = arg7;
1198 fcinfo.argnull[0] = false;
1199 fcinfo.argnull[1] = false;
1200 fcinfo.argnull[2] = false;
1201 fcinfo.argnull[3] = false;
1202 fcinfo.argnull[4] = false;
1203 fcinfo.argnull[5] = false;
1204 fcinfo.argnull[6] = false;
1206 result = FunctionCallInvoke(&fcinfo);
1208 /* Check for null result, since caller is clearly not expecting one */
1210 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1216 FunctionCall8Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
1217 Datum arg3, Datum arg4, Datum arg5,
1218 Datum arg6, Datum arg7, Datum arg8)
1220 FunctionCallInfoData fcinfo;
1223 InitFunctionCallInfoData(fcinfo, flinfo, 8, collation, NULL, NULL);
1225 fcinfo.arg[0] = arg1;
1226 fcinfo.arg[1] = arg2;
1227 fcinfo.arg[2] = arg3;
1228 fcinfo.arg[3] = arg4;
1229 fcinfo.arg[4] = arg5;
1230 fcinfo.arg[5] = arg6;
1231 fcinfo.arg[6] = arg7;
1232 fcinfo.arg[7] = arg8;
1233 fcinfo.argnull[0] = false;
1234 fcinfo.argnull[1] = false;
1235 fcinfo.argnull[2] = false;
1236 fcinfo.argnull[3] = false;
1237 fcinfo.argnull[4] = false;
1238 fcinfo.argnull[5] = false;
1239 fcinfo.argnull[6] = false;
1240 fcinfo.argnull[7] = false;
1242 result = FunctionCallInvoke(&fcinfo);
1244 /* Check for null result, since caller is clearly not expecting one */
1246 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1252 FunctionCall9Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
1253 Datum arg3, Datum arg4, Datum arg5,
1254 Datum arg6, Datum arg7, Datum arg8,
1257 FunctionCallInfoData fcinfo;
1260 InitFunctionCallInfoData(fcinfo, flinfo, 9, collation, NULL, NULL);
1262 fcinfo.arg[0] = arg1;
1263 fcinfo.arg[1] = arg2;
1264 fcinfo.arg[2] = arg3;
1265 fcinfo.arg[3] = arg4;
1266 fcinfo.arg[4] = arg5;
1267 fcinfo.arg[5] = arg6;
1268 fcinfo.arg[6] = arg7;
1269 fcinfo.arg[7] = arg8;
1270 fcinfo.arg[8] = arg9;
1271 fcinfo.argnull[0] = false;
1272 fcinfo.argnull[1] = false;
1273 fcinfo.argnull[2] = false;
1274 fcinfo.argnull[3] = false;
1275 fcinfo.argnull[4] = false;
1276 fcinfo.argnull[5] = false;
1277 fcinfo.argnull[6] = false;
1278 fcinfo.argnull[7] = false;
1279 fcinfo.argnull[8] = false;
1281 result = FunctionCallInvoke(&fcinfo);
1283 /* Check for null result, since caller is clearly not expecting one */
1285 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1292 * These are for invocation of a function identified by OID with a
1293 * directly-computed parameter list. Note that neither arguments nor result
1294 * are allowed to be NULL. These are essentially fmgr_info() followed
1295 * by FunctionCallN(). If the same function is to be invoked repeatedly,
1296 * do the fmgr_info() once and then use FunctionCallN().
1299 OidFunctionCall0Coll(Oid functionId, Oid collation)
1302 FunctionCallInfoData fcinfo;
1305 fmgr_info(functionId, &flinfo);
1307 InitFunctionCallInfoData(fcinfo, &flinfo, 0, collation, NULL, NULL);
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 OidFunctionCall1Coll(Oid functionId, Oid collation, Datum arg1)
1322 FunctionCallInfoData fcinfo;
1325 fmgr_info(functionId, &flinfo);
1327 InitFunctionCallInfoData(fcinfo, &flinfo, 1, collation, NULL, NULL);
1329 fcinfo.arg[0] = arg1;
1330 fcinfo.argnull[0] = false;
1332 result = FunctionCallInvoke(&fcinfo);
1334 /* Check for null result, since caller is clearly not expecting one */
1336 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1342 OidFunctionCall2Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2)
1345 FunctionCallInfoData fcinfo;
1348 fmgr_info(functionId, &flinfo);
1350 InitFunctionCallInfoData(fcinfo, &flinfo, 2, collation, NULL, NULL);
1352 fcinfo.arg[0] = arg1;
1353 fcinfo.arg[1] = arg2;
1354 fcinfo.argnull[0] = false;
1355 fcinfo.argnull[1] = false;
1357 result = FunctionCallInvoke(&fcinfo);
1359 /* Check for null result, since caller is clearly not expecting one */
1361 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1367 OidFunctionCall3Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
1371 FunctionCallInfoData fcinfo;
1374 fmgr_info(functionId, &flinfo);
1376 InitFunctionCallInfoData(fcinfo, &flinfo, 3, collation, NULL, NULL);
1378 fcinfo.arg[0] = arg1;
1379 fcinfo.arg[1] = arg2;
1380 fcinfo.arg[2] = arg3;
1381 fcinfo.argnull[0] = false;
1382 fcinfo.argnull[1] = false;
1383 fcinfo.argnull[2] = false;
1385 result = FunctionCallInvoke(&fcinfo);
1387 /* Check for null result, since caller is clearly not expecting one */
1389 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1395 OidFunctionCall4Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
1396 Datum arg3, Datum arg4)
1399 FunctionCallInfoData fcinfo;
1402 fmgr_info(functionId, &flinfo);
1404 InitFunctionCallInfoData(fcinfo, &flinfo, 4, collation, NULL, NULL);
1406 fcinfo.arg[0] = arg1;
1407 fcinfo.arg[1] = arg2;
1408 fcinfo.arg[2] = arg3;
1409 fcinfo.arg[3] = arg4;
1410 fcinfo.argnull[0] = false;
1411 fcinfo.argnull[1] = false;
1412 fcinfo.argnull[2] = false;
1413 fcinfo.argnull[3] = false;
1415 result = FunctionCallInvoke(&fcinfo);
1417 /* Check for null result, since caller is clearly not expecting one */
1419 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1425 OidFunctionCall5Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
1426 Datum arg3, Datum arg4, Datum arg5)
1429 FunctionCallInfoData fcinfo;
1432 fmgr_info(functionId, &flinfo);
1434 InitFunctionCallInfoData(fcinfo, &flinfo, 5, collation, NULL, NULL);
1436 fcinfo.arg[0] = arg1;
1437 fcinfo.arg[1] = arg2;
1438 fcinfo.arg[2] = arg3;
1439 fcinfo.arg[3] = arg4;
1440 fcinfo.arg[4] = arg5;
1441 fcinfo.argnull[0] = false;
1442 fcinfo.argnull[1] = false;
1443 fcinfo.argnull[2] = false;
1444 fcinfo.argnull[3] = false;
1445 fcinfo.argnull[4] = false;
1447 result = FunctionCallInvoke(&fcinfo);
1449 /* Check for null result, since caller is clearly not expecting one */
1451 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1457 OidFunctionCall6Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
1458 Datum arg3, Datum arg4, Datum arg5,
1462 FunctionCallInfoData fcinfo;
1465 fmgr_info(functionId, &flinfo);
1467 InitFunctionCallInfoData(fcinfo, &flinfo, 6, collation, NULL, NULL);
1469 fcinfo.arg[0] = arg1;
1470 fcinfo.arg[1] = arg2;
1471 fcinfo.arg[2] = arg3;
1472 fcinfo.arg[3] = arg4;
1473 fcinfo.arg[4] = arg5;
1474 fcinfo.arg[5] = arg6;
1475 fcinfo.argnull[0] = false;
1476 fcinfo.argnull[1] = false;
1477 fcinfo.argnull[2] = false;
1478 fcinfo.argnull[3] = false;
1479 fcinfo.argnull[4] = false;
1480 fcinfo.argnull[5] = false;
1482 result = FunctionCallInvoke(&fcinfo);
1484 /* Check for null result, since caller is clearly not expecting one */
1486 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1492 OidFunctionCall7Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
1493 Datum arg3, Datum arg4, Datum arg5,
1494 Datum arg6, Datum arg7)
1497 FunctionCallInfoData fcinfo;
1500 fmgr_info(functionId, &flinfo);
1502 InitFunctionCallInfoData(fcinfo, &flinfo, 7, collation, NULL, NULL);
1504 fcinfo.arg[0] = arg1;
1505 fcinfo.arg[1] = arg2;
1506 fcinfo.arg[2] = arg3;
1507 fcinfo.arg[3] = arg4;
1508 fcinfo.arg[4] = arg5;
1509 fcinfo.arg[5] = arg6;
1510 fcinfo.arg[6] = arg7;
1511 fcinfo.argnull[0] = false;
1512 fcinfo.argnull[1] = false;
1513 fcinfo.argnull[2] = false;
1514 fcinfo.argnull[3] = false;
1515 fcinfo.argnull[4] = false;
1516 fcinfo.argnull[5] = false;
1517 fcinfo.argnull[6] = false;
1519 result = FunctionCallInvoke(&fcinfo);
1521 /* Check for null result, since caller is clearly not expecting one */
1523 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1529 OidFunctionCall8Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
1530 Datum arg3, Datum arg4, Datum arg5,
1531 Datum arg6, Datum arg7, Datum arg8)
1534 FunctionCallInfoData fcinfo;
1537 fmgr_info(functionId, &flinfo);
1539 InitFunctionCallInfoData(fcinfo, &flinfo, 8, collation, NULL, NULL);
1541 fcinfo.arg[0] = arg1;
1542 fcinfo.arg[1] = arg2;
1543 fcinfo.arg[2] = arg3;
1544 fcinfo.arg[3] = arg4;
1545 fcinfo.arg[4] = arg5;
1546 fcinfo.arg[5] = arg6;
1547 fcinfo.arg[6] = arg7;
1548 fcinfo.arg[7] = arg8;
1549 fcinfo.argnull[0] = false;
1550 fcinfo.argnull[1] = false;
1551 fcinfo.argnull[2] = false;
1552 fcinfo.argnull[3] = false;
1553 fcinfo.argnull[4] = false;
1554 fcinfo.argnull[5] = false;
1555 fcinfo.argnull[6] = false;
1556 fcinfo.argnull[7] = 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 OidFunctionCall9Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
1569 Datum arg3, Datum arg4, Datum arg5,
1570 Datum arg6, Datum arg7, Datum arg8,
1574 FunctionCallInfoData fcinfo;
1577 fmgr_info(functionId, &flinfo);
1579 InitFunctionCallInfoData(fcinfo, &flinfo, 9, collation, NULL, NULL);
1581 fcinfo.arg[0] = arg1;
1582 fcinfo.arg[1] = arg2;
1583 fcinfo.arg[2] = arg3;
1584 fcinfo.arg[3] = arg4;
1585 fcinfo.arg[4] = arg5;
1586 fcinfo.arg[5] = arg6;
1587 fcinfo.arg[6] = arg7;
1588 fcinfo.arg[7] = arg8;
1589 fcinfo.arg[8] = arg9;
1590 fcinfo.argnull[0] = false;
1591 fcinfo.argnull[1] = false;
1592 fcinfo.argnull[2] = false;
1593 fcinfo.argnull[3] = false;
1594 fcinfo.argnull[4] = false;
1595 fcinfo.argnull[5] = false;
1596 fcinfo.argnull[6] = false;
1597 fcinfo.argnull[7] = false;
1598 fcinfo.argnull[8] = false;
1600 result = FunctionCallInvoke(&fcinfo);
1602 /* Check for null result, since caller is clearly not expecting one */
1604 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1611 * Special cases for convenient invocation of datatype I/O functions.
1615 * Call a previously-looked-up datatype input function.
1617 * "str" may be NULL to indicate we are reading a NULL. In this case
1618 * the caller should assume the result is NULL, but we'll call the input
1619 * function anyway if it's not strict. So this is almost but not quite
1620 * the same as FunctionCall3.
1623 InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
1625 FunctionCallInfoData fcinfo;
1628 if (str == NULL && flinfo->fn_strict)
1629 return (Datum) 0; /* just return null result */
1631 InitFunctionCallInfoData(fcinfo, flinfo, 3, InvalidOid, NULL, NULL);
1633 fcinfo.arg[0] = CStringGetDatum(str);
1634 fcinfo.arg[1] = ObjectIdGetDatum(typioparam);
1635 fcinfo.arg[2] = Int32GetDatum(typmod);
1636 fcinfo.argnull[0] = (str == NULL);
1637 fcinfo.argnull[1] = false;
1638 fcinfo.argnull[2] = false;
1640 result = FunctionCallInvoke(&fcinfo);
1642 /* Should get null result if and only if str is NULL */
1646 elog(ERROR, "input function %u returned non-NULL",
1647 fcinfo.flinfo->fn_oid);
1652 elog(ERROR, "input function %u returned NULL",
1653 fcinfo.flinfo->fn_oid);
1660 * Call a previously-looked-up datatype output function.
1662 * Do not call this on NULL datums.
1664 * This is currently little more than window dressing for FunctionCall1.
1667 OutputFunctionCall(FmgrInfo *flinfo, Datum val)
1669 return DatumGetCString(FunctionCall1(flinfo, val));
1673 * Call a previously-looked-up datatype binary-input function.
1675 * "buf" may be NULL to indicate we are reading a NULL. In this case
1676 * the caller should assume the result is NULL, but we'll call the receive
1677 * function anyway if it's not strict. So this is almost but not quite
1678 * the same as FunctionCall3.
1681 ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf,
1682 Oid typioparam, int32 typmod)
1684 FunctionCallInfoData fcinfo;
1687 if (buf == NULL && flinfo->fn_strict)
1688 return (Datum) 0; /* just return null result */
1690 InitFunctionCallInfoData(fcinfo, flinfo, 3, InvalidOid, NULL, NULL);
1692 fcinfo.arg[0] = PointerGetDatum(buf);
1693 fcinfo.arg[1] = ObjectIdGetDatum(typioparam);
1694 fcinfo.arg[2] = Int32GetDatum(typmod);
1695 fcinfo.argnull[0] = (buf == NULL);
1696 fcinfo.argnull[1] = false;
1697 fcinfo.argnull[2] = false;
1699 result = FunctionCallInvoke(&fcinfo);
1701 /* Should get null result if and only if buf is NULL */
1705 elog(ERROR, "receive function %u returned non-NULL",
1706 fcinfo.flinfo->fn_oid);
1711 elog(ERROR, "receive function %u returned NULL",
1712 fcinfo.flinfo->fn_oid);
1719 * Call a previously-looked-up datatype binary-output function.
1721 * Do not call this on NULL datums.
1723 * This is little more than window dressing for FunctionCall1, but it does
1724 * guarantee a non-toasted result, which strictly speaking the underlying
1728 SendFunctionCall(FmgrInfo *flinfo, Datum val)
1730 return DatumGetByteaP(FunctionCall1(flinfo, val));
1734 * As above, for I/O functions identified by OID. These are only to be used
1735 * in seldom-executed code paths. They are not only slow but leak memory.
1738 OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
1742 fmgr_info(functionId, &flinfo);
1743 return InputFunctionCall(&flinfo, str, typioparam, typmod);
1747 OidOutputFunctionCall(Oid functionId, Datum val)
1751 fmgr_info(functionId, &flinfo);
1752 return OutputFunctionCall(&flinfo, val);
1756 OidReceiveFunctionCall(Oid functionId, StringInfo buf,
1757 Oid typioparam, int32 typmod)
1761 fmgr_info(functionId, &flinfo);
1762 return ReceiveFunctionCall(&flinfo, buf, typioparam, typmod);
1766 OidSendFunctionCall(Oid functionId, Datum val)
1770 fmgr_info(functionId, &flinfo);
1771 return SendFunctionCall(&flinfo, val);
1775 /*-------------------------------------------------------------------------
1776 * Support routines for standard maybe-pass-by-reference datatypes
1778 * int8, float4, and float8 can be passed by value if Datum is wide enough.
1779 * (For backwards-compatibility reasons, we allow pass-by-ref to be chosen
1780 * at compile time even if pass-by-val is possible.)
1782 * Note: there is only one switch controlling the pass-by-value option for
1783 * both int8 and float8; this is to avoid making things unduly complicated
1784 * for the timestamp types, which might have either representation.
1785 *-------------------------------------------------------------------------
1788 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
1791 Int64GetDatum(int64 X)
1793 int64 *retval = (int64 *) palloc(sizeof(int64));
1796 return PointerGetDatum(retval);
1798 #endif /* USE_FLOAT8_BYVAL */
1800 #ifndef USE_FLOAT4_BYVAL
1803 Float4GetDatum(float4 X)
1805 float4 *retval = (float4 *) palloc(sizeof(float4));
1808 return PointerGetDatum(retval);
1812 #ifndef USE_FLOAT8_BYVAL
1815 Float8GetDatum(float8 X)
1817 float8 *retval = (float8 *) palloc(sizeof(float8));
1820 return PointerGetDatum(retval);
1825 /*-------------------------------------------------------------------------
1826 * Support routines for toastable datatypes
1827 *-------------------------------------------------------------------------
1831 pg_detoast_datum(struct varlena *datum)
1833 if (VARATT_IS_EXTENDED(datum))
1834 return heap_tuple_untoast_attr(datum);
1840 pg_detoast_datum_copy(struct varlena *datum)
1842 if (VARATT_IS_EXTENDED(datum))
1843 return heap_tuple_untoast_attr(datum);
1846 /* Make a modifiable copy of the varlena object */
1847 Size len = VARSIZE(datum);
1848 struct varlena *result = (struct varlena *) palloc(len);
1850 memcpy(result, datum, len);
1856 pg_detoast_datum_slice(struct varlena *datum, int32 first, int32 count)
1858 /* Only get the specified portion from the toast rel */
1859 return heap_tuple_untoast_attr_slice(datum, first, count);
1863 pg_detoast_datum_packed(struct varlena *datum)
1865 if (VARATT_IS_COMPRESSED(datum) || VARATT_IS_EXTERNAL(datum))
1866 return heap_tuple_untoast_attr(datum);
1871 /*-------------------------------------------------------------------------
1872 * Support routines for extracting info from fn_expr parse tree
1874 * These are needed by polymorphic functions, which accept multiple possible
1875 * input types and need help from the parser to know what they've got.
1876 * Also, some functions might be interested in whether a parameter is constant.
1877 * Functions taking VARIADIC ANY also need to know about the VARIADIC keyword.
1878 *-------------------------------------------------------------------------
1882 * Get the actual type OID of the function return type
1884 * Returns InvalidOid if information is not available
1887 get_fn_expr_rettype(FmgrInfo *flinfo)
1892 * can't return anything useful if we have no FmgrInfo or if its fn_expr
1893 * node has not been initialized
1895 if (!flinfo || !flinfo->fn_expr)
1898 expr = flinfo->fn_expr;
1900 return exprType(expr);
1904 * Get the actual type OID of a specific function argument (counting from 0)
1906 * Returns InvalidOid if information is not available
1909 get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
1912 * can't return anything useful if we have no FmgrInfo or if its fn_expr
1913 * node has not been initialized
1915 if (!flinfo || !flinfo->fn_expr)
1918 return get_call_expr_argtype(flinfo->fn_expr, argnum);
1922 * Get the actual type OID of a specific function argument (counting from 0),
1923 * but working from the calling expression tree instead of FmgrInfo
1925 * Returns InvalidOid if information is not available
1928 get_call_expr_argtype(Node *expr, int argnum)
1936 if (IsA(expr, FuncExpr))
1937 args = ((FuncExpr *) expr)->args;
1938 else if (IsA(expr, OpExpr))
1939 args = ((OpExpr *) expr)->args;
1940 else if (IsA(expr, DistinctExpr))
1941 args = ((DistinctExpr *) expr)->args;
1942 else if (IsA(expr, ScalarArrayOpExpr))
1943 args = ((ScalarArrayOpExpr *) expr)->args;
1944 else if (IsA(expr, NullIfExpr))
1945 args = ((NullIfExpr *) expr)->args;
1946 else if (IsA(expr, WindowFunc))
1947 args = ((WindowFunc *) expr)->args;
1951 if (argnum < 0 || argnum >= list_length(args))
1954 argtype = exprType((Node *) list_nth(args, argnum));
1957 * special hack for ScalarArrayOpExpr: what the underlying function will
1958 * actually get passed is the element type of the array.
1960 if (IsA(expr, ScalarArrayOpExpr) &&
1962 argtype = get_base_element_type(argtype);
1968 * Find out whether a specific function argument is constant for the
1969 * duration of a query
1971 * Returns false if information is not available
1974 get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum)
1977 * can't return anything useful if we have no FmgrInfo or if its fn_expr
1978 * node has not been initialized
1980 if (!flinfo || !flinfo->fn_expr)
1983 return get_call_expr_arg_stable(flinfo->fn_expr, argnum);
1987 * Find out whether a specific function argument is constant for the
1988 * duration of a query, but working from the calling expression tree
1990 * Returns false if information is not available
1993 get_call_expr_arg_stable(Node *expr, int argnum)
2001 if (IsA(expr, FuncExpr))
2002 args = ((FuncExpr *) expr)->args;
2003 else if (IsA(expr, OpExpr))
2004 args = ((OpExpr *) expr)->args;
2005 else if (IsA(expr, DistinctExpr))
2006 args = ((DistinctExpr *) expr)->args;
2007 else if (IsA(expr, ScalarArrayOpExpr))
2008 args = ((ScalarArrayOpExpr *) expr)->args;
2009 else if (IsA(expr, NullIfExpr))
2010 args = ((NullIfExpr *) expr)->args;
2011 else if (IsA(expr, WindowFunc))
2012 args = ((WindowFunc *) expr)->args;
2016 if (argnum < 0 || argnum >= list_length(args))
2019 arg = (Node *) list_nth(args, argnum);
2022 * Either a true Const or an external Param will have a value that doesn't
2023 * change during the execution of the query. In future we might want to
2024 * consider other cases too, e.g. now().
2026 if (IsA(arg, Const))
2028 if (IsA(arg, Param) &&
2029 ((Param *) arg)->paramkind == PARAM_EXTERN)
2036 * Get the VARIADIC flag from the function invocation
2038 * Returns false (the default assumption) if information is not available
2040 * Note this is generally only of interest to VARIADIC ANY functions
2043 get_fn_expr_variadic(FmgrInfo *flinfo)
2048 * can't return anything useful if we have no FmgrInfo or if its fn_expr
2049 * node has not been initialized
2051 if (!flinfo || !flinfo->fn_expr)
2054 expr = flinfo->fn_expr;
2056 if (IsA(expr, FuncExpr))
2057 return ((FuncExpr *) expr)->funcvariadic;
2062 /*-------------------------------------------------------------------------
2063 * Support routines for procedural language implementations
2064 *-------------------------------------------------------------------------
2068 * Verify that a validator is actually associated with the language of a
2069 * particular function and that the user has access to both the language and
2070 * the function. All validators should call this before doing anything
2071 * substantial. Doing so ensures a user cannot achieve anything with explicit
2072 * calls to validators that he could not achieve with CREATE FUNCTION or by
2073 * simply calling an existing function.
2075 * When this function returns false, callers should skip all validation work
2076 * and call PG_RETURN_VOID(). This never happens at present; it is reserved
2077 * for future expansion.
2079 * In particular, checking that the validator corresponds to the function's
2080 * language allows untrusted language validators to assume they process only
2081 * superuser-chosen source code. (Untrusted language call handlers, by
2082 * definition, do assume that.) A user lacking the USAGE language privilege
2083 * would be unable to reach the validator through CREATE FUNCTION, so we check
2084 * that to block explicit calls as well. Checking the EXECUTE privilege on
2085 * the function is often superfluous, because most users can clone the
2086 * function to get an executable copy. It is meaningful against users with no
2087 * database TEMP right and no permanent schema CREATE right, thereby unable to
2088 * create any function. Also, if the function tracks persistent state by
2089 * function OID or name, validating the original function might permit more
2090 * mischief than creating and validating a clone thereof.
2093 CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid)
2097 Form_pg_proc procStruct;
2098 Form_pg_language langStruct;
2099 AclResult aclresult;
2102 * Get the function's pg_proc entry. Throw a user-facing error for bad
2103 * OID, because validators can be called with user-specified OIDs.
2105 procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionOid));
2106 if (!HeapTupleIsValid(procTup))
2108 (errcode(ERRCODE_UNDEFINED_FUNCTION),
2109 errmsg("function with OID %u does not exist", functionOid)));
2110 procStruct = (Form_pg_proc) GETSTRUCT(procTup);
2113 * Fetch pg_language entry to know if this is the correct validation
2114 * function for that pg_proc entry.
2116 langTup = SearchSysCache1(LANGOID, ObjectIdGetDatum(procStruct->prolang));
2117 if (!HeapTupleIsValid(langTup))
2118 elog(ERROR, "cache lookup failed for language %u", procStruct->prolang);
2119 langStruct = (Form_pg_language) GETSTRUCT(langTup);
2121 if (langStruct->lanvalidator != validatorOid)
2123 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2124 errmsg("language validation function %u called for language %u instead of %u",
2125 validatorOid, procStruct->prolang,
2126 langStruct->lanvalidator)));
2128 /* first validate that we have permissions to use the language */
2129 aclresult = pg_language_aclcheck(procStruct->prolang, GetUserId(),
2131 if (aclresult != ACLCHECK_OK)
2132 aclcheck_error(aclresult, ACL_KIND_LANGUAGE,
2133 NameStr(langStruct->lanname));
2136 * Check whether we are allowed to execute the function itself. If we can
2137 * execute it, there should be no possible side-effect of
2138 * compiling/validation that execution can't have.
2140 aclresult = pg_proc_aclcheck(functionOid, GetUserId(), ACL_EXECUTE);
2141 if (aclresult != ACLCHECK_OK)
2142 aclcheck_error(aclresult, ACL_KIND_PROC, NameStr(procStruct->proname));
2144 ReleaseSysCache(procTup);
2145 ReleaseSysCache(langTup);