PostgreSQL Source Code  git master
fmgr.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * fmgr.c
4  * The Postgres function manager.
5  *
6  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/utils/fmgr/fmgr.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 
16 #include "postgres.h"
17 
18 #include "access/detoast.h"
19 #include "catalog/pg_language.h"
20 #include "catalog/pg_proc.h"
21 #include "catalog/pg_type.h"
22 #include "executor/functions.h"
23 #include "lib/stringinfo.h"
24 #include "miscadmin.h"
25 #include "nodes/makefuncs.h"
26 #include "nodes/nodeFuncs.h"
27 #include "pgstat.h"
28 #include "utils/acl.h"
29 #include "utils/builtins.h"
30 #include "utils/fmgrtab.h"
31 #include "utils/guc.h"
32 #include "utils/lsyscache.h"
33 #include "utils/syscache.h"
34 
35 /*
36  * Hooks for function calls
37  */
40 
41 /*
42  * Hashtable for fast lookup of external C functions
43  */
44 typedef struct
45 {
46  /* fn_oid is the hash key and so must be first! */
47  Oid fn_oid; /* OID of an external C function */
48  TransactionId fn_xmin; /* for checking up-to-dateness */
50  PGFunction user_fn; /* the function's address */
51  const Pg_finfo_record *inforec; /* address of its info record */
53 
54 static HTAB *CFuncHash = NULL;
55 
56 
57 static void fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt,
58  bool ignore_security);
59 static void fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple);
60 static void fmgr_info_other_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple);
61 static CFuncHashTabEntry *lookup_C_func(HeapTuple procedureTuple);
62 static void record_C_func(HeapTuple procedureTuple,
63  PGFunction user_fn, const Pg_finfo_record *inforec);
64 
65 /* extern so it's callable via JIT */
67 
68 
69 /*
70  * Lookup routines for builtin-function table. We can search by either Oid
71  * or name, but search by Oid is much faster.
72  */
73 
74 static const FmgrBuiltin *
76 {
77  uint16 index;
78 
79  /* fast lookup only possible if original oid still assigned */
80  if (id > fmgr_last_builtin_oid)
81  return NULL;
82 
83  /*
84  * Lookup function data. If there's a miss in that range it's likely a
85  * nonexistent function, returning NULL here will trigger an ERROR later.
86  */
89  return NULL;
90 
91  return &fmgr_builtins[index];
92 }
93 
94 /*
95  * Lookup a builtin by name. Note there can be more than one entry in
96  * the array with the same name, but they should all point to the same
97  * routine.
98  */
99 static const FmgrBuiltin *
101 {
102  int i;
103 
104  for (i = 0; i < fmgr_nbuiltins; i++)
105  {
106  if (strcmp(name, fmgr_builtins[i].funcName) == 0)
107  return fmgr_builtins + i;
108  }
109  return NULL;
110 }
111 
112 /*
113  * This routine fills a FmgrInfo struct, given the OID
114  * of the function to be called.
115  *
116  * The caller's CurrentMemoryContext is used as the fn_mcxt of the info
117  * struct; this means that any subsidiary data attached to the info struct
118  * (either by fmgr_info itself, or later on by a function call handler)
119  * will be allocated in that context. The caller must ensure that this
120  * context is at least as long-lived as the info struct itself. This is
121  * not a problem in typical cases where the info struct is on the stack or
122  * in freshly-palloc'd space. However, if one intends to store an info
123  * struct in a long-lived table, it's better to use fmgr_info_cxt.
124  */
125 void
126 fmgr_info(Oid functionId, FmgrInfo *finfo)
127 {
128  fmgr_info_cxt_security(functionId, finfo, CurrentMemoryContext, false);
129 }
130 
131 /*
132  * Fill a FmgrInfo struct, specifying a memory context in which its
133  * subsidiary data should go.
134  */
135 void
136 fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
137 {
138  fmgr_info_cxt_security(functionId, finfo, mcxt, false);
139 }
140 
141 /*
142  * This one does the actual work. ignore_security is ordinarily false
143  * but is set to true when we need to avoid recursion.
144  */
145 static void
147  bool ignore_security)
148 {
149  const FmgrBuiltin *fbp;
150  HeapTuple procedureTuple;
151  Form_pg_proc procedureStruct;
152  Datum prosrcdatum;
153  bool isnull;
154  char *prosrc;
155 
156  /*
157  * fn_oid *must* be filled in last. Some code assumes that if fn_oid is
158  * valid, the whole struct is valid. Some FmgrInfo struct's do survive
159  * elogs.
160  */
161  finfo->fn_oid = InvalidOid;
162  finfo->fn_extra = NULL;
163  finfo->fn_mcxt = mcxt;
164  finfo->fn_expr = NULL; /* caller may set this later */
165 
166  if ((fbp = fmgr_isbuiltin(functionId)) != NULL)
167  {
168  /*
169  * Fast path for builtin functions: don't bother consulting pg_proc
170  */
171  finfo->fn_nargs = fbp->nargs;
172  finfo->fn_strict = fbp->strict;
173  finfo->fn_retset = fbp->retset;
174  finfo->fn_stats = TRACK_FUNC_ALL; /* ie, never track */
175  finfo->fn_addr = fbp->func;
176  finfo->fn_oid = functionId;
177  return;
178  }
179 
180  /* Otherwise we need the pg_proc entry */
181  procedureTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
182  if (!HeapTupleIsValid(procedureTuple))
183  elog(ERROR, "cache lookup failed for function %u", functionId);
184  procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
185 
186  finfo->fn_nargs = procedureStruct->pronargs;
187  finfo->fn_strict = procedureStruct->proisstrict;
188  finfo->fn_retset = procedureStruct->proretset;
189 
190  /*
191  * If it has prosecdef set, non-null proconfig, or if a plugin wants to
192  * hook function entry/exit, use fmgr_security_definer call handler ---
193  * unless we are being called again by fmgr_security_definer or
194  * fmgr_info_other_lang.
195  *
196  * When using fmgr_security_definer, function stats tracking is always
197  * disabled at the outer level, and instead we set the flag properly in
198  * fmgr_security_definer's private flinfo and implement the tracking
199  * inside fmgr_security_definer. This loses the ability to charge the
200  * overhead of fmgr_security_definer to the function, but gains the
201  * ability to set the track_functions GUC as a local GUC parameter of an
202  * interesting function and have the right things happen.
203  */
204  if (!ignore_security &&
205  (procedureStruct->prosecdef ||
206  !heap_attisnull(procedureTuple, Anum_pg_proc_proconfig, NULL) ||
207  FmgrHookIsNeeded(functionId)))
208  {
210  finfo->fn_stats = TRACK_FUNC_ALL; /* ie, never track */
211  finfo->fn_oid = functionId;
212  ReleaseSysCache(procedureTuple);
213  return;
214  }
215 
216  switch (procedureStruct->prolang)
217  {
218  case INTERNALlanguageId:
219 
220  /*
221  * For an ordinary builtin function, we should never get here
222  * because the fmgr_isbuiltin() search above will have succeeded.
223  * However, if the user has done a CREATE FUNCTION to create an
224  * alias for a builtin function, we can end up here. In that case
225  * we have to look up the function by name. The name of the
226  * internal function is stored in prosrc (it doesn't have to be
227  * the same as the name of the alias!)
228  */
229  prosrcdatum = SysCacheGetAttr(PROCOID, procedureTuple,
230  Anum_pg_proc_prosrc, &isnull);
231  if (isnull)
232  elog(ERROR, "null prosrc");
233  prosrc = TextDatumGetCString(prosrcdatum);
234  fbp = fmgr_lookupByName(prosrc);
235  if (fbp == NULL)
236  ereport(ERROR,
237  (errcode(ERRCODE_UNDEFINED_FUNCTION),
238  errmsg("internal function \"%s\" is not in internal lookup table",
239  prosrc)));
240  pfree(prosrc);
241  /* Should we check that nargs, strict, retset match the table? */
242  finfo->fn_addr = fbp->func;
243  /* note this policy is also assumed in fast path above */
244  finfo->fn_stats = TRACK_FUNC_ALL; /* ie, never track */
245  break;
246 
247  case ClanguageId:
248  fmgr_info_C_lang(functionId, finfo, procedureTuple);
249  finfo->fn_stats = TRACK_FUNC_PL; /* ie, track if ALL */
250  break;
251 
252  case SQLlanguageId:
253  finfo->fn_addr = fmgr_sql;
254  finfo->fn_stats = TRACK_FUNC_PL; /* ie, track if ALL */
255  break;
256 
257  default:
258  fmgr_info_other_lang(functionId, finfo, procedureTuple);
259  finfo->fn_stats = TRACK_FUNC_OFF; /* ie, track if not OFF */
260  break;
261  }
262 
263  finfo->fn_oid = functionId;
264  ReleaseSysCache(procedureTuple);
265 }
266 
267 /*
268  * Return module and C function name providing implementation of functionId.
269  *
270  * If *mod == NULL and *fn == NULL, no C symbol is known to implement
271  * function.
272  *
273  * If *mod == NULL and *fn != NULL, the function is implemented by a symbol in
274  * the main binary.
275  *
276  * If *mod != NULL and *fn != NULL the function is implemented in an extension
277  * shared object.
278  *
279  * The returned module and function names are pstrdup'ed into the current
280  * memory context.
281  */
282 void
283 fmgr_symbol(Oid functionId, char **mod, char **fn)
284 {
285  HeapTuple procedureTuple;
286  Form_pg_proc procedureStruct;
287  bool isnull;
288  Datum prosrcattr;
289  Datum probinattr;
290 
291  procedureTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
292  if (!HeapTupleIsValid(procedureTuple))
293  elog(ERROR, "cache lookup failed for function %u", functionId);
294  procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
295 
296  if (procedureStruct->prosecdef ||
297  !heap_attisnull(procedureTuple, Anum_pg_proc_proconfig, NULL) ||
298  FmgrHookIsNeeded(functionId))
299  {
300  *mod = NULL; /* core binary */
301  *fn = pstrdup("fmgr_security_definer");
302  ReleaseSysCache(procedureTuple);
303  return;
304  }
305 
306  /* see fmgr_info_cxt_security for the individual cases */
307  switch (procedureStruct->prolang)
308  {
309  case INTERNALlanguageId:
310  prosrcattr = SysCacheGetAttr(PROCOID, procedureTuple,
311  Anum_pg_proc_prosrc, &isnull);
312  if (isnull)
313  elog(ERROR, "null prosrc");
314 
315  *mod = NULL; /* core binary */
316  *fn = TextDatumGetCString(prosrcattr);
317  break;
318 
319  case ClanguageId:
320  prosrcattr = SysCacheGetAttr(PROCOID, procedureTuple,
321  Anum_pg_proc_prosrc, &isnull);
322  if (isnull)
323  elog(ERROR, "null prosrc for C function %u", functionId);
324 
325  probinattr = SysCacheGetAttr(PROCOID, procedureTuple,
326  Anum_pg_proc_probin, &isnull);
327  if (isnull)
328  elog(ERROR, "null probin for C function %u", functionId);
329 
330  /*
331  * No need to check symbol presence / API version here, already
332  * checked in fmgr_info_cxt_security.
333  */
334  *mod = TextDatumGetCString(probinattr);
335  *fn = TextDatumGetCString(prosrcattr);
336  break;
337 
338  case SQLlanguageId:
339  *mod = NULL; /* core binary */
340  *fn = pstrdup("fmgr_sql");
341  break;
342 
343  default:
344  *mod = NULL;
345  *fn = NULL; /* unknown, pass pointer */
346  break;
347  }
348 
349  ReleaseSysCache(procedureTuple);
350 }
351 
352 
353 /*
354  * Special fmgr_info processing for C-language functions. Note that
355  * finfo->fn_oid is not valid yet.
356  */
357 static void
358 fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
359 {
360  CFuncHashTabEntry *hashentry;
361  PGFunction user_fn;
362  const Pg_finfo_record *inforec;
363  bool isnull;
364 
365  /*
366  * See if we have the function address cached already
367  */
368  hashentry = lookup_C_func(procedureTuple);
369  if (hashentry)
370  {
371  user_fn = hashentry->user_fn;
372  inforec = hashentry->inforec;
373  }
374  else
375  {
376  Datum prosrcattr,
377  probinattr;
378  char *prosrcstring,
379  *probinstring;
380  void *libraryhandle;
381 
382  /*
383  * Get prosrc and probin strings (link symbol and library filename).
384  * While in general these columns might be null, that's not allowed
385  * for C-language functions.
386  */
387  prosrcattr = SysCacheGetAttr(PROCOID, procedureTuple,
388  Anum_pg_proc_prosrc, &isnull);
389  if (isnull)
390  elog(ERROR, "null prosrc for C function %u", functionId);
391  prosrcstring = TextDatumGetCString(prosrcattr);
392 
393  probinattr = SysCacheGetAttr(PROCOID, procedureTuple,
394  Anum_pg_proc_probin, &isnull);
395  if (isnull)
396  elog(ERROR, "null probin for C function %u", functionId);
397  probinstring = TextDatumGetCString(probinattr);
398 
399  /* Look up the function itself */
400  user_fn = load_external_function(probinstring, prosrcstring, true,
401  &libraryhandle);
402 
403  /* Get the function information record (real or default) */
404  inforec = fetch_finfo_record(libraryhandle, prosrcstring);
405 
406  /* Cache the addresses for later calls */
407  record_C_func(procedureTuple, user_fn, inforec);
408 
409  pfree(prosrcstring);
410  pfree(probinstring);
411  }
412 
413  switch (inforec->api_version)
414  {
415  case 1:
416  /* New style: call directly */
417  finfo->fn_addr = user_fn;
418  break;
419  default:
420  /* Shouldn't get here if fetch_finfo_record did its job */
421  elog(ERROR, "unrecognized function API version: %d",
422  inforec->api_version);
423  break;
424  }
425 }
426 
427 /*
428  * Special fmgr_info processing for other-language functions. Note
429  * that finfo->fn_oid is not valid yet.
430  */
431 static void
432 fmgr_info_other_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
433 {
434  Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
435  Oid language = procedureStruct->prolang;
436  HeapTuple languageTuple;
437  Form_pg_language languageStruct;
438  FmgrInfo plfinfo;
439 
440  languageTuple = SearchSysCache1(LANGOID, ObjectIdGetDatum(language));
441  if (!HeapTupleIsValid(languageTuple))
442  elog(ERROR, "cache lookup failed for language %u", language);
443  languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
444 
445  /*
446  * Look up the language's call handler function, ignoring any attributes
447  * that would normally cause insertion of fmgr_security_definer. We need
448  * to get back a bare pointer to the actual C-language function.
449  */
450  fmgr_info_cxt_security(languageStruct->lanplcallfoid, &plfinfo,
451  CurrentMemoryContext, true);
452  finfo->fn_addr = plfinfo.fn_addr;
453 
454  ReleaseSysCache(languageTuple);
455 }
456 
457 /*
458  * Fetch and validate the information record for the given external function.
459  * The function is specified by a handle for the containing library
460  * (obtained from load_external_function) as well as the function name.
461  *
462  * If no info function exists for the given name an error is raised.
463  *
464  * This function is broken out of fmgr_info_C_lang so that fmgr_c_validator
465  * can validate the information record for a function not yet entered into
466  * pg_proc.
467  */
468 const Pg_finfo_record *
469 fetch_finfo_record(void *filehandle, const char *funcname)
470 {
471  char *infofuncname;
472  PGFInfoFunction infofunc;
473  const Pg_finfo_record *inforec;
474 
475  infofuncname = psprintf("pg_finfo_%s", funcname);
476 
477  /* Try to look up the info function */
478  infofunc = (PGFInfoFunction) lookup_external_function(filehandle,
479  infofuncname);
480  if (infofunc == NULL)
481  {
482  ereport(ERROR,
483  (errcode(ERRCODE_UNDEFINED_FUNCTION),
484  errmsg("could not find function information for function \"%s\"",
485  funcname),
486  errhint("SQL-callable functions need an accompanying PG_FUNCTION_INFO_V1(funcname).")));
487  return NULL; /* silence compiler */
488  }
489 
490  /* Found, so call it */
491  inforec = (*infofunc) ();
492 
493  /* Validate result as best we can */
494  if (inforec == NULL)
495  elog(ERROR, "null result from info function \"%s\"", infofuncname);
496  switch (inforec->api_version)
497  {
498  case 1:
499  /* OK, no additional fields to validate */
500  break;
501  default:
502  ereport(ERROR,
503  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
504  errmsg("unrecognized API version %d reported by info function \"%s\"",
505  inforec->api_version, infofuncname)));
506  break;
507  }
508 
509  pfree(infofuncname);
510  return inforec;
511 }
512 
513 
514 /*-------------------------------------------------------------------------
515  * Routines for caching lookup information for external C functions.
516  *
517  * The routines in dfmgr.c are relatively slow, so we try to avoid running
518  * them more than once per external function per session. We use a hash table
519  * with the function OID as the lookup key.
520  *-------------------------------------------------------------------------
521  */
522 
523 /*
524  * lookup_C_func: try to find a C function in the hash table
525  *
526  * If an entry exists and is up to date, return it; else return NULL
527  */
528 static CFuncHashTabEntry *
529 lookup_C_func(HeapTuple procedureTuple)
530 {
531  Oid fn_oid = ((Form_pg_proc) GETSTRUCT(procedureTuple))->oid;
532  CFuncHashTabEntry *entry;
533 
534  if (CFuncHash == NULL)
535  return NULL; /* no table yet */
536  entry = (CFuncHashTabEntry *)
538  &fn_oid,
539  HASH_FIND,
540  NULL);
541  if (entry == NULL)
542  return NULL; /* no such entry */
543  if (entry->fn_xmin == HeapTupleHeaderGetRawXmin(procedureTuple->t_data) &&
544  ItemPointerEquals(&entry->fn_tid, &procedureTuple->t_self))
545  return entry; /* OK */
546  return NULL; /* entry is out of date */
547 }
548 
549 /*
550  * record_C_func: enter (or update) info about a C function in the hash table
551  */
552 static void
553 record_C_func(HeapTuple procedureTuple,
554  PGFunction user_fn, const Pg_finfo_record *inforec)
555 {
556  Oid fn_oid = ((Form_pg_proc) GETSTRUCT(procedureTuple))->oid;
557  CFuncHashTabEntry *entry;
558  bool found;
559 
560  /* Create the hash table if it doesn't exist yet */
561  if (CFuncHash == NULL)
562  {
563  HASHCTL hash_ctl;
564 
565  hash_ctl.keysize = sizeof(Oid);
566  hash_ctl.entrysize = sizeof(CFuncHashTabEntry);
567  CFuncHash = hash_create("CFuncHash",
568  100,
569  &hash_ctl,
571  }
572 
573  entry = (CFuncHashTabEntry *)
575  &fn_oid,
576  HASH_ENTER,
577  &found);
578  /* OID is already filled in */
579  entry->fn_xmin = HeapTupleHeaderGetRawXmin(procedureTuple->t_data);
580  entry->fn_tid = procedureTuple->t_self;
581  entry->user_fn = user_fn;
582  entry->inforec = inforec;
583 }
584 
585 
586 /*
587  * Copy an FmgrInfo struct
588  *
589  * This is inherently somewhat bogus since we can't reliably duplicate
590  * language-dependent subsidiary info. We cheat by zeroing fn_extra,
591  * instead, meaning that subsidiary info will have to be recomputed.
592  */
593 void
594 fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo,
595  MemoryContext destcxt)
596 {
597  memcpy(dstinfo, srcinfo, sizeof(FmgrInfo));
598  dstinfo->fn_mcxt = destcxt;
599  dstinfo->fn_extra = NULL;
600 }
601 
602 
603 /*
604  * Specialized lookup routine for fmgr_internal_validator: given the alleged
605  * name of an internal function, return the OID of the function.
606  * If the name is not recognized, return InvalidOid.
607  */
608 Oid
610 {
611  const FmgrBuiltin *fbp = fmgr_lookupByName(proname);
612 
613  if (fbp == NULL)
614  return InvalidOid;
615  return fbp->foid;
616 }
617 
618 
619 /*
620  * Support for security-definer and proconfig-using functions. We support
621  * both of these features using the same call handler, because they are
622  * often used together and it would be inefficient (as well as notationally
623  * messy) to have two levels of call handler involved.
624  */
626 {
627  FmgrInfo flinfo; /* lookup info for target function */
628  Oid userid; /* userid to set, or InvalidOid */
629  ArrayType *proconfig; /* GUC values to set, or NULL */
630  Datum arg; /* passthrough argument for plugin modules */
631 };
632 
633 /*
634  * Function handler for security-definer/proconfig/plugin-hooked functions.
635  * We extract the OID of the actual function and do a fmgr lookup again.
636  * Then we fetch the pg_proc row and copy the owner ID and proconfig fields.
637  * (All this info is cached for the duration of the current query.)
638  * To execute a call, we temporarily replace the flinfo with the cached
639  * and looked-up one, while keeping the outer fcinfo (which contains all
640  * the actual arguments, etc.) intact. This is not re-entrant, but then
641  * the fcinfo itself can't be used reentrantly anyway.
642  */
643 extern Datum
645 {
646  Datum result;
647  struct fmgr_security_definer_cache *volatile fcache;
648  FmgrInfo *save_flinfo;
649  Oid save_userid;
650  int save_sec_context;
651  volatile int save_nestlevel;
652  PgStat_FunctionCallUsage fcusage;
653 
654  if (!fcinfo->flinfo->fn_extra)
655  {
656  HeapTuple tuple;
657  Form_pg_proc procedureStruct;
658  Datum datum;
659  bool isnull;
660  MemoryContext oldcxt;
661 
662  fcache = MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt,
663  sizeof(*fcache));
664 
665  fmgr_info_cxt_security(fcinfo->flinfo->fn_oid, &fcache->flinfo,
666  fcinfo->flinfo->fn_mcxt, true);
667  fcache->flinfo.fn_expr = fcinfo->flinfo->fn_expr;
668 
669  tuple = SearchSysCache1(PROCOID,
670  ObjectIdGetDatum(fcinfo->flinfo->fn_oid));
671  if (!HeapTupleIsValid(tuple))
672  elog(ERROR, "cache lookup failed for function %u",
673  fcinfo->flinfo->fn_oid);
674  procedureStruct = (Form_pg_proc) GETSTRUCT(tuple);
675 
676  if (procedureStruct->prosecdef)
677  fcache->userid = procedureStruct->proowner;
678 
679  datum = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_proconfig,
680  &isnull);
681  if (!isnull)
682  {
683  oldcxt = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
684  fcache->proconfig = DatumGetArrayTypePCopy(datum);
685  MemoryContextSwitchTo(oldcxt);
686  }
687 
688  ReleaseSysCache(tuple);
689 
690  fcinfo->flinfo->fn_extra = fcache;
691  }
692  else
693  fcache = fcinfo->flinfo->fn_extra;
694 
695  /* GetUserIdAndSecContext is cheap enough that no harm in a wasted call */
696  GetUserIdAndSecContext(&save_userid, &save_sec_context);
697  if (fcache->proconfig) /* Need a new GUC nesting level */
698  save_nestlevel = NewGUCNestLevel();
699  else
700  save_nestlevel = 0; /* keep compiler quiet */
701 
702  if (OidIsValid(fcache->userid))
704  save_sec_context | SECURITY_LOCAL_USERID_CHANGE);
705 
706  if (fcache->proconfig)
707  {
708  ProcessGUCArray(fcache->proconfig,
712  }
713 
714  /* function manager hook */
715  if (fmgr_hook)
716  (*fmgr_hook) (FHET_START, &fcache->flinfo, &fcache->arg);
717 
718  /*
719  * We don't need to restore GUC or userid settings on error, because the
720  * ensuing xact or subxact abort will do that. The PG_TRY block is only
721  * needed to clean up the flinfo link.
722  */
723  save_flinfo = fcinfo->flinfo;
724 
725  PG_TRY();
726  {
727  fcinfo->flinfo = &fcache->flinfo;
728 
729  /* See notes in fmgr_info_cxt_security */
730  pgstat_init_function_usage(fcinfo, &fcusage);
731 
732  result = FunctionCallInvoke(fcinfo);
733 
734  /*
735  * We could be calling either a regular or a set-returning function,
736  * so we have to test to see what finalize flag to use.
737  */
738  pgstat_end_function_usage(&fcusage,
739  (fcinfo->resultinfo == NULL ||
740  !IsA(fcinfo->resultinfo, ReturnSetInfo) ||
741  ((ReturnSetInfo *) fcinfo->resultinfo)->isDone != ExprMultipleResult));
742  }
743  PG_CATCH();
744  {
745  fcinfo->flinfo = save_flinfo;
746  if (fmgr_hook)
747  (*fmgr_hook) (FHET_ABORT, &fcache->flinfo, &fcache->arg);
748  PG_RE_THROW();
749  }
750  PG_END_TRY();
751 
752  fcinfo->flinfo = save_flinfo;
753 
754  if (fcache->proconfig)
755  AtEOXact_GUC(true, save_nestlevel);
756  if (OidIsValid(fcache->userid))
757  SetUserIdAndSecContext(save_userid, save_sec_context);
758  if (fmgr_hook)
759  (*fmgr_hook) (FHET_END, &fcache->flinfo, &fcache->arg);
760 
761  return result;
762 }
763 
764 
765 /*-------------------------------------------------------------------------
766  * Support routines for callers of fmgr-compatible functions
767  *-------------------------------------------------------------------------
768  */
769 
770 /*
771  * These are for invocation of a specifically named function with a
772  * directly-computed parameter list. Note that neither arguments nor result
773  * are allowed to be NULL. Also, the function cannot be one that needs to
774  * look at FmgrInfo, since there won't be any.
775  */
776 Datum
778 {
779  LOCAL_FCINFO(fcinfo, 1);
780  Datum result;
781 
782  InitFunctionCallInfoData(*fcinfo, NULL, 1, collation, NULL, NULL);
783 
784  fcinfo->args[0].value = arg1;
785  fcinfo->args[0].isnull = false;
786 
787  result = (*func) (fcinfo);
788 
789  /* Check for null result, since caller is clearly not expecting one */
790  if (fcinfo->isnull)
791  elog(ERROR, "function %p returned NULL", (void *) func);
792 
793  return result;
794 }
795 
796 Datum
797 DirectFunctionCall2Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2)
798 {
799  LOCAL_FCINFO(fcinfo, 2);
800  Datum result;
801 
802  InitFunctionCallInfoData(*fcinfo, NULL, 2, collation, NULL, NULL);
803 
804  fcinfo->args[0].value = arg1;
805  fcinfo->args[0].isnull = false;
806  fcinfo->args[1].value = arg2;
807  fcinfo->args[1].isnull = false;
808 
809  result = (*func) (fcinfo);
810 
811  /* Check for null result, since caller is clearly not expecting one */
812  if (fcinfo->isnull)
813  elog(ERROR, "function %p returned NULL", (void *) func);
814 
815  return result;
816 }
817 
818 Datum
819 DirectFunctionCall3Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
820  Datum arg3)
821 {
822  LOCAL_FCINFO(fcinfo, 3);
823  Datum result;
824 
825  InitFunctionCallInfoData(*fcinfo, NULL, 3, collation, NULL, NULL);
826 
827  fcinfo->args[0].value = arg1;
828  fcinfo->args[0].isnull = false;
829  fcinfo->args[1].value = arg2;
830  fcinfo->args[1].isnull = false;
831  fcinfo->args[2].value = arg3;
832  fcinfo->args[2].isnull = false;
833 
834  result = (*func) (fcinfo);
835 
836  /* Check for null result, since caller is clearly not expecting one */
837  if (fcinfo->isnull)
838  elog(ERROR, "function %p returned NULL", (void *) func);
839 
840  return result;
841 }
842 
843 Datum
844 DirectFunctionCall4Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
845  Datum arg3, Datum arg4)
846 {
847  LOCAL_FCINFO(fcinfo, 4);
848  Datum result;
849 
850  InitFunctionCallInfoData(*fcinfo, NULL, 4, collation, NULL, NULL);
851 
852  fcinfo->args[0].value = arg1;
853  fcinfo->args[0].isnull = false;
854  fcinfo->args[1].value = arg2;
855  fcinfo->args[1].isnull = false;
856  fcinfo->args[2].value = arg3;
857  fcinfo->args[2].isnull = false;
858  fcinfo->args[3].value = arg4;
859  fcinfo->args[3].isnull = false;
860 
861  result = (*func) (fcinfo);
862 
863  /* Check for null result, since caller is clearly not expecting one */
864  if (fcinfo->isnull)
865  elog(ERROR, "function %p returned NULL", (void *) func);
866 
867  return result;
868 }
869 
870 Datum
871 DirectFunctionCall5Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
872  Datum arg3, Datum arg4, Datum arg5)
873 {
874  LOCAL_FCINFO(fcinfo, 5);
875  Datum result;
876 
877  InitFunctionCallInfoData(*fcinfo, NULL, 5, collation, NULL, NULL);
878 
879  fcinfo->args[0].value = arg1;
880  fcinfo->args[0].isnull = false;
881  fcinfo->args[1].value = arg2;
882  fcinfo->args[1].isnull = false;
883  fcinfo->args[2].value = arg3;
884  fcinfo->args[2].isnull = false;
885  fcinfo->args[3].value = arg4;
886  fcinfo->args[3].isnull = false;
887  fcinfo->args[4].value = arg5;
888  fcinfo->args[4].isnull = false;
889 
890  result = (*func) (fcinfo);
891 
892  /* Check for null result, since caller is clearly not expecting one */
893  if (fcinfo->isnull)
894  elog(ERROR, "function %p returned NULL", (void *) func);
895 
896  return result;
897 }
898 
899 Datum
900 DirectFunctionCall6Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
901  Datum arg3, Datum arg4, Datum arg5,
902  Datum arg6)
903 {
904  LOCAL_FCINFO(fcinfo, 6);
905  Datum result;
906 
907  InitFunctionCallInfoData(*fcinfo, NULL, 6, collation, NULL, NULL);
908 
909  fcinfo->args[0].value = arg1;
910  fcinfo->args[0].isnull = false;
911  fcinfo->args[1].value = arg2;
912  fcinfo->args[1].isnull = false;
913  fcinfo->args[2].value = arg3;
914  fcinfo->args[2].isnull = false;
915  fcinfo->args[3].value = arg4;
916  fcinfo->args[3].isnull = false;
917  fcinfo->args[4].value = arg5;
918  fcinfo->args[4].isnull = false;
919  fcinfo->args[5].value = arg6;
920  fcinfo->args[5].isnull = false;
921 
922  result = (*func) (fcinfo);
923 
924  /* Check for null result, since caller is clearly not expecting one */
925  if (fcinfo->isnull)
926  elog(ERROR, "function %p returned NULL", (void *) func);
927 
928  return result;
929 }
930 
931 Datum
932 DirectFunctionCall7Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
933  Datum arg3, Datum arg4, Datum arg5,
934  Datum arg6, Datum arg7)
935 {
936  LOCAL_FCINFO(fcinfo, 7);
937  Datum result;
938 
939  InitFunctionCallInfoData(*fcinfo, NULL, 7, collation, NULL, NULL);
940 
941  fcinfo->args[0].value = arg1;
942  fcinfo->args[0].isnull = false;
943  fcinfo->args[1].value = arg2;
944  fcinfo->args[1].isnull = false;
945  fcinfo->args[2].value = arg3;
946  fcinfo->args[2].isnull = false;
947  fcinfo->args[3].value = arg4;
948  fcinfo->args[3].isnull = false;
949  fcinfo->args[4].value = arg5;
950  fcinfo->args[4].isnull = false;
951  fcinfo->args[5].value = arg6;
952  fcinfo->args[5].isnull = false;
953  fcinfo->args[6].value = arg7;
954  fcinfo->args[6].isnull = false;
955 
956  result = (*func) (fcinfo);
957 
958  /* Check for null result, since caller is clearly not expecting one */
959  if (fcinfo->isnull)
960  elog(ERROR, "function %p returned NULL", (void *) func);
961 
962  return result;
963 }
964 
965 Datum
966 DirectFunctionCall8Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
967  Datum arg3, Datum arg4, Datum arg5,
968  Datum arg6, Datum arg7, Datum arg8)
969 {
970  LOCAL_FCINFO(fcinfo, 8);
971  Datum result;
972 
973  InitFunctionCallInfoData(*fcinfo, NULL, 8, collation, NULL, NULL);
974 
975  fcinfo->args[0].value = arg1;
976  fcinfo->args[0].isnull = false;
977  fcinfo->args[1].value = arg2;
978  fcinfo->args[1].isnull = false;
979  fcinfo->args[2].value = arg3;
980  fcinfo->args[2].isnull = false;
981  fcinfo->args[3].value = arg4;
982  fcinfo->args[3].isnull = false;
983  fcinfo->args[4].value = arg5;
984  fcinfo->args[4].isnull = false;
985  fcinfo->args[5].value = arg6;
986  fcinfo->args[5].isnull = false;
987  fcinfo->args[6].value = arg7;
988  fcinfo->args[6].isnull = false;
989  fcinfo->args[7].value = arg8;
990  fcinfo->args[7].isnull = false;
991 
992  result = (*func) (fcinfo);
993 
994  /* Check for null result, since caller is clearly not expecting one */
995  if (fcinfo->isnull)
996  elog(ERROR, "function %p returned NULL", (void *) func);
997 
998  return result;
999 }
1000 
1001 Datum
1002 DirectFunctionCall9Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
1003  Datum arg3, Datum arg4, Datum arg5,
1004  Datum arg6, Datum arg7, Datum arg8,
1005  Datum arg9)
1006 {
1007  LOCAL_FCINFO(fcinfo, 9);
1008  Datum result;
1009 
1010  InitFunctionCallInfoData(*fcinfo, NULL, 9, collation, NULL, NULL);
1011 
1012  fcinfo->args[0].value = arg1;
1013  fcinfo->args[0].isnull = false;
1014  fcinfo->args[1].value = arg2;
1015  fcinfo->args[1].isnull = false;
1016  fcinfo->args[2].value = arg3;
1017  fcinfo->args[2].isnull = false;
1018  fcinfo->args[3].value = arg4;
1019  fcinfo->args[3].isnull = false;
1020  fcinfo->args[4].value = arg5;
1021  fcinfo->args[4].isnull = false;
1022  fcinfo->args[5].value = arg6;
1023  fcinfo->args[5].isnull = false;
1024  fcinfo->args[6].value = arg7;
1025  fcinfo->args[6].isnull = false;
1026  fcinfo->args[7].value = arg8;
1027  fcinfo->args[7].isnull = false;
1028  fcinfo->args[8].value = arg9;
1029  fcinfo->args[8].isnull = false;
1030 
1031  result = (*func) (fcinfo);
1032 
1033  /* Check for null result, since caller is clearly not expecting one */
1034  if (fcinfo->isnull)
1035  elog(ERROR, "function %p returned NULL", (void *) func);
1036 
1037  return result;
1038 }
1039 
1040 /*
1041  * These functions work like the DirectFunctionCall functions except that
1042  * they use the flinfo parameter to initialise the fcinfo for the call.
1043  * It's recommended that the callee only use the fn_extra and fn_mcxt
1044  * fields, as other fields will typically describe the calling function
1045  * not the callee. Conversely, the calling function should not have
1046  * used fn_extra, unless its use is known to be compatible with the callee's.
1047  */
1048 
1049 Datum
1051 {
1052  LOCAL_FCINFO(fcinfo, 1);
1053  Datum result;
1054 
1055  InitFunctionCallInfoData(*fcinfo, flinfo, 1, collation, NULL, NULL);
1056 
1057  fcinfo->args[0].value = arg1;
1058  fcinfo->args[0].isnull = false;
1059 
1060  result = (*func) (fcinfo);
1061 
1062  /* Check for null result, since caller is clearly not expecting one */
1063  if (fcinfo->isnull)
1064  elog(ERROR, "function %p returned NULL", (void *) func);
1065 
1066  return result;
1067 }
1068 
1069 Datum
1071 {
1072  LOCAL_FCINFO(fcinfo, 2);
1073  Datum result;
1074 
1075  InitFunctionCallInfoData(*fcinfo, flinfo, 2, collation, NULL, NULL);
1076 
1077  fcinfo->args[0].value = arg1;
1078  fcinfo->args[0].isnull = false;
1079  fcinfo->args[1].value = arg2;
1080  fcinfo->args[1].isnull = false;
1081 
1082  result = (*func) (fcinfo);
1083 
1084  /* Check for null result, since caller is clearly not expecting one */
1085  if (fcinfo->isnull)
1086  elog(ERROR, "function %p returned NULL", (void *) func);
1087 
1088  return result;
1089 }
1090 
1091 /*
1092  * These are for invocation of a previously-looked-up function with a
1093  * directly-computed parameter list. Note that neither arguments nor result
1094  * are allowed to be NULL.
1095  */
1096 Datum
1098 {
1099  LOCAL_FCINFO(fcinfo, 0);
1100  Datum result;
1101 
1102  InitFunctionCallInfoData(*fcinfo, flinfo, 0, collation, NULL, NULL);
1103 
1104  result = FunctionCallInvoke(fcinfo);
1105 
1106  /* Check for null result, since caller is clearly not expecting one */
1107  if (fcinfo->isnull)
1108  elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
1109 
1110  return result;
1111 }
1112 
1113 Datum
1115 {
1116  LOCAL_FCINFO(fcinfo, 1);
1117  Datum result;
1118 
1119  InitFunctionCallInfoData(*fcinfo, flinfo, 1, collation, NULL, NULL);
1120 
1121  fcinfo->args[0].value = arg1;
1122  fcinfo->args[0].isnull = false;
1123 
1124  result = FunctionCallInvoke(fcinfo);
1125 
1126  /* Check for null result, since caller is clearly not expecting one */
1127  if (fcinfo->isnull)
1128  elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
1129 
1130  return result;
1131 }
1132 
1133 Datum
1135 {
1136  LOCAL_FCINFO(fcinfo, 2);
1137  Datum result;
1138 
1139  InitFunctionCallInfoData(*fcinfo, flinfo, 2, collation, NULL, NULL);
1140 
1141  fcinfo->args[0].value = arg1;
1142  fcinfo->args[0].isnull = false;
1143  fcinfo->args[1].value = arg2;
1144  fcinfo->args[1].isnull = false;
1145 
1146  result = FunctionCallInvoke(fcinfo);
1147 
1148  /* Check for null result, since caller is clearly not expecting one */
1149  if (fcinfo->isnull)
1150  elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
1151 
1152  return result;
1153 }
1154 
1155 Datum
1157  Datum arg3)
1158 {
1159  LOCAL_FCINFO(fcinfo, 3);
1160  Datum result;
1161 
1162  InitFunctionCallInfoData(*fcinfo, flinfo, 3, collation, NULL, NULL);
1163 
1164  fcinfo->args[0].value = arg1;
1165  fcinfo->args[0].isnull = false;
1166  fcinfo->args[1].value = arg2;
1167  fcinfo->args[1].isnull = false;
1168  fcinfo->args[2].value = arg3;
1169  fcinfo->args[2].isnull = false;
1170 
1171  result = FunctionCallInvoke(fcinfo);
1172 
1173  /* Check for null result, since caller is clearly not expecting one */
1174  if (fcinfo->isnull)
1175  elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
1176 
1177  return result;
1178 }
1179 
1180 Datum
1182  Datum arg3, Datum arg4)
1183 {
1184  LOCAL_FCINFO(fcinfo, 4);
1185  Datum result;
1186 
1187  InitFunctionCallInfoData(*fcinfo, flinfo, 4, collation, NULL, NULL);
1188 
1189  fcinfo->args[0].value = arg1;
1190  fcinfo->args[0].isnull = false;
1191  fcinfo->args[1].value = arg2;
1192  fcinfo->args[1].isnull = false;
1193  fcinfo->args[2].value = arg3;
1194  fcinfo->args[2].isnull = false;
1195  fcinfo->args[3].value = arg4;
1196  fcinfo->args[3].isnull = false;
1197 
1198  result = FunctionCallInvoke(fcinfo);
1199 
1200  /* Check for null result, since caller is clearly not expecting one */
1201  if (fcinfo->isnull)
1202  elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
1203 
1204  return result;
1205 }
1206 
1207 Datum
1209  Datum arg3, Datum arg4, Datum arg5)
1210 {
1211  LOCAL_FCINFO(fcinfo, 5);
1212  Datum result;
1213 
1214  InitFunctionCallInfoData(*fcinfo, flinfo, 5, collation, NULL, NULL);
1215 
1216  fcinfo->args[0].value = arg1;
1217  fcinfo->args[0].isnull = false;
1218  fcinfo->args[1].value = arg2;
1219  fcinfo->args[1].isnull = false;
1220  fcinfo->args[2].value = arg3;
1221  fcinfo->args[2].isnull = false;
1222  fcinfo->args[3].value = arg4;
1223  fcinfo->args[3].isnull = false;
1224  fcinfo->args[4].value = arg5;
1225  fcinfo->args[4].isnull = false;
1226 
1227  result = FunctionCallInvoke(fcinfo);
1228 
1229  /* Check for null result, since caller is clearly not expecting one */
1230  if (fcinfo->isnull)
1231  elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
1232 
1233  return result;
1234 }
1235 
1236 Datum
1238  Datum arg3, Datum arg4, Datum arg5,
1239  Datum arg6)
1240 {
1241  LOCAL_FCINFO(fcinfo, 6);
1242  Datum result;
1243 
1244  InitFunctionCallInfoData(*fcinfo, flinfo, 6, collation, NULL, NULL);
1245 
1246  fcinfo->args[0].value = arg1;
1247  fcinfo->args[0].isnull = false;
1248  fcinfo->args[1].value = arg2;
1249  fcinfo->args[1].isnull = false;
1250  fcinfo->args[2].value = arg3;
1251  fcinfo->args[2].isnull = false;
1252  fcinfo->args[3].value = arg4;
1253  fcinfo->args[3].isnull = false;
1254  fcinfo->args[4].value = arg5;
1255  fcinfo->args[4].isnull = false;
1256  fcinfo->args[5].value = arg6;
1257  fcinfo->args[5].isnull = false;
1258 
1259  result = FunctionCallInvoke(fcinfo);
1260 
1261  /* Check for null result, since caller is clearly not expecting one */
1262  if (fcinfo->isnull)
1263  elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
1264 
1265  return result;
1266 }
1267 
1268 Datum
1270  Datum arg3, Datum arg4, Datum arg5,
1271  Datum arg6, Datum arg7)
1272 {
1273  LOCAL_FCINFO(fcinfo, 7);
1274  Datum result;
1275 
1276  InitFunctionCallInfoData(*fcinfo, flinfo, 7, collation, NULL, NULL);
1277 
1278  fcinfo->args[0].value = arg1;
1279  fcinfo->args[0].isnull = false;
1280  fcinfo->args[1].value = arg2;
1281  fcinfo->args[1].isnull = false;
1282  fcinfo->args[2].value = arg3;
1283  fcinfo->args[2].isnull = false;
1284  fcinfo->args[3].value = arg4;
1285  fcinfo->args[3].isnull = false;
1286  fcinfo->args[4].value = arg5;
1287  fcinfo->args[4].isnull = false;
1288  fcinfo->args[5].value = arg6;
1289  fcinfo->args[5].isnull = false;
1290  fcinfo->args[6].value = arg7;
1291  fcinfo->args[6].isnull = false;
1292 
1293  result = FunctionCallInvoke(fcinfo);
1294 
1295  /* Check for null result, since caller is clearly not expecting one */
1296  if (fcinfo->isnull)
1297  elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
1298 
1299  return result;
1300 }
1301 
1302 Datum
1304  Datum arg3, Datum arg4, Datum arg5,
1305  Datum arg6, Datum arg7, Datum arg8)
1306 {
1307  LOCAL_FCINFO(fcinfo, 8);
1308  Datum result;
1309 
1310  InitFunctionCallInfoData(*fcinfo, flinfo, 8, collation, NULL, NULL);
1311 
1312  fcinfo->args[0].value = arg1;
1313  fcinfo->args[0].isnull = false;
1314  fcinfo->args[1].value = arg2;
1315  fcinfo->args[1].isnull = false;
1316  fcinfo->args[2].value = arg3;
1317  fcinfo->args[2].isnull = false;
1318  fcinfo->args[3].value = arg4;
1319  fcinfo->args[3].isnull = false;
1320  fcinfo->args[4].value = arg5;
1321  fcinfo->args[4].isnull = false;
1322  fcinfo->args[5].value = arg6;
1323  fcinfo->args[5].isnull = false;
1324  fcinfo->args[6].value = arg7;
1325  fcinfo->args[6].isnull = false;
1326  fcinfo->args[7].value = arg8;
1327  fcinfo->args[7].isnull = false;
1328 
1329  result = FunctionCallInvoke(fcinfo);
1330 
1331  /* Check for null result, since caller is clearly not expecting one */
1332  if (fcinfo->isnull)
1333  elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
1334 
1335  return result;
1336 }
1337 
1338 Datum
1340  Datum arg3, Datum arg4, Datum arg5,
1341  Datum arg6, Datum arg7, Datum arg8,
1342  Datum arg9)
1343 {
1344  LOCAL_FCINFO(fcinfo, 9);
1345  Datum result;
1346 
1347  InitFunctionCallInfoData(*fcinfo, flinfo, 9, collation, NULL, NULL);
1348 
1349  fcinfo->args[0].value = arg1;
1350  fcinfo->args[0].isnull = false;
1351  fcinfo->args[1].value = arg2;
1352  fcinfo->args[1].isnull = false;
1353  fcinfo->args[2].value = arg3;
1354  fcinfo->args[2].isnull = false;
1355  fcinfo->args[3].value = arg4;
1356  fcinfo->args[3].isnull = false;
1357  fcinfo->args[4].value = arg5;
1358  fcinfo->args[4].isnull = false;
1359  fcinfo->args[5].value = arg6;
1360  fcinfo->args[5].isnull = false;
1361  fcinfo->args[6].value = arg7;
1362  fcinfo->args[6].isnull = false;
1363  fcinfo->args[7].value = arg8;
1364  fcinfo->args[7].isnull = false;
1365  fcinfo->args[8].value = arg9;
1366  fcinfo->args[8].isnull = false;
1367 
1368  result = FunctionCallInvoke(fcinfo);
1369 
1370  /* Check for null result, since caller is clearly not expecting one */
1371  if (fcinfo->isnull)
1372  elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
1373 
1374  return result;
1375 }
1376 
1377 
1378 /*
1379  * These are for invocation of a function identified by OID with a
1380  * directly-computed parameter list. Note that neither arguments nor result
1381  * are allowed to be NULL. These are essentially fmgr_info() followed
1382  * by FunctionCallN(). If the same function is to be invoked repeatedly,
1383  * do the fmgr_info() once and then use FunctionCallN().
1384  */
1385 Datum
1386 OidFunctionCall0Coll(Oid functionId, Oid collation)
1387 {
1388  FmgrInfo flinfo;
1389 
1390  fmgr_info(functionId, &flinfo);
1391 
1392  return FunctionCall0Coll(&flinfo, collation);
1393 }
1394 
1395 Datum
1396 OidFunctionCall1Coll(Oid functionId, Oid collation, Datum arg1)
1397 {
1398  FmgrInfo flinfo;
1399 
1400  fmgr_info(functionId, &flinfo);
1401 
1402  return FunctionCall1Coll(&flinfo, collation, arg1);
1403 }
1404 
1405 Datum
1406 OidFunctionCall2Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2)
1407 {
1408  FmgrInfo flinfo;
1409 
1410  fmgr_info(functionId, &flinfo);
1411 
1412  return FunctionCall2Coll(&flinfo, collation, arg1, arg2);
1413 }
1414 
1415 Datum
1416 OidFunctionCall3Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
1417  Datum arg3)
1418 {
1419  FmgrInfo flinfo;
1420 
1421  fmgr_info(functionId, &flinfo);
1422 
1423  return FunctionCall3Coll(&flinfo, collation, arg1, arg2, arg3);
1424 }
1425 
1426 Datum
1427 OidFunctionCall4Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
1428  Datum arg3, Datum arg4)
1429 {
1430  FmgrInfo flinfo;
1431 
1432  fmgr_info(functionId, &flinfo);
1433 
1434  return FunctionCall4Coll(&flinfo, collation, arg1, arg2, arg3, arg4);
1435 }
1436 
1437 Datum
1438 OidFunctionCall5Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
1439  Datum arg3, Datum arg4, Datum arg5)
1440 {
1441  FmgrInfo flinfo;
1442 
1443  fmgr_info(functionId, &flinfo);
1444 
1445  return FunctionCall5Coll(&flinfo, collation, arg1, arg2, arg3, arg4, arg5);
1446 }
1447 
1448 Datum
1449 OidFunctionCall6Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
1450  Datum arg3, Datum arg4, Datum arg5,
1451  Datum arg6)
1452 {
1453  FmgrInfo flinfo;
1454 
1455  fmgr_info(functionId, &flinfo);
1456 
1457  return FunctionCall6Coll(&flinfo, collation, arg1, arg2, arg3, arg4, arg5,
1458  arg6);
1459 }
1460 
1461 Datum
1462 OidFunctionCall7Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
1463  Datum arg3, Datum arg4, Datum arg5,
1464  Datum arg6, Datum arg7)
1465 {
1466  FmgrInfo flinfo;
1467 
1468  fmgr_info(functionId, &flinfo);
1469 
1470  return FunctionCall7Coll(&flinfo, collation, arg1, arg2, arg3, arg4, arg5,
1471  arg6, arg7);
1472 }
1473 
1474 Datum
1475 OidFunctionCall8Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
1476  Datum arg3, Datum arg4, Datum arg5,
1477  Datum arg6, Datum arg7, Datum arg8)
1478 {
1479  FmgrInfo flinfo;
1480 
1481  fmgr_info(functionId, &flinfo);
1482 
1483  return FunctionCall8Coll(&flinfo, collation, arg1, arg2, arg3, arg4, arg5,
1484  arg6, arg7, arg8);
1485 }
1486 
1487 Datum
1488 OidFunctionCall9Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
1489  Datum arg3, Datum arg4, Datum arg5,
1490  Datum arg6, Datum arg7, Datum arg8,
1491  Datum arg9)
1492 {
1493  FmgrInfo flinfo;
1494 
1495  fmgr_info(functionId, &flinfo);
1496 
1497  return FunctionCall9Coll(&flinfo, collation, arg1, arg2, arg3, arg4, arg5,
1498  arg6, arg7, arg8, arg9);
1499 }
1500 
1501 
1502 /*
1503  * Special cases for convenient invocation of datatype I/O functions.
1504  */
1505 
1506 /*
1507  * Call a previously-looked-up datatype input function.
1508  *
1509  * "str" may be NULL to indicate we are reading a NULL. In this case
1510  * the caller should assume the result is NULL, but we'll call the input
1511  * function anyway if it's not strict. So this is almost but not quite
1512  * the same as FunctionCall3.
1513  */
1514 Datum
1515 InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
1516 {
1517  LOCAL_FCINFO(fcinfo, 3);
1518  Datum result;
1519 
1520  if (str == NULL && flinfo->fn_strict)
1521  return (Datum) 0; /* just return null result */
1522 
1523  InitFunctionCallInfoData(*fcinfo, flinfo, 3, InvalidOid, NULL, NULL);
1524 
1525  fcinfo->args[0].value = CStringGetDatum(str);
1526  fcinfo->args[0].isnull = false;
1527  fcinfo->args[1].value = ObjectIdGetDatum(typioparam);
1528  fcinfo->args[1].isnull = false;
1529  fcinfo->args[2].value = Int32GetDatum(typmod);
1530  fcinfo->args[2].isnull = false;
1531 
1532  result = FunctionCallInvoke(fcinfo);
1533 
1534  /* Should get null result if and only if str is NULL */
1535  if (str == NULL)
1536  {
1537  if (!fcinfo->isnull)
1538  elog(ERROR, "input function %u returned non-NULL",
1539  flinfo->fn_oid);
1540  }
1541  else
1542  {
1543  if (fcinfo->isnull)
1544  elog(ERROR, "input function %u returned NULL",
1545  flinfo->fn_oid);
1546  }
1547 
1548  return result;
1549 }
1550 
1551 /*
1552  * Call a previously-looked-up datatype output function.
1553  *
1554  * Do not call this on NULL datums.
1555  *
1556  * This is currently little more than window dressing for FunctionCall1.
1557  */
1558 char *
1560 {
1562 }
1563 
1564 /*
1565  * Call a previously-looked-up datatype binary-input function.
1566  *
1567  * "buf" may be NULL to indicate we are reading a NULL. In this case
1568  * the caller should assume the result is NULL, but we'll call the receive
1569  * function anyway if it's not strict. So this is almost but not quite
1570  * the same as FunctionCall3.
1571  */
1572 Datum
1574  Oid typioparam, int32 typmod)
1575 {
1576  LOCAL_FCINFO(fcinfo, 3);
1577  Datum result;
1578 
1579  if (buf == NULL && flinfo->fn_strict)
1580  return (Datum) 0; /* just return null result */
1581 
1582  InitFunctionCallInfoData(*fcinfo, flinfo, 3, InvalidOid, NULL, NULL);
1583 
1584  fcinfo->args[0].value = PointerGetDatum(buf);
1585  fcinfo->args[0].isnull = false;
1586  fcinfo->args[1].value = ObjectIdGetDatum(typioparam);
1587  fcinfo->args[1].isnull = false;
1588  fcinfo->args[2].value = Int32GetDatum(typmod);
1589  fcinfo->args[2].isnull = false;
1590 
1591  result = FunctionCallInvoke(fcinfo);
1592 
1593  /* Should get null result if and only if buf is NULL */
1594  if (buf == NULL)
1595  {
1596  if (!fcinfo->isnull)
1597  elog(ERROR, "receive function %u returned non-NULL",
1598  flinfo->fn_oid);
1599  }
1600  else
1601  {
1602  if (fcinfo->isnull)
1603  elog(ERROR, "receive function %u returned NULL",
1604  flinfo->fn_oid);
1605  }
1606 
1607  return result;
1608 }
1609 
1610 /*
1611  * Call a previously-looked-up datatype binary-output function.
1612  *
1613  * Do not call this on NULL datums.
1614  *
1615  * This is little more than window dressing for FunctionCall1, but it does
1616  * guarantee a non-toasted result, which strictly speaking the underlying
1617  * function doesn't.
1618  */
1619 bytea *
1621 {
1623 }
1624 
1625 /*
1626  * As above, for I/O functions identified by OID. These are only to be used
1627  * in seldom-executed code paths. They are not only slow but leak memory.
1628  */
1629 Datum
1630 OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
1631 {
1632  FmgrInfo flinfo;
1633 
1634  fmgr_info(functionId, &flinfo);
1635  return InputFunctionCall(&flinfo, str, typioparam, typmod);
1636 }
1637 
1638 char *
1640 {
1641  FmgrInfo flinfo;
1642 
1643  fmgr_info(functionId, &flinfo);
1644  return OutputFunctionCall(&flinfo, val);
1645 }
1646 
1647 Datum
1649  Oid typioparam, int32 typmod)
1650 {
1651  FmgrInfo flinfo;
1652 
1653  fmgr_info(functionId, &flinfo);
1654  return ReceiveFunctionCall(&flinfo, buf, typioparam, typmod);
1655 }
1656 
1657 bytea *
1659 {
1660  FmgrInfo flinfo;
1661 
1662  fmgr_info(functionId, &flinfo);
1663  return SendFunctionCall(&flinfo, val);
1664 }
1665 
1666 
1667 /*-------------------------------------------------------------------------
1668  * Support routines for standard maybe-pass-by-reference datatypes
1669  *
1670  * int8 and float8 can be passed by value if Datum is wide enough.
1671  * (For backwards-compatibility reasons, we allow pass-by-ref to be chosen
1672  * at compile time even if pass-by-val is possible.)
1673  *
1674  * Note: there is only one switch controlling the pass-by-value option for
1675  * both int8 and float8; this is to avoid making things unduly complicated
1676  * for the timestamp types, which might have either representation.
1677  *-------------------------------------------------------------------------
1678  */
1679 
1680 #ifndef USE_FLOAT8_BYVAL /* controls int8 too */
1681 
1682 Datum
1684 {
1685  int64 *retval = (int64 *) palloc(sizeof(int64));
1686 
1687  *retval = X;
1688  return PointerGetDatum(retval);
1689 }
1690 
1691 Datum
1693 {
1694  float8 *retval = (float8 *) palloc(sizeof(float8));
1695 
1696  *retval = X;
1697  return PointerGetDatum(retval);
1698 }
1699 #endif /* USE_FLOAT8_BYVAL */
1700 
1701 
1702 /*-------------------------------------------------------------------------
1703  * Support routines for toastable datatypes
1704  *-------------------------------------------------------------------------
1705  */
1706 
1707 struct varlena *
1709 {
1710  if (VARATT_IS_EXTENDED(datum))
1711  return detoast_attr(datum);
1712  else
1713  return datum;
1714 }
1715 
1716 struct varlena *
1718 {
1719  if (VARATT_IS_EXTENDED(datum))
1720  return detoast_attr(datum);
1721  else
1722  {
1723  /* Make a modifiable copy of the varlena object */
1724  Size len = VARSIZE(datum);
1725  struct varlena *result = (struct varlena *) palloc(len);
1726 
1727  memcpy(result, datum, len);
1728  return result;
1729  }
1730 }
1731 
1732 struct varlena *
1733 pg_detoast_datum_slice(struct varlena *datum, int32 first, int32 count)
1734 {
1735  /* Only get the specified portion from the toast rel */
1736  return detoast_attr_slice(datum, first, count);
1737 }
1738 
1739 struct varlena *
1741 {
1742  if (VARATT_IS_COMPRESSED(datum) || VARATT_IS_EXTERNAL(datum))
1743  return detoast_attr(datum);
1744  else
1745  return datum;
1746 }
1747 
1748 /*-------------------------------------------------------------------------
1749  * Support routines for extracting info from fn_expr parse tree
1750  *
1751  * These are needed by polymorphic functions, which accept multiple possible
1752  * input types and need help from the parser to know what they've got.
1753  * Also, some functions might be interested in whether a parameter is constant.
1754  * Functions taking VARIADIC ANY also need to know about the VARIADIC keyword.
1755  *-------------------------------------------------------------------------
1756  */
1757 
1758 /*
1759  * Get the actual type OID of the function return type
1760  *
1761  * Returns InvalidOid if information is not available
1762  */
1763 Oid
1765 {
1766  Node *expr;
1767 
1768  /*
1769  * can't return anything useful if we have no FmgrInfo or if its fn_expr
1770  * node has not been initialized
1771  */
1772  if (!flinfo || !flinfo->fn_expr)
1773  return InvalidOid;
1774 
1775  expr = flinfo->fn_expr;
1776 
1777  return exprType(expr);
1778 }
1779 
1780 /*
1781  * Get the actual type OID of a specific function argument (counting from 0)
1782  *
1783  * Returns InvalidOid if information is not available
1784  */
1785 Oid
1786 get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
1787 {
1788  /*
1789  * can't return anything useful if we have no FmgrInfo or if its fn_expr
1790  * node has not been initialized
1791  */
1792  if (!flinfo || !flinfo->fn_expr)
1793  return InvalidOid;
1794 
1795  return get_call_expr_argtype(flinfo->fn_expr, argnum);
1796 }
1797 
1798 /*
1799  * Get the actual type OID of a specific function argument (counting from 0),
1800  * but working from the calling expression tree instead of FmgrInfo
1801  *
1802  * Returns InvalidOid if information is not available
1803  */
1804 Oid
1805 get_call_expr_argtype(Node *expr, int argnum)
1806 {
1807  List *args;
1808  Oid argtype;
1809 
1810  if (expr == NULL)
1811  return InvalidOid;
1812 
1813  if (IsA(expr, FuncExpr))
1814  args = ((FuncExpr *) expr)->args;
1815  else if (IsA(expr, OpExpr))
1816  args = ((OpExpr *) expr)->args;
1817  else if (IsA(expr, DistinctExpr))
1818  args = ((DistinctExpr *) expr)->args;
1819  else if (IsA(expr, ScalarArrayOpExpr))
1820  args = ((ScalarArrayOpExpr *) expr)->args;
1821  else if (IsA(expr, NullIfExpr))
1822  args = ((NullIfExpr *) expr)->args;
1823  else if (IsA(expr, WindowFunc))
1824  args = ((WindowFunc *) expr)->args;
1825  else
1826  return InvalidOid;
1827 
1828  if (argnum < 0 || argnum >= list_length(args))
1829  return InvalidOid;
1830 
1831  argtype = exprType((Node *) list_nth(args, argnum));
1832 
1833  /*
1834  * special hack for ScalarArrayOpExpr: what the underlying function will
1835  * actually get passed is the element type of the array.
1836  */
1837  if (IsA(expr, ScalarArrayOpExpr) &&
1838  argnum == 1)
1839  argtype = get_base_element_type(argtype);
1840 
1841  return argtype;
1842 }
1843 
1844 /*
1845  * Find out whether a specific function argument is constant for the
1846  * duration of a query
1847  *
1848  * Returns false if information is not available
1849  */
1850 bool
1851 get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum)
1852 {
1853  /*
1854  * can't return anything useful if we have no FmgrInfo or if its fn_expr
1855  * node has not been initialized
1856  */
1857  if (!flinfo || !flinfo->fn_expr)
1858  return false;
1859 
1860  return get_call_expr_arg_stable(flinfo->fn_expr, argnum);
1861 }
1862 
1863 /*
1864  * Find out whether a specific function argument is constant for the
1865  * duration of a query, but working from the calling expression tree
1866  *
1867  * Returns false if information is not available
1868  */
1869 bool
1870 get_call_expr_arg_stable(Node *expr, int argnum)
1871 {
1872  List *args;
1873  Node *arg;
1874 
1875  if (expr == NULL)
1876  return false;
1877 
1878  if (IsA(expr, FuncExpr))
1879  args = ((FuncExpr *) expr)->args;
1880  else if (IsA(expr, OpExpr))
1881  args = ((OpExpr *) expr)->args;
1882  else if (IsA(expr, DistinctExpr))
1883  args = ((DistinctExpr *) expr)->args;
1884  else if (IsA(expr, ScalarArrayOpExpr))
1885  args = ((ScalarArrayOpExpr *) expr)->args;
1886  else if (IsA(expr, NullIfExpr))
1887  args = ((NullIfExpr *) expr)->args;
1888  else if (IsA(expr, WindowFunc))
1889  args = ((WindowFunc *) expr)->args;
1890  else
1891  return false;
1892 
1893  if (argnum < 0 || argnum >= list_length(args))
1894  return false;
1895 
1896  arg = (Node *) list_nth(args, argnum);
1897 
1898  /*
1899  * Either a true Const or an external Param will have a value that doesn't
1900  * change during the execution of the query. In future we might want to
1901  * consider other cases too, e.g. now().
1902  */
1903  if (IsA(arg, Const))
1904  return true;
1905  if (IsA(arg, Param) &&
1906  ((Param *) arg)->paramkind == PARAM_EXTERN)
1907  return true;
1908 
1909  return false;
1910 }
1911 
1912 /*
1913  * Get the VARIADIC flag from the function invocation
1914  *
1915  * Returns false (the default assumption) if information is not available
1916  *
1917  * Note this is generally only of interest to VARIADIC ANY functions
1918  */
1919 bool
1921 {
1922  Node *expr;
1923 
1924  /*
1925  * can't return anything useful if we have no FmgrInfo or if its fn_expr
1926  * node has not been initialized
1927  */
1928  if (!flinfo || !flinfo->fn_expr)
1929  return false;
1930 
1931  expr = flinfo->fn_expr;
1932 
1933  if (IsA(expr, FuncExpr))
1934  return ((FuncExpr *) expr)->funcvariadic;
1935  else
1936  return false;
1937 }
1938 
1939 /*
1940  * Set options to FmgrInfo of opclass support function.
1941  *
1942  * Opclass support functions are called outside of expressions. Thanks to that
1943  * we can use fn_expr to store opclass options as bytea constant.
1944  */
1945 void
1947 {
1948  flinfo->fn_expr = (Node *) makeConst(BYTEAOID, -1, InvalidOid, -1,
1950  options == NULL, false);
1951 }
1952 
1953 /*
1954  * Check if options are defined for opclass support function.
1955  */
1956 bool
1958 {
1959  if (flinfo && flinfo->fn_expr && IsA(flinfo->fn_expr, Const))
1960  {
1961  Const *expr = (Const *) flinfo->fn_expr;
1962 
1963  if (expr->consttype == BYTEAOID)
1964  return !expr->constisnull;
1965  }
1966  return false;
1967 }
1968 
1969 /*
1970  * Get options for opclass support function.
1971  */
1972 bytea *
1974 {
1975  if (flinfo && flinfo->fn_expr && IsA(flinfo->fn_expr, Const))
1976  {
1977  Const *expr = (Const *) flinfo->fn_expr;
1978 
1979  if (expr->consttype == BYTEAOID)
1980  return expr->constisnull ? NULL : DatumGetByteaP(expr->constvalue);
1981  }
1982 
1983  ereport(ERROR,
1984  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1985  errmsg("operator class options info is absent in function call context")));
1986 
1987  return NULL;
1988 }
1989 
1990 /*-------------------------------------------------------------------------
1991  * Support routines for procedural language implementations
1992  *-------------------------------------------------------------------------
1993  */
1994 
1995 /*
1996  * Verify that a validator is actually associated with the language of a
1997  * particular function and that the user has access to both the language and
1998  * the function. All validators should call this before doing anything
1999  * substantial. Doing so ensures a user cannot achieve anything with explicit
2000  * calls to validators that he could not achieve with CREATE FUNCTION or by
2001  * simply calling an existing function.
2002  *
2003  * When this function returns false, callers should skip all validation work
2004  * and call PG_RETURN_VOID(). This never happens at present; it is reserved
2005  * for future expansion.
2006  *
2007  * In particular, checking that the validator corresponds to the function's
2008  * language allows untrusted language validators to assume they process only
2009  * superuser-chosen source code. (Untrusted language call handlers, by
2010  * definition, do assume that.) A user lacking the USAGE language privilege
2011  * would be unable to reach the validator through CREATE FUNCTION, so we check
2012  * that to block explicit calls as well. Checking the EXECUTE privilege on
2013  * the function is often superfluous, because most users can clone the
2014  * function to get an executable copy. It is meaningful against users with no
2015  * database TEMP right and no permanent schema CREATE right, thereby unable to
2016  * create any function. Also, if the function tracks persistent state by
2017  * function OID or name, validating the original function might permit more
2018  * mischief than creating and validating a clone thereof.
2019  */
2020 bool
2021 CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid)
2022 {
2023  HeapTuple procTup;
2024  HeapTuple langTup;
2025  Form_pg_proc procStruct;
2026  Form_pg_language langStruct;
2027  AclResult aclresult;
2028 
2029  /*
2030  * Get the function's pg_proc entry. Throw a user-facing error for bad
2031  * OID, because validators can be called with user-specified OIDs.
2032  */
2033  procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionOid));
2034  if (!HeapTupleIsValid(procTup))
2035  ereport(ERROR,
2036  (errcode(ERRCODE_UNDEFINED_FUNCTION),
2037  errmsg("function with OID %u does not exist", functionOid)));
2038  procStruct = (Form_pg_proc) GETSTRUCT(procTup);
2039 
2040  /*
2041  * Fetch pg_language entry to know if this is the correct validation
2042  * function for that pg_proc entry.
2043  */
2044  langTup = SearchSysCache1(LANGOID, ObjectIdGetDatum(procStruct->prolang));
2045  if (!HeapTupleIsValid(langTup))
2046  elog(ERROR, "cache lookup failed for language %u", procStruct->prolang);
2047  langStruct = (Form_pg_language) GETSTRUCT(langTup);
2048 
2049  if (langStruct->lanvalidator != validatorOid)
2050  ereport(ERROR,
2051  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2052  errmsg("language validation function %u called for language %u instead of %u",
2053  validatorOid, procStruct->prolang,
2054  langStruct->lanvalidator)));
2055 
2056  /* first validate that we have permissions to use the language */
2057  aclresult = pg_language_aclcheck(procStruct->prolang, GetUserId(),
2058  ACL_USAGE);
2059  if (aclresult != ACLCHECK_OK)
2060  aclcheck_error(aclresult, OBJECT_LANGUAGE,
2061  NameStr(langStruct->lanname));
2062 
2063  /*
2064  * Check whether we are allowed to execute the function itself. If we can
2065  * execute it, there should be no possible side-effect of
2066  * compiling/validation that execution can't have.
2067  */
2068  aclresult = pg_proc_aclcheck(functionOid, GetUserId(), ACL_EXECUTE);
2069  if (aclresult != ACLCHECK_OK)
2070  aclcheck_error(aclresult, OBJECT_FUNCTION, NameStr(procStruct->proname));
2071 
2072  ReleaseSysCache(procTup);
2073  ReleaseSysCache(langTup);
2074 
2075  return true;
2076 }
AclResult
Definition: acl.h:181
@ ACLCHECK_OK
Definition: acl.h:182
AclResult pg_language_aclcheck(Oid lang_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:5083
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3512
AclResult pg_proc_aclcheck(Oid proc_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:5071
#define DatumGetArrayTypePCopy(X)
Definition: array.h:255
#define TextDatumGetCString(d)
Definition: builtins.h:86
#define NameStr(name)
Definition: c.h:692
unsigned short uint16
Definition: c.h:451
#define PGDLLIMPORT
Definition: c.h:1342
signed int int32
Definition: c.h:440
double float8
Definition: c.h:576
uint32 TransactionId
Definition: c.h:598
#define OidIsValid(objectId)
Definition: c.h:721
size_t Size
Definition: c.h:551
struct varlena * detoast_attr(struct varlena *attr)
Definition: detoast.c:116
struct varlena * detoast_attr_slice(struct varlena *attr, int32 sliceoffset, int32 slicelength)
Definition: detoast.c:205
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:105
void * lookup_external_function(void *filehandle, const char *funcname)
Definition: dfmgr.c:166
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
int errhint(const char *fmt,...)
Definition: elog.c:1151
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define PG_RE_THROW()
Definition: elog.h:340
#define PG_END_TRY()
Definition: elog.h:324
#define PG_TRY()
Definition: elog.h:299
#define ERROR
Definition: elog.h:33
#define PG_CATCH()
Definition: elog.h:309
#define ereport(elevel,...)
Definition: elog.h:143
const char * name
Definition: encode.c:561
@ ExprMultipleResult
Definition: execnodes.h:281
Datum FunctionCall4Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4)
Definition: fmgr.c:1181
Datum OidFunctionCall2Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1406
void set_fn_opclass_options(FmgrInfo *flinfo, bytea *options)
Definition: fmgr.c:1946
Oid fmgr_internal_function(const char *proname)
Definition: fmgr.c:609
struct varlena * pg_detoast_datum_packed(struct varlena *datum)
Definition: fmgr.c:1740
Datum OidFunctionCall9Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6, Datum arg7, Datum arg8, Datum arg9)
Definition: fmgr.c:1488
bool get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1851
Datum OidFunctionCall6Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6)
Definition: fmgr.c:1449
Datum FunctionCall6Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6)
Definition: fmgr.c:1237
Datum OidFunctionCall5Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5)
Definition: fmgr.c:1438
Datum FunctionCall8Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6, Datum arg7, Datum arg8)
Definition: fmgr.c:1303
struct varlena * pg_detoast_datum_copy(struct varlena *datum)
Definition: fmgr.c:1717
bool CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid)
Definition: fmgr.c:2021
Datum OidReceiveFunctionCall(Oid functionId, StringInfo buf, Oid typioparam, int32 typmod)
Definition: fmgr.c:1648
bytea * get_fn_opclass_options(FmgrInfo *flinfo)
Definition: fmgr.c:1973
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1515
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1134
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:126
Datum OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1630
const Pg_finfo_record * fetch_finfo_record(void *filehandle, const char *funcname)
Definition: fmgr.c:469
PGDLLIMPORT needs_fmgr_hook_type needs_fmgr_hook
Definition: fmgr.c:38
Datum DirectFunctionCall2Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:797
static void fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
Definition: fmgr.c:358
Datum FunctionCall5Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5)
Definition: fmgr.c:1208
static void fmgr_info_other_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
Definition: fmgr.c:432
Datum OidFunctionCall3Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2, Datum arg3)
Definition: fmgr.c:1416
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:136
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1683
Datum OidFunctionCall8Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6, Datum arg7, Datum arg8)
Definition: fmgr.c:1475
Datum OidFunctionCall4Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4)
Definition: fmgr.c:1427
Datum DirectFunctionCall4Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4)
Definition: fmgr.c:844
static const FmgrBuiltin * fmgr_isbuiltin(Oid id)
Definition: fmgr.c:75
Datum OidFunctionCall7Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6, Datum arg7)
Definition: fmgr.c:1462
PGDLLIMPORT fmgr_hook_type fmgr_hook
Definition: fmgr.c:39
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1692
bool has_fn_opclass_options(FmgrInfo *flinfo)
Definition: fmgr.c:1957
Datum CallerFInfoFunctionCall2(PGFunction func, FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1070
Datum CallerFInfoFunctionCall1(PGFunction func, FmgrInfo *flinfo, Oid collation, Datum arg1)
Definition: fmgr.c:1050
Datum DirectFunctionCall6Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6)
Definition: fmgr.c:900
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1639
Datum DirectFunctionCall5Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5)
Definition: fmgr.c:871
static HTAB * CFuncHash
Definition: fmgr.c:54
Datum OidFunctionCall1Coll(Oid functionId, Oid collation, Datum arg1)
Definition: fmgr.c:1396
static const FmgrBuiltin * fmgr_lookupByName(const char *name)
Definition: fmgr.c:100
struct varlena * pg_detoast_datum_slice(struct varlena *datum, int32 first, int32 count)
Definition: fmgr.c:1733
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:1920
Datum DirectFunctionCall1Coll(PGFunction func, Oid collation, Datum arg1)
Definition: fmgr.c:777
Datum FunctionCall3Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3)
Definition: fmgr.c:1156
Datum DirectFunctionCall3Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2, Datum arg3)
Definition: fmgr.c:819
Oid get_call_expr_argtype(Node *expr, int argnum)
Definition: fmgr.c:1805
Datum FunctionCall7Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6, Datum arg7)
Definition: fmgr.c:1269
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1786
Datum OidFunctionCall0Coll(Oid functionId, Oid collation)
Definition: fmgr.c:1386
void fmgr_symbol(Oid functionId, char **mod, char **fn)
Definition: fmgr.c:283
Datum DirectFunctionCall9Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6, Datum arg7, Datum arg8, Datum arg9)
Definition: fmgr.c:1002
bytea * SendFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1620
Datum FunctionCall1Coll(FmgrInfo *flinfo, Oid collation, Datum arg1)
Definition: fmgr.c:1114
Datum fmgr_security_definer(PG_FUNCTION_ARGS)
Definition: fmgr.c:644
bytea * OidSendFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1658
Oid get_fn_expr_rettype(FmgrInfo *flinfo)
Definition: fmgr.c:1764
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1559
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
Definition: fmgr.c:594
static void record_C_func(HeapTuple procedureTuple, PGFunction user_fn, const Pg_finfo_record *inforec)
Definition: fmgr.c:553
static void fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt, bool ignore_security)
Definition: fmgr.c:146
Datum FunctionCall0Coll(FmgrInfo *flinfo, Oid collation)
Definition: fmgr.c:1097
Datum FunctionCall9Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6, Datum arg7, Datum arg8, Datum arg9)
Definition: fmgr.c:1339
struct varlena * pg_detoast_datum(struct varlena *datum)
Definition: fmgr.c:1708
Datum DirectFunctionCall7Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6, Datum arg7)
Definition: fmgr.c:932
static CFuncHashTabEntry * lookup_C_func(HeapTuple procedureTuple)
Definition: fmgr.c:529
bool get_call_expr_arg_stable(Node *expr, int argnum)
Definition: fmgr.c:1870
Datum ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf, Oid typioparam, int32 typmod)
Definition: fmgr.c:1573
Datum DirectFunctionCall8Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6, Datum arg7, Datum arg8)
Definition: fmgr.c:966
bool(* needs_fmgr_hook_type)(Oid fn_oid)
Definition: fmgr.h:770
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:150
#define LOCAL_FCINFO(name, nargs)
Definition: fmgr.h:110
@ FHET_END
Definition: fmgr.h:766
@ FHET_ABORT
Definition: fmgr.h:767
@ FHET_START
Definition: fmgr.h:765
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:649
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:172
const Pg_finfo_record *(* PGFInfoFunction)(void)
Definition: fmgr.h:401
void(* fmgr_hook_type)(FmgrHookEventType event, FmgrInfo *flinfo, Datum *arg)
Definition: fmgr.h:772
Datum(* PGFunction)(FunctionCallInfo fcinfo)
Definition: fmgr.h:40
#define FmgrHookIsNeeded(fn_oid)
Definition: fmgr.h:778
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define DatumGetByteaP(X)
Definition: fmgr.h:331
PGDLLIMPORT const Oid fmgr_last_builtin_oid
PGDLLIMPORT const int fmgr_nbuiltins
PGDLLIMPORT const FmgrBuiltin fmgr_builtins[]
#define InvalidOidBuiltinMapping
Definition: fmgrtab.h:46
PGDLLIMPORT const uint16 fmgr_builtin_oid_index[]
Datum fmgr_sql(PG_FUNCTION_ARGS)
Definition: functions.c:1019
int NewGUCNestLevel(void)
Definition: guc.c:6475
void ProcessGUCArray(ArrayType *array, GucContext context, GucSource source, GucAction action)
Definition: guc.c:11371
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:6489
@ GUC_ACTION_SAVE
Definition: guc.h:200
@ PGC_S_SESSION
Definition: guc.h:123
@ PGC_SUSET
Definition: guc.h:75
@ PGC_USERSET
Definition: guc.h:76
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:359
@ HASH_FIND
Definition: hsearch.h:113
@ HASH_ENTER
Definition: hsearch.h:114
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define HeapTupleHeaderGetRawXmin(tup)
Definition: htup_details.h:303
#define GETSTRUCT(TUP)
Definition: htup_details.h:649
long val
Definition: informix.c:664
int i
Definition: isn.c:73
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:29
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2789
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:300
char * pstrdup(const char *in)
Definition: mcxt.c:1305
void pfree(void *pointer)
Definition: mcxt.c:1175
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:906
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
void * palloc(Size size)
Definition: mcxt.c:1068
#define SECURITY_LOCAL_USERID_CHANGE
Definition: miscadmin.h:312
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition: miscinit.c:602
Oid GetUserId(void)
Definition: miscinit.c:491
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:609
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
#define IsA(nodeptr, _type_)
Definition: nodes.h:625
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define ACL_USAGE
Definition: parsenodes.h:90
@ OBJECT_LANGUAGE
Definition: parsenodes.h:2156
@ OBJECT_FUNCTION
Definition: parsenodes.h:2154
#define ACL_EXECUTE
Definition: parsenodes.h:89
void * arg
const void size_t len
FormData_pg_language * Form_pg_language
Definition: pg_language.h:65
static int list_length(const List *l)
Definition: pg_list.h:150
static void * list_nth(const List *list, int n)
Definition: pg_list.h:297
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
NameData proname
Definition: pg_proc.h:35
static char * buf
Definition: pg_test_fsync.c:67
@ TRACK_FUNC_PL
Definition: pgstat.h:63
@ TRACK_FUNC_ALL
Definition: pgstat.h:64
@ TRACK_FUNC_OFF
Definition: pgstat.h:62
void pgstat_init_function_usage(FunctionCallInfo fcinfo, PgStat_FunctionCallUsage *fcu)
void pgstat_end_function_usage(PgStat_FunctionCallUsage *fcu, bool finalize)
#define CStringGetDatum(X)
Definition: postgres.h:622
#define VARATT_IS_EXTENDED(PTR)
Definition: postgres.h:340
uintptr_t Datum
Definition: postgres.h:411
#define VARATT_IS_COMPRESSED(PTR)
Definition: postgres.h:325
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define Int32GetDatum(X)
Definition: postgres.h:523
#define VARSIZE(PTR)
Definition: postgres.h:316
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:326
#define DatumGetCString(X)
Definition: postgres.h:610
#define PointerGetDatum(X)
Definition: postgres.h:600
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
@ PARAM_EXTERN
Definition: primnodes.h:299
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
const Pg_finfo_record * inforec
Definition: fmgr.c:51
TransactionId fn_xmin
Definition: fmgr.c:48
ItemPointerData fn_tid
Definition: fmgr.c:49
PGFunction user_fn
Definition: fmgr.c:50
Oid consttype
Definition: primnodes.h:256
Datum constvalue
Definition: primnodes.h:260
bool constisnull
Definition: primnodes.h:261
bool strict
Definition: fmgrtab.h:29
bool retset
Definition: fmgrtab.h:30
PGFunction func
Definition: fmgrtab.h:32
Oid foid
Definition: fmgrtab.h:27
short nargs
Definition: fmgrtab.h:28
Definition: fmgr.h:57
void * fn_extra
Definition: fmgr.h:64
short fn_nargs
Definition: fmgr.h:60
fmNodePtr fn_expr
Definition: fmgr.h:66
PGFunction fn_addr
Definition: fmgr.h:58
unsigned char fn_stats
Definition: fmgr.h:63
MemoryContext fn_mcxt
Definition: fmgr.h:65
Oid fn_oid
Definition: fmgr.h:59
bool fn_retset
Definition: fmgr.h:62
bool fn_strict
Definition: fmgr.h:61
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76
Definition: dynahash.c:220
ItemPointerData t_self
Definition: htup.h:65
HeapTupleHeader t_data
Definition: htup.h:68
Definition: pg_list.h:52
Definition: nodes.h:575
int api_version
Definition: fmgr.h:396
ArrayType * proconfig
Definition: fmgr.c:629
Definition: type.h:90
Definition: c.h:633
bool superuser(void)
Definition: superuser.c:46
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1221
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1173
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1434
@ PROCOID
Definition: syscache.h:79
@ LANGOID
Definition: syscache.h:68
static void * fn(void *arg)