1 /*-------------------------------------------------------------------------
4 * The Postgres function manager.
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/utils/fmgr/fmgr.c,v 1.112 2008/01/01 19:45:53 momjian Exp $
13 *-------------------------------------------------------------------------
18 #include "access/heapam.h"
19 #include "access/tuptoaster.h"
20 #include "catalog/pg_language.h"
21 #include "catalog/pg_proc.h"
22 #include "executor/functions.h"
23 #include "miscadmin.h"
24 #include "parser/parse_expr.h"
25 #include "utils/builtins.h"
26 #include "utils/fmgrtab.h"
27 #include "utils/guc.h"
28 #include "utils/lsyscache.h"
29 #include "utils/syscache.h"
33 * Declaration for old-style function pointer type. This is now used only
34 * in fmgr_oldstyle() and is no longer exported.
36 * The m68k SVR4 ABI defines that pointers are returned in %a0 instead of
37 * %d0. So if a function pointer is declared to return a pointer, the
38 * compiler may look only into %a0, but if the called function was declared
39 * to return an integer type, it puts its value only into %d0. So the
40 * caller doesn't pick up the correct return value. The solution is to
41 * declare the function pointer to return int, so the compiler picks up the
42 * return value from %d0. (Functions returning pointers put their value
43 * *additionally* into %d0 for compatibility.) The price is that there are
44 * some warnings about int->pointer conversions ... which we can suppress
45 * with suitably ugly casts in fmgr_oldstyle().
47 #if (defined(__mc68000__) || (defined(__m68k__))) && defined(__ELF__)
48 typedef int32 (*func_ptr) ();
50 typedef char *(*func_ptr) ();
54 * For an oldstyle function, fn_extra points to a record like this:
58 func_ptr func; /* Address of the oldstyle function */
59 bool arg_toastable[FUNC_MAX_ARGS]; /* is n'th arg of a toastable
64 * Hashtable for fast lookup of external C functions
68 /* fn_oid is the hash key and so must be first! */
69 Oid fn_oid; /* OID of an external C function */
70 TransactionId fn_xmin; /* for checking up-to-dateness */
71 ItemPointerData fn_tid;
72 PGFunction user_fn; /* the function's address */
73 const Pg_finfo_record *inforec; /* address of its info record */
76 static HTAB *CFuncHash = NULL;
79 static void fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt,
80 bool ignore_security);
81 static void fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple);
82 static void fmgr_info_other_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple);
83 static CFuncHashTabEntry *lookup_C_func(HeapTuple procedureTuple);
84 static void record_C_func(HeapTuple procedureTuple,
85 PGFunction user_fn, const Pg_finfo_record *inforec);
86 static Datum fmgr_oldstyle(PG_FUNCTION_ARGS);
87 static Datum fmgr_security_definer(PG_FUNCTION_ARGS);
91 * Lookup routines for builtin-function table. We can search by either Oid
92 * or name, but search by Oid is much faster.
95 static const FmgrBuiltin *
96 fmgr_isbuiltin(Oid id)
99 int high = fmgr_nbuiltins - 1;
102 * Loop invariant: low is the first index that could contain target entry,
103 * and high is the last index that could contain it.
107 int i = (high + low) / 2;
108 const FmgrBuiltin *ptr = &fmgr_builtins[i];
112 else if (id > ptr->foid)
121 * Lookup a builtin by name. Note there can be more than one entry in
122 * the array with the same name, but they should all point to the same
125 static const FmgrBuiltin *
126 fmgr_lookupByName(const char *name)
130 for (i = 0; i < fmgr_nbuiltins; i++)
132 if (strcmp(name, fmgr_builtins[i].funcName) == 0)
133 return fmgr_builtins + i;
139 * This routine fills a FmgrInfo struct, given the OID
140 * of the function to be called.
142 * The caller's CurrentMemoryContext is used as the fn_mcxt of the info
143 * struct; this means that any subsidiary data attached to the info struct
144 * (either by fmgr_info itself, or later on by a function call handler)
145 * will be allocated in that context. The caller must ensure that this
146 * context is at least as long-lived as the info struct itself. This is
147 * not a problem in typical cases where the info struct is on the stack or
148 * in freshly-palloc'd space. However, if one intends to store an info
149 * struct in a long-lived table, it's better to use fmgr_info_cxt.
152 fmgr_info(Oid functionId, FmgrInfo *finfo)
154 fmgr_info_cxt(functionId, finfo, CurrentMemoryContext);
158 * Fill a FmgrInfo struct, specifying a memory context in which its
159 * subsidiary data should go.
162 fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
164 fmgr_info_cxt_security(functionId, finfo, mcxt, false);
168 * This one does the actual work. ignore_security is ordinarily false
169 * but is set to true by fmgr_security_definer to avoid infinite
173 fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt,
174 bool ignore_security)
176 const FmgrBuiltin *fbp;
177 HeapTuple procedureTuple;
178 Form_pg_proc procedureStruct;
184 * fn_oid *must* be filled in last. Some code assumes that if fn_oid is
185 * valid, the whole struct is valid. Some FmgrInfo struct's do survive
188 finfo->fn_oid = InvalidOid;
189 finfo->fn_extra = NULL;
190 finfo->fn_mcxt = mcxt;
191 finfo->fn_expr = NULL; /* caller may set this later */
193 if ((fbp = fmgr_isbuiltin(functionId)) != NULL)
196 * Fast path for builtin functions: don't bother consulting pg_proc
198 finfo->fn_nargs = fbp->nargs;
199 finfo->fn_strict = fbp->strict;
200 finfo->fn_retset = fbp->retset;
201 finfo->fn_addr = fbp->func;
202 finfo->fn_oid = functionId;
206 /* Otherwise we need the pg_proc entry */
207 procedureTuple = SearchSysCache(PROCOID,
208 ObjectIdGetDatum(functionId),
210 if (!HeapTupleIsValid(procedureTuple))
211 elog(ERROR, "cache lookup failed for function %u", functionId);
212 procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
214 finfo->fn_nargs = procedureStruct->pronargs;
215 finfo->fn_strict = procedureStruct->proisstrict;
216 finfo->fn_retset = procedureStruct->proretset;
219 * If it has prosecdef set, or non-null proconfig, use
220 * fmgr_security_definer call handler.
222 if (!ignore_security &&
223 (procedureStruct->prosecdef ||
224 !heap_attisnull(procedureTuple, Anum_pg_proc_proconfig)))
226 finfo->fn_addr = fmgr_security_definer;
227 finfo->fn_oid = functionId;
228 ReleaseSysCache(procedureTuple);
232 switch (procedureStruct->prolang)
234 case INTERNALlanguageId:
237 * For an ordinary builtin function, we should never get here
238 * because the isbuiltin() search above will have succeeded.
239 * However, if the user has done a CREATE FUNCTION to create an
240 * alias for a builtin function, we can end up here. In that case
241 * we have to look up the function by name. The name of the
242 * internal function is stored in prosrc (it doesn't have to be
243 * the same as the name of the alias!)
245 prosrcdatum = SysCacheGetAttr(PROCOID, procedureTuple,
246 Anum_pg_proc_prosrc, &isnull);
248 elog(ERROR, "null prosrc");
249 prosrc = DatumGetCString(DirectFunctionCall1(textout,
251 fbp = fmgr_lookupByName(prosrc);
254 (errcode(ERRCODE_UNDEFINED_FUNCTION),
255 errmsg("internal function \"%s\" is not in internal lookup table",
258 /* Should we check that nargs, strict, retset match the table? */
259 finfo->fn_addr = fbp->func;
263 fmgr_info_C_lang(functionId, finfo, procedureTuple);
267 finfo->fn_addr = fmgr_sql;
271 fmgr_info_other_lang(functionId, finfo, procedureTuple);
275 finfo->fn_oid = functionId;
276 ReleaseSysCache(procedureTuple);
280 * Special fmgr_info processing for C-language functions. Note that
281 * finfo->fn_oid is not valid yet.
284 fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
286 Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
287 CFuncHashTabEntry *hashentry;
289 const Pg_finfo_record *inforec;
290 Oldstyle_fnextra *fnextra;
295 * See if we have the function address cached already
297 hashentry = lookup_C_func(procedureTuple);
300 user_fn = hashentry->user_fn;
301 inforec = hashentry->inforec;
312 * Get prosrc and probin strings (link symbol and library filename)
314 prosrcattr = SysCacheGetAttr(PROCOID, procedureTuple,
315 Anum_pg_proc_prosrc, &isnull);
317 elog(ERROR, "null prosrc for function %u", functionId);
318 prosrcstring = DatumGetCString(DirectFunctionCall1(textout,
321 probinattr = SysCacheGetAttr(PROCOID, procedureTuple,
322 Anum_pg_proc_probin, &isnull);
324 elog(ERROR, "null probin for function %u", functionId);
325 probinstring = DatumGetCString(DirectFunctionCall1(textout,
328 /* Look up the function itself */
329 user_fn = load_external_function(probinstring, prosrcstring, true,
332 /* Get the function information record (real or default) */
333 inforec = fetch_finfo_record(libraryhandle, prosrcstring);
335 /* Cache the addresses for later calls */
336 record_C_func(procedureTuple, user_fn, inforec);
342 switch (inforec->api_version)
345 /* Old style: need to use a handler */
346 finfo->fn_addr = fmgr_oldstyle;
347 fnextra = (Oldstyle_fnextra *)
348 MemoryContextAllocZero(finfo->fn_mcxt,
349 sizeof(Oldstyle_fnextra));
350 finfo->fn_extra = (void *) fnextra;
351 fnextra->func = (func_ptr) user_fn;
352 for (i = 0; i < procedureStruct->pronargs; i++)
354 fnextra->arg_toastable[i] =
355 TypeIsToastable(procedureStruct->proargtypes.values[i]);
359 /* New style: call directly */
360 finfo->fn_addr = user_fn;
363 /* Shouldn't get here if fetch_finfo_record did its job */
364 elog(ERROR, "unrecognized function API version: %d",
365 inforec->api_version);
371 * Special fmgr_info processing for other-language functions. Note
372 * that finfo->fn_oid is not valid yet.
375 fmgr_info_other_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
377 Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
378 Oid language = procedureStruct->prolang;
379 HeapTuple languageTuple;
380 Form_pg_language languageStruct;
383 languageTuple = SearchSysCache(LANGOID,
384 ObjectIdGetDatum(language),
386 if (!HeapTupleIsValid(languageTuple))
387 elog(ERROR, "cache lookup failed for language %u", language);
388 languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
390 fmgr_info(languageStruct->lanplcallfoid, &plfinfo);
391 finfo->fn_addr = plfinfo.fn_addr;
394 * If lookup of the PL handler function produced nonnull fn_extra,
395 * complain --- it must be an oldstyle function! We no longer support
396 * oldstyle PL handlers.
398 if (plfinfo.fn_extra != NULL)
399 elog(ERROR, "language %u has old-style handler", language);
401 ReleaseSysCache(languageTuple);
405 * Fetch and validate the information record for the given external function.
406 * The function is specified by a handle for the containing library
407 * (obtained from load_external_function) as well as the function name.
409 * If no info function exists for the given name, it is not an error.
410 * Instead we return a default info record for a version-0 function.
411 * We want to raise an error here only if the info function returns
414 * This function is broken out of fmgr_info_C_lang so that fmgr_c_validator
415 * can validate the information record for a function not yet entered into
418 const Pg_finfo_record *
419 fetch_finfo_record(void *filehandle, char *funcname)
422 PGFInfoFunction infofunc;
423 const Pg_finfo_record *inforec;
424 static Pg_finfo_record default_inforec = {0};
426 /* Compute name of info func */
427 infofuncname = (char *) palloc(strlen(funcname) + 10);
428 strcpy(infofuncname, "pg_finfo_");
429 strcat(infofuncname, funcname);
431 /* Try to look up the info function */
432 infofunc = (PGFInfoFunction) lookup_external_function(filehandle,
434 if (infofunc == NULL)
436 /* Not found --- assume version 0 */
438 return &default_inforec;
441 /* Found, so call it */
442 inforec = (*infofunc) ();
444 /* Validate result as best we can */
446 elog(ERROR, "null result from info function \"%s\"", infofuncname);
447 switch (inforec->api_version)
451 /* OK, no additional fields to validate */
455 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
456 errmsg("unrecognized API version %d reported by info function \"%s\"",
457 inforec->api_version, infofuncname)));
466 /*-------------------------------------------------------------------------
467 * Routines for caching lookup information for external C functions.
469 * The routines in dfmgr.c are relatively slow, so we try to avoid running
470 * them more than once per external function per session. We use a hash table
471 * with the function OID as the lookup key.
472 *-------------------------------------------------------------------------
476 * lookup_C_func: try to find a C function in the hash table
478 * If an entry exists and is up to date, return it; else return NULL
480 static CFuncHashTabEntry *
481 lookup_C_func(HeapTuple procedureTuple)
483 Oid fn_oid = HeapTupleGetOid(procedureTuple);
484 CFuncHashTabEntry *entry;
486 if (CFuncHash == NULL)
487 return NULL; /* no table yet */
488 entry = (CFuncHashTabEntry *)
489 hash_search(CFuncHash,
494 return NULL; /* no such entry */
495 if (entry->fn_xmin == HeapTupleHeaderGetXmin(procedureTuple->t_data) &&
496 ItemPointerEquals(&entry->fn_tid, &procedureTuple->t_self))
497 return entry; /* OK */
498 return NULL; /* entry is out of date */
502 * record_C_func: enter (or update) info about a C function in the hash table
505 record_C_func(HeapTuple procedureTuple,
506 PGFunction user_fn, const Pg_finfo_record *inforec)
508 Oid fn_oid = HeapTupleGetOid(procedureTuple);
509 CFuncHashTabEntry *entry;
512 /* Create the hash table if it doesn't exist yet */
513 if (CFuncHash == NULL)
517 MemSet(&hash_ctl, 0, sizeof(hash_ctl));
518 hash_ctl.keysize = sizeof(Oid);
519 hash_ctl.entrysize = sizeof(CFuncHashTabEntry);
520 hash_ctl.hash = oid_hash;
521 CFuncHash = hash_create("CFuncHash",
524 HASH_ELEM | HASH_FUNCTION);
527 entry = (CFuncHashTabEntry *)
528 hash_search(CFuncHash,
532 /* OID is already filled in */
533 entry->fn_xmin = HeapTupleHeaderGetXmin(procedureTuple->t_data);
534 entry->fn_tid = procedureTuple->t_self;
535 entry->user_fn = user_fn;
536 entry->inforec = inforec;
540 * clear_external_function_hash: remove entries for a library being closed
542 * Presently we just zap the entire hash table, but later it might be worth
543 * the effort to remove only the entries associated with the given handle.
546 clear_external_function_hash(void *filehandle)
549 hash_destroy(CFuncHash);
555 * Copy an FmgrInfo struct
557 * This is inherently somewhat bogus since we can't reliably duplicate
558 * language-dependent subsidiary info. We cheat by zeroing fn_extra,
559 * instead, meaning that subsidiary info will have to be recomputed.
562 fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo,
563 MemoryContext destcxt)
565 memcpy(dstinfo, srcinfo, sizeof(FmgrInfo));
566 dstinfo->fn_mcxt = destcxt;
567 if (dstinfo->fn_addr == fmgr_oldstyle)
569 /* For oldstyle functions we must copy fn_extra */
570 Oldstyle_fnextra *fnextra;
572 fnextra = (Oldstyle_fnextra *)
573 MemoryContextAlloc(destcxt, sizeof(Oldstyle_fnextra));
574 memcpy(fnextra, srcinfo->fn_extra, sizeof(Oldstyle_fnextra));
575 dstinfo->fn_extra = (void *) fnextra;
578 dstinfo->fn_extra = NULL;
583 * Specialized lookup routine for fmgr_internal_validator: given the alleged
584 * name of an internal function, return the OID of the function.
585 * If the name is not recognized, return InvalidOid.
588 fmgr_internal_function(const char *proname)
590 const FmgrBuiltin *fbp = fmgr_lookupByName(proname);
599 * Handler for old-style "C" language functions
602 fmgr_oldstyle(PG_FUNCTION_ARGS)
604 Oldstyle_fnextra *fnextra;
605 int n_arguments = fcinfo->nargs;
611 if (fcinfo->flinfo == NULL || fcinfo->flinfo->fn_extra == NULL)
612 elog(ERROR, "fmgr_oldstyle received NULL pointer");
613 fnextra = (Oldstyle_fnextra *) fcinfo->flinfo->fn_extra;
616 * Result is NULL if any argument is NULL, but we still call the function
617 * (peculiar, but that's the way it worked before, and after all this is a
618 * backwards-compatibility wrapper). Note, however, that we'll never get
619 * here with NULL arguments if the function is marked strict.
621 * We also need to detoast any TOAST-ed inputs, since it's unlikely that
622 * an old-style function knows about TOASTing.
625 for (i = 0; i < n_arguments; i++)
629 else if (fnextra->arg_toastable[i])
630 fcinfo->arg[i] = PointerGetDatum(PG_DETOAST_DATUM(fcinfo->arg[i]));
632 fcinfo->isnull = isnull;
634 user_fn = fnextra->func;
639 returnValue = (char *) (*user_fn) ();
644 * nullvalue() used to use isNull to check if arg is NULL; perhaps
645 * there are other functions still out there that also rely on
646 * this undocumented hack?
648 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
652 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
656 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
661 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
667 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
674 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
682 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
691 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
701 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
712 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
724 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
737 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
751 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
766 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
782 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
799 returnValue = (char *) (*user_fn) (fcinfo->arg[0],
819 * Increasing FUNC_MAX_ARGS doesn't automatically add cases to the
820 * above code, so mention the actual value in this error not
821 * FUNC_MAX_ARGS. You could add cases to the above if you needed
822 * to support old-style functions with many arguments, but making
823 * 'em be new-style is probably a better idea.
826 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
827 errmsg("function %u has too many arguments (%d, maximum is %d)",
828 fcinfo->flinfo->fn_oid, n_arguments, 16)));
829 returnValue = NULL; /* keep compiler quiet */
833 return (Datum) returnValue;
838 * Support for security-definer and proconfig-using functions. We support
839 * both of these features using the same call handler, because they are
840 * often used together and it would be inefficient (as well as notationally
841 * messy) to have two levels of call handler involved.
843 struct fmgr_security_definer_cache
845 FmgrInfo flinfo; /* lookup info for target function */
846 Oid userid; /* userid to set, or InvalidOid */
847 ArrayType *proconfig; /* GUC values to set, or NULL */
851 * Function handler for security-definer/proconfig functions. We extract the
852 * OID of the actual function and do a fmgr lookup again. Then we fetch the
853 * pg_proc row and copy the owner ID and proconfig fields. (All this info
854 * is cached for the duration of the current query.) To execute a call,
855 * we temporarily replace the flinfo with the cached/looked-up one, while
856 * keeping the outer fcinfo (which contains all the actual arguments, etc.)
857 * intact. This is not re-entrant, but then the fcinfo itself can't be used
858 * re-entrantly anyway.
861 fmgr_security_definer(PG_FUNCTION_ARGS)
864 struct fmgr_security_definer_cache *volatile fcache;
865 FmgrInfo *save_flinfo;
867 volatile int save_nestlevel;
869 if (!fcinfo->flinfo->fn_extra)
872 Form_pg_proc procedureStruct;
875 MemoryContext oldcxt;
877 fcache = MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt,
880 fmgr_info_cxt_security(fcinfo->flinfo->fn_oid, &fcache->flinfo,
881 fcinfo->flinfo->fn_mcxt, true);
882 fcache->flinfo.fn_expr = fcinfo->flinfo->fn_expr;
884 tuple = SearchSysCache(PROCOID,
885 ObjectIdGetDatum(fcinfo->flinfo->fn_oid),
887 if (!HeapTupleIsValid(tuple))
888 elog(ERROR, "cache lookup failed for function %u",
889 fcinfo->flinfo->fn_oid);
890 procedureStruct = (Form_pg_proc) GETSTRUCT(tuple);
892 if (procedureStruct->prosecdef)
893 fcache->userid = procedureStruct->proowner;
895 datum = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_proconfig,
899 oldcxt = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
900 fcache->proconfig = DatumGetArrayTypePCopy(datum);
901 MemoryContextSwitchTo(oldcxt);
904 ReleaseSysCache(tuple);
906 fcinfo->flinfo->fn_extra = fcache;
909 fcache = fcinfo->flinfo->fn_extra;
911 save_flinfo = fcinfo->flinfo;
912 /* GetUserId is cheap enough that no harm in a wasted call */
913 save_userid = GetUserId();
914 if (fcache->proconfig) /* Need a new GUC nesting level */
915 save_nestlevel = NewGUCNestLevel();
917 save_nestlevel = 0; /* keep compiler quiet */
921 fcinfo->flinfo = &fcache->flinfo;
923 if (OidIsValid(fcache->userid))
924 SetUserId(fcache->userid);
926 if (fcache->proconfig)
928 ProcessGUCArray(fcache->proconfig,
929 (superuser() ? PGC_SUSET : PGC_USERSET),
934 result = FunctionCallInvoke(fcinfo);
938 fcinfo->flinfo = save_flinfo;
939 /* We don't need to restore GUC settings, outer xact abort will */
940 if (OidIsValid(fcache->userid))
941 SetUserId(save_userid);
946 fcinfo->flinfo = save_flinfo;
947 if (fcache->proconfig)
948 AtEOXact_GUC(true, save_nestlevel);
949 if (OidIsValid(fcache->userid))
950 SetUserId(save_userid);
956 /*-------------------------------------------------------------------------
957 * Support routines for callers of fmgr-compatible functions
958 *-------------------------------------------------------------------------
962 * These are for invocation of a specifically named function with a
963 * directly-computed parameter list. Note that neither arguments nor result
964 * are allowed to be NULL. Also, the function cannot be one that needs to
965 * look at FmgrInfo, since there won't be any.
968 DirectFunctionCall1(PGFunction func, Datum arg1)
970 FunctionCallInfoData fcinfo;
973 InitFunctionCallInfoData(fcinfo, NULL, 1, NULL, NULL);
975 fcinfo.arg[0] = arg1;
976 fcinfo.argnull[0] = false;
978 result = (*func) (&fcinfo);
980 /* Check for null result, since caller is clearly not expecting one */
982 elog(ERROR, "function %p returned NULL", (void *) func);
988 DirectFunctionCall2(PGFunction func, Datum arg1, Datum arg2)
990 FunctionCallInfoData fcinfo;
993 InitFunctionCallInfoData(fcinfo, NULL, 2, NULL, NULL);
995 fcinfo.arg[0] = arg1;
996 fcinfo.arg[1] = arg2;
997 fcinfo.argnull[0] = false;
998 fcinfo.argnull[1] = false;
1000 result = (*func) (&fcinfo);
1002 /* Check for null result, since caller is clearly not expecting one */
1004 elog(ERROR, "function %p returned NULL", (void *) func);
1010 DirectFunctionCall3(PGFunction func, Datum arg1, Datum arg2,
1013 FunctionCallInfoData fcinfo;
1016 InitFunctionCallInfoData(fcinfo, NULL, 3, NULL, NULL);
1018 fcinfo.arg[0] = arg1;
1019 fcinfo.arg[1] = arg2;
1020 fcinfo.arg[2] = arg3;
1021 fcinfo.argnull[0] = false;
1022 fcinfo.argnull[1] = false;
1023 fcinfo.argnull[2] = false;
1025 result = (*func) (&fcinfo);
1027 /* Check for null result, since caller is clearly not expecting one */
1029 elog(ERROR, "function %p returned NULL", (void *) func);
1035 DirectFunctionCall4(PGFunction func, Datum arg1, Datum arg2,
1036 Datum arg3, Datum arg4)
1038 FunctionCallInfoData fcinfo;
1041 InitFunctionCallInfoData(fcinfo, NULL, 4, NULL, NULL);
1043 fcinfo.arg[0] = arg1;
1044 fcinfo.arg[1] = arg2;
1045 fcinfo.arg[2] = arg3;
1046 fcinfo.arg[3] = arg4;
1047 fcinfo.argnull[0] = false;
1048 fcinfo.argnull[1] = false;
1049 fcinfo.argnull[2] = false;
1050 fcinfo.argnull[3] = false;
1052 result = (*func) (&fcinfo);
1054 /* Check for null result, since caller is clearly not expecting one */
1056 elog(ERROR, "function %p returned NULL", (void *) func);
1062 DirectFunctionCall5(PGFunction func, Datum arg1, Datum arg2,
1063 Datum arg3, Datum arg4, Datum arg5)
1065 FunctionCallInfoData fcinfo;
1068 InitFunctionCallInfoData(fcinfo, NULL, 5, NULL, NULL);
1070 fcinfo.arg[0] = arg1;
1071 fcinfo.arg[1] = arg2;
1072 fcinfo.arg[2] = arg3;
1073 fcinfo.arg[3] = arg4;
1074 fcinfo.arg[4] = arg5;
1075 fcinfo.argnull[0] = false;
1076 fcinfo.argnull[1] = false;
1077 fcinfo.argnull[2] = false;
1078 fcinfo.argnull[3] = false;
1079 fcinfo.argnull[4] = false;
1081 result = (*func) (&fcinfo);
1083 /* Check for null result, since caller is clearly not expecting one */
1085 elog(ERROR, "function %p returned NULL", (void *) func);
1091 DirectFunctionCall6(PGFunction func, Datum arg1, Datum arg2,
1092 Datum arg3, Datum arg4, Datum arg5,
1095 FunctionCallInfoData fcinfo;
1098 InitFunctionCallInfoData(fcinfo, NULL, 6, NULL, NULL);
1100 fcinfo.arg[0] = arg1;
1101 fcinfo.arg[1] = arg2;
1102 fcinfo.arg[2] = arg3;
1103 fcinfo.arg[3] = arg4;
1104 fcinfo.arg[4] = arg5;
1105 fcinfo.arg[5] = arg6;
1106 fcinfo.argnull[0] = false;
1107 fcinfo.argnull[1] = false;
1108 fcinfo.argnull[2] = false;
1109 fcinfo.argnull[3] = false;
1110 fcinfo.argnull[4] = false;
1111 fcinfo.argnull[5] = false;
1113 result = (*func) (&fcinfo);
1115 /* Check for null result, since caller is clearly not expecting one */
1117 elog(ERROR, "function %p returned NULL", (void *) func);
1123 DirectFunctionCall7(PGFunction func, Datum arg1, Datum arg2,
1124 Datum arg3, Datum arg4, Datum arg5,
1125 Datum arg6, Datum arg7)
1127 FunctionCallInfoData fcinfo;
1130 InitFunctionCallInfoData(fcinfo, NULL, 7, NULL, NULL);
1132 fcinfo.arg[0] = arg1;
1133 fcinfo.arg[1] = arg2;
1134 fcinfo.arg[2] = arg3;
1135 fcinfo.arg[3] = arg4;
1136 fcinfo.arg[4] = arg5;
1137 fcinfo.arg[5] = arg6;
1138 fcinfo.arg[6] = arg7;
1139 fcinfo.argnull[0] = false;
1140 fcinfo.argnull[1] = false;
1141 fcinfo.argnull[2] = false;
1142 fcinfo.argnull[3] = false;
1143 fcinfo.argnull[4] = false;
1144 fcinfo.argnull[5] = false;
1145 fcinfo.argnull[6] = false;
1147 result = (*func) (&fcinfo);
1149 /* Check for null result, since caller is clearly not expecting one */
1151 elog(ERROR, "function %p returned NULL", (void *) func);
1157 DirectFunctionCall8(PGFunction func, Datum arg1, Datum arg2,
1158 Datum arg3, Datum arg4, Datum arg5,
1159 Datum arg6, Datum arg7, Datum arg8)
1161 FunctionCallInfoData fcinfo;
1164 InitFunctionCallInfoData(fcinfo, NULL, 8, NULL, NULL);
1166 fcinfo.arg[0] = arg1;
1167 fcinfo.arg[1] = arg2;
1168 fcinfo.arg[2] = arg3;
1169 fcinfo.arg[3] = arg4;
1170 fcinfo.arg[4] = arg5;
1171 fcinfo.arg[5] = arg6;
1172 fcinfo.arg[6] = arg7;
1173 fcinfo.arg[7] = arg8;
1174 fcinfo.argnull[0] = false;
1175 fcinfo.argnull[1] = false;
1176 fcinfo.argnull[2] = false;
1177 fcinfo.argnull[3] = false;
1178 fcinfo.argnull[4] = false;
1179 fcinfo.argnull[5] = false;
1180 fcinfo.argnull[6] = false;
1181 fcinfo.argnull[7] = false;
1183 result = (*func) (&fcinfo);
1185 /* Check for null result, since caller is clearly not expecting one */
1187 elog(ERROR, "function %p returned NULL", (void *) func);
1193 DirectFunctionCall9(PGFunction func, Datum arg1, Datum arg2,
1194 Datum arg3, Datum arg4, Datum arg5,
1195 Datum arg6, Datum arg7, Datum arg8,
1198 FunctionCallInfoData fcinfo;
1201 InitFunctionCallInfoData(fcinfo, NULL, 9, NULL, NULL);
1203 fcinfo.arg[0] = arg1;
1204 fcinfo.arg[1] = arg2;
1205 fcinfo.arg[2] = arg3;
1206 fcinfo.arg[3] = arg4;
1207 fcinfo.arg[4] = arg5;
1208 fcinfo.arg[5] = arg6;
1209 fcinfo.arg[6] = arg7;
1210 fcinfo.arg[7] = arg8;
1211 fcinfo.arg[8] = arg9;
1212 fcinfo.argnull[0] = false;
1213 fcinfo.argnull[1] = false;
1214 fcinfo.argnull[2] = false;
1215 fcinfo.argnull[3] = false;
1216 fcinfo.argnull[4] = false;
1217 fcinfo.argnull[5] = false;
1218 fcinfo.argnull[6] = false;
1219 fcinfo.argnull[7] = false;
1220 fcinfo.argnull[8] = false;
1222 result = (*func) (&fcinfo);
1224 /* Check for null result, since caller is clearly not expecting one */
1226 elog(ERROR, "function %p returned NULL", (void *) func);
1233 * These are for invocation of a previously-looked-up function with a
1234 * directly-computed parameter list. Note that neither arguments nor result
1235 * are allowed to be NULL.
1238 FunctionCall1(FmgrInfo *flinfo, Datum arg1)
1240 FunctionCallInfoData fcinfo;
1243 InitFunctionCallInfoData(fcinfo, flinfo, 1, NULL, NULL);
1245 fcinfo.arg[0] = arg1;
1246 fcinfo.argnull[0] = false;
1248 result = FunctionCallInvoke(&fcinfo);
1250 /* Check for null result, since caller is clearly not expecting one */
1252 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1258 FunctionCall2(FmgrInfo *flinfo, Datum arg1, Datum arg2)
1261 * XXX if you change this routine, see also the inlined version in
1262 * utils/sort/tuplesort.c!
1264 FunctionCallInfoData fcinfo;
1267 InitFunctionCallInfoData(fcinfo, flinfo, 2, NULL, NULL);
1269 fcinfo.arg[0] = arg1;
1270 fcinfo.arg[1] = arg2;
1271 fcinfo.argnull[0] = false;
1272 fcinfo.argnull[1] = false;
1274 result = FunctionCallInvoke(&fcinfo);
1276 /* Check for null result, since caller is clearly not expecting one */
1278 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1284 FunctionCall3(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1287 FunctionCallInfoData fcinfo;
1290 InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);
1292 fcinfo.arg[0] = arg1;
1293 fcinfo.arg[1] = arg2;
1294 fcinfo.arg[2] = arg3;
1295 fcinfo.argnull[0] = false;
1296 fcinfo.argnull[1] = false;
1297 fcinfo.argnull[2] = false;
1299 result = FunctionCallInvoke(&fcinfo);
1301 /* Check for null result, since caller is clearly not expecting one */
1303 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1309 FunctionCall4(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1310 Datum arg3, Datum arg4)
1312 FunctionCallInfoData fcinfo;
1315 InitFunctionCallInfoData(fcinfo, flinfo, 4, NULL, NULL);
1317 fcinfo.arg[0] = arg1;
1318 fcinfo.arg[1] = arg2;
1319 fcinfo.arg[2] = arg3;
1320 fcinfo.arg[3] = arg4;
1321 fcinfo.argnull[0] = false;
1322 fcinfo.argnull[1] = false;
1323 fcinfo.argnull[2] = false;
1324 fcinfo.argnull[3] = false;
1326 result = FunctionCallInvoke(&fcinfo);
1328 /* Check for null result, since caller is clearly not expecting one */
1330 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1336 FunctionCall5(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1337 Datum arg3, Datum arg4, Datum arg5)
1339 FunctionCallInfoData fcinfo;
1342 InitFunctionCallInfoData(fcinfo, flinfo, 5, NULL, NULL);
1344 fcinfo.arg[0] = arg1;
1345 fcinfo.arg[1] = arg2;
1346 fcinfo.arg[2] = arg3;
1347 fcinfo.arg[3] = arg4;
1348 fcinfo.arg[4] = arg5;
1349 fcinfo.argnull[0] = false;
1350 fcinfo.argnull[1] = false;
1351 fcinfo.argnull[2] = false;
1352 fcinfo.argnull[3] = false;
1353 fcinfo.argnull[4] = false;
1355 result = FunctionCallInvoke(&fcinfo);
1357 /* Check for null result, since caller is clearly not expecting one */
1359 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1365 FunctionCall6(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1366 Datum arg3, Datum arg4, Datum arg5,
1369 FunctionCallInfoData fcinfo;
1372 InitFunctionCallInfoData(fcinfo, flinfo, 6, NULL, NULL);
1374 fcinfo.arg[0] = arg1;
1375 fcinfo.arg[1] = arg2;
1376 fcinfo.arg[2] = arg3;
1377 fcinfo.arg[3] = arg4;
1378 fcinfo.arg[4] = arg5;
1379 fcinfo.arg[5] = arg6;
1380 fcinfo.argnull[0] = false;
1381 fcinfo.argnull[1] = false;
1382 fcinfo.argnull[2] = false;
1383 fcinfo.argnull[3] = false;
1384 fcinfo.argnull[4] = false;
1385 fcinfo.argnull[5] = false;
1387 result = FunctionCallInvoke(&fcinfo);
1389 /* Check for null result, since caller is clearly not expecting one */
1391 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1397 FunctionCall7(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1398 Datum arg3, Datum arg4, Datum arg5,
1399 Datum arg6, Datum arg7)
1401 FunctionCallInfoData fcinfo;
1404 InitFunctionCallInfoData(fcinfo, flinfo, 7, NULL, NULL);
1406 fcinfo.arg[0] = arg1;
1407 fcinfo.arg[1] = arg2;
1408 fcinfo.arg[2] = arg3;
1409 fcinfo.arg[3] = arg4;
1410 fcinfo.arg[4] = arg5;
1411 fcinfo.arg[5] = arg6;
1412 fcinfo.arg[6] = arg7;
1413 fcinfo.argnull[0] = false;
1414 fcinfo.argnull[1] = false;
1415 fcinfo.argnull[2] = false;
1416 fcinfo.argnull[3] = false;
1417 fcinfo.argnull[4] = false;
1418 fcinfo.argnull[5] = false;
1419 fcinfo.argnull[6] = false;
1421 result = FunctionCallInvoke(&fcinfo);
1423 /* Check for null result, since caller is clearly not expecting one */
1425 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1431 FunctionCall8(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1432 Datum arg3, Datum arg4, Datum arg5,
1433 Datum arg6, Datum arg7, Datum arg8)
1435 FunctionCallInfoData fcinfo;
1438 InitFunctionCallInfoData(fcinfo, flinfo, 8, NULL, NULL);
1440 fcinfo.arg[0] = arg1;
1441 fcinfo.arg[1] = arg2;
1442 fcinfo.arg[2] = arg3;
1443 fcinfo.arg[3] = arg4;
1444 fcinfo.arg[4] = arg5;
1445 fcinfo.arg[5] = arg6;
1446 fcinfo.arg[6] = arg7;
1447 fcinfo.arg[7] = arg8;
1448 fcinfo.argnull[0] = false;
1449 fcinfo.argnull[1] = false;
1450 fcinfo.argnull[2] = false;
1451 fcinfo.argnull[3] = false;
1452 fcinfo.argnull[4] = false;
1453 fcinfo.argnull[5] = false;
1454 fcinfo.argnull[6] = false;
1455 fcinfo.argnull[7] = false;
1457 result = FunctionCallInvoke(&fcinfo);
1459 /* Check for null result, since caller is clearly not expecting one */
1461 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1467 FunctionCall9(FmgrInfo *flinfo, Datum arg1, Datum arg2,
1468 Datum arg3, Datum arg4, Datum arg5,
1469 Datum arg6, Datum arg7, Datum arg8,
1472 FunctionCallInfoData fcinfo;
1475 InitFunctionCallInfoData(fcinfo, flinfo, 9, NULL, NULL);
1477 fcinfo.arg[0] = arg1;
1478 fcinfo.arg[1] = arg2;
1479 fcinfo.arg[2] = arg3;
1480 fcinfo.arg[3] = arg4;
1481 fcinfo.arg[4] = arg5;
1482 fcinfo.arg[5] = arg6;
1483 fcinfo.arg[6] = arg7;
1484 fcinfo.arg[7] = arg8;
1485 fcinfo.arg[8] = arg9;
1486 fcinfo.argnull[0] = false;
1487 fcinfo.argnull[1] = false;
1488 fcinfo.argnull[2] = false;
1489 fcinfo.argnull[3] = false;
1490 fcinfo.argnull[4] = false;
1491 fcinfo.argnull[5] = false;
1492 fcinfo.argnull[6] = false;
1493 fcinfo.argnull[7] = false;
1494 fcinfo.argnull[8] = false;
1496 result = FunctionCallInvoke(&fcinfo);
1498 /* Check for null result, since caller is clearly not expecting one */
1500 elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1507 * These are for invocation of a function identified by OID with a
1508 * directly-computed parameter list. Note that neither arguments nor result
1509 * are allowed to be NULL. These are essentially fmgr_info() followed
1510 * by FunctionCallN(). If the same function is to be invoked repeatedly,
1511 * do the fmgr_info() once and then use FunctionCallN().
1514 OidFunctionCall1(Oid functionId, Datum arg1)
1517 FunctionCallInfoData fcinfo;
1520 fmgr_info(functionId, &flinfo);
1522 InitFunctionCallInfoData(fcinfo, &flinfo, 1, NULL, NULL);
1524 fcinfo.arg[0] = arg1;
1525 fcinfo.argnull[0] = false;
1527 result = FunctionCallInvoke(&fcinfo);
1529 /* Check for null result, since caller is clearly not expecting one */
1531 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1537 OidFunctionCall2(Oid functionId, Datum arg1, Datum arg2)
1540 FunctionCallInfoData fcinfo;
1543 fmgr_info(functionId, &flinfo);
1545 InitFunctionCallInfoData(fcinfo, &flinfo, 2, NULL, NULL);
1547 fcinfo.arg[0] = arg1;
1548 fcinfo.arg[1] = arg2;
1549 fcinfo.argnull[0] = false;
1550 fcinfo.argnull[1] = false;
1552 result = FunctionCallInvoke(&fcinfo);
1554 /* Check for null result, since caller is clearly not expecting one */
1556 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1562 OidFunctionCall3(Oid functionId, Datum arg1, Datum arg2,
1566 FunctionCallInfoData fcinfo;
1569 fmgr_info(functionId, &flinfo);
1571 InitFunctionCallInfoData(fcinfo, &flinfo, 3, NULL, NULL);
1573 fcinfo.arg[0] = arg1;
1574 fcinfo.arg[1] = arg2;
1575 fcinfo.arg[2] = arg3;
1576 fcinfo.argnull[0] = false;
1577 fcinfo.argnull[1] = false;
1578 fcinfo.argnull[2] = false;
1580 result = FunctionCallInvoke(&fcinfo);
1582 /* Check for null result, since caller is clearly not expecting one */
1584 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1590 OidFunctionCall4(Oid functionId, Datum arg1, Datum arg2,
1591 Datum arg3, Datum arg4)
1594 FunctionCallInfoData fcinfo;
1597 fmgr_info(functionId, &flinfo);
1599 InitFunctionCallInfoData(fcinfo, &flinfo, 4, NULL, NULL);
1601 fcinfo.arg[0] = arg1;
1602 fcinfo.arg[1] = arg2;
1603 fcinfo.arg[2] = arg3;
1604 fcinfo.arg[3] = arg4;
1605 fcinfo.argnull[0] = false;
1606 fcinfo.argnull[1] = false;
1607 fcinfo.argnull[2] = false;
1608 fcinfo.argnull[3] = false;
1610 result = FunctionCallInvoke(&fcinfo);
1612 /* Check for null result, since caller is clearly not expecting one */
1614 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1620 OidFunctionCall5(Oid functionId, Datum arg1, Datum arg2,
1621 Datum arg3, Datum arg4, Datum arg5)
1624 FunctionCallInfoData fcinfo;
1627 fmgr_info(functionId, &flinfo);
1629 InitFunctionCallInfoData(fcinfo, &flinfo, 5, NULL, NULL);
1631 fcinfo.arg[0] = arg1;
1632 fcinfo.arg[1] = arg2;
1633 fcinfo.arg[2] = arg3;
1634 fcinfo.arg[3] = arg4;
1635 fcinfo.arg[4] = arg5;
1636 fcinfo.argnull[0] = false;
1637 fcinfo.argnull[1] = false;
1638 fcinfo.argnull[2] = false;
1639 fcinfo.argnull[3] = false;
1640 fcinfo.argnull[4] = false;
1642 result = FunctionCallInvoke(&fcinfo);
1644 /* Check for null result, since caller is clearly not expecting one */
1646 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1652 OidFunctionCall6(Oid functionId, Datum arg1, Datum arg2,
1653 Datum arg3, Datum arg4, Datum arg5,
1657 FunctionCallInfoData fcinfo;
1660 fmgr_info(functionId, &flinfo);
1662 InitFunctionCallInfoData(fcinfo, &flinfo, 6, NULL, NULL);
1664 fcinfo.arg[0] = arg1;
1665 fcinfo.arg[1] = arg2;
1666 fcinfo.arg[2] = arg3;
1667 fcinfo.arg[3] = arg4;
1668 fcinfo.arg[4] = arg5;
1669 fcinfo.arg[5] = arg6;
1670 fcinfo.argnull[0] = false;
1671 fcinfo.argnull[1] = false;
1672 fcinfo.argnull[2] = false;
1673 fcinfo.argnull[3] = false;
1674 fcinfo.argnull[4] = false;
1675 fcinfo.argnull[5] = false;
1677 result = FunctionCallInvoke(&fcinfo);
1679 /* Check for null result, since caller is clearly not expecting one */
1681 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1687 OidFunctionCall7(Oid functionId, Datum arg1, Datum arg2,
1688 Datum arg3, Datum arg4, Datum arg5,
1689 Datum arg6, Datum arg7)
1692 FunctionCallInfoData fcinfo;
1695 fmgr_info(functionId, &flinfo);
1697 InitFunctionCallInfoData(fcinfo, &flinfo, 7, NULL, NULL);
1699 fcinfo.arg[0] = arg1;
1700 fcinfo.arg[1] = arg2;
1701 fcinfo.arg[2] = arg3;
1702 fcinfo.arg[3] = arg4;
1703 fcinfo.arg[4] = arg5;
1704 fcinfo.arg[5] = arg6;
1705 fcinfo.arg[6] = arg7;
1706 fcinfo.argnull[0] = false;
1707 fcinfo.argnull[1] = false;
1708 fcinfo.argnull[2] = false;
1709 fcinfo.argnull[3] = false;
1710 fcinfo.argnull[4] = false;
1711 fcinfo.argnull[5] = false;
1712 fcinfo.argnull[6] = false;
1714 result = FunctionCallInvoke(&fcinfo);
1716 /* Check for null result, since caller is clearly not expecting one */
1718 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1724 OidFunctionCall8(Oid functionId, Datum arg1, Datum arg2,
1725 Datum arg3, Datum arg4, Datum arg5,
1726 Datum arg6, Datum arg7, Datum arg8)
1729 FunctionCallInfoData fcinfo;
1732 fmgr_info(functionId, &flinfo);
1734 InitFunctionCallInfoData(fcinfo, &flinfo, 8, NULL, NULL);
1736 fcinfo.arg[0] = arg1;
1737 fcinfo.arg[1] = arg2;
1738 fcinfo.arg[2] = arg3;
1739 fcinfo.arg[3] = arg4;
1740 fcinfo.arg[4] = arg5;
1741 fcinfo.arg[5] = arg6;
1742 fcinfo.arg[6] = arg7;
1743 fcinfo.arg[7] = arg8;
1744 fcinfo.argnull[0] = false;
1745 fcinfo.argnull[1] = false;
1746 fcinfo.argnull[2] = false;
1747 fcinfo.argnull[3] = false;
1748 fcinfo.argnull[4] = false;
1749 fcinfo.argnull[5] = false;
1750 fcinfo.argnull[6] = false;
1751 fcinfo.argnull[7] = false;
1753 result = FunctionCallInvoke(&fcinfo);
1755 /* Check for null result, since caller is clearly not expecting one */
1757 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1763 OidFunctionCall9(Oid functionId, Datum arg1, Datum arg2,
1764 Datum arg3, Datum arg4, Datum arg5,
1765 Datum arg6, Datum arg7, Datum arg8,
1769 FunctionCallInfoData fcinfo;
1772 fmgr_info(functionId, &flinfo);
1774 InitFunctionCallInfoData(fcinfo, &flinfo, 9, NULL, NULL);
1776 fcinfo.arg[0] = arg1;
1777 fcinfo.arg[1] = arg2;
1778 fcinfo.arg[2] = arg3;
1779 fcinfo.arg[3] = arg4;
1780 fcinfo.arg[4] = arg5;
1781 fcinfo.arg[5] = arg6;
1782 fcinfo.arg[6] = arg7;
1783 fcinfo.arg[7] = arg8;
1784 fcinfo.arg[8] = arg9;
1785 fcinfo.argnull[0] = false;
1786 fcinfo.argnull[1] = false;
1787 fcinfo.argnull[2] = false;
1788 fcinfo.argnull[3] = false;
1789 fcinfo.argnull[4] = false;
1790 fcinfo.argnull[5] = false;
1791 fcinfo.argnull[6] = false;
1792 fcinfo.argnull[7] = false;
1793 fcinfo.argnull[8] = false;
1795 result = FunctionCallInvoke(&fcinfo);
1797 /* Check for null result, since caller is clearly not expecting one */
1799 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1806 * Special cases for convenient invocation of datatype I/O functions.
1810 * Call a previously-looked-up datatype input function.
1812 * "str" may be NULL to indicate we are reading a NULL. In this case
1813 * the caller should assume the result is NULL, but we'll call the input
1814 * function anyway if it's not strict. So this is almost but not quite
1815 * the same as FunctionCall3.
1818 InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
1820 FunctionCallInfoData fcinfo;
1823 if (str == NULL && flinfo->fn_strict)
1824 return (Datum) 0; /* just return null result */
1826 InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);
1828 fcinfo.arg[0] = CStringGetDatum(str);
1829 fcinfo.arg[1] = ObjectIdGetDatum(typioparam);
1830 fcinfo.arg[2] = Int32GetDatum(typmod);
1831 fcinfo.argnull[0] = (str == NULL);
1832 fcinfo.argnull[1] = false;
1833 fcinfo.argnull[2] = false;
1835 result = FunctionCallInvoke(&fcinfo);
1837 /* Should get null result if and only if str is NULL */
1841 elog(ERROR, "input function %u returned non-NULL",
1842 fcinfo.flinfo->fn_oid);
1847 elog(ERROR, "input function %u returned NULL",
1848 fcinfo.flinfo->fn_oid);
1855 * Call a previously-looked-up datatype output function.
1857 * Do not call this on NULL datums.
1859 * This is mere window dressing for FunctionCall1, but its use is recommended
1860 * anyway so that code invoking output functions can be identified easily.
1863 OutputFunctionCall(FmgrInfo *flinfo, Datum val)
1865 return DatumGetCString(FunctionCall1(flinfo, val));
1869 * Call a previously-looked-up datatype binary-input function.
1871 * "buf" may be NULL to indicate we are reading a NULL. In this case
1872 * the caller should assume the result is NULL, but we'll call the receive
1873 * function anyway if it's not strict. So this is almost but not quite
1874 * the same as FunctionCall3.
1877 ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf,
1878 Oid typioparam, int32 typmod)
1880 FunctionCallInfoData fcinfo;
1883 if (buf == NULL && flinfo->fn_strict)
1884 return (Datum) 0; /* just return null result */
1886 InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);
1888 fcinfo.arg[0] = PointerGetDatum(buf);
1889 fcinfo.arg[1] = ObjectIdGetDatum(typioparam);
1890 fcinfo.arg[2] = Int32GetDatum(typmod);
1891 fcinfo.argnull[0] = (buf == NULL);
1892 fcinfo.argnull[1] = false;
1893 fcinfo.argnull[2] = false;
1895 result = FunctionCallInvoke(&fcinfo);
1897 /* Should get null result if and only if buf is NULL */
1901 elog(ERROR, "receive function %u returned non-NULL",
1902 fcinfo.flinfo->fn_oid);
1907 elog(ERROR, "receive function %u returned NULL",
1908 fcinfo.flinfo->fn_oid);
1915 * Call a previously-looked-up datatype binary-output function.
1917 * Do not call this on NULL datums.
1919 * This is little more than window dressing for FunctionCall1, but its use is
1920 * recommended anyway so that code invoking output functions can be identified
1921 * easily. Note however that it does guarantee a non-toasted result.
1924 SendFunctionCall(FmgrInfo *flinfo, Datum val)
1926 return DatumGetByteaP(FunctionCall1(flinfo, val));
1930 * As above, for I/O functions identified by OID. These are only to be used
1931 * in seldom-executed code paths. They are not only slow but leak memory.
1934 OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
1938 fmgr_info(functionId, &flinfo);
1939 return InputFunctionCall(&flinfo, str, typioparam, typmod);
1943 OidOutputFunctionCall(Oid functionId, Datum val)
1947 fmgr_info(functionId, &flinfo);
1948 return OutputFunctionCall(&flinfo, val);
1952 OidReceiveFunctionCall(Oid functionId, StringInfo buf,
1953 Oid typioparam, int32 typmod)
1957 fmgr_info(functionId, &flinfo);
1958 return ReceiveFunctionCall(&flinfo, buf, typioparam, typmod);
1962 OidSendFunctionCall(Oid functionId, Datum val)
1966 fmgr_info(functionId, &flinfo);
1967 return SendFunctionCall(&flinfo, val);
1972 * !!! OLD INTERFACE !!!
1974 * fmgr() is the only remaining vestige of the old-style caller support
1975 * functions. It's no longer used anywhere in the Postgres distribution,
1976 * but we should leave it around for a release or two to ease the transition
1977 * for user-supplied C functions. OidFunctionCallN() replaces it for new
1980 * DEPRECATED, DO NOT USE IN NEW CODE
1983 fmgr(Oid procedureId,...)
1986 FunctionCallInfoData fcinfo;
1990 fmgr_info(procedureId, &flinfo);
1992 MemSet(&fcinfo, 0, sizeof(fcinfo));
1993 fcinfo.flinfo = &flinfo;
1994 fcinfo.nargs = flinfo.fn_nargs;
1995 n_arguments = fcinfo.nargs;
1997 if (n_arguments > 0)
2002 if (n_arguments > FUNC_MAX_ARGS)
2004 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
2005 errmsg("function %u has too many arguments (%d, maximum is %d)",
2006 flinfo.fn_oid, n_arguments, FUNC_MAX_ARGS)));
2007 va_start(pvar, procedureId);
2008 for (i = 0; i < n_arguments; i++)
2009 fcinfo.arg[i] = (Datum) va_arg(pvar, char *);
2013 result = FunctionCallInvoke(&fcinfo);
2015 /* Check for null result, since caller is clearly not expecting one */
2017 elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
2019 return (char *) result;
2023 /*-------------------------------------------------------------------------
2024 * Support routines for standard pass-by-reference datatypes
2026 * Note: at some point, at least on some platforms, these might become
2027 * pass-by-value types. Obviously Datum must be >= 8 bytes to allow
2028 * int64 or float8 to be pass-by-value. I think that Float4GetDatum
2029 * and Float8GetDatum will need to be out-of-line routines anyway,
2030 * since just casting from float to Datum will not do the right thing;
2031 * some kind of trick with pointer-casting or a union will be needed.
2032 *-------------------------------------------------------------------------
2036 Int64GetDatum(int64 X)
2038 #ifndef INT64_IS_BUSTED
2039 int64 *retval = (int64 *) palloc(sizeof(int64));
2042 return PointerGetDatum(retval);
2043 #else /* INT64_IS_BUSTED */
2046 * On a machine with no 64-bit-int C datatype, sizeof(int64) will not be
2047 * 8, but we want Int64GetDatum to return an 8-byte object anyway, with
2048 * zeroes in the unused bits. This is needed so that, for example, hash
2049 * join of int8 will behave properly.
2051 int64 *retval = (int64 *) palloc0(Max(sizeof(int64), 8));
2054 return PointerGetDatum(retval);
2055 #endif /* INT64_IS_BUSTED */
2059 Float4GetDatum(float4 X)
2061 float4 *retval = (float4 *) palloc(sizeof(float4));
2064 return PointerGetDatum(retval);
2068 Float8GetDatum(float8 X)
2070 float8 *retval = (float8 *) palloc(sizeof(float8));
2073 return PointerGetDatum(retval);
2076 /*-------------------------------------------------------------------------
2077 * Support routines for toastable datatypes
2078 *-------------------------------------------------------------------------
2082 pg_detoast_datum(struct varlena * datum)
2084 if (VARATT_IS_EXTENDED(datum))
2085 return heap_tuple_untoast_attr(datum);
2091 pg_detoast_datum_copy(struct varlena * datum)
2093 if (VARATT_IS_EXTENDED(datum))
2094 return heap_tuple_untoast_attr(datum);
2097 /* Make a modifiable copy of the varlena object */
2098 Size len = VARSIZE(datum);
2099 struct varlena *result = (struct varlena *) palloc(len);
2101 memcpy(result, datum, len);
2107 pg_detoast_datum_slice(struct varlena * datum, int32 first, int32 count)
2109 /* Only get the specified portion from the toast rel */
2110 return heap_tuple_untoast_attr_slice(datum, first, count);
2114 pg_detoast_datum_packed(struct varlena * datum)
2116 if (VARATT_IS_COMPRESSED(datum) || VARATT_IS_EXTERNAL(datum))
2117 return heap_tuple_untoast_attr(datum);
2122 /*-------------------------------------------------------------------------
2123 * Support routines for extracting info from fn_expr parse tree
2125 * These are needed by polymorphic functions, which accept multiple possible
2126 * input types and need help from the parser to know what they've got.
2127 *-------------------------------------------------------------------------
2131 * Get the actual type OID of the function return type
2133 * Returns InvalidOid if information is not available
2136 get_fn_expr_rettype(FmgrInfo *flinfo)
2141 * can't return anything useful if we have no FmgrInfo or if its fn_expr
2142 * node has not been initialized
2144 if (!flinfo || !flinfo->fn_expr)
2147 expr = flinfo->fn_expr;
2149 return exprType(expr);
2153 * Get the actual type OID of a specific function argument (counting from 0)
2155 * Returns InvalidOid if information is not available
2158 get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
2161 * can't return anything useful if we have no FmgrInfo or if its fn_expr
2162 * node has not been initialized
2164 if (!flinfo || !flinfo->fn_expr)
2167 return get_call_expr_argtype(flinfo->fn_expr, argnum);
2171 * Get the actual type OID of a specific function argument (counting from 0),
2172 * but working from the calling expression tree instead of FmgrInfo
2174 * Returns InvalidOid if information is not available
2177 get_call_expr_argtype(Node *expr, int argnum)
2185 if (IsA(expr, FuncExpr))
2186 args = ((FuncExpr *) expr)->args;
2187 else if (IsA(expr, OpExpr))
2188 args = ((OpExpr *) expr)->args;
2189 else if (IsA(expr, DistinctExpr))
2190 args = ((DistinctExpr *) expr)->args;
2191 else if (IsA(expr, ScalarArrayOpExpr))
2192 args = ((ScalarArrayOpExpr *) expr)->args;
2193 else if (IsA(expr, ArrayCoerceExpr))
2194 args = list_make1(((ArrayCoerceExpr *) expr)->arg);
2195 else if (IsA(expr, NullIfExpr))
2196 args = ((NullIfExpr *) expr)->args;
2200 if (argnum < 0 || argnum >= list_length(args))
2203 argtype = exprType((Node *) list_nth(args, argnum));
2206 * special hack for ScalarArrayOpExpr and ArrayCoerceExpr: what the
2207 * underlying function will actually get passed is the element type of the
2210 if (IsA(expr, ScalarArrayOpExpr) &&
2212 argtype = get_element_type(argtype);
2213 else if (IsA(expr, ArrayCoerceExpr) &&
2215 argtype = get_element_type(argtype);