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