1 /*-------------------------------------------------------------------------
4 * The Postgres function manager.
6 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/utils/fmgr/fmgr.c,v 1.100 2006/04/04 19:35:36 tgl Exp $
13 *-------------------------------------------------------------------------
18 #include "access/tuptoaster.h"
19 #include "catalog/pg_language.h"
20 #include "catalog/pg_proc.h"
21 #include "executor/functions.h"
22 #include "miscadmin.h"
23 #include "parser/parse_expr.h"
24 #include "utils/builtins.h"
25 #include "utils/fmgrtab.h"
26 #include "utils/lsyscache.h"
27 #include "utils/syscache.h"
30 * Declaration for old-style function pointer type. This is now used only
31 * in fmgr_oldstyle() and is no longer exported.
33 * The m68k SVR4 ABI defines that pointers are returned in %a0 instead of
34 * %d0. So if a function pointer is declared to return a pointer, the
35 * compiler may look only into %a0, but if the called function was declared
36 * to return an integer type, it puts its value only into %d0. So the
37 * caller doesn't pick up the correct return value. The solution is to
38 * declare the function pointer to return int, so the compiler picks up the
39 * return value from %d0. (Functions returning pointers put their value
40 * *additionally* into %d0 for compatibility.) The price is that there are
41 * some warnings about int->pointer conversions...
43 #if (defined(__mc68000__) || (defined(__m68k__))) && defined(__ELF__)
44 typedef int32 (*func_ptr) ();
46 typedef char *(*func_ptr) ();
50 * For an oldstyle function, fn_extra points to a record like this:
54 func_ptr func; /* Address of the oldstyle function */
55 bool arg_toastable[FUNC_MAX_ARGS]; /* is n'th arg of a toastable
60 * Hashtable for fast lookup of external C functions
64 /* fn_oid is the hash key and so must be first! */
65 Oid fn_oid; /* OID of an external C function */
66 TransactionId fn_xmin; /* for checking up-to-dateness */
68 PGFunction user_fn; /* the function's address */
69 Pg_finfo_record *inforec; /* address of its info record */
72 static HTAB *CFuncHash = NULL;
75 static void fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt,
76 bool ignore_security);
77 static void fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple);
78 static void fmgr_info_other_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple);
79 static CFuncHashTabEntry *lookup_C_func(HeapTuple procedureTuple);
80 static void record_C_func(HeapTuple procedureTuple,
81 PGFunction user_fn, Pg_finfo_record *inforec);
82 static Datum fmgr_oldstyle(PG_FUNCTION_ARGS);
83 static Datum fmgr_security_definer(PG_FUNCTION_ARGS);
87 * Lookup routines for builtin-function table. We can search by either Oid
88 * or name, but search by Oid is much faster.
91 static const FmgrBuiltin *
92 fmgr_isbuiltin(Oid id)
95 int high = fmgr_nbuiltins - 1;
98 * Loop invariant: low is the first index that could contain target entry,
99 * and high is the last index that could contain it.
103 int i = (high + low) / 2;
104 const FmgrBuiltin *ptr = &fmgr_builtins[i];
108 else if (id > ptr->foid)
117 * Lookup a builtin by name. Note there can be more than one entry in
118 * the array with the same name, but they should all point to the same
121 static const FmgrBuiltin *
122 fmgr_lookupByName(const char *name)
126 for (i = 0; i < fmgr_nbuiltins; i++)
128 if (strcmp(name, fmgr_builtins[i].funcName) == 0)
129 return fmgr_builtins + i;
135 * This routine fills a FmgrInfo struct, given the OID
136 * of the function to be called.
138 * The caller's CurrentMemoryContext is used as the fn_mcxt of the info
139 * struct; this means that any subsidiary data attached to the info struct
140 * (either by fmgr_info itself, or later on by a function call handler)
141 * will be allocated in that context. The caller must ensure that this
142 * context is at least as long-lived as the info struct itself. This is
143 * not a problem in typical cases where the info struct is on the stack or
144 * in freshly-palloc'd space. However, if one intends to store an info
145 * struct in a long-lived table, it's better to use fmgr_info_cxt.
148 fmgr_info(Oid functionId, FmgrInfo *finfo)
150 fmgr_info_cxt(functionId, finfo, CurrentMemoryContext);
154 * Fill a FmgrInfo struct, specifying a memory context in which its
155 * subsidiary data should go.
158 fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
160 fmgr_info_cxt_security(functionId, finfo, mcxt, false);
164 * This one does the actual work. ignore_security is ordinarily false
165 * but is set to true by fmgr_security_definer to avoid infinite
169 fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt,
170 bool ignore_security)
172 const FmgrBuiltin *fbp;
173 HeapTuple procedureTuple;
174 Form_pg_proc procedureStruct;
180 * fn_oid *must* be filled in last. Some code assumes that if fn_oid is
181 * valid, the whole struct is valid. Some FmgrInfo struct's do survive
184 finfo->fn_oid = InvalidOid;
185 finfo->fn_extra = NULL;
186 finfo->fn_mcxt = mcxt;
187 finfo->fn_expr = NULL; /* caller may set this later */
189 if ((fbp = fmgr_isbuiltin(functionId)) != NULL)
192 * Fast path for builtin functions: don't bother consulting pg_proc
194 finfo->fn_nargs = fbp->nargs;
195 finfo->fn_strict = fbp->strict;
196 finfo->fn_retset = fbp->retset;
197 finfo->fn_addr = fbp->func;
198 finfo->fn_oid = functionId;
202 /* Otherwise we need the pg_proc entry */
203 procedureTuple = SearchSysCache(PROCOID,
204 ObjectIdGetDatum(functionId),
206 if (!HeapTupleIsValid(procedureTuple))
207 elog(ERROR, "cache lookup failed for function %u", functionId);
208 procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
210 finfo->fn_nargs = procedureStruct->pronargs;
211 finfo->fn_strict = procedureStruct->proisstrict;
212 finfo->fn_retset = procedureStruct->proretset;
214 if (procedureStruct->prosecdef && !ignore_security)
216 finfo->fn_addr = fmgr_security_definer;
217 finfo->fn_oid = functionId;
218 ReleaseSysCache(procedureTuple);
222 switch (procedureStruct->prolang)
224 case INTERNALlanguageId:
227 * For an ordinary builtin function, we should never get here
228 * because the isbuiltin() search above will have succeeded.
229 * However, if the user has done a CREATE FUNCTION to create an
230 * alias for a builtin function, we can end up here. In that case
231 * we have to look up the function by name. The name of the
232 * internal function is stored in prosrc (it doesn't have to be
233 * the same as the name of the alias!)
235 prosrcdatum = SysCacheGetAttr(PROCOID, procedureTuple,
236 Anum_pg_proc_prosrc, &isnull);
238 elog(ERROR, "null prosrc");
239 prosrc = DatumGetCString(DirectFunctionCall1(textout,
241 fbp = fmgr_lookupByName(prosrc);
244 (errcode(ERRCODE_UNDEFINED_FUNCTION),
245 errmsg("internal function \"%s\" is not in internal lookup table",
248 /* Should we check that nargs, strict, retset match the table? */
249 finfo->fn_addr = fbp->func;
253 fmgr_info_C_lang(functionId, finfo, procedureTuple);
257 finfo->fn_addr = fmgr_sql;
261 fmgr_info_other_lang(functionId, finfo, procedureTuple);
265 finfo->fn_oid = functionId;
266 ReleaseSysCache(procedureTuple);
270 * Special fmgr_info processing for C-language functions. Note that
271 * finfo->fn_oid is not valid yet.
274 fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
276 Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
277 CFuncHashTabEntry *hashentry;
279 Pg_finfo_record *inforec;
280 Oldstyle_fnextra *fnextra;
285 * See if we have the function address cached already
287 hashentry = lookup_C_func(procedureTuple);
290 user_fn = hashentry->user_fn;
291 inforec = hashentry->inforec;
302 * Get prosrc and probin strings (link symbol and library filename)
304 prosrcattr = SysCacheGetAttr(PROCOID, procedureTuple,
305 Anum_pg_proc_prosrc, &isnull);
307 elog(ERROR, "null prosrc for function %u", functionId);
308 prosrcstring = DatumGetCString(DirectFunctionCall1(textout,
311 probinattr = SysCacheGetAttr(PROCOID, procedureTuple,
312 Anum_pg_proc_probin, &isnull);
314 elog(ERROR, "null probin for function %u", functionId);
315 probinstring = DatumGetCString(DirectFunctionCall1(textout,
318 /* Look up the function itself */
319 user_fn = load_external_function(probinstring, prosrcstring, true,
322 /* Get the function information record (real or default) */
323 inforec = fetch_finfo_record(libraryhandle, prosrcstring);
325 /* Cache the addresses for later calls */
326 record_C_func(procedureTuple, user_fn, inforec);
332 switch (inforec->api_version)
335 /* Old style: need to use a handler */
336 finfo->fn_addr = fmgr_oldstyle;
337 fnextra = (Oldstyle_fnextra *)
338 MemoryContextAllocZero(finfo->fn_mcxt,
339 sizeof(Oldstyle_fnextra));
340 finfo->fn_extra = (void *) fnextra;
341 fnextra->func = (func_ptr) user_fn;
342 for (i = 0; i < procedureStruct->pronargs; i++)
344 fnextra->arg_toastable[i] =
345 TypeIsToastable(procedureStruct->proargtypes.values[i]);
349 /* New style: call directly */
350 finfo->fn_addr = user_fn;
353 /* Shouldn't get here if fetch_finfo_record did its job */
354 elog(ERROR, "unrecognized function API version: %d",
355 inforec->api_version);
361 * Special fmgr_info processing for other-language functions. Note
362 * that finfo->fn_oid is not valid yet.
365 fmgr_info_other_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
367 Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
368 Oid language = procedureStruct->prolang;
369 HeapTuple languageTuple;
370 Form_pg_language languageStruct;
373 languageTuple = SearchSysCache(LANGOID,
374 ObjectIdGetDatum(language),
376 if (!HeapTupleIsValid(languageTuple))
377 elog(ERROR, "cache lookup failed for language %u", language);
378 languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
380 fmgr_info(languageStruct->lanplcallfoid, &plfinfo);
381 finfo->fn_addr = plfinfo.fn_addr;
384 * If lookup of the PL handler function produced nonnull fn_extra,
385 * complain --- it must be an oldstyle function! We no longer support
386 * oldstyle PL handlers.
388 if (plfinfo.fn_extra != NULL)
389 elog(ERROR, "language %u has old-style handler", language);
391 ReleaseSysCache(languageTuple);
395 * Fetch and validate the information record for the given external function.
396 * The function is specified by a handle for the containing library
397 * (obtained from load_external_function) as well as the function name.
399 * If no info function exists for the given name, it is not an error.
400 * Instead we return a default info record for a version-0 function.
401 * We want to raise an error here only if the info function returns
404 * This function is broken out of fmgr_info_C_lang so that fmgr_c_validator
405 * can validate the information record for a function not yet entered into
409 fetch_finfo_record(void *filehandle, char *funcname)
412 PGFInfoFunction infofunc;
413 Pg_finfo_record *inforec;
414 static Pg_finfo_record default_inforec = {0};
416 /* Compute name of info func */
417 infofuncname = (char *) palloc(strlen(funcname) + 10);
418 strcpy(infofuncname, "pg_finfo_");
419 strcat(infofuncname, funcname);
421 /* Try to look up the info function */
422 infofunc = (PGFInfoFunction) lookup_external_function(filehandle,
424 if (infofunc == NULL)
426 /* Not found --- assume version 0 */
428 return &default_inforec;
431 /* Found, so call it */
432 inforec = (*infofunc) ();
434 /* Validate result as best we can */
436 elog(ERROR, "null result from info function \"%s\"", infofuncname);
437 switch (inforec->api_version)
441 /* OK, no additional fields to validate */
445 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
446 errmsg("unrecognized API version %d reported by info function \"%s\"",
447 inforec->api_version, infofuncname)));
456 /*-------------------------------------------------------------------------
457 * Routines for caching lookup information for external C functions.
459 * The routines in dfmgr.c are relatively slow, so we try to avoid running
460 * them more than once per external function per session. We use a hash table
461 * with the function OID as the lookup key.
462 *-------------------------------------------------------------------------
466 * lookup_C_func: try to find a C function in the hash table
468 * If an entry exists and is up to date, return it; else return NULL
470 static CFuncHashTabEntry *
471 lookup_C_func(HeapTuple procedureTuple)
473 Oid fn_oid = HeapTupleGetOid(procedureTuple);
474 CFuncHashTabEntry *entry;
476 if (CFuncHash == NULL)
477 return NULL; /* no table yet */
478 entry = (CFuncHashTabEntry *)
479 hash_search(CFuncHash,
484 return NULL; /* no such entry */
485 if (entry->fn_xmin == HeapTupleHeaderGetXmin(procedureTuple->t_data) &&
486 entry->fn_cmin == HeapTupleHeaderGetCmin(procedureTuple->t_data))
487 return entry; /* OK */
488 return NULL; /* entry is out of date */
492 * record_C_func: enter (or update) info about a C function in the hash table
495 record_C_func(HeapTuple procedureTuple,
496 PGFunction user_fn, Pg_finfo_record *inforec)
498 Oid fn_oid = HeapTupleGetOid(procedureTuple);
499 CFuncHashTabEntry *entry;
502 /* Create the hash table if it doesn't exist yet */
503 if (CFuncHash == NULL)
507 MemSet(&hash_ctl, 0, sizeof(hash_ctl));
508 hash_ctl.keysize = sizeof(Oid);
509 hash_ctl.entrysize = sizeof(CFuncHashTabEntry);
510 hash_ctl.hash = oid_hash;
511 CFuncHash = hash_create("CFuncHash",
514 HASH_ELEM | HASH_FUNCTION);
517 entry = (CFuncHashTabEntry *)
518 hash_search(CFuncHash,
522 /* OID is already filled in */
523 entry->fn_xmin = HeapTupleHeaderGetXmin(procedureTuple->t_data);
524 entry->fn_cmin = HeapTupleHeaderGetCmin(procedureTuple->t_data);
525 entry->user_fn = user_fn;
526 entry->inforec = inforec;
530 * clear_external_function_hash: remove entries for a library being closed
532 * Presently we just zap the entire hash table, but later it might be worth
533 * the effort to remove only the entries associated with the given handle.
536 clear_external_function_hash(void *filehandle)
539 hash_destroy(CFuncHash);
545 * Copy an FmgrInfo struct
547 * This is inherently somewhat bogus since we can't reliably duplicate
548 * language-dependent subsidiary info. We cheat by zeroing fn_extra,
549 * instead, meaning that subsidiary info will have to be recomputed.
552 fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo,
553 MemoryContext destcxt)
555 memcpy(dstinfo, srcinfo, sizeof(FmgrInfo));
556 dstinfo->fn_mcxt = destcxt;
557 if (dstinfo->fn_addr == fmgr_oldstyle)
559 /* For oldstyle functions we must copy fn_extra */
560 Oldstyle_fnextra *fnextra;
562 fnextra = (Oldstyle_fnextra *)
563 MemoryContextAlloc(destcxt, sizeof(Oldstyle_fnextra));
564 memcpy(fnextra, srcinfo->fn_extra, sizeof(Oldstyle_fnextra));
565 dstinfo->fn_extra = (void *) fnextra;
568 dstinfo->fn_extra = NULL;
573 * Specialized lookup routine for fmgr_internal_validator: given the alleged
574 * name of an internal function, return the OID of the function.
575 * If the name is not recognized, return InvalidOid.
578 fmgr_internal_function(const char *proname)
580 const FmgrBuiltin *fbp = fmgr_lookupByName(proname);
589 * Handler for old-style "C" language functions
592 fmgr_oldstyle(PG_FUNCTION_ARGS)
594 Oldstyle_fnextra *fnextra;
595 int n_arguments = fcinfo->nargs;
601 if (fcinfo->flinfo == NULL || fcinfo->flinfo->fn_extra == NULL)
602 elog(ERROR, "fmgr_oldstyle received NULL pointer");
603 fnextra = (Oldstyle_fnextra *) fcinfo->flinfo->fn_extra;
606 * Result is NULL if any argument is NULL, but we still call the function
607 * (peculiar, but that's the way it worked before, and after all this is a
608 * backwards-compatibility wrapper). Note, however, that we'll never get
609 * here with NULL arguments if the function is marked strict.
611 * We also need to detoast any TOAST-ed inputs, since it's unlikely that
612 * an old-style function knows about TOASTing.
615 for (i = 0; i < n_arguments; i++)
619 else if (fnextra->arg_toastable[i])
620 fcinfo->arg[i] = PointerGetDatum(PG_DETOAST_DATUM(fcinfo->arg[i]));
622 fcinfo->isnull = isnull;
624 user_fn = fnextra->func;
629 returnValue = (*user_fn) ();
634 * nullvalue() used to use isNull to check if arg is NULL; perhaps
635 * there are other functions still out there that also rely on
636 * this undocumented hack?
638 returnValue = (*user_fn) (fcinfo->arg[0], &fcinfo->isnull);
641 returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1]);
644 returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
648 returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
649 fcinfo->arg[2], fcinfo->arg[3]);
652 returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
653 fcinfo->arg[2], fcinfo->arg[3],
657 returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
658 fcinfo->arg[2], fcinfo->arg[3],
659 fcinfo->arg[4], fcinfo->arg[5]);
662 returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
663 fcinfo->arg[2], fcinfo->arg[3],
664 fcinfo->arg[4], fcinfo->arg[5],
668 returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
669 fcinfo->arg[2], fcinfo->arg[3],
670 fcinfo->arg[4], fcinfo->arg[5],
671 fcinfo->arg[6], fcinfo->arg[7]);
674 returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
675 fcinfo->arg[2], fcinfo->arg[3],
676 fcinfo->arg[4], fcinfo->arg[5],
677 fcinfo->arg[6], fcinfo->arg[7],
681 returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
682 fcinfo->arg[2], fcinfo->arg[3],
683 fcinfo->arg[4], fcinfo->arg[5],
684 fcinfo->arg[6], fcinfo->arg[7],
685 fcinfo->arg[8], fcinfo->arg[9]);
688 returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
689 fcinfo->arg[2], fcinfo->arg[3],
690 fcinfo->arg[4], fcinfo->arg[5],
691 fcinfo->arg[6], fcinfo->arg[7],
692 fcinfo->arg[8], fcinfo->arg[9],
696 returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
697 fcinfo->arg[2], fcinfo->arg[3],
698 fcinfo->arg[4], fcinfo->arg[5],
699 fcinfo->arg[6], fcinfo->arg[7],
700 fcinfo->arg[8], fcinfo->arg[9],
701 fcinfo->arg[10], fcinfo->arg[11]);
704 returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
705 fcinfo->arg[2], fcinfo->arg[3],
706 fcinfo->arg[4], fcinfo->arg[5],
707 fcinfo->arg[6], fcinfo->arg[7],
708 fcinfo->arg[8], fcinfo->arg[9],
709 fcinfo->arg[10], fcinfo->arg[11],
713 returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
714 fcinfo->arg[2], fcinfo->arg[3],
715 fcinfo->arg[4], fcinfo->arg[5],
716 fcinfo->arg[6], fcinfo->arg[7],
717 fcinfo->arg[8], fcinfo->arg[9],
718 fcinfo->arg[10], fcinfo->arg[11],
719 fcinfo->arg[12], fcinfo->arg[13]);
722 returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
723 fcinfo->arg[2], fcinfo->arg[3],
724 fcinfo->arg[4], fcinfo->arg[5],
725 fcinfo->arg[6], fcinfo->arg[7],
726 fcinfo->arg[8], fcinfo->arg[9],
727 fcinfo->arg[10], fcinfo->arg[11],
728 fcinfo->arg[12], fcinfo->arg[13],
732 returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
733 fcinfo->arg[2], fcinfo->arg[3],
734 fcinfo->arg[4], fcinfo->arg[5],
735 fcinfo->arg[6], fcinfo->arg[7],
736 fcinfo->arg[8], fcinfo->arg[9],
737 fcinfo->arg[10], fcinfo->arg[11],
738 fcinfo->arg[12], fcinfo->arg[13],
739 fcinfo->arg[14], fcinfo->arg[15]);
744 * Increasing FUNC_MAX_ARGS doesn't automatically add cases to the
745 * above code, so mention the actual value in this error not
746 * FUNC_MAX_ARGS. You could add cases to the above if you needed
747 * to support old-style functions with many arguments, but making
748 * 'em be new-style is probably a better idea.
751 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
752 errmsg("function %u has too many arguments (%d, maximum is %d)",
753 fcinfo->flinfo->fn_oid, n_arguments, 16)));
754 returnValue = NULL; /* keep compiler quiet */
758 return (Datum) returnValue;
763 * Support for security definer functions
766 struct fmgr_security_definer_cache
773 * Function handler for security definer functions. We extract the
774 * OID of the actual function and do a fmgr lookup again. Then we
775 * look up the owner of the function and cache both the fmgr info and
776 * the owner ID. During the call we temporarily replace the flinfo
777 * with the cached/looked-up one, while keeping the outer fcinfo
778 * (which contains all the actual arguments, etc.) intact.
781 fmgr_security_definer(PG_FUNCTION_ARGS)
784 FmgrInfo *save_flinfo;
785 struct fmgr_security_definer_cache *volatile fcache;
789 if (!fcinfo->flinfo->fn_extra)
791 fcache = MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt,
794 fmgr_info_cxt_security(fcinfo->flinfo->fn_oid, &fcache->flinfo,
795 fcinfo->flinfo->fn_mcxt, true);
797 tuple = SearchSysCache(PROCOID,
798 ObjectIdGetDatum(fcinfo->flinfo->fn_oid),
800 if (!HeapTupleIsValid(tuple))
801 elog(ERROR, "cache lookup failed for function %u",
802 fcinfo->flinfo->fn_oid);
803 fcache->userid = ((Form_pg_proc) GETSTRUCT(tuple))->proowner;
804 ReleaseSysCache(tuple);
806 fcinfo->flinfo->fn_extra = fcache;
809 fcache = fcinfo->flinfo->fn_extra;
811 save_flinfo = fcinfo->flinfo;
812 save_userid = GetUserId();
816 fcinfo->flinfo = &fcache->flinfo;
817 SetUserId(fcache->userid);
819 result = FunctionCallInvoke(fcinfo);
823 fcinfo->flinfo = save_flinfo;
824 SetUserId(save_userid);
829 fcinfo->flinfo = save_flinfo;
830 SetUserId(save_userid);
836 /*-------------------------------------------------------------------------
837 * Support routines for callers of fmgr-compatible functions
838 *-------------------------------------------------------------------------
842 * These are for invocation of a specifically named function with a
843 * directly-computed parameter list. Note that neither arguments nor result
844 * are allowed to be NULL. Also, the function cannot be one that needs to
845 * look at FmgrInfo, since there won't be any.
848 DirectFunctionCall1(PGFunction func, Datum arg1)
850 FunctionCallInfoData fcinfo;
853 InitFunctionCallInfoData(fcinfo, NULL, 1, NULL, NULL);
855 fcinfo.arg[0] = arg1;
856 fcinfo.argnull[0] = false;
858 result = (*func) (&fcinfo);
860 /* Check for null result, since caller is clearly not expecting one */
862 elog(ERROR, "function %p returned NULL", (void *) func);
868 DirectFunctionCall2(PGFunction func, Datum arg1, Datum arg2)
870 FunctionCallInfoData fcinfo;
873 InitFunctionCallInfoData(fcinfo, NULL, 2, NULL, NULL);
875 fcinfo.arg[0] = arg1;
876 fcinfo.arg[1] = arg2;
877 fcinfo.argnull[0] = false;
878 fcinfo.argnull[1] = false;
880 result = (*func) (&fcinfo);
882 /* Check for null result, since caller is clearly not expecting one */
884 elog(ERROR, "function %p returned NULL", (void *) func);
890 DirectFunctionCall3(PGFunction func, Datum arg1, Datum arg2,
893 FunctionCallInfoData fcinfo;
896 InitFunctionCallInfoData(fcinfo, NULL, 3, NULL, NULL);
898 fcinfo.arg[0] = arg1;
899 fcinfo.arg[1] = arg2;
900 fcinfo.arg[2] = arg3;
901 fcinfo.argnull[0] = false;
902 fcinfo.argnull[1] = false;
903 fcinfo.argnull[2] = false;
905 result = (*func) (&fcinfo);
907 /* Check for null result, since caller is clearly not expecting one */
909 elog(ERROR, "function %p returned NULL", (void *) func);
915 DirectFunctionCall4(PGFunction func, Datum arg1, Datum arg2,
916 Datum arg3, Datum arg4)
918 FunctionCallInfoData fcinfo;
921 InitFunctionCallInfoData(fcinfo, NULL, 4, NULL, NULL);
923 fcinfo.arg[0] = arg1;
924 fcinfo.arg[1] = arg2;
925 fcinfo.arg[2] = arg3;
926 fcinfo.arg[3] = arg4;
927 fcinfo.argnull[0] = false;
928 fcinfo.argnull[1] = false;
929 fcinfo.argnull[2] = false;
930 fcinfo.argnull[3] = false;
932 result = (*func) (&fcinfo);
934 /* Check for null result, since caller is clearly not expecting one */
936 elog(ERROR, "function %p returned NULL", (void *) func);
942 DirectFunctionCall5(PGFunction func, Datum arg1, Datum arg2,
943 Datum arg3, Datum arg4, Datum arg5)
945 FunctionCallInfoData fcinfo;
948 InitFunctionCallInfoData(fcinfo, NULL, 5, NULL, NULL);
950 fcinfo.arg[0] = arg1;
951 fcinfo.arg[1] = arg2;
952 fcinfo.arg[2] = arg3;
953 fcinfo.arg[3] = arg4;
954 fcinfo.arg[4] = arg5;
955 fcinfo.argnull[0] = false;
956 fcinfo.argnull[1] = false;
957 fcinfo.argnull[2] = false;
958 fcinfo.argnull[3] = false;
959 fcinfo.argnull[4] = 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 DirectFunctionCall6(PGFunction func, Datum arg1, Datum arg2,
972 Datum arg3, Datum arg4, Datum arg5,
975 FunctionCallInfoData fcinfo;
978 InitFunctionCallInfoData(fcinfo, NULL, 6, NULL, NULL);
980 fcinfo.arg[0] = arg1;
981 fcinfo.arg[1] = arg2;
982 fcinfo.arg[2] = arg3;
983 fcinfo.arg[3] = arg4;
984 fcinfo.arg[4] = arg5;
985 fcinfo.arg[5] = arg6;
986 fcinfo.argnull[0] = false;
987 fcinfo.argnull[1] = false;
988 fcinfo.argnull[2] = false;
989 fcinfo.argnull[3] = false;
990 fcinfo.argnull[4] = false;
991 fcinfo.argnull[5] = false;
993 result = (*func) (&fcinfo);
995 /* Check for null result, since caller is clearly not expecting one */
997 elog(ERROR, "function %p returned NULL", (void *) func);
1003 DirectFunctionCall7(PGFunction func, Datum arg1, Datum arg2,
1004 Datum arg3, Datum arg4, Datum arg5,
1005 Datum arg6, Datum arg7)
1007 FunctionCallInfoData fcinfo;
1010 InitFunctionCallInfoData(fcinfo, NULL, 7, NULL, NULL);
1012 fcinfo.arg[0] = arg1;
1013 fcinfo.arg[1] = arg2;
1014 fcinfo.arg[2] = arg3;
1015 fcinfo.arg[3] = arg4;
1016 fcinfo.arg[4] = arg5;
1017 fcinfo.arg[5] = arg6;
1018 fcinfo.arg[6] = arg7;
1019 fcinfo.argnull[0] = false;
1020 fcinfo.argnull[1] = false;
1021 fcinfo.argnull[2] = false;
1022 fcinfo.argnull[3] = false;
1023 fcinfo.argnull[4] = false;
1024 fcinfo.argnull[5] = false;
1025 fcinfo.argnull[6] = false;
1027 result = (*func) (&fcinfo);
1029 /* Check for null result, since caller is clearly not expecting one */
1031 elog(ERROR, "function %p returned NULL", (void *) func);
1037 DirectFunctionCall8(PGFunction func, Datum arg1, Datum arg2,
1038 Datum arg3, Datum arg4, Datum arg5,
1039 Datum arg6, Datum arg7, Datum arg8)
1041 FunctionCallInfoData fcinfo;
1044 InitFunctionCallInfoData(fcinfo, NULL, 8, NULL, NULL);
1046 fcinfo.arg[0] = arg1;
1047 fcinfo.arg[1] = arg2;
1048 fcinfo.arg[2] = arg3;
1049 fcinfo.arg[3] = arg4;
1050 fcinfo.arg[4] = arg5;
1051 fcinfo.arg[5] = arg6;
1052 fcinfo.arg[6] = arg7;
1053 fcinfo.arg[7] = arg8;
1054 fcinfo.argnull[0] = false;
1055 fcinfo.argnull[1] = false;
1056 fcinfo.argnull[2] = false;
1057 fcinfo.argnull[3] = false;
1058 fcinfo.argnull[4] = false;
1059 fcinfo.argnull[5] = false;
1060 fcinfo.argnull[6] = false;
1061 fcinfo.argnull[7] = false;
1063 result = (*func) (&fcinfo);
1065 /* Check for null result, since caller is clearly not expecting one */
1067 elog(ERROR, "function %p returned NULL", (void *) func);
1073 DirectFunctionCall9(PGFunction func, Datum arg1, Datum arg2,
1074 Datum arg3, Datum arg4, Datum arg5,
1075 Datum arg6, Datum arg7, Datum arg8,
1078 FunctionCallInfoData fcinfo;
1081 InitFunctionCallInfoData(fcinfo, NULL, 9, NULL, NULL);
1083 fcinfo.arg[0] = arg1;
1084 fcinfo.arg[1] = arg2;
1085 fcinfo.arg[2] = arg3;
1086 fcinfo.arg[3] = arg4;
1087 fcinfo.arg[4] = arg5;
1088 fcinfo.arg[5] = arg6;
1089 fcinfo.arg[6] = arg7;
1090 fcinfo.arg[7] = arg8;
1091 fcinfo.arg[8] = arg9;
1092 fcinfo.argnull[0] = false;
1093 fcinfo.argnull[1] = false;
1094 fcinfo.argnull[2] = false;
1095 fcinfo.argnull[3] = false;
1096 fcinfo.argnull[4] = false;
1097 fcinfo.argnull[5] = false;
1098 fcinfo.argnull[6] = false;
1099 fcinfo.argnull[7] = false;
1100 fcinfo.argnull[8] = false;
1102 result = (*func) (&fcinfo);
1104 /* Check for null result, since caller is clearly not expecting one */
1106 elog(ERROR, "function %p returned NULL", (void *) func);
1113 * These are for invocation of a previously-looked-up function with a
1114 * directly-computed parameter list. Note that neither arguments nor result
1115 * are allowed to be NULL.
1118 FunctionCall1(FmgrInfo *flinfo, Datum arg1)
1120 FunctionCallInfoData fcinfo;
1123 InitFunctionCallInfoData(fcinfo, flinfo, 1, NULL, NULL);
1125 fcinfo.arg[0] = arg1;
1126 fcinfo.argnull[0] = false;
1128 result = FunctionCallInvoke(&fcinfo);
1130 /* Check for null result, since caller is clearly not expecting one */
1132 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1138 FunctionCall2(FmgrInfo *flinfo, Datum arg1, Datum arg2)
1141 * XXX if you change this routine, see also the inlined version in
1142 * utils/sort/tuplesort.c!
1144 FunctionCallInfoData fcinfo;
1147 InitFunctionCallInfoData(fcinfo, flinfo, 2, NULL, NULL);
1149 fcinfo.arg[0] = arg1;
1150 fcinfo.arg[1] = arg2;
1151 fcinfo.argnull[0] = false;
1152 fcinfo.argnull[1] = false;
1154 result = FunctionCallInvoke(&fcinfo);
1156 /* Check for null result, since caller is clearly not expecting one */
1158 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1164 FunctionCall3(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1167 FunctionCallInfoData fcinfo;
1170 InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);
1172 fcinfo.arg[0] = arg1;
1173 fcinfo.arg[1] = arg2;
1174 fcinfo.arg[2] = arg3;
1175 fcinfo.argnull[0] = false;
1176 fcinfo.argnull[1] = false;
1177 fcinfo.argnull[2] = false;
1179 result = FunctionCallInvoke(&fcinfo);
1181 /* Check for null result, since caller is clearly not expecting one */
1183 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1189 FunctionCall4(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1190 Datum arg3, Datum arg4)
1192 FunctionCallInfoData fcinfo;
1195 InitFunctionCallInfoData(fcinfo, flinfo, 4, NULL, NULL);
1197 fcinfo.arg[0] = arg1;
1198 fcinfo.arg[1] = arg2;
1199 fcinfo.arg[2] = arg3;
1200 fcinfo.arg[3] = arg4;
1201 fcinfo.argnull[0] = false;
1202 fcinfo.argnull[1] = false;
1203 fcinfo.argnull[2] = false;
1204 fcinfo.argnull[3] = 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 FunctionCall5(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1217 Datum arg3, Datum arg4, Datum arg5)
1219 FunctionCallInfoData fcinfo;
1222 InitFunctionCallInfoData(fcinfo, flinfo, 5, NULL, NULL);
1224 fcinfo.arg[0] = arg1;
1225 fcinfo.arg[1] = arg2;
1226 fcinfo.arg[2] = arg3;
1227 fcinfo.arg[3] = arg4;
1228 fcinfo.arg[4] = arg5;
1229 fcinfo.argnull[0] = false;
1230 fcinfo.argnull[1] = false;
1231 fcinfo.argnull[2] = false;
1232 fcinfo.argnull[3] = false;
1233 fcinfo.argnull[4] = false;
1235 result = FunctionCallInvoke(&fcinfo);
1237 /* Check for null result, since caller is clearly not expecting one */
1239 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1245 FunctionCall6(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1246 Datum arg3, Datum arg4, Datum arg5,
1249 FunctionCallInfoData fcinfo;
1252 InitFunctionCallInfoData(fcinfo, flinfo, 6, NULL, NULL);
1254 fcinfo.arg[0] = arg1;
1255 fcinfo.arg[1] = arg2;
1256 fcinfo.arg[2] = arg3;
1257 fcinfo.arg[3] = arg4;
1258 fcinfo.arg[4] = arg5;
1259 fcinfo.arg[5] = arg6;
1260 fcinfo.argnull[0] = false;
1261 fcinfo.argnull[1] = false;
1262 fcinfo.argnull[2] = false;
1263 fcinfo.argnull[3] = false;
1264 fcinfo.argnull[4] = false;
1265 fcinfo.argnull[5] = false;
1267 result = FunctionCallInvoke(&fcinfo);
1269 /* Check for null result, since caller is clearly not expecting one */
1271 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1277 FunctionCall7(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1278 Datum arg3, Datum arg4, Datum arg5,
1279 Datum arg6, Datum arg7)
1281 FunctionCallInfoData fcinfo;
1284 InitFunctionCallInfoData(fcinfo, flinfo, 7, NULL, NULL);
1286 fcinfo.arg[0] = arg1;
1287 fcinfo.arg[1] = arg2;
1288 fcinfo.arg[2] = arg3;
1289 fcinfo.arg[3] = arg4;
1290 fcinfo.arg[4] = arg5;
1291 fcinfo.arg[5] = arg6;
1292 fcinfo.arg[6] = arg7;
1293 fcinfo.argnull[0] = false;
1294 fcinfo.argnull[1] = false;
1295 fcinfo.argnull[2] = false;
1296 fcinfo.argnull[3] = false;
1297 fcinfo.argnull[4] = false;
1298 fcinfo.argnull[5] = false;
1299 fcinfo.argnull[6] = false;
1301 result = FunctionCallInvoke(&fcinfo);
1303 /* Check for null result, since caller is clearly not expecting one */
1305 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1311 FunctionCall8(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1312 Datum arg3, Datum arg4, Datum arg5,
1313 Datum arg6, Datum arg7, Datum arg8)
1315 FunctionCallInfoData fcinfo;
1318 InitFunctionCallInfoData(fcinfo, flinfo, 8, NULL, NULL);
1320 fcinfo.arg[0] = arg1;
1321 fcinfo.arg[1] = arg2;
1322 fcinfo.arg[2] = arg3;
1323 fcinfo.arg[3] = arg4;
1324 fcinfo.arg[4] = arg5;
1325 fcinfo.arg[5] = arg6;
1326 fcinfo.arg[6] = arg7;
1327 fcinfo.arg[7] = arg8;
1328 fcinfo.argnull[0] = false;
1329 fcinfo.argnull[1] = false;
1330 fcinfo.argnull[2] = false;
1331 fcinfo.argnull[3] = false;
1332 fcinfo.argnull[4] = false;
1333 fcinfo.argnull[5] = false;
1334 fcinfo.argnull[6] = false;
1335 fcinfo.argnull[7] = false;
1337 result = FunctionCallInvoke(&fcinfo);
1339 /* Check for null result, since caller is clearly not expecting one */
1341 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1347 FunctionCall9(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1348 Datum arg3, Datum arg4, Datum arg5,
1349 Datum arg6, Datum arg7, Datum arg8,
1352 FunctionCallInfoData fcinfo;
1355 InitFunctionCallInfoData(fcinfo, flinfo, 9, NULL, NULL);
1357 fcinfo.arg[0] = arg1;
1358 fcinfo.arg[1] = arg2;
1359 fcinfo.arg[2] = arg3;
1360 fcinfo.arg[3] = arg4;
1361 fcinfo.arg[4] = arg5;
1362 fcinfo.arg[5] = arg6;
1363 fcinfo.arg[6] = arg7;
1364 fcinfo.arg[7] = arg8;
1365 fcinfo.arg[8] = arg9;
1366 fcinfo.argnull[0] = false;
1367 fcinfo.argnull[1] = false;
1368 fcinfo.argnull[2] = false;
1369 fcinfo.argnull[3] = false;
1370 fcinfo.argnull[4] = false;
1371 fcinfo.argnull[5] = false;
1372 fcinfo.argnull[6] = false;
1373 fcinfo.argnull[7] = false;
1374 fcinfo.argnull[8] = false;
1376 result = FunctionCallInvoke(&fcinfo);
1378 /* Check for null result, since caller is clearly not expecting one */
1380 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1387 * These are for invocation of a function identified by OID with a
1388 * directly-computed parameter list. Note that neither arguments nor result
1389 * are allowed to be NULL. These are essentially fmgr_info() followed
1390 * by FunctionCallN(). If the same function is to be invoked repeatedly,
1391 * do the fmgr_info() once and then use FunctionCallN().
1394 OidFunctionCall1(Oid functionId, Datum arg1)
1397 FunctionCallInfoData fcinfo;
1400 fmgr_info(functionId, &flinfo);
1402 InitFunctionCallInfoData(fcinfo, &flinfo, 1, NULL, NULL);
1404 fcinfo.arg[0] = arg1;
1405 fcinfo.argnull[0] = false;
1407 result = FunctionCallInvoke(&fcinfo);
1409 /* Check for null result, since caller is clearly not expecting one */
1411 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1417 OidFunctionCall2(Oid functionId, Datum arg1, Datum arg2)
1420 FunctionCallInfoData fcinfo;
1423 fmgr_info(functionId, &flinfo);
1425 InitFunctionCallInfoData(fcinfo, &flinfo, 2, NULL, NULL);
1427 fcinfo.arg[0] = arg1;
1428 fcinfo.arg[1] = arg2;
1429 fcinfo.argnull[0] = false;
1430 fcinfo.argnull[1] = false;
1432 result = FunctionCallInvoke(&fcinfo);
1434 /* Check for null result, since caller is clearly not expecting one */
1436 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1442 OidFunctionCall3(Oid functionId, Datum arg1, Datum arg2,
1446 FunctionCallInfoData fcinfo;
1449 fmgr_info(functionId, &flinfo);
1451 InitFunctionCallInfoData(fcinfo, &flinfo, 3, NULL, NULL);
1453 fcinfo.arg[0] = arg1;
1454 fcinfo.arg[1] = arg2;
1455 fcinfo.arg[2] = arg3;
1456 fcinfo.argnull[0] = false;
1457 fcinfo.argnull[1] = false;
1458 fcinfo.argnull[2] = false;
1460 result = FunctionCallInvoke(&fcinfo);
1462 /* Check for null result, since caller is clearly not expecting one */
1464 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1470 OidFunctionCall4(Oid functionId, Datum arg1, Datum arg2,
1471 Datum arg3, Datum arg4)
1474 FunctionCallInfoData fcinfo;
1477 fmgr_info(functionId, &flinfo);
1479 InitFunctionCallInfoData(fcinfo, &flinfo, 4, NULL, NULL);
1481 fcinfo.arg[0] = arg1;
1482 fcinfo.arg[1] = arg2;
1483 fcinfo.arg[2] = arg3;
1484 fcinfo.arg[3] = arg4;
1485 fcinfo.argnull[0] = false;
1486 fcinfo.argnull[1] = false;
1487 fcinfo.argnull[2] = false;
1488 fcinfo.argnull[3] = false;
1490 result = FunctionCallInvoke(&fcinfo);
1492 /* Check for null result, since caller is clearly not expecting one */
1494 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1500 OidFunctionCall5(Oid functionId, Datum arg1, Datum arg2,
1501 Datum arg3, Datum arg4, Datum arg5)
1504 FunctionCallInfoData fcinfo;
1507 fmgr_info(functionId, &flinfo);
1509 InitFunctionCallInfoData(fcinfo, &flinfo, 5, NULL, NULL);
1511 fcinfo.arg[0] = arg1;
1512 fcinfo.arg[1] = arg2;
1513 fcinfo.arg[2] = arg3;
1514 fcinfo.arg[3] = arg4;
1515 fcinfo.arg[4] = arg5;
1516 fcinfo.argnull[0] = false;
1517 fcinfo.argnull[1] = false;
1518 fcinfo.argnull[2] = false;
1519 fcinfo.argnull[3] = false;
1520 fcinfo.argnull[4] = false;
1522 result = FunctionCallInvoke(&fcinfo);
1524 /* Check for null result, since caller is clearly not expecting one */
1526 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1532 OidFunctionCall6(Oid functionId, Datum arg1, Datum arg2,
1533 Datum arg3, Datum arg4, Datum arg5,
1537 FunctionCallInfoData fcinfo;
1540 fmgr_info(functionId, &flinfo);
1542 InitFunctionCallInfoData(fcinfo, &flinfo, 6, NULL, NULL);
1544 fcinfo.arg[0] = arg1;
1545 fcinfo.arg[1] = arg2;
1546 fcinfo.arg[2] = arg3;
1547 fcinfo.arg[3] = arg4;
1548 fcinfo.arg[4] = arg5;
1549 fcinfo.arg[5] = arg6;
1550 fcinfo.argnull[0] = false;
1551 fcinfo.argnull[1] = false;
1552 fcinfo.argnull[2] = false;
1553 fcinfo.argnull[3] = false;
1554 fcinfo.argnull[4] = false;
1555 fcinfo.argnull[5] = false;
1557 result = FunctionCallInvoke(&fcinfo);
1559 /* Check for null result, since caller is clearly not expecting one */
1561 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1567 OidFunctionCall7(Oid functionId, Datum arg1, Datum arg2,
1568 Datum arg3, Datum arg4, Datum arg5,
1569 Datum arg6, Datum arg7)
1572 FunctionCallInfoData fcinfo;
1575 fmgr_info(functionId, &flinfo);
1577 InitFunctionCallInfoData(fcinfo, &flinfo, 7, NULL, NULL);
1579 fcinfo.arg[0] = arg1;
1580 fcinfo.arg[1] = arg2;
1581 fcinfo.arg[2] = arg3;
1582 fcinfo.arg[3] = arg4;
1583 fcinfo.arg[4] = arg5;
1584 fcinfo.arg[5] = arg6;
1585 fcinfo.arg[6] = arg7;
1586 fcinfo.argnull[0] = false;
1587 fcinfo.argnull[1] = false;
1588 fcinfo.argnull[2] = false;
1589 fcinfo.argnull[3] = false;
1590 fcinfo.argnull[4] = false;
1591 fcinfo.argnull[5] = false;
1592 fcinfo.argnull[6] = false;
1594 result = FunctionCallInvoke(&fcinfo);
1596 /* Check for null result, since caller is clearly not expecting one */
1598 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1604 OidFunctionCall8(Oid functionId, Datum arg1, Datum arg2,
1605 Datum arg3, Datum arg4, Datum arg5,
1606 Datum arg6, Datum arg7, Datum arg8)
1609 FunctionCallInfoData fcinfo;
1612 fmgr_info(functionId, &flinfo);
1614 InitFunctionCallInfoData(fcinfo, &flinfo, 8, NULL, NULL);
1616 fcinfo.arg[0] = arg1;
1617 fcinfo.arg[1] = arg2;
1618 fcinfo.arg[2] = arg3;
1619 fcinfo.arg[3] = arg4;
1620 fcinfo.arg[4] = arg5;
1621 fcinfo.arg[5] = arg6;
1622 fcinfo.arg[6] = arg7;
1623 fcinfo.arg[7] = arg8;
1624 fcinfo.argnull[0] = false;
1625 fcinfo.argnull[1] = false;
1626 fcinfo.argnull[2] = false;
1627 fcinfo.argnull[3] = false;
1628 fcinfo.argnull[4] = false;
1629 fcinfo.argnull[5] = false;
1630 fcinfo.argnull[6] = false;
1631 fcinfo.argnull[7] = false;
1633 result = FunctionCallInvoke(&fcinfo);
1635 /* Check for null result, since caller is clearly not expecting one */
1637 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1643 OidFunctionCall9(Oid functionId, Datum arg1, Datum arg2,
1644 Datum arg3, Datum arg4, Datum arg5,
1645 Datum arg6, Datum arg7, Datum arg8,
1649 FunctionCallInfoData fcinfo;
1652 fmgr_info(functionId, &flinfo);
1654 InitFunctionCallInfoData(fcinfo, &flinfo, 9, NULL, NULL);
1656 fcinfo.arg[0] = arg1;
1657 fcinfo.arg[1] = arg2;
1658 fcinfo.arg[2] = arg3;
1659 fcinfo.arg[3] = arg4;
1660 fcinfo.arg[4] = arg5;
1661 fcinfo.arg[5] = arg6;
1662 fcinfo.arg[6] = arg7;
1663 fcinfo.arg[7] = arg8;
1664 fcinfo.arg[8] = arg9;
1665 fcinfo.argnull[0] = false;
1666 fcinfo.argnull[1] = false;
1667 fcinfo.argnull[2] = false;
1668 fcinfo.argnull[3] = false;
1669 fcinfo.argnull[4] = false;
1670 fcinfo.argnull[5] = false;
1671 fcinfo.argnull[6] = false;
1672 fcinfo.argnull[7] = false;
1673 fcinfo.argnull[8] = false;
1675 result = FunctionCallInvoke(&fcinfo);
1677 /* Check for null result, since caller is clearly not expecting one */
1679 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1686 * Special cases for convenient invocation of datatype I/O functions.
1690 * Call a previously-looked-up datatype input function.
1692 * "str" may be NULL to indicate we are reading a NULL. In this case
1693 * the caller should assume the result is NULL, but we'll call the input
1694 * function anyway if it's not strict. So this is almost but not quite
1695 * the same as FunctionCall3.
1698 InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
1700 FunctionCallInfoData fcinfo;
1703 if (str == NULL && flinfo->fn_strict)
1704 return (Datum) 0; /* just return null result */
1706 InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);
1708 fcinfo.arg[0] = CStringGetDatum(str);
1709 fcinfo.arg[1] = ObjectIdGetDatum(typioparam);
1710 fcinfo.arg[2] = Int32GetDatum(typmod);
1711 fcinfo.argnull[0] = (str == NULL);
1712 fcinfo.argnull[1] = false;
1713 fcinfo.argnull[2] = false;
1715 result = FunctionCallInvoke(&fcinfo);
1717 /* Should get null result if and only if str is NULL */
1721 elog(ERROR, "input function %u returned non-NULL",
1722 fcinfo.flinfo->fn_oid);
1727 elog(ERROR, "input function %u returned NULL",
1728 fcinfo.flinfo->fn_oid);
1735 * Call a previously-looked-up datatype output function.
1737 * Do not call this on NULL datums.
1739 * This is mere window dressing for FunctionCall1, but its use is recommended
1740 * anyway so that code invoking output functions can be identified easily.
1743 OutputFunctionCall(FmgrInfo *flinfo, Datum val)
1745 return DatumGetCString(FunctionCall1(flinfo, val));
1749 * Call a previously-looked-up datatype binary-input function.
1751 * "buf" may be NULL to indicate we are reading a NULL. In this case
1752 * the caller should assume the result is NULL, but we'll call the receive
1753 * function anyway if it's not strict. So this is almost but not quite
1754 * the same as FunctionCall3.
1757 ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf,
1758 Oid typioparam, int32 typmod)
1760 FunctionCallInfoData fcinfo;
1763 if (buf == NULL && flinfo->fn_strict)
1764 return (Datum) 0; /* just return null result */
1766 InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);
1768 fcinfo.arg[0] = PointerGetDatum(buf);
1769 fcinfo.arg[1] = ObjectIdGetDatum(typioparam);
1770 fcinfo.arg[2] = Int32GetDatum(typmod);
1771 fcinfo.argnull[0] = (buf == NULL);
1772 fcinfo.argnull[1] = false;
1773 fcinfo.argnull[2] = false;
1775 result = FunctionCallInvoke(&fcinfo);
1777 /* Should get null result if and only if buf is NULL */
1781 elog(ERROR, "receive function %u returned non-NULL",
1782 fcinfo.flinfo->fn_oid);
1787 elog(ERROR, "receive function %u returned NULL",
1788 fcinfo.flinfo->fn_oid);
1795 * Call a previously-looked-up datatype binary-output function.
1797 * Do not call this on NULL datums.
1799 * This is little more than window dressing for FunctionCall1, but its use is
1800 * recommended anyway so that code invoking output functions can be identified
1801 * easily. Note however that it does guarantee a non-toasted result.
1804 SendFunctionCall(FmgrInfo *flinfo, Datum val)
1806 return DatumGetByteaP(FunctionCall1(flinfo, val));
1810 * As above, for I/O functions identified by OID. These are only to be used
1811 * in seldom-executed code paths. They are not only slow but leak memory.
1814 OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
1818 fmgr_info(functionId, &flinfo);
1819 return InputFunctionCall(&flinfo, str, typioparam, typmod);
1823 OidOutputFunctionCall(Oid functionId, Datum val)
1827 fmgr_info(functionId, &flinfo);
1828 return OutputFunctionCall(&flinfo, val);
1832 OidReceiveFunctionCall(Oid functionId, StringInfo buf,
1833 Oid typioparam, int32 typmod)
1837 fmgr_info(functionId, &flinfo);
1838 return ReceiveFunctionCall(&flinfo, buf, typioparam, typmod);
1842 OidSendFunctionCall(Oid functionId, Datum val)
1846 fmgr_info(functionId, &flinfo);
1847 return SendFunctionCall(&flinfo, val);
1852 * !!! OLD INTERFACE !!!
1854 * fmgr() is the only remaining vestige of the old-style caller support
1855 * functions. It's no longer used anywhere in the Postgres distribution,
1856 * but we should leave it around for a release or two to ease the transition
1857 * for user-supplied C functions. OidFunctionCallN() replaces it for new
1860 * DEPRECATED, DO NOT USE IN NEW CODE
1863 fmgr(Oid procedureId,...)
1866 FunctionCallInfoData fcinfo;
1870 fmgr_info(procedureId, &flinfo);
1872 MemSet(&fcinfo, 0, sizeof(fcinfo));
1873 fcinfo.flinfo = &flinfo;
1874 fcinfo.nargs = flinfo.fn_nargs;
1875 n_arguments = fcinfo.nargs;
1877 if (n_arguments > 0)
1882 if (n_arguments > FUNC_MAX_ARGS)
1884 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
1885 errmsg("function %u has too many arguments (%d, maximum is %d)",
1886 flinfo.fn_oid, n_arguments, FUNC_MAX_ARGS)));
1887 va_start(pvar, procedureId);
1888 for (i = 0; i < n_arguments; i++)
1889 fcinfo.arg[i] = (Datum) va_arg(pvar, char *);
1893 result = FunctionCallInvoke(&fcinfo);
1895 /* Check for null result, since caller is clearly not expecting one */
1897 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1899 return (char *) result;
1903 /*-------------------------------------------------------------------------
1904 * Support routines for standard pass-by-reference datatypes
1906 * Note: at some point, at least on some platforms, these might become
1907 * pass-by-value types. Obviously Datum must be >= 8 bytes to allow
1908 * int64 or float8 to be pass-by-value. I think that Float4GetDatum
1909 * and Float8GetDatum will need to be out-of-line routines anyway,
1910 * since just casting from float to Datum will not do the right thing;
1911 * some kind of trick with pointer-casting or a union will be needed.
1912 *-------------------------------------------------------------------------
1916 Int64GetDatum(int64 X)
1918 #ifndef INT64_IS_BUSTED
1919 int64 *retval = (int64 *) palloc(sizeof(int64));
1922 return PointerGetDatum(retval);
1923 #else /* INT64_IS_BUSTED */
1926 * On a machine with no 64-bit-int C datatype, sizeof(int64) will not be
1927 * 8, but we want Int64GetDatum to return an 8-byte object anyway, with
1928 * zeroes in the unused bits. This is needed so that, for example, hash
1929 * join of int8 will behave properly.
1931 int64 *retval = (int64 *) palloc0(Max(sizeof(int64), 8));
1934 return PointerGetDatum(retval);
1935 #endif /* INT64_IS_BUSTED */
1939 Float4GetDatum(float4 X)
1941 float4 *retval = (float4 *) palloc(sizeof(float4));
1944 return PointerGetDatum(retval);
1948 Float8GetDatum(float8 X)
1950 float8 *retval = (float8 *) palloc(sizeof(float8));
1953 return PointerGetDatum(retval);
1956 /*-------------------------------------------------------------------------
1957 * Support routines for toastable datatypes
1958 *-------------------------------------------------------------------------
1962 pg_detoast_datum(struct varlena * datum)
1964 if (VARATT_IS_EXTENDED(datum))
1965 return (struct varlena *) heap_tuple_untoast_attr((varattrib *) datum);
1971 pg_detoast_datum_copy(struct varlena * datum)
1973 if (VARATT_IS_EXTENDED(datum))
1974 return (struct varlena *) heap_tuple_untoast_attr((varattrib *) datum);
1977 /* Make a modifiable copy of the varlena object */
1978 Size len = VARSIZE(datum);
1979 struct varlena *result = (struct varlena *) palloc(len);
1981 memcpy(result, datum, len);
1987 pg_detoast_datum_slice(struct varlena * datum, int32 first, int32 count)
1989 /* Only get the specified portion from the toast rel */
1990 return (struct varlena *) heap_tuple_untoast_attr_slice((varattrib *) datum, first, count);
1993 /*-------------------------------------------------------------------------
1994 * Support routines for extracting info from fn_expr parse tree
1996 * These are needed by polymorphic functions, which accept multiple possible
1997 * input types and need help from the parser to know what they've got.
1998 *-------------------------------------------------------------------------
2002 * Get the actual type OID of the function return type
2004 * Returns InvalidOid if information is not available
2007 get_fn_expr_rettype(FmgrInfo *flinfo)
2012 * can't return anything useful if we have no FmgrInfo or if its fn_expr
2013 * node has not been initialized
2015 if (!flinfo || !flinfo->fn_expr)
2018 expr = flinfo->fn_expr;
2020 return exprType(expr);
2024 * Get the actual type OID of a specific function argument (counting from 0)
2026 * Returns InvalidOid if information is not available
2029 get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
2032 * can't return anything useful if we have no FmgrInfo or if its fn_expr
2033 * node has not been initialized
2035 if (!flinfo || !flinfo->fn_expr)
2038 return get_call_expr_argtype(flinfo->fn_expr, argnum);
2042 * Get the actual type OID of a specific function argument (counting from 0),
2043 * but working from the calling expression tree instead of FmgrInfo
2045 * Returns InvalidOid if information is not available
2048 get_call_expr_argtype(Node *expr, int argnum)
2056 if (IsA(expr, FuncExpr))
2057 args = ((FuncExpr *) expr)->args;
2058 else if (IsA(expr, OpExpr))
2059 args = ((OpExpr *) expr)->args;
2060 else if (IsA(expr, DistinctExpr))
2061 args = ((DistinctExpr *) expr)->args;
2062 else if (IsA(expr, ScalarArrayOpExpr))
2063 args = ((ScalarArrayOpExpr *) expr)->args;
2064 else if (IsA(expr, NullIfExpr))
2065 args = ((NullIfExpr *) expr)->args;
2069 if (argnum < 0 || argnum >= list_length(args))
2072 argtype = exprType((Node *) list_nth(args, argnum));
2075 * special hack for ScalarArrayOpExpr: what the underlying function will
2076 * actually get passed is the element type of the array.
2078 if (IsA(expr, ScalarArrayOpExpr) &&
2080 argtype = get_element_type(argtype);