PostgreSQL Source Code  git master
pg_proc.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_proc.c
4  * routines to support manipulation of the pg_proc relation
5  *
6  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/catalog/pg_proc.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include "access/htup_details.h"
18 #include "access/table.h"
19 #include "access/xact.h"
20 #include "catalog/catalog.h"
21 #include "catalog/dependency.h"
22 #include "catalog/indexing.h"
23 #include "catalog/objectaccess.h"
24 #include "catalog/pg_language.h"
25 #include "catalog/pg_namespace.h"
26 #include "catalog/pg_proc.h"
27 #include "catalog/pg_transform.h"
28 #include "catalog/pg_type.h"
29 #include "commands/defrem.h"
30 #include "executor/functions.h"
31 #include "funcapi.h"
32 #include "mb/pg_wchar.h"
33 #include "miscadmin.h"
34 #include "nodes/nodeFuncs.h"
35 #include "parser/analyze.h"
36 #include "parser/parse_coerce.h"
37 #include "parser/parse_type.h"
38 #include "tcop/pquery.h"
39 #include "tcop/tcopprot.h"
40 #include "utils/acl.h"
41 #include "utils/builtins.h"
42 #include "utils/lsyscache.h"
43 #include "utils/regproc.h"
44 #include "utils/rel.h"
45 #include "utils/syscache.h"
46 
47 
48 typedef struct
49 {
50  char *proname;
51  char *prosrc;
53 
54 static void sql_function_parse_error_callback(void *arg);
55 static int match_prosrc_to_query(const char *prosrc, const char *queryText,
56  int cursorpos);
57 static bool match_prosrc_to_literal(const char *prosrc, const char *literal,
58  int cursorpos, int *newcursorpos);
59 
60 
61 /* ----------------------------------------------------------------
62  * ProcedureCreate
63  *
64  * Note: allParameterTypes, parameterModes, parameterNames, trftypes, and proconfig
65  * are either arrays of the proper types or NULL. We declare them Datum,
66  * not "ArrayType *", to avoid importing array.h into pg_proc.h.
67  * ----------------------------------------------------------------
68  */
70 ProcedureCreate(const char *procedureName,
71  Oid procNamespace,
72  bool replace,
73  bool returnsSet,
74  Oid returnType,
75  Oid proowner,
76  Oid languageObjectId,
77  Oid languageValidator,
78  const char *prosrc,
79  const char *probin,
80  Node *prosqlbody,
81  char prokind,
82  bool security_definer,
83  bool isLeakProof,
84  bool isStrict,
85  char volatility,
86  char parallel,
87  oidvector *parameterTypes,
88  Datum allParameterTypes,
89  Datum parameterModes,
90  Datum parameterNames,
91  List *parameterDefaults,
92  Datum trftypes,
93  Datum proconfig,
94  Oid prosupport,
95  float4 procost,
96  float4 prorows)
97 {
98  Oid retval;
99  int parameterCount;
100  int allParamCount;
101  Oid *allParams;
102  char *paramModes = NULL;
103  Oid variadicType = InvalidOid;
104  Acl *proacl = NULL;
105  Relation rel;
106  HeapTuple tup;
107  HeapTuple oldtup;
108  bool nulls[Natts_pg_proc];
109  Datum values[Natts_pg_proc];
110  bool replaces[Natts_pg_proc];
111  NameData procname;
112  TupleDesc tupDesc;
113  bool is_update;
114  ObjectAddress myself,
115  referenced;
116  char *detailmsg;
117  int i;
118  Oid trfid;
119  ObjectAddresses *addrs;
120 
121  /*
122  * sanity checks
123  */
124  Assert(PointerIsValid(prosrc) || PointerIsValid(prosqlbody));
125 
126  parameterCount = parameterTypes->dim1;
127  if (parameterCount < 0 || parameterCount > FUNC_MAX_ARGS)
128  ereport(ERROR,
129  (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
130  errmsg_plural("functions cannot have more than %d argument",
131  "functions cannot have more than %d arguments",
132  FUNC_MAX_ARGS,
133  FUNC_MAX_ARGS)));
134  /* note: the above is correct, we do NOT count output arguments */
135 
136  /* Deconstruct array inputs */
137  if (allParameterTypes != PointerGetDatum(NULL))
138  {
139  /*
140  * We expect the array to be a 1-D OID array; verify that. We don't
141  * need to use deconstruct_array() since the array data is just going
142  * to look like a C array of OID values.
143  */
144  ArrayType *allParamArray = (ArrayType *) DatumGetPointer(allParameterTypes);
145 
146  allParamCount = ARR_DIMS(allParamArray)[0];
147  if (ARR_NDIM(allParamArray) != 1 ||
148  allParamCount <= 0 ||
149  ARR_HASNULL(allParamArray) ||
150  ARR_ELEMTYPE(allParamArray) != OIDOID)
151  elog(ERROR, "allParameterTypes is not a 1-D Oid array");
152  allParams = (Oid *) ARR_DATA_PTR(allParamArray);
153  Assert(allParamCount >= parameterCount);
154  /* we assume caller got the contents right */
155  }
156  else
157  {
158  allParamCount = parameterCount;
159  allParams = parameterTypes->values;
160  }
161 
162  if (parameterModes != PointerGetDatum(NULL))
163  {
164  /*
165  * We expect the array to be a 1-D CHAR array; verify that. We don't
166  * need to use deconstruct_array() since the array data is just going
167  * to look like a C array of char values.
168  */
169  ArrayType *modesArray = (ArrayType *) DatumGetPointer(parameterModes);
170 
171  if (ARR_NDIM(modesArray) != 1 ||
172  ARR_DIMS(modesArray)[0] != allParamCount ||
173  ARR_HASNULL(modesArray) ||
174  ARR_ELEMTYPE(modesArray) != CHAROID)
175  elog(ERROR, "parameterModes is not a 1-D char array");
176  paramModes = (char *) ARR_DATA_PTR(modesArray);
177  }
178 
179  /*
180  * Do not allow polymorphic return type unless there is a polymorphic
181  * input argument that we can use to deduce the actual return type.
182  */
183  detailmsg = check_valid_polymorphic_signature(returnType,
184  parameterTypes->values,
185  parameterCount);
186  if (detailmsg)
187  ereport(ERROR,
188  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
189  errmsg("cannot determine result data type"),
190  errdetail_internal("%s", detailmsg)));
191 
192  /*
193  * Also, do not allow return type INTERNAL unless at least one input
194  * argument is INTERNAL.
195  */
196  detailmsg = check_valid_internal_signature(returnType,
197  parameterTypes->values,
198  parameterCount);
199  if (detailmsg)
200  ereport(ERROR,
201  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
202  errmsg("unsafe use of pseudo-type \"internal\""),
203  errdetail_internal("%s", detailmsg)));
204 
205  /*
206  * Apply the same tests to any OUT arguments.
207  */
208  if (allParameterTypes != PointerGetDatum(NULL))
209  {
210  for (i = 0; i < allParamCount; i++)
211  {
212  if (paramModes == NULL ||
213  paramModes[i] == PROARGMODE_IN ||
214  paramModes[i] == PROARGMODE_VARIADIC)
215  continue; /* ignore input-only params */
216 
217  detailmsg = check_valid_polymorphic_signature(allParams[i],
218  parameterTypes->values,
219  parameterCount);
220  if (detailmsg)
221  ereport(ERROR,
222  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
223  errmsg("cannot determine result data type"),
224  errdetail_internal("%s", detailmsg)));
225  detailmsg = check_valid_internal_signature(allParams[i],
226  parameterTypes->values,
227  parameterCount);
228  if (detailmsg)
229  ereport(ERROR,
230  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
231  errmsg("unsafe use of pseudo-type \"internal\""),
232  errdetail_internal("%s", detailmsg)));
233  }
234  }
235 
236  /* Identify variadic argument type, if any */
237  if (paramModes != NULL)
238  {
239  /*
240  * Only the last input parameter can be variadic; if it is, save its
241  * element type. Errors here are just elog since caller should have
242  * checked this already.
243  */
244  for (i = 0; i < allParamCount; i++)
245  {
246  switch (paramModes[i])
247  {
248  case PROARGMODE_IN:
249  case PROARGMODE_INOUT:
250  if (OidIsValid(variadicType))
251  elog(ERROR, "variadic parameter must be last");
252  break;
253  case PROARGMODE_OUT:
254  if (OidIsValid(variadicType) && prokind == PROKIND_PROCEDURE)
255  elog(ERROR, "variadic parameter must be last");
256  break;
257  case PROARGMODE_TABLE:
258  /* okay */
259  break;
260  case PROARGMODE_VARIADIC:
261  if (OidIsValid(variadicType))
262  elog(ERROR, "variadic parameter must be last");
263  switch (allParams[i])
264  {
265  case ANYOID:
266  variadicType = ANYOID;
267  break;
268  case ANYARRAYOID:
269  variadicType = ANYELEMENTOID;
270  break;
271  case ANYCOMPATIBLEARRAYOID:
272  variadicType = ANYCOMPATIBLEOID;
273  break;
274  default:
275  variadicType = get_element_type(allParams[i]);
276  if (!OidIsValid(variadicType))
277  elog(ERROR, "variadic parameter is not an array");
278  break;
279  }
280  break;
281  default:
282  elog(ERROR, "invalid parameter mode '%c'", paramModes[i]);
283  break;
284  }
285  }
286  }
287 
288  /*
289  * All seems OK; prepare the data to be inserted into pg_proc.
290  */
291 
292  for (i = 0; i < Natts_pg_proc; ++i)
293  {
294  nulls[i] = false;
295  values[i] = (Datum) 0;
296  replaces[i] = true;
297  }
298 
299  namestrcpy(&procname, procedureName);
300  values[Anum_pg_proc_proname - 1] = NameGetDatum(&procname);
301  values[Anum_pg_proc_pronamespace - 1] = ObjectIdGetDatum(procNamespace);
302  values[Anum_pg_proc_proowner - 1] = ObjectIdGetDatum(proowner);
303  values[Anum_pg_proc_prolang - 1] = ObjectIdGetDatum(languageObjectId);
304  values[Anum_pg_proc_procost - 1] = Float4GetDatum(procost);
305  values[Anum_pg_proc_prorows - 1] = Float4GetDatum(prorows);
306  values[Anum_pg_proc_provariadic - 1] = ObjectIdGetDatum(variadicType);
307  values[Anum_pg_proc_prosupport - 1] = ObjectIdGetDatum(prosupport);
308  values[Anum_pg_proc_prokind - 1] = CharGetDatum(prokind);
309  values[Anum_pg_proc_prosecdef - 1] = BoolGetDatum(security_definer);
310  values[Anum_pg_proc_proleakproof - 1] = BoolGetDatum(isLeakProof);
311  values[Anum_pg_proc_proisstrict - 1] = BoolGetDatum(isStrict);
312  values[Anum_pg_proc_proretset - 1] = BoolGetDatum(returnsSet);
313  values[Anum_pg_proc_provolatile - 1] = CharGetDatum(volatility);
314  values[Anum_pg_proc_proparallel - 1] = CharGetDatum(parallel);
315  values[Anum_pg_proc_pronargs - 1] = UInt16GetDatum(parameterCount);
316  values[Anum_pg_proc_pronargdefaults - 1] = UInt16GetDatum(list_length(parameterDefaults));
317  values[Anum_pg_proc_prorettype - 1] = ObjectIdGetDatum(returnType);
318  values[Anum_pg_proc_proargtypes - 1] = PointerGetDatum(parameterTypes);
319  if (allParameterTypes != PointerGetDatum(NULL))
320  values[Anum_pg_proc_proallargtypes - 1] = allParameterTypes;
321  else
322  nulls[Anum_pg_proc_proallargtypes - 1] = true;
323  if (parameterModes != PointerGetDatum(NULL))
324  values[Anum_pg_proc_proargmodes - 1] = parameterModes;
325  else
326  nulls[Anum_pg_proc_proargmodes - 1] = true;
327  if (parameterNames != PointerGetDatum(NULL))
328  values[Anum_pg_proc_proargnames - 1] = parameterNames;
329  else
330  nulls[Anum_pg_proc_proargnames - 1] = true;
331  if (parameterDefaults != NIL)
332  values[Anum_pg_proc_proargdefaults - 1] = CStringGetTextDatum(nodeToString(parameterDefaults));
333  else
334  nulls[Anum_pg_proc_proargdefaults - 1] = true;
335  if (trftypes != PointerGetDatum(NULL))
336  values[Anum_pg_proc_protrftypes - 1] = trftypes;
337  else
338  nulls[Anum_pg_proc_protrftypes - 1] = true;
339  if (prosrc)
340  values[Anum_pg_proc_prosrc - 1] = CStringGetTextDatum(prosrc);
341  else
342  nulls[Anum_pg_proc_prosrc - 1] = true;
343  if (probin)
344  values[Anum_pg_proc_probin - 1] = CStringGetTextDatum(probin);
345  else
346  nulls[Anum_pg_proc_probin - 1] = true;
347  if (prosqlbody)
348  values[Anum_pg_proc_prosqlbody - 1] = CStringGetTextDatum(nodeToString(prosqlbody));
349  else
350  nulls[Anum_pg_proc_prosqlbody - 1] = true;
351  if (proconfig != PointerGetDatum(NULL))
352  values[Anum_pg_proc_proconfig - 1] = proconfig;
353  else
354  nulls[Anum_pg_proc_proconfig - 1] = true;
355  /* proacl will be determined later */
356 
357  rel = table_open(ProcedureRelationId, RowExclusiveLock);
358  tupDesc = RelationGetDescr(rel);
359 
360  /* Check for pre-existing definition */
362  PointerGetDatum(procedureName),
363  PointerGetDatum(parameterTypes),
364  ObjectIdGetDatum(procNamespace));
365 
366  if (HeapTupleIsValid(oldtup))
367  {
368  /* There is one; okay to replace it? */
369  Form_pg_proc oldproc = (Form_pg_proc) GETSTRUCT(oldtup);
370  Datum proargnames;
371  bool isnull;
372  const char *dropcmd;
373 
374  if (!replace)
375  ereport(ERROR,
376  (errcode(ERRCODE_DUPLICATE_FUNCTION),
377  errmsg("function \"%s\" already exists with same argument types",
378  procedureName)));
379  if (!pg_proc_ownercheck(oldproc->oid, proowner))
381  procedureName);
382 
383  /* Not okay to change routine kind */
384  if (oldproc->prokind != prokind)
385  ereport(ERROR,
386  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
387  errmsg("cannot change routine kind"),
388  (oldproc->prokind == PROKIND_AGGREGATE ?
389  errdetail("\"%s\" is an aggregate function.", procedureName) :
390  oldproc->prokind == PROKIND_FUNCTION ?
391  errdetail("\"%s\" is a function.", procedureName) :
392  oldproc->prokind == PROKIND_PROCEDURE ?
393  errdetail("\"%s\" is a procedure.", procedureName) :
394  oldproc->prokind == PROKIND_WINDOW ?
395  errdetail("\"%s\" is a window function.", procedureName) :
396  0)));
397 
398  dropcmd = (prokind == PROKIND_PROCEDURE ? "DROP PROCEDURE" :
399  prokind == PROKIND_AGGREGATE ? "DROP AGGREGATE" :
400  "DROP FUNCTION");
401 
402  /*
403  * Not okay to change the return type of the existing proc, since
404  * existing rules, views, etc may depend on the return type.
405  *
406  * In case of a procedure, a changing return type means that whether
407  * the procedure has output parameters was changed. Since there is no
408  * user visible return type, we produce a more specific error message.
409  */
410  if (returnType != oldproc->prorettype ||
411  returnsSet != oldproc->proretset)
412  ereport(ERROR,
413  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
414  prokind == PROKIND_PROCEDURE
415  ? errmsg("cannot change whether a procedure has output parameters")
416  : errmsg("cannot change return type of existing function"),
417 
418  /*
419  * translator: first %s is DROP FUNCTION, DROP PROCEDURE, or DROP
420  * AGGREGATE
421  */
422  errhint("Use %s %s first.",
423  dropcmd,
424  format_procedure(oldproc->oid))));
425 
426  /*
427  * If it returns RECORD, check for possible change of record type
428  * implied by OUT parameters
429  */
430  if (returnType == RECORDOID)
431  {
432  TupleDesc olddesc;
433  TupleDesc newdesc;
434 
435  olddesc = build_function_result_tupdesc_t(oldtup);
436  newdesc = build_function_result_tupdesc_d(prokind,
437  allParameterTypes,
438  parameterModes,
439  parameterNames);
440  if (olddesc == NULL && newdesc == NULL)
441  /* ok, both are runtime-defined RECORDs */ ;
442  else if (olddesc == NULL || newdesc == NULL ||
443  !equalTupleDescs(olddesc, newdesc))
444  ereport(ERROR,
445  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
446  errmsg("cannot change return type of existing function"),
447  errdetail("Row type defined by OUT parameters is different."),
448  /* translator: first %s is DROP FUNCTION or DROP PROCEDURE */
449  errhint("Use %s %s first.",
450  dropcmd,
451  format_procedure(oldproc->oid))));
452  }
453 
454  /*
455  * If there were any named input parameters, check to make sure the
456  * names have not been changed, as this could break existing calls. We
457  * allow adding names to formerly unnamed parameters, though.
458  */
459  proargnames = SysCacheGetAttr(PROCNAMEARGSNSP, oldtup,
460  Anum_pg_proc_proargnames,
461  &isnull);
462  if (!isnull)
463  {
464  Datum proargmodes;
465  char **old_arg_names;
466  char **new_arg_names;
467  int n_old_arg_names;
468  int n_new_arg_names;
469  int j;
470 
471  proargmodes = SysCacheGetAttr(PROCNAMEARGSNSP, oldtup,
472  Anum_pg_proc_proargmodes,
473  &isnull);
474  if (isnull)
475  proargmodes = PointerGetDatum(NULL); /* just to be sure */
476 
477  n_old_arg_names = get_func_input_arg_names(prokind,
478  proargnames,
479  proargmodes,
480  &old_arg_names);
481  n_new_arg_names = get_func_input_arg_names(prokind,
482  parameterNames,
483  parameterModes,
484  &new_arg_names);
485  for (j = 0; j < n_old_arg_names; j++)
486  {
487  if (old_arg_names[j] == NULL)
488  continue;
489  if (j >= n_new_arg_names || new_arg_names[j] == NULL ||
490  strcmp(old_arg_names[j], new_arg_names[j]) != 0)
491  ereport(ERROR,
492  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
493  errmsg("cannot change name of input parameter \"%s\"",
494  old_arg_names[j]),
495  /* translator: first %s is DROP FUNCTION or DROP PROCEDURE */
496  errhint("Use %s %s first.",
497  dropcmd,
498  format_procedure(oldproc->oid))));
499  }
500  }
501 
502  /*
503  * If there are existing defaults, check compatibility: redefinition
504  * must not remove any defaults nor change their types. (Removing a
505  * default might cause a function to fail to satisfy an existing call.
506  * Changing type would only be possible if the associated parameter is
507  * polymorphic, and in such cases a change of default type might alter
508  * the resolved output type of existing calls.)
509  */
510  if (oldproc->pronargdefaults != 0)
511  {
512  Datum proargdefaults;
513  List *oldDefaults;
514  ListCell *oldlc;
515  ListCell *newlc;
516 
517  if (list_length(parameterDefaults) < oldproc->pronargdefaults)
518  ereport(ERROR,
519  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
520  errmsg("cannot remove parameter defaults from existing function"),
521  /* translator: first %s is DROP FUNCTION or DROP PROCEDURE */
522  errhint("Use %s %s first.",
523  dropcmd,
524  format_procedure(oldproc->oid))));
525 
526  proargdefaults = SysCacheGetAttr(PROCNAMEARGSNSP, oldtup,
527  Anum_pg_proc_proargdefaults,
528  &isnull);
529  Assert(!isnull);
530  oldDefaults = castNode(List, stringToNode(TextDatumGetCString(proargdefaults)));
531  Assert(list_length(oldDefaults) == oldproc->pronargdefaults);
532 
533  /* new list can have more defaults than old, advance over 'em */
534  newlc = list_nth_cell(parameterDefaults,
535  list_length(parameterDefaults) -
536  oldproc->pronargdefaults);
537 
538  foreach(oldlc, oldDefaults)
539  {
540  Node *oldDef = (Node *) lfirst(oldlc);
541  Node *newDef = (Node *) lfirst(newlc);
542 
543  if (exprType(oldDef) != exprType(newDef))
544  ereport(ERROR,
545  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
546  errmsg("cannot change data type of existing parameter default value"),
547  /* translator: first %s is DROP FUNCTION or DROP PROCEDURE */
548  errhint("Use %s %s first.",
549  dropcmd,
550  format_procedure(oldproc->oid))));
551  newlc = lnext(parameterDefaults, newlc);
552  }
553  }
554 
555  /*
556  * Do not change existing oid, ownership or permissions, either. Note
557  * dependency-update code below has to agree with this decision.
558  */
559  replaces[Anum_pg_proc_oid - 1] = false;
560  replaces[Anum_pg_proc_proowner - 1] = false;
561  replaces[Anum_pg_proc_proacl - 1] = false;
562 
563  /* Okay, do it... */
564  tup = heap_modify_tuple(oldtup, tupDesc, values, nulls, replaces);
565  CatalogTupleUpdate(rel, &tup->t_self, tup);
566 
567  ReleaseSysCache(oldtup);
568  is_update = true;
569  }
570  else
571  {
572  /* Creating a new procedure */
573  Oid newOid;
574 
575  /* First, get default permissions and set up proacl */
576  proacl = get_user_default_acl(OBJECT_FUNCTION, proowner,
577  procNamespace);
578  if (proacl != NULL)
579  values[Anum_pg_proc_proacl - 1] = PointerGetDatum(proacl);
580  else
581  nulls[Anum_pg_proc_proacl - 1] = true;
582 
584  Anum_pg_proc_oid);
585  values[Anum_pg_proc_oid - 1] = ObjectIdGetDatum(newOid);
586  tup = heap_form_tuple(tupDesc, values, nulls);
587  CatalogTupleInsert(rel, tup);
588  is_update = false;
589  }
590 
591 
592  retval = ((Form_pg_proc) GETSTRUCT(tup))->oid;
593 
594  /*
595  * Create dependencies for the new function. If we are updating an
596  * existing function, first delete any existing pg_depend entries.
597  * (However, since we are not changing ownership or permissions, the
598  * shared dependencies do *not* need to change, and we leave them alone.)
599  */
600  if (is_update)
601  deleteDependencyRecordsFor(ProcedureRelationId, retval, true);
602 
603  addrs = new_object_addresses();
604 
605  ObjectAddressSet(myself, ProcedureRelationId, retval);
606 
607  /* dependency on namespace */
608  ObjectAddressSet(referenced, NamespaceRelationId, procNamespace);
609  add_exact_object_address(&referenced, addrs);
610 
611  /* dependency on implementation language */
612  ObjectAddressSet(referenced, LanguageRelationId, languageObjectId);
613  add_exact_object_address(&referenced, addrs);
614 
615  /* dependency on return type */
616  ObjectAddressSet(referenced, TypeRelationId, returnType);
617  add_exact_object_address(&referenced, addrs);
618 
619  /* dependency on transform used by return type, if any */
620  if ((trfid = get_transform_oid(returnType, languageObjectId, true)))
621  {
622  ObjectAddressSet(referenced, TransformRelationId, trfid);
623  add_exact_object_address(&referenced, addrs);
624  }
625 
626  /* dependency on parameter types */
627  for (i = 0; i < allParamCount; i++)
628  {
629  ObjectAddressSet(referenced, TypeRelationId, allParams[i]);
630  add_exact_object_address(&referenced, addrs);
631 
632  /* dependency on transform used by parameter type, if any */
633  if ((trfid = get_transform_oid(allParams[i], languageObjectId, true)))
634  {
635  ObjectAddressSet(referenced, TransformRelationId, trfid);
636  add_exact_object_address(&referenced, addrs);
637  }
638  }
639 
640  /* dependency on support function, if any */
641  if (OidIsValid(prosupport))
642  {
643  ObjectAddressSet(referenced, ProcedureRelationId, prosupport);
644  add_exact_object_address(&referenced, addrs);
645  }
646 
648  free_object_addresses(addrs);
649 
650  /* dependency on SQL routine body */
651  if (languageObjectId == SQLlanguageId && prosqlbody)
652  recordDependencyOnExpr(&myself, prosqlbody, NIL, DEPENDENCY_NORMAL);
653 
654  /* dependency on parameter default expressions */
655  if (parameterDefaults)
656  recordDependencyOnExpr(&myself, (Node *) parameterDefaults,
658 
659  /* dependency on owner */
660  if (!is_update)
661  recordDependencyOnOwner(ProcedureRelationId, retval, proowner);
662 
663  /* dependency on any roles mentioned in ACL */
664  if (!is_update)
665  recordDependencyOnNewAcl(ProcedureRelationId, retval, 0,
666  proowner, proacl);
667 
668  /* dependency on extension */
669  recordDependencyOnCurrentExtension(&myself, is_update);
670 
671  heap_freetuple(tup);
672 
673  /* Post creation hook for new function */
674  InvokeObjectPostCreateHook(ProcedureRelationId, retval, 0);
675 
677 
678  /* Verify function body */
679  if (OidIsValid(languageValidator))
680  {
681  ArrayType *set_items = NULL;
682  int save_nestlevel = 0;
683 
684  /* Advance command counter so new tuple can be seen by validator */
686 
687  /*
688  * Set per-function configuration parameters so that the validation is
689  * done with the environment the function expects. However, if
690  * check_function_bodies is off, we don't do this, because that would
691  * create dump ordering hazards that pg_dump doesn't know how to deal
692  * with. (For example, a SET clause might refer to a not-yet-created
693  * text search configuration.) This means that the validator
694  * shouldn't complain about anything that might depend on a GUC
695  * parameter when check_function_bodies is off.
696  */
698  {
699  set_items = (ArrayType *) DatumGetPointer(proconfig);
700  if (set_items) /* Need a new GUC nesting level */
701  {
702  save_nestlevel = NewGUCNestLevel();
703  ProcessGUCArray(set_items,
707  }
708  }
709 
710  OidFunctionCall1(languageValidator, ObjectIdGetDatum(retval));
711 
712  if (set_items)
713  AtEOXact_GUC(true, save_nestlevel);
714  }
715 
716  return myself;
717 }
718 
719 
720 
721 /*
722  * Validator for internal functions
723  *
724  * Check that the given internal function name (the "prosrc" value) is
725  * a known builtin function.
726  */
727 Datum
729 {
730  Oid funcoid = PG_GETARG_OID(0);
731  HeapTuple tuple;
732  bool isnull;
733  Datum tmp;
734  char *prosrc;
735 
736  if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid))
737  PG_RETURN_VOID();
738 
739  /*
740  * We do not honor check_function_bodies since it's unlikely the function
741  * name will be found later if it isn't there now.
742  */
743 
744  tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
745  if (!HeapTupleIsValid(tuple))
746  elog(ERROR, "cache lookup failed for function %u", funcoid);
747 
748  tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
749  if (isnull)
750  elog(ERROR, "null prosrc");
751  prosrc = TextDatumGetCString(tmp);
752 
753  if (fmgr_internal_function(prosrc) == InvalidOid)
754  ereport(ERROR,
755  (errcode(ERRCODE_UNDEFINED_FUNCTION),
756  errmsg("there is no built-in function named \"%s\"",
757  prosrc)));
758 
759  ReleaseSysCache(tuple);
760 
761  PG_RETURN_VOID();
762 }
763 
764 
765 
766 /*
767  * Validator for C language functions
768  *
769  * Make sure that the library file exists, is loadable, and contains
770  * the specified link symbol. Also check for a valid function
771  * information record.
772  */
773 Datum
775 {
776  Oid funcoid = PG_GETARG_OID(0);
777  void *libraryhandle;
778  HeapTuple tuple;
779  bool isnull;
780  Datum tmp;
781  char *prosrc;
782  char *probin;
783 
784  if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid))
785  PG_RETURN_VOID();
786 
787  /*
788  * It'd be most consistent to skip the check if !check_function_bodies,
789  * but the purpose of that switch is to be helpful for pg_dump loading,
790  * and for pg_dump loading it's much better if we *do* check.
791  */
792 
793  tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
794  if (!HeapTupleIsValid(tuple))
795  elog(ERROR, "cache lookup failed for function %u", funcoid);
796 
797  tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
798  if (isnull)
799  elog(ERROR, "null prosrc for C function %u", funcoid);
800  prosrc = TextDatumGetCString(tmp);
801 
802  tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_probin, &isnull);
803  if (isnull)
804  elog(ERROR, "null probin for C function %u", funcoid);
805  probin = TextDatumGetCString(tmp);
806 
807  (void) load_external_function(probin, prosrc, true, &libraryhandle);
808  (void) fetch_finfo_record(libraryhandle, prosrc);
809 
810  ReleaseSysCache(tuple);
811 
812  PG_RETURN_VOID();
813 }
814 
815 
816 /*
817  * Validator for SQL language functions
818  *
819  * Parse it here in order to be sure that it contains no syntax errors.
820  */
821 Datum
823 {
824  Oid funcoid = PG_GETARG_OID(0);
825  HeapTuple tuple;
826  Form_pg_proc proc;
827  List *raw_parsetree_list;
828  List *querytree_list;
829  ListCell *lc;
830  bool isnull;
831  Datum tmp;
832  char *prosrc;
833  parse_error_callback_arg callback_arg;
834  ErrorContextCallback sqlerrcontext;
835  bool haspolyarg;
836  int i;
837 
838  if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid))
839  PG_RETURN_VOID();
840 
841  tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
842  if (!HeapTupleIsValid(tuple))
843  elog(ERROR, "cache lookup failed for function %u", funcoid);
844  proc = (Form_pg_proc) GETSTRUCT(tuple);
845 
846  /* Disallow pseudotype result */
847  /* except for RECORD, VOID, or polymorphic */
848  if (get_typtype(proc->prorettype) == TYPTYPE_PSEUDO &&
849  proc->prorettype != RECORDOID &&
850  proc->prorettype != VOIDOID &&
851  !IsPolymorphicType(proc->prorettype))
852  ereport(ERROR,
853  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
854  errmsg("SQL functions cannot return type %s",
855  format_type_be(proc->prorettype))));
856 
857  /* Disallow pseudotypes in arguments */
858  /* except for polymorphic */
859  haspolyarg = false;
860  for (i = 0; i < proc->pronargs; i++)
861  {
862  if (get_typtype(proc->proargtypes.values[i]) == TYPTYPE_PSEUDO)
863  {
864  if (IsPolymorphicType(proc->proargtypes.values[i]))
865  haspolyarg = true;
866  else
867  ereport(ERROR,
868  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
869  errmsg("SQL functions cannot have arguments of type %s",
870  format_type_be(proc->proargtypes.values[i]))));
871  }
872  }
873 
874  /* Postpone body checks if !check_function_bodies */
876  {
877  /*
878  * Setup error traceback support for ereport().
879  */
880  callback_arg.proname = NameStr(proc->proname);
881  callback_arg.prosrc = NULL;
882 
884  sqlerrcontext.arg = (void *) &callback_arg;
885  sqlerrcontext.previous = error_context_stack;
886  error_context_stack = &sqlerrcontext;
887 
888  tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
889  if (isnull)
890  {
891  Node *n;
892 
893  tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosqlbody, &isnull);
894  if (isnull)
895  elog(ERROR, "null prosrc and prosqlbody");
896 
898  if (IsA(n, List))
899  querytree_list = castNode(List, n);
900  else
901  querytree_list = list_make1(list_make1(n));
902  }
903  else
904  {
905  prosrc = TextDatumGetCString(tmp);
906 
907  callback_arg.prosrc = prosrc;
908 
909  /*
910  * We can't do full prechecking of the function definition if there
911  * are any polymorphic input types, because actual datatypes of
912  * expression results will be unresolvable. The check will be done at
913  * runtime instead.
914  *
915  * We can run the text through the raw parser though; this will at
916  * least catch silly syntactic errors.
917  */
918  raw_parsetree_list = pg_parse_query(prosrc);
919  querytree_list = NIL;
920 
921  if (!haspolyarg)
922  {
923  /*
924  * OK to do full precheck: analyze and rewrite the queries, then
925  * verify the result type.
926  */
928 
929  /* But first, set up parameter information */
930  pinfo = prepare_sql_fn_parse_info(tuple, NULL, InvalidOid);
931 
932  foreach(lc, raw_parsetree_list)
933  {
934  RawStmt *parsetree = lfirst_node(RawStmt, lc);
935  List *querytree_sublist;
936 
937  querytree_sublist = pg_analyze_and_rewrite_params(parsetree,
938  prosrc,
940  pinfo,
941  NULL);
942  querytree_list = lappend(querytree_list,
943  querytree_sublist);
944  }
945  }
946  }
947 
948  if (!haspolyarg)
949  {
950  Oid rettype;
951  TupleDesc rettupdesc;
952 
953  check_sql_fn_statements(querytree_list);
954 
955  (void) get_func_result_type(funcoid, &rettype, &rettupdesc);
956 
957  (void) check_sql_fn_retval(querytree_list,
958  rettype, rettupdesc,
959  false, NULL);
960  }
961 
962  error_context_stack = sqlerrcontext.previous;
963  }
964 
965  ReleaseSysCache(tuple);
966 
967  PG_RETURN_VOID();
968 }
969 
970 /*
971  * Error context callback for handling errors in SQL function definitions
972  */
973 static void
975 {
976  parse_error_callback_arg *callback_arg = (parse_error_callback_arg *) arg;
977 
978  /* See if it's a syntax error; if so, transpose to CREATE FUNCTION */
979  if (!function_parse_error_transpose(callback_arg->prosrc))
980  {
981  /* If it's not a syntax error, push info onto context stack */
982  errcontext("SQL function \"%s\"", callback_arg->proname);
983  }
984 }
985 
986 /*
987  * Adjust a syntax error occurring inside the function body of a CREATE
988  * FUNCTION or DO command. This can be used by any function validator or
989  * anonymous-block handler, not only for SQL-language functions.
990  * It is assumed that the syntax error position is initially relative to the
991  * function body string (as passed in). If possible, we adjust the position
992  * to reference the original command text; if we can't manage that, we set
993  * up an "internal query" syntax error instead.
994  *
995  * Returns true if a syntax error was processed, false if not.
996  */
997 bool
999 {
1000  int origerrposition;
1001  int newerrposition;
1002  const char *queryText;
1003 
1004  if (!prosrc)
1005  return false;
1006 
1007  /*
1008  * Nothing to do unless we are dealing with a syntax error that has a
1009  * cursor position.
1010  *
1011  * Some PLs may prefer to report the error position as an internal error
1012  * to begin with, so check that too.
1013  */
1014  origerrposition = geterrposition();
1015  if (origerrposition <= 0)
1016  {
1017  origerrposition = getinternalerrposition();
1018  if (origerrposition <= 0)
1019  return false;
1020  }
1021 
1022  /* We can get the original query text from the active portal (hack...) */
1024  queryText = ActivePortal->sourceText;
1025 
1026  /* Try to locate the prosrc in the original text */
1027  newerrposition = match_prosrc_to_query(prosrc, queryText, origerrposition);
1028 
1029  if (newerrposition > 0)
1030  {
1031  /* Successful, so fix error position to reference original query */
1032  errposition(newerrposition);
1033  /* Get rid of any report of the error as an "internal query" */
1035  internalerrquery(NULL);
1036  }
1037  else
1038  {
1039  /*
1040  * If unsuccessful, convert the position to an internal position
1041  * marker and give the function text as the internal query.
1042  */
1043  errposition(0);
1044  internalerrposition(origerrposition);
1045  internalerrquery(prosrc);
1046  }
1047 
1048  return true;
1049 }
1050 
1051 /*
1052  * Try to locate the string literal containing the function body in the
1053  * given text of the CREATE FUNCTION or DO command. If successful, return
1054  * the character (not byte) index within the command corresponding to the
1055  * given character index within the literal. If not successful, return 0.
1056  */
1057 static int
1058 match_prosrc_to_query(const char *prosrc, const char *queryText,
1059  int cursorpos)
1060 {
1061  /*
1062  * Rather than fully parsing the original command, we just scan the
1063  * command looking for $prosrc$ or 'prosrc'. This could be fooled (though
1064  * not in any very probable scenarios), so fail if we find more than one
1065  * match.
1066  */
1067  int prosrclen = strlen(prosrc);
1068  int querylen = strlen(queryText);
1069  int matchpos = 0;
1070  int curpos;
1071  int newcursorpos;
1072 
1073  for (curpos = 0; curpos < querylen - prosrclen; curpos++)
1074  {
1075  if (queryText[curpos] == '$' &&
1076  strncmp(prosrc, &queryText[curpos + 1], prosrclen) == 0 &&
1077  queryText[curpos + 1 + prosrclen] == '$')
1078  {
1079  /*
1080  * Found a $foo$ match. Since there are no embedded quoting
1081  * characters in a dollar-quoted literal, we don't have to do any
1082  * fancy arithmetic; just offset by the starting position.
1083  */
1084  if (matchpos)
1085  return 0; /* multiple matches, fail */
1086  matchpos = pg_mbstrlen_with_len(queryText, curpos + 1)
1087  + cursorpos;
1088  }
1089  else if (queryText[curpos] == '\'' &&
1090  match_prosrc_to_literal(prosrc, &queryText[curpos + 1],
1091  cursorpos, &newcursorpos))
1092  {
1093  /*
1094  * Found a 'foo' match. match_prosrc_to_literal() has adjusted
1095  * for any quotes or backslashes embedded in the literal.
1096  */
1097  if (matchpos)
1098  return 0; /* multiple matches, fail */
1099  matchpos = pg_mbstrlen_with_len(queryText, curpos + 1)
1100  + newcursorpos;
1101  }
1102  }
1103 
1104  return matchpos;
1105 }
1106 
1107 /*
1108  * Try to match the given source text to a single-quoted literal.
1109  * If successful, adjust newcursorpos to correspond to the character
1110  * (not byte) index corresponding to cursorpos in the source text.
1111  *
1112  * At entry, literal points just past a ' character. We must check for the
1113  * trailing quote.
1114  */
1115 static bool
1116 match_prosrc_to_literal(const char *prosrc, const char *literal,
1117  int cursorpos, int *newcursorpos)
1118 {
1119  int newcp = cursorpos;
1120  int chlen;
1121 
1122  /*
1123  * This implementation handles backslashes and doubled quotes in the
1124  * string literal. It does not handle the SQL syntax for literals
1125  * continued across line boundaries.
1126  *
1127  * We do the comparison a character at a time, not a byte at a time, so
1128  * that we can do the correct cursorpos math.
1129  */
1130  while (*prosrc)
1131  {
1132  cursorpos--; /* characters left before cursor */
1133 
1134  /*
1135  * Check for backslashes and doubled quotes in the literal; adjust
1136  * newcp when one is found before the cursor.
1137  */
1138  if (*literal == '\\')
1139  {
1140  literal++;
1141  if (cursorpos > 0)
1142  newcp++;
1143  }
1144  else if (*literal == '\'')
1145  {
1146  if (literal[1] != '\'')
1147  goto fail;
1148  literal++;
1149  if (cursorpos > 0)
1150  newcp++;
1151  }
1152  chlen = pg_mblen(prosrc);
1153  if (strncmp(prosrc, literal, chlen) != 0)
1154  goto fail;
1155  prosrc += chlen;
1156  literal += chlen;
1157  }
1158 
1159  if (*literal == '\'' && literal[1] != '\'')
1160  {
1161  /* success */
1162  *newcursorpos = newcp;
1163  return true;
1164  }
1165 
1166 fail:
1167  /* Must set *newcursorpos to suppress compiler warning */
1168  *newcursorpos = newcp;
1169  return false;
1170 }
1171 
1172 List *
1174 {
1175  ArrayType *array = DatumGetArrayTypeP(datum);
1176  Datum *values;
1177  int nelems;
1178  int i;
1179  List *result = NIL;
1180 
1181  deconstruct_array(array,
1182  OIDOID,
1183  sizeof(Oid), true, TYPALIGN_INT,
1184  &values, NULL, &nelems);
1185  for (i = 0; i < nelems; i++)
1186  result = lappend_oid(result, values[i]);
1187  return result;
1188 }
static Datum Float4GetDatum(float4 X)
Definition: postgres.h:725
#define NIL
Definition: pg_list.h:65
Definition: c.h:660
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:322
bool check_sql_fn_retval(List *queryTreeLists, Oid rettype, TupleDesc rettupdesc, bool insertDroppedCols, List **resultTargetList)
Definition: functions.c:1618
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
#define NameGetDatum(X)
Definition: postgres.h:639
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
int errhint(const char *fmt,...)
Definition: elog.c:1156
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:151
char * check_valid_polymorphic_signature(Oid ret_type, const Oid *declared_arg_types, int nargs)
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:322
static ListCell * list_nth_cell(const List *list, int n)
Definition: pg_list.h:256
#define RelationGetDescr(relation)
Definition: rel.h:483
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1019
#define castNode(_type_, nodeptr)
Definition: nodes.h:608
void namestrcpy(Name name, const char *str)
Definition: name.c:233
Portal ActivePortal
Definition: pquery.c:35
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2706
#define PointerGetDatum(X)
Definition: postgres.h:600
void sql_fn_parser_setup(struct ParseState *pstate, SQLFunctionParseInfoPtr pinfo)
Definition: functions.c:266
void record_object_address_dependencies(const ObjectAddress *depender, ObjectAddresses *referenced, DependencyType behavior)
Definition: dependency.c:2834
TupleDesc build_function_result_tupdesc_d(char prokind, Datum proallargtypes, Datum proargmodes, Datum proargnames)
Definition: funcapi.c:1654
long deleteDependencyRecordsFor(Oid classId, Oid objectId, bool skipExtensionDeps)
Definition: pg_depend.c:273
Definition: nodes.h:539
int errcode(int sqlerrcode)
Definition: elog.c:698
void * stringToNode(const char *str)
Definition: read.c:89
bool superuser(void)
Definition: superuser.c:46
char get_typtype(Oid typid)
Definition: lsyscache.c:2576
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
Datum fmgr_c_validator(PG_FUNCTION_ARGS)
Definition: pg_proc.c:774
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
Definition: dependency.c:2625
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:165
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2570
bool check_function_bodies
Definition: guc.c:555
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2866
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
void(* callback)(void *arg)
Definition: elog.h:247
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
struct ErrorContextCallback * previous
Definition: elog.h:246
#define OidIsValid(objectId)
Definition: c.h:710
int errdetail_internal(const char *fmt,...)
Definition: elog.c:1069
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:1000
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define FUNC_MAX_ARGS
Oid fmgr_internal_function(const char *proname)
Definition: fmgr.c:626
#define list_make1(x1)
Definition: pg_list.h:206
List * oid_array_to_list(Datum datum)
Definition: pg_proc.c:1173
Oid get_transform_oid(Oid type_id, Oid lang_id, bool missing_ok)
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3313
TupleDesc build_function_result_tupdesc_t(HeapTuple procTuple)
Definition: funcapi.c:1604
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
bool CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid)
Definition: fmgr.c:2038
#define ARR_DIMS(a)
Definition: array.h:287
List * pg_parse_query(const char *query_string)
Definition: postgres.c:576
Definition: guc.h:75
HeapTuple SearchSysCache3(int cacheId, Datum key1, Datum key2, Datum key3)
Definition: syscache.c:1149
ItemPointerData t_self
Definition: htup.h:65
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:664
#define lfirst_node(type, lc)
Definition: pg_list.h:172
Definition: c.h:675
void(* ParserSetupHook)(struct ParseState *pstate, void *arg)
Definition: params.h:108
#define ARR_DATA_PTR(a)
Definition: array.h:315
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:668
int dim1
Definition: c.h:666
#define RowExclusiveLock
Definition: lockdefs.h:38
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:6235
int errdetail(const char *fmt,...)
Definition: elog.c:1042
void recordDependencyOnExpr(const ObjectAddress *depender, Node *expr, List *rtable, DependencyType behavior)
Definition: dependency.c:1689
Datum fmgr_sql_validator(PG_FUNCTION_ARGS)
Definition: pg_proc.c:822
int geterrposition(void)
Definition: elog.c:1417
static bool match_prosrc_to_literal(const char *prosrc, const char *literal, int cursorpos, int *newcursorpos)
Definition: pg_proc.c:1116
#define ARR_HASNULL(a)
Definition: array.h:284
int getinternalerrposition(void)
Definition: elog.c:1434
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:107
List * lappend(List *list, void *datum)
Definition: list.c:336
TypeFuncClass get_func_result_type(Oid functionId, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:300
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
float float4
Definition: c.h:564
#define TextDatumGetCString(d)
Definition: builtins.h:83
uintptr_t Datum
Definition: postgres.h:411
void CommandCounterIncrement(void)
Definition: xact.c:1021
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
const char * sourceText
Definition: portal.h:135
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
#define BoolGetDatum(X)
Definition: postgres.h:446
#define InvalidOid
Definition: postgres_ext.h:36
void ProcessGUCArray(ArrayType *array, GucContext context, GucSource source, GucAction action)
Definition: guc.c:11034
static void sql_function_parse_error_callback(void *arg)
Definition: pg_proc.c:974
#define ereport(elevel,...)
Definition: elog.h:157
int internalerrquery(const char *query)
Definition: elog.c:1321
char * format_procedure(Oid procedure_oid)
Definition: regproc.c:322
void check_sql_fn_statements(List *queryTreeLists)
Definition: functions.c:1524
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
char * check_valid_internal_signature(Oid ret_type, const Oid *declared_arg_types, int nargs)
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:221
bool function_parse_error_transpose(const char *prosrc)
Definition: pg_proc.c:998
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:149
PortalStatus status
Definition: portal.h:150
int pg_mblen(const char *mbstr)
Definition: mbutils.c:966
List * pg_analyze_and_rewrite_params(RawStmt *parsetree, const char *query_string, ParserSetupHook parserSetup, void *parserSetupArg, QueryEnvironment *queryEnv)
Definition: postgres.c:661
#define ARR_NDIM(a)
Definition: array.h:283
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
const Pg_finfo_record * fetch_finfo_record(void *filehandle, const char *funcname)
Definition: fmgr.c:472
#define CharGetDatum(X)
Definition: postgres.h:460
#define DatumGetPointer(X)
Definition: postgres.h:593
void recordDependencyOnNewAcl(Oid classId, Oid objectId, int32 objsubId, Oid ownerId, Acl *acl)
Definition: aclchk.c:5602
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3488
static Datum values[MAXATTR]
Definition: bootstrap.c:166
int NewGUCNestLevel(void)
Definition: guc.c:6221
Datum fmgr_internal_validator(PG_FUNCTION_ARGS)
Definition: pg_proc.c:728
#define ProcedureOidIndexId
Definition: pg_proc.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
int i
#define errcontext
Definition: elog.h:204
#define NameStr(name)
Definition: c.h:681
#define CStringGetTextDatum(s)
Definition: builtins.h:82
char * nodeToString(const void *obj)
Definition: outfuncs.c:4526
bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
Definition: tupdesc.c:402
void * arg
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
SQLFunctionParseInfoPtr prepare_sql_fn_parse_info(HeapTuple procedureTuple, Node *call_expr, Oid inputCollation)
Definition: functions.c:176
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
ObjectAddress ProcedureCreate(const char *procedureName, Oid procNamespace, bool replace, bool returnsSet, Oid returnType, Oid proowner, Oid languageObjectId, Oid languageValidator, const char *prosrc, const char *probin, Node *prosqlbody, char prokind, bool security_definer, bool isLeakProof, bool isStrict, char volatility, char parallel, oidvector *parameterTypes, Datum allParameterTypes, Datum parameterModes, Datum parameterNames, List *parameterDefaults, Datum trftypes, Datum proconfig, Oid prosupport, float4 procost, float4 prorows)
Definition: pg_proc.c:70
Definition: pg_list.h:50
#define PointerIsValid(pointer)
Definition: c.h:698
#define ARR_ELEMTYPE(a)
Definition: array.h:285
#define UInt16GetDatum(X)
Definition: postgres.h:509
static int match_prosrc_to_query(const char *prosrc, const char *queryText, int cursorpos)
Definition: pg_proc.c:1058
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:221
bool pg_proc_ownercheck(Oid proc_oid, Oid roleid)
Definition: aclchk.c:4901
int get_func_input_arg_names(char prokind, Datum proargnames, Datum proargmodes, char ***arg_names)
Definition: funcapi.c:1412
int errposition(int cursorpos)
Definition: elog.c:1285
Acl * get_user_default_acl(ObjectType objtype, Oid ownerId, Oid nsp_oid)
Definition: aclchk.c:5526
int internalerrposition(int cursorpos)
Definition: elog.c:1301
#define DatumGetArrayTypeP(X)
Definition: array.h:254