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