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