1/*-------------------------------------------------------------------------
4 * The Postgres function manager.
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/utils/fmgr/fmgr.c
13 *-------------------------------------------------------------------------
38 * Hooks for function calls
44 * Hashtable for fast lookup of external C functions
48 /* fn_oid is the hash key and so must be first! */
60 bool ignore_security);
67/* extern so it's callable via JIT */
72 * Lookup routines for builtin-function table. We can search by either Oid
73 * or name, but search by Oid is much faster.
81 /* fast lookup only possible if original oid still assigned */
86 * Lookup function data. If there's a miss in that range it's likely a
87 * nonexistent function, returning NULL here will trigger an ERROR later.
97 * Lookup a builtin by name. Note there can be more than one entry in
98 * the array with the same name, but they should all point to the same
115 * This routine fills a FmgrInfo struct, given the OID
116 * of the function to be called.
118 * The caller's CurrentMemoryContext is used as the fn_mcxt of the info
119 * struct; this means that any subsidiary data attached to the info struct
120 * (either by fmgr_info itself, or later on by a function call handler)
121 * will be allocated in that context. The caller must ensure that this
122 * context is at least as long-lived as the info struct itself. This is
123 * not a problem in typical cases where the info struct is on the stack or
124 * in freshly-palloc'd space. However, if one intends to store an info
125 * struct in a long-lived table, it's better to use fmgr_info_cxt.
134 * Fill a FmgrInfo struct, specifying a memory context in which its
135 * subsidiary data should go.
144 * This one does the actual work. ignore_security is ordinarily false
145 * but is set to true when we need to avoid recursion.
149 bool ignore_security)
158 * fn_oid *must* be filled in last. Some code assumes that if fn_oid is
159 * valid, the whole struct is valid. Some FmgrInfo struct's do survive
165 finfo->
fn_expr = NULL;
/* caller may set this later */
170 * Fast path for builtin functions: don't bother consulting pg_proc
177 finfo->
fn_oid = functionId;
181 /* Otherwise we need the pg_proc entry */
184 elog(
ERROR,
"cache lookup failed for function %u", functionId);
187 finfo->
fn_nargs = procedureStruct->pronargs;
188 finfo->
fn_strict = procedureStruct->proisstrict;
189 finfo->
fn_retset = procedureStruct->proretset;
192 * If it has prosecdef set, non-null proconfig, or if a plugin wants to
193 * hook function entry/exit, use fmgr_security_definer call handler ---
194 * unless we are being called again by fmgr_security_definer or
195 * fmgr_info_other_lang.
197 * When using fmgr_security_definer, function stats tracking is always
198 * disabled at the outer level, and instead we set the flag properly in
199 * fmgr_security_definer's private flinfo and implement the tracking
200 * inside fmgr_security_definer. This loses the ability to charge the
201 * overhead of fmgr_security_definer to the function, but gains the
202 * ability to set the track_functions GUC as a local GUC parameter of an
203 * interesting function and have the right things happen.
205 if (!ignore_security &&
206 (procedureStruct->prosecdef ||
212 finfo->
fn_oid = functionId;
217 switch (procedureStruct->prolang)
219 case INTERNALlanguageId:
222 * For an ordinary builtin function, we should never get here
223 * because the fmgr_isbuiltin() search above will have succeeded.
224 * However, if the user has done a CREATE FUNCTION to create an
225 * alias for a builtin function, we can end up here. In that case
226 * we have to look up the function by name. The name of the
227 * internal function is stored in prosrc (it doesn't have to be
228 * the same as the name of the alias!)
231 Anum_pg_proc_prosrc);
236 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
237 errmsg(
"internal function \"%s\" is not in internal lookup table",
240 /* Should we check that nargs, strict, retset match the table? */
242 /* note this policy is also assumed in fast path above */
262 finfo->
fn_oid = functionId;
267 * Return module and C function name providing implementation of functionId.
269 * If *mod == NULL and *fn == NULL, no C symbol is known to implement
272 * If *mod == NULL and *fn != NULL, the function is implemented by a symbol in
275 * If *mod != NULL and *fn != NULL the function is implemented in an extension
278 * The returned module and function names are pstrdup'ed into the current
291 elog(
ERROR,
"cache lookup failed for function %u", functionId);
294 if (procedureStruct->prosecdef ||
298 *mod = NULL;
/* core binary */
304 /* see fmgr_info_cxt_security for the individual cases */
305 switch (procedureStruct->prolang)
307 case INTERNALlanguageId:
309 Anum_pg_proc_prosrc);
311 *mod = NULL;
/* core binary */
317 Anum_pg_proc_prosrc);
320 Anum_pg_proc_probin);
323 * No need to check symbol presence / API version here, already
324 * checked in fmgr_info_cxt_security.
331 *mod = NULL;
/* core binary */
337 *
fn = NULL;
/* unknown, pass pointer */
346 * Special fmgr_info processing for C-language functions. Note that
347 * finfo->fn_oid is not valid yet.
357 * See if we have the function address cached already
374 * Get prosrc and probin strings (link symbol and library filename).
375 * While in general these columns might be null, that's not allowed
376 * for C-language functions.
379 Anum_pg_proc_prosrc);
383 Anum_pg_proc_probin);
386 /* Look up the function itself */
390 /* Get the function information record (real or default) */
393 /* Cache the addresses for later calls */
403 /* New style: call directly */
407 /* Shouldn't get here if fetch_finfo_record did its job */
408 elog(
ERROR,
"unrecognized function API version: %d",
415 * Special fmgr_info processing for other-language functions. Note
416 * that finfo->fn_oid is not valid yet.
422 Oid language = procedureStruct->prolang;
429 elog(
ERROR,
"cache lookup failed for language %u", language);
433 * Look up the language's call handler function, ignoring any attributes
434 * that would normally cause insertion of fmgr_security_definer. We need
435 * to get back a bare pointer to the actual C-language function.
445 * Fetch and validate the information record for the given external function.
446 * The function is specified by a handle for the containing library
447 * (obtained from load_external_function) as well as the function name.
449 * If no info function exists for the given name an error is raised.
451 * This function is broken out of fmgr_info_C_lang so that fmgr_c_validator
452 * can validate the information record for a function not yet entered into
464 /* Try to look up the info function */
467 if (infofunc == NULL)
470 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
471 errmsg(
"could not find function information for function \"%s\"",
473 errhint(
"SQL-callable functions need an accompanying PG_FUNCTION_INFO_V1(funcname).")));
474 return NULL;
/* silence compiler */
477 /* Found, so call it */
478 inforec = (*infofunc) ();
480 /* Validate result as best we can */
482 elog(
ERROR,
"null result from info function \"%s\"", infofuncname);
486 /* OK, no additional fields to validate */
490 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
491 errmsg(
"unrecognized API version %d reported by info function \"%s\"",
501/*-------------------------------------------------------------------------
502 * Routines for caching lookup information for external C functions.
504 * The routines in dfmgr.c are relatively slow, so we try to avoid running
505 * them more than once per external function per session. We use a hash table
506 * with the function OID as the lookup key.
507 *-------------------------------------------------------------------------
511 * lookup_C_func: try to find a C function in the hash table
513 * If an entry exists and is up to date, return it; else return NULL
522 return NULL;
/* no table yet */
529 return NULL;
/* no such entry */
532 return entry;
/* OK */
533 return NULL;
/* entry is out of date */
537 * record_C_func: enter (or update) info about a C function in the hash table
547 /* Create the hash table if it doesn't exist yet */
565 /* OID is already filled in */
574 * Copy an FmgrInfo struct
576 * This is inherently somewhat bogus since we can't reliably duplicate
577 * language-dependent subsidiary info. We cheat by zeroing fn_extra,
578 * instead, meaning that subsidiary info will have to be recomputed.
584 memcpy(dstinfo, srcinfo,
sizeof(
FmgrInfo));
591 * Specialized lookup routine for fmgr_internal_validator: given the alleged
592 * name of an internal function, return the OID of the function.
593 * If the name is not recognized, return InvalidOid.
607 * Support for security-definer and proconfig-using functions. We support
608 * both of these features using the same call handler, because they are
609 * often used together and it would be inefficient (as well as notationally
610 * messy) to have two levels of call handler involved.
619 Datum arg;
/* passthrough argument for plugin modules */
623 * Function handler for security-definer/proconfig/plugin-hooked functions.
624 * We extract the OID of the actual function and do a fmgr lookup again.
625 * Then we fetch the pg_proc row and copy the owner ID and proconfig fields.
626 * (All this info is cached for the duration of the current query.)
627 * To execute a call, we temporarily replace the flinfo with the cached
628 * and looked-up one, while keeping the outer fcinfo (which contains all
629 * the actual arguments, etc.) intact. This is not re-entrant, but then
630 * the fcinfo itself can't be used reentrantly anyway.
639 int save_sec_context;
646 if (!fcinfo->flinfo->fn_extra)
658 fcinfo->flinfo->
fn_mcxt,
true);
664 elog(
ERROR,
"cache lookup failed for function %u",
665 fcinfo->flinfo->fn_oid);
668 if (procedureStruct->prosecdef)
669 fcache->
userid = procedureStruct->proowner;
683 /* transform names to config handles to avoid lookup cost */
698 fcinfo->flinfo->fn_extra = fcache;
703 /* GetUserIdAndSecContext is cheap enough that no harm in a wasted call */
705 if (fcache->
configNames !=
NIL)
/* Need a new GUC nesting level */
708 save_nestlevel = 0;
/* keep compiler quiet */
730 /* function manager hook */
735 * We don't need to restore GUC or userid settings on error, because the
736 * ensuing xact or subxact abort will do that. The PG_TRY block is only
737 * needed to clean up the flinfo link.
739 save_flinfo = fcinfo->flinfo;
743 fcinfo->flinfo = &fcache->
flinfo;
745 /* See notes in fmgr_info_cxt_security */
751 * We could be calling either a regular or a set-returning function,
752 * so we have to test to see what finalize flag to use.
755 (fcinfo->resultinfo == NULL ||
761 fcinfo->flinfo = save_flinfo;
768 fcinfo->flinfo = save_flinfo;
781/*-------------------------------------------------------------------------
782 * Support routines for callers of fmgr-compatible functions
783 *-------------------------------------------------------------------------
787 * These are for invocation of a specifically named function with a
788 * directly-computed parameter list. Note that neither arguments nor result
789 * are allowed to be NULL. Also, the function cannot be one that needs to
790 * look at FmgrInfo, since there won't be any.
800 fcinfo->args[0].value = arg1;
801 fcinfo->args[0].isnull =
false;
803 result = (*func) (fcinfo);
805 /* Check for null result, since caller is clearly not expecting one */
807 elog(
ERROR,
"function %p returned NULL", (
void *) func);
820 fcinfo->args[0].value = arg1;
821 fcinfo->args[0].isnull =
false;
822 fcinfo->args[1].value = arg2;
823 fcinfo->args[1].isnull =
false;
825 result = (*func) (fcinfo);
827 /* Check for null result, since caller is clearly not expecting one */
829 elog(
ERROR,
"function %p returned NULL", (
void *) func);
843 fcinfo->args[0].value = arg1;
844 fcinfo->args[0].isnull =
false;
845 fcinfo->args[1].value = arg2;
846 fcinfo->args[1].isnull =
false;
847 fcinfo->args[2].value = arg3;
848 fcinfo->args[2].isnull =
false;
850 result = (*func) (fcinfo);
852 /* Check for null result, since caller is clearly not expecting one */
854 elog(
ERROR,
"function %p returned NULL", (
void *) func);
868 fcinfo->args[0].value = arg1;
869 fcinfo->args[0].isnull =
false;
870 fcinfo->args[1].value = arg2;
871 fcinfo->args[1].isnull =
false;
872 fcinfo->args[2].value = arg3;
873 fcinfo->args[2].isnull =
false;
874 fcinfo->args[3].value = arg4;
875 fcinfo->args[3].isnull =
false;
877 result = (*func) (fcinfo);
879 /* Check for null result, since caller is clearly not expecting one */
881 elog(
ERROR,
"function %p returned NULL", (
void *) func);
895 fcinfo->args[0].value = arg1;
896 fcinfo->args[0].isnull =
false;
897 fcinfo->args[1].value = arg2;
898 fcinfo->args[1].isnull =
false;
899 fcinfo->args[2].value = arg3;
900 fcinfo->args[2].isnull =
false;
901 fcinfo->args[3].value = arg4;
902 fcinfo->args[3].isnull =
false;
903 fcinfo->args[4].value = arg5;
904 fcinfo->args[4].isnull =
false;
906 result = (*func) (fcinfo);
908 /* Check for null result, since caller is clearly not expecting one */
910 elog(
ERROR,
"function %p returned NULL", (
void *) func);
925 fcinfo->args[0].value = arg1;
926 fcinfo->args[0].isnull =
false;
927 fcinfo->args[1].value = arg2;
928 fcinfo->args[1].isnull =
false;
929 fcinfo->args[2].value = arg3;
930 fcinfo->args[2].isnull =
false;
931 fcinfo->args[3].value = arg4;
932 fcinfo->args[3].isnull =
false;
933 fcinfo->args[4].value = arg5;
934 fcinfo->args[4].isnull =
false;
935 fcinfo->args[5].value = arg6;
936 fcinfo->args[5].isnull =
false;
938 result = (*func) (fcinfo);
940 /* Check for null result, since caller is clearly not expecting one */
942 elog(
ERROR,
"function %p returned NULL", (
void *) func);
957 fcinfo->args[0].value = arg1;
958 fcinfo->args[0].isnull =
false;
959 fcinfo->args[1].value = arg2;
960 fcinfo->args[1].isnull =
false;
961 fcinfo->args[2].value = arg3;
962 fcinfo->args[2].isnull =
false;
963 fcinfo->args[3].value = arg4;
964 fcinfo->args[3].isnull =
false;
965 fcinfo->args[4].value = arg5;
966 fcinfo->args[4].isnull =
false;
967 fcinfo->args[5].value = arg6;
968 fcinfo->args[5].isnull =
false;
969 fcinfo->args[6].value = arg7;
970 fcinfo->args[6].isnull =
false;
972 result = (*func) (fcinfo);
974 /* Check for null result, since caller is clearly not expecting one */
976 elog(
ERROR,
"function %p returned NULL", (
void *) func);
991 fcinfo->args[0].value = arg1;
992 fcinfo->args[0].isnull =
false;
993 fcinfo->args[1].value = arg2;
994 fcinfo->args[1].isnull =
false;
995 fcinfo->args[2].value = arg3;
996 fcinfo->args[2].isnull =
false;
997 fcinfo->args[3].value = arg4;
998 fcinfo->args[3].isnull =
false;
999 fcinfo->args[4].value = arg5;
1000 fcinfo->args[4].isnull =
false;
1001 fcinfo->args[5].value = arg6;
1002 fcinfo->args[5].isnull =
false;
1003 fcinfo->args[6].value = arg7;
1004 fcinfo->args[6].isnull =
false;
1005 fcinfo->args[7].value = arg8;
1006 fcinfo->args[7].isnull =
false;
1008 result = (*func) (fcinfo);
1010 /* Check for null result, since caller is clearly not expecting one */
1012 elog(
ERROR,
"function %p returned NULL", (
void *) func);
1028 fcinfo->args[0].value = arg1;
1029 fcinfo->args[0].isnull =
false;
1030 fcinfo->args[1].value = arg2;
1031 fcinfo->args[1].isnull =
false;
1032 fcinfo->args[2].value = arg3;
1033 fcinfo->args[2].isnull =
false;
1034 fcinfo->args[3].value = arg4;
1035 fcinfo->args[3].isnull =
false;
1036 fcinfo->args[4].value = arg5;
1037 fcinfo->args[4].isnull =
false;
1038 fcinfo->args[5].value = arg6;
1039 fcinfo->args[5].isnull =
false;
1040 fcinfo->args[6].value = arg7;
1041 fcinfo->args[6].isnull =
false;
1042 fcinfo->args[7].value = arg8;
1043 fcinfo->args[7].isnull =
false;
1044 fcinfo->args[8].value = arg9;
1045 fcinfo->args[8].isnull =
false;
1047 result = (*func) (fcinfo);
1049 /* Check for null result, since caller is clearly not expecting one */
1051 elog(
ERROR,
"function %p returned NULL", (
void *) func);
1057 * These functions work like the DirectFunctionCall functions except that
1058 * they use the flinfo parameter to initialise the fcinfo for the call.
1059 * It's recommended that the callee only use the fn_extra and fn_mcxt
1060 * fields, as other fields will typically describe the calling function
1061 * not the callee. Conversely, the calling function should not have
1062 * used fn_extra, unless its use is known to be compatible with the callee's.
1073 fcinfo->args[0].value = arg1;
1074 fcinfo->args[0].isnull =
false;
1076 result = (*func) (fcinfo);
1078 /* Check for null result, since caller is clearly not expecting one */
1080 elog(
ERROR,
"function %p returned NULL", (
void *) func);
1093 fcinfo->args[0].value = arg1;
1094 fcinfo->args[0].isnull =
false;
1095 fcinfo->args[1].value = arg2;
1096 fcinfo->args[1].isnull =
false;
1098 result = (*func) (fcinfo);
1100 /* Check for null result, since caller is clearly not expecting one */
1102 elog(
ERROR,
"function %p returned NULL", (
void *) func);
1108 * These are for invocation of a previously-looked-up function with a
1109 * directly-computed parameter list. Note that neither arguments nor result
1110 * are allowed to be NULL.
1122 /* Check for null result, since caller is clearly not expecting one */
1137 fcinfo->args[0].value = arg1;
1138 fcinfo->args[0].isnull =
false;
1142 /* Check for null result, since caller is clearly not expecting one */
1157 fcinfo->args[0].value = arg1;
1158 fcinfo->args[0].isnull =
false;
1159 fcinfo->args[1].value = arg2;
1160 fcinfo->args[1].isnull =
false;
1164 /* Check for null result, since caller is clearly not expecting one */
1180 fcinfo->args[0].value = arg1;
1181 fcinfo->args[0].isnull =
false;
1182 fcinfo->args[1].value = arg2;
1183 fcinfo->args[1].isnull =
false;
1184 fcinfo->args[2].value = arg3;
1185 fcinfo->args[2].isnull =
false;
1189 /* Check for null result, since caller is clearly not expecting one */
1205 fcinfo->args[0].value = arg1;
1206 fcinfo->args[0].isnull =
false;
1207 fcinfo->args[1].value = arg2;
1208 fcinfo->args[1].isnull =
false;
1209 fcinfo->args[2].value = arg3;
1210 fcinfo->args[2].isnull =
false;
1211 fcinfo->args[3].value = arg4;
1212 fcinfo->args[3].isnull =
false;
1216 /* Check for null result, since caller is clearly not expecting one */
1232 fcinfo->args[0].value = arg1;
1233 fcinfo->args[0].isnull =
false;
1234 fcinfo->args[1].value = arg2;
1235 fcinfo->args[1].isnull =
false;
1236 fcinfo->args[2].value = arg3;
1237 fcinfo->args[2].isnull =
false;
1238 fcinfo->args[3].value = arg4;
1239 fcinfo->args[3].isnull =
false;
1240 fcinfo->args[4].value = arg5;
1241 fcinfo->args[4].isnull =
false;
1245 /* Check for null result, since caller is clearly not expecting one */
1262 fcinfo->args[0].value = arg1;
1263 fcinfo->args[0].isnull =
false;
1264 fcinfo->args[1].value = arg2;
1265 fcinfo->args[1].isnull =
false;
1266 fcinfo->args[2].value = arg3;
1267 fcinfo->args[2].isnull =
false;
1268 fcinfo->args[3].value = arg4;
1269 fcinfo->args[3].isnull =
false;
1270 fcinfo->args[4].value = arg5;
1271 fcinfo->args[4].isnull =
false;
1272 fcinfo->args[5].value = arg6;
1273 fcinfo->args[5].isnull =
false;
1277 /* Check for null result, since caller is clearly not expecting one */
1294 fcinfo->args[0].value = arg1;
1295 fcinfo->args[0].isnull =
false;
1296 fcinfo->args[1].value = arg2;
1297 fcinfo->args[1].isnull =
false;
1298 fcinfo->args[2].value = arg3;
1299 fcinfo->args[2].isnull =
false;
1300 fcinfo->args[3].value = arg4;
1301 fcinfo->args[3].isnull =
false;
1302 fcinfo->args[4].value = arg5;
1303 fcinfo->args[4].isnull =
false;
1304 fcinfo->args[5].value = arg6;
1305 fcinfo->args[5].isnull =
false;
1306 fcinfo->args[6].value = arg7;
1307 fcinfo->args[6].isnull =
false;
1311 /* Check for null result, since caller is clearly not expecting one */
1328 fcinfo->args[0].value = arg1;
1329 fcinfo->args[0].isnull =
false;
1330 fcinfo->args[1].value = arg2;
1331 fcinfo->args[1].isnull =
false;
1332 fcinfo->args[2].value = arg3;
1333 fcinfo->args[2].isnull =
false;
1334 fcinfo->args[3].value = arg4;
1335 fcinfo->args[3].isnull =
false;
1336 fcinfo->args[4].value = arg5;
1337 fcinfo->args[4].isnull =
false;
1338 fcinfo->args[5].value = arg6;
1339 fcinfo->args[5].isnull =
false;
1340 fcinfo->args[6].value = arg7;
1341 fcinfo->args[6].isnull =
false;
1342 fcinfo->args[7].value = arg8;
1343 fcinfo->args[7].isnull =
false;
1347 /* Check for null result, since caller is clearly not expecting one */
1365 fcinfo->args[0].value = arg1;
1366 fcinfo->args[0].isnull =
false;
1367 fcinfo->args[1].value = arg2;
1368 fcinfo->args[1].isnull =
false;
1369 fcinfo->args[2].value = arg3;
1370 fcinfo->args[2].isnull =
false;
1371 fcinfo->args[3].value = arg4;
1372 fcinfo->args[3].isnull =
false;
1373 fcinfo->args[4].value = arg5;
1374 fcinfo->args[4].isnull =
false;
1375 fcinfo->args[5].value = arg6;
1376 fcinfo->args[5].isnull =
false;
1377 fcinfo->args[6].value = arg7;
1378 fcinfo->args[6].isnull =
false;
1379 fcinfo->args[7].value = arg8;
1380 fcinfo->args[7].isnull =
false;
1381 fcinfo->args[8].value = arg9;
1382 fcinfo->args[8].isnull =
false;
1386 /* Check for null result, since caller is clearly not expecting one */
1395 * These are for invocation of a function identified by OID with a
1396 * directly-computed parameter list. Note that neither arguments nor result
1397 * are allowed to be NULL. These are essentially fmgr_info() followed
1398 * by FunctionCallN(). If the same function is to be invoked repeatedly,
1399 * do the fmgr_info() once and then use FunctionCallN().
1514 arg6, arg7, arg8, arg9);
1519 * Special cases for convenient invocation of datatype I/O functions.
1523 * Call a previously-looked-up datatype input function.
1525 * "str" may be NULL to indicate we are reading a NULL. In this case
1526 * the caller should assume the result is NULL, but we'll call the input
1527 * function anyway if it's not strict. So this is almost but not quite
1528 * the same as FunctionCall3.
1537 return (
Datum) 0;
/* just return null result */
1542 fcinfo->args[0].isnull =
false;
1544 fcinfo->args[1].isnull =
false;
1546 fcinfo->args[2].isnull =
false;
1550 /* Should get null result if and only if str is NULL */
1553 if (!fcinfo->isnull)
1554 elog(
ERROR,
"input function %u returned non-NULL",
1560 elog(
ERROR,
"input function %u returned NULL",
1568 * Call a previously-looked-up datatype input function, with non-exception
1569 * handling of "soft" errors.
1571 * This is basically like InputFunctionCall, but the converted Datum is
1572 * returned into *result while the function result is true for success or
1573 * false for failure. Also, the caller may pass an ErrorSaveContext node.
1575 * If escontext points to an ErrorSaveContext, any "soft" errors detected by
1576 * the input function will be reported by filling the escontext struct and
1577 * returning false. (The caller can choose to test SOFT_ERROR_OCCURRED(),
1578 * but checking the function result instead is usually cheaper.)
1580 * If escontext does not point to an ErrorSaveContext, errors are reported
1581 * via ereport(ERROR), so that there is no functional difference from
1582 * InputFunctionCall; the result will always be true if control returns.
1594 *result = (
Datum) 0;
/* just return null result */
1601 fcinfo->args[0].isnull =
false;
1603 fcinfo->args[1].isnull =
false;
1605 fcinfo->args[2].isnull =
false;
1609 /* Result value is garbage, and could be null, if an error was reported */
1613 /* Otherwise, should get null result if and only if str is NULL */
1616 if (!fcinfo->isnull)
1617 elog(
ERROR,
"input function %u returned non-NULL",
1623 elog(
ERROR,
"input function %u returned NULL",
1631 * Call a directly-named datatype input function, with non-exception
1632 * handling of "soft" errors.
1634 * This is like InputFunctionCallSafe, except that it is given a direct
1635 * pointer to the C function to call. We assume that that function is
1636 * strict. Also, the function cannot be one that needs to
1637 * look at FmgrInfo, since there won't be any.
1649 *result = (
Datum) 0;
/* just return null result */
1656 fcinfo->args[0].isnull =
false;
1658 fcinfo->args[1].isnull =
false;
1660 fcinfo->args[2].isnull =
false;
1662 *result = (*func) (fcinfo);
1664 /* Result value is garbage, and could be null, if an error was reported */
1668 /* Otherwise, shouldn't get null result */
1670 elog(
ERROR,
"input function %p returned NULL", (
void *) func);
1676 * Call a previously-looked-up datatype output function.
1678 * Do not call this on NULL datums.
1680 * This is currently little more than window dressing for FunctionCall1.
1689 * Call a previously-looked-up datatype binary-input function.
1691 * "buf" may be NULL to indicate we are reading a NULL. In this case
1692 * the caller should assume the result is NULL, but we'll call the receive
1693 * function anyway if it's not strict. So this is almost but not quite
1694 * the same as FunctionCall3.
1704 return (
Datum) 0;
/* just return null result */
1709 fcinfo->args[0].isnull =
false;
1711 fcinfo->args[1].isnull =
false;
1713 fcinfo->args[2].isnull =
false;
1717 /* Should get null result if and only if buf is NULL */
1720 if (!fcinfo->isnull)
1721 elog(
ERROR,
"receive function %u returned non-NULL",
1727 elog(
ERROR,
"receive function %u returned NULL",
1735 * Call a previously-looked-up datatype binary-output function.
1737 * Do not call this on NULL datums.
1739 * This is little more than window dressing for FunctionCall1, but it does
1740 * guarantee a non-toasted result, which strictly speaking the underlying
1750 * As above, for I/O functions identified by OID. These are only to be used
1751 * in seldom-executed code paths. They are not only slow but leak memory.
1791/*-------------------------------------------------------------------------
1792 * Support routines for toastable datatypes
1793 *-------------------------------------------------------------------------
1812 /* Make a modifiable copy of the varlena object */
1816 memcpy(result, datum,
len);
1824 /* Only get the specified portion from the toast rel */
1837/*-------------------------------------------------------------------------
1838 * Support routines for extracting info from fn_expr parse tree
1840 * These are needed by polymorphic functions, which accept multiple possible
1841 * input types and need help from the parser to know what they've got.
1842 * Also, some functions might be interested in whether a parameter is constant.
1843 * Functions taking VARIADIC ANY also need to know about the VARIADIC keyword.
1844 *-------------------------------------------------------------------------
1848 * Get the actual type OID of the function return type
1850 * Returns InvalidOid if information is not available
1858 * can't return anything useful if we have no FmgrInfo or if its fn_expr
1859 * node has not been initialized
1861 if (!flinfo || !flinfo->
fn_expr)
1870 * Get the actual type OID of a specific function argument (counting from 0)
1872 * Returns InvalidOid if information is not available
1878 * can't return anything useful if we have no FmgrInfo or if its fn_expr
1879 * node has not been initialized
1881 if (!flinfo || !flinfo->
fn_expr)
1888 * Get the actual type OID of a specific function argument (counting from 0),
1889 * but working from the calling expression tree instead of FmgrInfo
1891 * Returns InvalidOid if information is not available
1923 * special hack for ScalarArrayOpExpr: what the underlying function will
1924 * actually get passed is the element type of the array.
1934 * Find out whether a specific function argument is constant for the
1935 * duration of a query
1937 * Returns false if information is not available
1943 * can't return anything useful if we have no FmgrInfo or if its fn_expr
1944 * node has not been initialized
1946 if (!flinfo || !flinfo->
fn_expr)
1953 * Find out whether a specific function argument is constant for the
1954 * duration of a query, but working from the calling expression tree
1956 * Returns false if information is not available
1988 * Either a true Const or an external Param will have a value that doesn't
1989 * change during the execution of the query. In future we might want to
1990 * consider other cases too, e.g. now().
2002 * Get the VARIADIC flag from the function invocation
2004 * Returns false (the default assumption) if information is not available
2006 * Note this is generally only of interest to VARIADIC ANY functions
2014 * can't return anything useful if we have no FmgrInfo or if its fn_expr
2015 * node has not been initialized
2017 if (!flinfo || !flinfo->
fn_expr)
2023 return ((
FuncExpr *) expr)->funcvariadic;
2029 * Set options to FmgrInfo of opclass support function.
2031 * Opclass support functions are called outside of expressions. Thanks to that
2032 * we can use fn_expr to store opclass options as bytea constant.
2043 * Check if options are defined for opclass support function.
2053 return !expr->constisnull;
2059 * Get options for opclass support function.
2069 return expr->constisnull ? NULL :
DatumGetByteaP(expr->constvalue);
2073 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2074 errmsg(
"operator class options info is absent in function call context")));
2079/*-------------------------------------------------------------------------
2080 * Support routines for procedural language implementations
2081 *-------------------------------------------------------------------------
2085 * Verify that a validator is actually associated with the language of a
2086 * particular function and that the user has access to both the language and
2087 * the function. All validators should call this before doing anything
2088 * substantial. Doing so ensures a user cannot achieve anything with explicit
2089 * calls to validators that he could not achieve with CREATE FUNCTION or by
2090 * simply calling an existing function.
2092 * When this function returns false, callers should skip all validation work
2093 * and call PG_RETURN_VOID(). This never happens at present; it is reserved
2094 * for future expansion.
2096 * In particular, checking that the validator corresponds to the function's
2097 * language allows untrusted language validators to assume they process only
2098 * superuser-chosen source code. (Untrusted language call handlers, by
2099 * definition, do assume that.) A user lacking the USAGE language privilege
2100 * would be unable to reach the validator through CREATE FUNCTION, so we check
2101 * that to block explicit calls as well. Checking the EXECUTE privilege on
2102 * the function is often superfluous, because most users can clone the
2103 * function to get an executable copy. It is meaningful against users with no
2104 * database TEMP right and no permanent schema CREATE right, thereby unable to
2105 * create any function. Also, if the function tracks persistent state by
2106 * function OID or name, validating the original function might permit more
2107 * mischief than creating and validating a clone thereof.
2119 * Get the function's pg_proc entry. Throw a user-facing error for bad
2120 * OID, because validators can be called with user-specified OIDs.
2125 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
2126 errmsg(
"function with OID %u does not exist", functionOid)));
2130 * Fetch pg_language entry to know if this is the correct validation
2131 * function for that pg_proc entry.
2135 elog(
ERROR,
"cache lookup failed for language %u", procStruct->prolang);
2138 if (langStruct->lanvalidator != validatorOid)
2140 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2141 errmsg(
"language validation function %u called for language %u instead of %u",
2142 validatorOid, procStruct->prolang,
2143 langStruct->lanvalidator)));
2145 /* first validate that we have permissions to use the language */
2150 NameStr(langStruct->lanname));
2153 * Check whether we are allowed to execute the function itself. If we can
2154 * execute it, there should be no possible side-effect of
2155 * compiling/validation that execution can't have.
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
#define DatumGetArrayTypeP(X)
#define TextDatumGetCString(d)
#define OidIsValid(objectId)
struct varlena * detoast_attr(struct varlena *attr)
struct varlena * detoast_attr_slice(struct varlena *attr, int32 sliceoffset, int32 slicelength)
void * lookup_external_function(void *filehandle, const char *funcname)
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
Datum FunctionCall4Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4)
Datum OidFunctionCall2Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2)
void set_fn_opclass_options(FmgrInfo *flinfo, bytea *options)
Oid fmgr_internal_function(const char *proname)
Datum OidFunctionCall9Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6, Datum arg7, Datum arg8, Datum arg9)
bool get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum)
struct varlena * pg_detoast_datum_copy(struct varlena *datum)
struct varlena * pg_detoast_datum_slice(struct varlena *datum, int32 first, int32 count)
Datum OidFunctionCall6Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6)
Datum FunctionCall6Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6)
Datum OidFunctionCall5Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5)
Datum FunctionCall8Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6, Datum arg7, Datum arg8)
bool CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid)
Datum OidReceiveFunctionCall(Oid functionId, StringInfo buf, Oid typioparam, int32 typmod)
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
static const FmgrBuiltin * fmgr_lookupByName(const char *name)
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Datum OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
PGDLLIMPORT needs_fmgr_hook_type needs_fmgr_hook
Datum DirectFunctionCall2Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2)
static void fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
bool DirectInputFunctionCallSafe(PGFunction func, char *str, Oid typioparam, int32 typmod, Node *escontext, Datum *result)
struct varlena * pg_detoast_datum_packed(struct varlena *datum)
char * OidOutputFunctionCall(Oid functionId, Datum val)
Datum FunctionCall5Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5)
static void fmgr_info_other_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
const Pg_finfo_record * fetch_finfo_record(void *filehandle, const char *funcname)
struct varlena * pg_detoast_datum(struct varlena *datum)
Datum OidFunctionCall3Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2, Datum arg3)
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Datum OidFunctionCall8Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6, Datum arg7, Datum arg8)
bytea * SendFunctionCall(FmgrInfo *flinfo, Datum val)
Datum OidFunctionCall4Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4)
Datum DirectFunctionCall4Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4)
Datum OidFunctionCall7Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6, Datum arg7)
PGDLLIMPORT fmgr_hook_type fmgr_hook
bool has_fn_opclass_options(FmgrInfo *flinfo)
bool InputFunctionCallSafe(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod, Node *escontext, Datum *result)
Datum CallerFInfoFunctionCall2(PGFunction func, FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Datum CallerFInfoFunctionCall1(PGFunction func, FmgrInfo *flinfo, Oid collation, Datum arg1)
Datum DirectFunctionCall6Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6)
Datum DirectFunctionCall5Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5)
Datum OidFunctionCall1Coll(Oid functionId, Oid collation, Datum arg1)
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Datum DirectFunctionCall1Coll(PGFunction func, Oid collation, Datum arg1)
bytea * get_fn_opclass_options(FmgrInfo *flinfo)
Datum FunctionCall3Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3)
Datum DirectFunctionCall3Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2, Datum arg3)
Oid get_call_expr_argtype(Node *expr, int argnum)
Datum FunctionCall7Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6, Datum arg7)
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Datum OidFunctionCall0Coll(Oid functionId, Oid collation)
void fmgr_symbol(Oid functionId, char **mod, char **fn)
Datum DirectFunctionCall9Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6, Datum arg7, Datum arg8, Datum arg9)
bytea * OidSendFunctionCall(Oid functionId, Datum val)
Datum FunctionCall1Coll(FmgrInfo *flinfo, Oid collation, Datum arg1)
Datum fmgr_security_definer(PG_FUNCTION_ARGS)
Oid get_fn_expr_rettype(FmgrInfo *flinfo)
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
static void record_C_func(HeapTuple procedureTuple, PGFunction user_fn, const Pg_finfo_record *inforec)
static void fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt, bool ignore_security)
Datum FunctionCall0Coll(FmgrInfo *flinfo, Oid collation)
Datum FunctionCall9Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6, Datum arg7, Datum arg8, Datum arg9)
Datum DirectFunctionCall7Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6, Datum arg7)
static CFuncHashTabEntry * lookup_C_func(HeapTuple procedureTuple)
static const FmgrBuiltin * fmgr_isbuiltin(Oid id)
bool get_call_expr_arg_stable(Node *expr, int argnum)
Datum ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf, Oid typioparam, int32 typmod)
Datum DirectFunctionCall8Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6, Datum arg7, Datum arg8)
const Pg_finfo_record *(* PGFInfoFunction)(void)
bool(* needs_fmgr_hook_type)(Oid fn_oid)
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
#define LOCAL_FCINFO(name, nargs)
#define FunctionCall1(flinfo, arg1)
#define FunctionCallInvoke(fcinfo)
void(* fmgr_hook_type)(FmgrHookEventType event, FmgrInfo *flinfo, Datum *arg)
Datum(* PGFunction)(FunctionCallInfo fcinfo)
#define FmgrHookIsNeeded(fn_oid)
#define DatumGetByteaP(X)
PGDLLIMPORT const Oid fmgr_last_builtin_oid
PGDLLIMPORT const int fmgr_nbuiltins
PGDLLIMPORT const FmgrBuiltin fmgr_builtins[]
#define InvalidOidBuiltinMapping
PGDLLIMPORT const uint16 fmgr_builtin_oid_index[]
Datum fmgr_sql(PG_FUNCTION_ARGS)
int NewGUCNestLevel(void)
config_handle * get_config_handle(const char *name)
int set_config_with_handle(const char *name, config_handle *handle, const char *value, GucContext context, GucSource source, Oid srole, GucAction action, bool changeVal, int elevel, bool is_reload)
void TransformGUCArray(ArrayType *array, List **names, List **values)
void AtEOXact_GUC(bool isCommit, int nestLevel)
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
#define HeapTupleIsValid(tuple)
static TransactionId HeapTupleHeaderGetRawXmin(const HeapTupleHeaderData *tup)
static void * GETSTRUCT(const HeapTupleData *tuple)
if(TABLE==NULL||TABLE_index==NULL)
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
List * lappend(List *list, void *datum)
Oid get_base_element_type(Oid typid)
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
void * MemoryContextAllocZero(MemoryContext context, Size size)
char * pstrdup(const char *in)
void pfree(void *pointer)
MemoryContext CurrentMemoryContext
#define SECURITY_LOCAL_USERID_CHANGE
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
void SetUserIdAndSecContext(Oid userid, int sec_context)
#define SOFT_ERROR_OCCURRED(escontext)
Oid exprType(const Node *expr)
#define IsA(nodeptr, _type_)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
FormData_pg_language * Form_pg_language
static int list_length(const List *l)
#define forthree(cell1, list1, cell2, list2, cell3, list3)
static void * list_nth(const List *list, int n)
FormData_pg_proc * Form_pg_proc
static rewind_source * source
void pgstat_init_function_usage(FunctionCallInfo fcinfo, PgStat_FunctionCallUsage *fcu)
void pgstat_end_function_usage(PgStat_FunctionCallUsage *fcu, bool finalize)
static Datum PointerGetDatum(const void *X)
static Datum ObjectIdGetDatum(Oid X)
static char * DatumGetCString(Datum X)
static Datum CStringGetDatum(const char *X)
static Datum Int32GetDatum(int32 X)
char * psprintf(const char *fmt,...)
const Pg_finfo_record * inforec
void ReleaseSysCache(HeapTuple tuple)
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
static void * fn(void *arg)
static bool VARATT_IS_EXTENDED(const void *PTR)
static bool VARATT_IS_EXTERNAL(const void *PTR)
static Size VARSIZE(const void *PTR)
static bool VARATT_IS_COMPRESSED(const void *PTR)