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