PostgreSQL Source Code  git master
pg_proc.h File Reference
#include "catalog/genbki.h"
#include "catalog/pg_proc_d.h"
#include "catalog/objectaddress.h"
#include "nodes/pg_list.h"
Include dependency graph for pg_proc.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef FormData_pg_procForm_pg_proc
 

Functions

 CATALOG (pg_proc, 1255, ProcedureRelationId) BKI_BOOTSTRAP BKI_ROWTYPE_OID(81
 
Oid pronamespace BKI_DEFAULT (PGNSP)
 
Oid proowner BKI_DEFAULT (PGUID)
 
Oid prolang BKI_DEFAULT (internal) BKI_LOOKUP(pg_language)
 
float4 procost BKI_DEFAULT (1)
 
float4 prorows BKI_DEFAULT (0)
 
char prokind BKI_DEFAULT (f)
 
bool proisstrict BKI_DEFAULT (t)
 
char provolatile BKI_DEFAULT (i)
 
char proparallel BKI_DEFAULT (s)
 
Oid prorettype BKI_LOOKUP (pg_type)
 
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, 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)
 
bool function_parse_error_transpose (const char *prosrc)
 
Listoid_array_to_list (Datum datum)
 

Variables

ProcedureRelation_Rowtype_Id BKI_SCHEMA_MACRO
 
NameData proname
 
int16 pronargs
 
 FormData_pg_proc
 

Typedef Documentation

◆ Form_pg_proc

Definition at line 134 of file pg_proc.h.

Function Documentation

◆ BKI_DEFAULT() [1/9]

Oid pronamespace BKI_DEFAULT ( PGNSP  )

◆ BKI_DEFAULT() [2/9]

Oid proowner BKI_DEFAULT ( PGUID  )

◆ BKI_DEFAULT() [3/9]

Oid prolang BKI_DEFAULT ( internal  )

◆ BKI_DEFAULT() [4/9]

float4 procost BKI_DEFAULT ( )

◆ BKI_DEFAULT() [5/9]

float4 prorows BKI_DEFAULT ( )

◆ BKI_DEFAULT() [6/9]

char prokind BKI_DEFAULT ( )

◆ BKI_DEFAULT() [7/9]

bool proisstrict BKI_DEFAULT ( )

◆ BKI_DEFAULT() [8/9]

char provolatile BKI_DEFAULT ( i  )

◆ BKI_DEFAULT() [9/9]

char proparallel BKI_DEFAULT ( )

◆ BKI_LOOKUP()

Oid prorettype BKI_LOOKUP ( pg_type  )

◆ CATALOG()

CATALOG ( pg_proc  ,
1255  ,
ProcedureRelationId   
)

◆ function_parse_error_transpose()

bool function_parse_error_transpose ( const char *  prosrc)

Definition at line 987 of file pg_proc.c.

References ActivePortal, Assert, errposition(), geterrposition(), getinternalerrposition(), internalerrposition(), internalerrquery(), match_prosrc_to_query(), PORTAL_ACTIVE, PortalData::sourceText, and PortalData::status.

Referenced by plpgsql_compile_error_callback(), and sql_function_parse_error_callback().

988 {
989  int origerrposition;
990  int newerrposition;
991  const char *queryText;
992 
993  /*
994  * Nothing to do unless we are dealing with a syntax error that has a
995  * cursor position.
996  *
997  * Some PLs may prefer to report the error position as an internal error
998  * to begin with, so check that too.
999  */
1000  origerrposition = geterrposition();
1001  if (origerrposition <= 0)
1002  {
1003  origerrposition = getinternalerrposition();
1004  if (origerrposition <= 0)
1005  return false;
1006  }
1007 
1008  /* We can get the original query text from the active portal (hack...) */
1010  queryText = ActivePortal->sourceText;
1011 
1012  /* Try to locate the prosrc in the original text */
1013  newerrposition = match_prosrc_to_query(prosrc, queryText, origerrposition);
1014 
1015  if (newerrposition > 0)
1016  {
1017  /* Successful, so fix error position to reference original query */
1018  errposition(newerrposition);
1019  /* Get rid of any report of the error as an "internal query" */
1021  internalerrquery(NULL);
1022  }
1023  else
1024  {
1025  /*
1026  * If unsuccessful, convert the position to an internal position
1027  * marker and give the function text as the internal query.
1028  */
1029  errposition(0);
1030  internalerrposition(origerrposition);
1031  internalerrquery(prosrc);
1032  }
1033 
1034  return true;
1035 }
Portal ActivePortal
Definition: pquery.c:35
int geterrposition(void)
Definition: elog.c:1244
int getinternalerrposition(void)
Definition: elog.c:1261
const char * sourceText
Definition: portal.h:134
int internalerrquery(const char *query)
Definition: elog.c:1148
#define Assert(condition)
Definition: c.h:732
PortalStatus status
Definition: portal.h:148
static int match_prosrc_to_query(const char *prosrc, const char *queryText, int cursorpos)
Definition: pg_proc.c:1044
int errposition(int cursorpos)
Definition: elog.c:1112
int internalerrposition(int cursorpos)
Definition: elog.c:1128

◆ oid_array_to_list()

List* oid_array_to_list ( Datum  datum)

Definition at line 1159 of file pg_proc.c.

References DatumGetArrayTypeP, deconstruct_array(), i, lappend_oid(), NIL, and values.

Referenced by compile_plperl_function(), and PLy_procedure_create().

1160 {
1161  ArrayType *array = DatumGetArrayTypeP(datum);
1162  Datum *values;
1163  int nelems;
1164  int i;
1165  List *result = NIL;
1166 
1167  deconstruct_array(array,
1168  OIDOID,
1169  sizeof(Oid), true, 'i',
1170  &values, NULL, &nelems);
1171  for (i = 0; i < nelems; i++)
1172  result = lappend_oid(result, values[i]);
1173  return result;
1174 }
#define NIL
Definition: pg_list.h:65
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:357
uintptr_t Datum
Definition: postgres.h:367
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3461
static Datum values[MAXATTR]
Definition: bootstrap.c:167
int i
Definition: pg_list.h:50
#define DatumGetArrayTypeP(X)
Definition: array.h:249

◆ ProcedureCreate()

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,
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 at line 68 of file pg_proc.c.

References aclcheck_error(), ACLCHECK_NOT_OWNER, ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_NDIM, Assert, AtEOXact_GUC(), BoolGetDatum, build_function_result_tupdesc_d(), build_function_result_tupdesc_t(), castNode, CatalogTupleInsert(), CatalogTupleUpdate(), CharGetDatum, check_function_bodies, ObjectAddress::classId, CommandCounterIncrement(), CStringGetTextDatum, DatumGetPointer, deleteDependencyRecordsFor(), DEPENDENCY_NORMAL, oidvector::dim1, elog, equalTupleDescs(), ereport, errcode(), errdetail(), errhint(), errmsg(), errmsg_plural(), ERROR, exprType(), Float4GetDatum(), format_procedure(), FUNC_MAX_ARGS, get_element_type(), get_func_input_arg_names(), get_transform_oid(), get_user_default_acl(), GetNewOidWithIndex(), GETSTRUCT, GUC_ACTION_SAVE, heap_form_tuple(), heap_freetuple(), heap_modify_tuple(), HeapTupleIsValid, i, InvalidOid, InvokeObjectPostCreateHook, lfirst, list_length(), list_nth_cell(), lnext(), NameGetDatum, namestrcpy(), NewGUCNestLevel(), NIL, nodeToString(), OBJECT_FUNCTION, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, OidFunctionCall1, OidIsValid, pg_proc_ownercheck(), PGC_S_SESSION, PGC_SUSET, PGC_USERSET, PointerGetDatum, PointerIsValid, ProcedureOidIndexId, ProcessGUCArray(), PROCNAMEARGSNSP, recordDependencyOn(), recordDependencyOnCurrentExtension(), recordDependencyOnExpr(), recordDependencyOnNewAcl(), recordDependencyOnOwner(), RelationGetDescr, ReleaseSysCache(), RowExclusiveLock, SearchSysCache3(), stringToNode(), superuser(), SysCacheGetAttr(), HeapTupleData::t_self, table_close(), table_open(), TextDatumGetCString, UInt16GetDatum, values, and oidvector::values.

Referenced by AggregateCreate(), CreateFunction(), CreateProceduralLanguage(), and makeRangeConstructors().

94 {
95  Oid retval;
96  int parameterCount;
97  int allParamCount;
98  Oid *allParams;
99  char *paramModes = NULL;
100  bool genericInParam = false;
101  bool genericOutParam = false;
102  bool anyrangeInParam = false;
103  bool anyrangeOutParam = false;
104  bool internalInParam = false;
105  bool internalOutParam = false;
106  Oid variadicType = InvalidOid;
107  Acl *proacl = NULL;
108  Relation rel;
109  HeapTuple tup;
110  HeapTuple oldtup;
111  bool nulls[Natts_pg_proc];
112  Datum values[Natts_pg_proc];
113  bool replaces[Natts_pg_proc];
114  NameData procname;
115  TupleDesc tupDesc;
116  bool is_update;
117  ObjectAddress myself,
118  referenced;
119  int i;
120  Oid trfid;
121 
122  /*
123  * sanity checks
124  */
125  Assert(PointerIsValid(prosrc));
126 
127  parameterCount = parameterTypes->dim1;
128  if (parameterCount < 0 || parameterCount > FUNC_MAX_ARGS)
129  ereport(ERROR,
130  (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
131  errmsg_plural("functions cannot have more than %d argument",
132  "functions cannot have more than %d arguments",
133  FUNC_MAX_ARGS,
134  FUNC_MAX_ARGS)));
135  /* note: the above is correct, we do NOT count output arguments */
136 
137  /* Deconstruct array inputs */
138  if (allParameterTypes != PointerGetDatum(NULL))
139  {
140  /*
141  * We expect the array to be a 1-D OID array; verify that. We don't
142  * need to use deconstruct_array() since the array data is just going
143  * to look like a C array of OID values.
144  */
145  ArrayType *allParamArray = (ArrayType *) DatumGetPointer(allParameterTypes);
146 
147  allParamCount = ARR_DIMS(allParamArray)[0];
148  if (ARR_NDIM(allParamArray) != 1 ||
149  allParamCount <= 0 ||
150  ARR_HASNULL(allParamArray) ||
151  ARR_ELEMTYPE(allParamArray) != OIDOID)
152  elog(ERROR, "allParameterTypes is not a 1-D Oid array");
153  allParams = (Oid *) ARR_DATA_PTR(allParamArray);
154  Assert(allParamCount >= parameterCount);
155  /* we assume caller got the contents right */
156  }
157  else
158  {
159  allParamCount = parameterCount;
160  allParams = parameterTypes->values;
161  }
162 
163  if (parameterModes != PointerGetDatum(NULL))
164  {
165  /*
166  * We expect the array to be a 1-D CHAR array; verify that. We don't
167  * need to use deconstruct_array() since the array data is just going
168  * to look like a C array of char values.
169  */
170  ArrayType *modesArray = (ArrayType *) DatumGetPointer(parameterModes);
171 
172  if (ARR_NDIM(modesArray) != 1 ||
173  ARR_DIMS(modesArray)[0] != allParamCount ||
174  ARR_HASNULL(modesArray) ||
175  ARR_ELEMTYPE(modesArray) != CHAROID)
176  elog(ERROR, "parameterModes is not a 1-D char array");
177  paramModes = (char *) ARR_DATA_PTR(modesArray);
178  }
179 
180  /*
181  * Detect whether we have polymorphic or INTERNAL arguments. The first
182  * loop checks input arguments, the second output arguments.
183  */
184  for (i = 0; i < parameterCount; i++)
185  {
186  switch (parameterTypes->values[i])
187  {
188  case ANYARRAYOID:
189  case ANYELEMENTOID:
190  case ANYNONARRAYOID:
191  case ANYENUMOID:
192  genericInParam = true;
193  break;
194  case ANYRANGEOID:
195  genericInParam = true;
196  anyrangeInParam = true;
197  break;
198  case INTERNALOID:
199  internalInParam = true;
200  break;
201  }
202  }
203 
204  if (allParameterTypes != PointerGetDatum(NULL))
205  {
206  for (i = 0; i < allParamCount; i++)
207  {
208  if (paramModes == NULL ||
209  paramModes[i] == PROARGMODE_IN ||
210  paramModes[i] == PROARGMODE_VARIADIC)
211  continue; /* ignore input-only params */
212 
213  switch (allParams[i])
214  {
215  case ANYARRAYOID:
216  case ANYELEMENTOID:
217  case ANYNONARRAYOID:
218  case ANYENUMOID:
219  genericOutParam = true;
220  break;
221  case ANYRANGEOID:
222  genericOutParam = true;
223  anyrangeOutParam = true;
224  break;
225  case INTERNALOID:
226  internalOutParam = true;
227  break;
228  }
229  }
230  }
231 
232  /*
233  * Do not allow polymorphic return type unless at least one input argument
234  * is polymorphic. ANYRANGE return type is even stricter: must have an
235  * ANYRANGE input (since we can't deduce the specific range type from
236  * ANYELEMENT). Also, do not allow return type INTERNAL unless at least
237  * one input argument is INTERNAL.
238  */
239  if ((IsPolymorphicType(returnType) || genericOutParam)
240  && !genericInParam)
241  ereport(ERROR,
242  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
243  errmsg("cannot determine result data type"),
244  errdetail("A function returning a polymorphic type must have at least one polymorphic argument.")));
245 
246  if ((returnType == ANYRANGEOID || anyrangeOutParam) &&
247  !anyrangeInParam)
248  ereport(ERROR,
249  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
250  errmsg("cannot determine result data type"),
251  errdetail("A function returning \"anyrange\" must have at least one \"anyrange\" argument.")));
252 
253  if ((returnType == INTERNALOID || internalOutParam) && !internalInParam)
254  ereport(ERROR,
255  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
256  errmsg("unsafe use of pseudo-type \"internal\""),
257  errdetail("A function returning \"internal\" must have at least one \"internal\" argument.")));
258 
259  if (paramModes != NULL)
260  {
261  /*
262  * Only the last input parameter can be variadic; if it is, save its
263  * element type. Errors here are just elog since caller should have
264  * checked this already.
265  */
266  for (i = 0; i < allParamCount; i++)
267  {
268  switch (paramModes[i])
269  {
270  case PROARGMODE_IN:
271  case PROARGMODE_INOUT:
272  if (OidIsValid(variadicType))
273  elog(ERROR, "variadic parameter must be last");
274  break;
275  case PROARGMODE_OUT:
276  case PROARGMODE_TABLE:
277  /* okay */
278  break;
279  case PROARGMODE_VARIADIC:
280  if (OidIsValid(variadicType))
281  elog(ERROR, "variadic parameter must be last");
282  switch (allParams[i])
283  {
284  case ANYOID:
285  variadicType = ANYOID;
286  break;
287  case ANYARRAYOID:
288  variadicType = ANYELEMENTOID;
289  break;
290  default:
291  variadicType = get_element_type(allParams[i]);
292  if (!OidIsValid(variadicType))
293  elog(ERROR, "variadic parameter is not an array");
294  break;
295  }
296  break;
297  default:
298  elog(ERROR, "invalid parameter mode '%c'", paramModes[i]);
299  break;
300  }
301  }
302  }
303 
304  /*
305  * All seems OK; prepare the data to be inserted into pg_proc.
306  */
307 
308  for (i = 0; i < Natts_pg_proc; ++i)
309  {
310  nulls[i] = false;
311  values[i] = (Datum) 0;
312  replaces[i] = true;
313  }
314 
315  namestrcpy(&procname, procedureName);
316  values[Anum_pg_proc_proname - 1] = NameGetDatum(&procname);
317  values[Anum_pg_proc_pronamespace - 1] = ObjectIdGetDatum(procNamespace);
318  values[Anum_pg_proc_proowner - 1] = ObjectIdGetDatum(proowner);
319  values[Anum_pg_proc_prolang - 1] = ObjectIdGetDatum(languageObjectId);
320  values[Anum_pg_proc_procost - 1] = Float4GetDatum(procost);
321  values[Anum_pg_proc_prorows - 1] = Float4GetDatum(prorows);
322  values[Anum_pg_proc_provariadic - 1] = ObjectIdGetDatum(variadicType);
323  values[Anum_pg_proc_prosupport - 1] = ObjectIdGetDatum(prosupport);
324  values[Anum_pg_proc_prokind - 1] = CharGetDatum(prokind);
325  values[Anum_pg_proc_prosecdef - 1] = BoolGetDatum(security_definer);
326  values[Anum_pg_proc_proleakproof - 1] = BoolGetDatum(isLeakProof);
327  values[Anum_pg_proc_proisstrict - 1] = BoolGetDatum(isStrict);
328  values[Anum_pg_proc_proretset - 1] = BoolGetDatum(returnsSet);
329  values[Anum_pg_proc_provolatile - 1] = CharGetDatum(volatility);
330  values[Anum_pg_proc_proparallel - 1] = CharGetDatum(parallel);
331  values[Anum_pg_proc_pronargs - 1] = UInt16GetDatum(parameterCount);
332  values[Anum_pg_proc_pronargdefaults - 1] = UInt16GetDatum(list_length(parameterDefaults));
333  values[Anum_pg_proc_prorettype - 1] = ObjectIdGetDatum(returnType);
334  values[Anum_pg_proc_proargtypes - 1] = PointerGetDatum(parameterTypes);
335  if (allParameterTypes != PointerGetDatum(NULL))
336  values[Anum_pg_proc_proallargtypes - 1] = allParameterTypes;
337  else
338  nulls[Anum_pg_proc_proallargtypes - 1] = true;
339  if (parameterModes != PointerGetDatum(NULL))
340  values[Anum_pg_proc_proargmodes - 1] = parameterModes;
341  else
342  nulls[Anum_pg_proc_proargmodes - 1] = true;
343  if (parameterNames != PointerGetDatum(NULL))
344  values[Anum_pg_proc_proargnames - 1] = parameterNames;
345  else
346  nulls[Anum_pg_proc_proargnames - 1] = true;
347  if (parameterDefaults != NIL)
348  values[Anum_pg_proc_proargdefaults - 1] = CStringGetTextDatum(nodeToString(parameterDefaults));
349  else
350  nulls[Anum_pg_proc_proargdefaults - 1] = true;
351  if (trftypes != PointerGetDatum(NULL))
352  values[Anum_pg_proc_protrftypes - 1] = trftypes;
353  else
354  nulls[Anum_pg_proc_protrftypes - 1] = true;
355  values[Anum_pg_proc_prosrc - 1] = CStringGetTextDatum(prosrc);
356  if (probin)
357  values[Anum_pg_proc_probin - 1] = CStringGetTextDatum(probin);
358  else
359  nulls[Anum_pg_proc_probin - 1] = true;
360  if (proconfig != PointerGetDatum(NULL))
361  values[Anum_pg_proc_proconfig - 1] = proconfig;
362  else
363  nulls[Anum_pg_proc_proconfig - 1] = true;
364  /* proacl will be determined later */
365 
366  rel = table_open(ProcedureRelationId, RowExclusiveLock);
367  tupDesc = RelationGetDescr(rel);
368 
369  /* Check for pre-existing definition */
371  PointerGetDatum(procedureName),
372  PointerGetDatum(parameterTypes),
373  ObjectIdGetDatum(procNamespace));
374 
375  if (HeapTupleIsValid(oldtup))
376  {
377  /* There is one; okay to replace it? */
378  Form_pg_proc oldproc = (Form_pg_proc) GETSTRUCT(oldtup);
379  Datum proargnames;
380  bool isnull;
381  const char *dropcmd;
382 
383  if (!replace)
384  ereport(ERROR,
385  (errcode(ERRCODE_DUPLICATE_FUNCTION),
386  errmsg("function \"%s\" already exists with same argument types",
387  procedureName)));
388  if (!pg_proc_ownercheck(oldproc->oid, proowner))
390  procedureName);
391 
392  /* Not okay to change routine kind */
393  if (oldproc->prokind != prokind)
394  ereport(ERROR,
395  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
396  errmsg("cannot change routine kind"),
397  (oldproc->prokind == PROKIND_AGGREGATE ?
398  errdetail("\"%s\" is an aggregate function.", procedureName) :
399  oldproc->prokind == PROKIND_FUNCTION ?
400  errdetail("\"%s\" is a function.", procedureName) :
401  oldproc->prokind == PROKIND_PROCEDURE ?
402  errdetail("\"%s\" is a procedure.", procedureName) :
403  oldproc->prokind == PROKIND_WINDOW ?
404  errdetail("\"%s\" is a window function.", procedureName) :
405  0)));
406 
407  dropcmd = (prokind == PROKIND_PROCEDURE ? "DROP PROCEDURE" :
408  prokind == PROKIND_AGGREGATE ? "DROP AGGREGATE" :
409  "DROP FUNCTION");
410 
411  /*
412  * Not okay to change the return type of the existing proc, since
413  * existing rules, views, etc may depend on the return type.
414  *
415  * In case of a procedure, a changing return type means that whether
416  * the procedure has output parameters was changed. Since there is no
417  * user visible return type, we produce a more specific error message.
418  */
419  if (returnType != oldproc->prorettype ||
420  returnsSet != oldproc->proretset)
421  ereport(ERROR,
422  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
423  prokind == PROKIND_PROCEDURE
424  ? errmsg("cannot change whether a procedure has output parameters")
425  : errmsg("cannot change return type of existing function"),
426 
427  /*
428  * translator: first %s is DROP FUNCTION, DROP PROCEDURE, or DROP
429  * AGGREGATE
430  */
431  errhint("Use %s %s first.",
432  dropcmd,
433  format_procedure(oldproc->oid))));
434 
435  /*
436  * If it returns RECORD, check for possible change of record type
437  * implied by OUT parameters
438  */
439  if (returnType == RECORDOID)
440  {
441  TupleDesc olddesc;
442  TupleDesc newdesc;
443 
444  olddesc = build_function_result_tupdesc_t(oldtup);
445  newdesc = build_function_result_tupdesc_d(prokind,
446  allParameterTypes,
447  parameterModes,
448  parameterNames);
449  if (olddesc == NULL && newdesc == NULL)
450  /* ok, both are runtime-defined RECORDs */ ;
451  else if (olddesc == NULL || newdesc == NULL ||
452  !equalTupleDescs(olddesc, newdesc))
453  ereport(ERROR,
454  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
455  errmsg("cannot change return type of existing function"),
456  errdetail("Row type defined by OUT parameters is different."),
457  /* translator: first %s is DROP FUNCTION or DROP PROCEDURE */
458  errhint("Use %s %s first.",
459  dropcmd,
460  format_procedure(oldproc->oid))));
461  }
462 
463  /*
464  * If there were any named input parameters, check to make sure the
465  * names have not been changed, as this could break existing calls. We
466  * allow adding names to formerly unnamed parameters, though.
467  */
468  proargnames = SysCacheGetAttr(PROCNAMEARGSNSP, oldtup,
469  Anum_pg_proc_proargnames,
470  &isnull);
471  if (!isnull)
472  {
473  Datum proargmodes;
474  char **old_arg_names;
475  char **new_arg_names;
476  int n_old_arg_names;
477  int n_new_arg_names;
478  int j;
479 
480  proargmodes = SysCacheGetAttr(PROCNAMEARGSNSP, oldtup,
481  Anum_pg_proc_proargmodes,
482  &isnull);
483  if (isnull)
484  proargmodes = PointerGetDatum(NULL); /* just to be sure */
485 
486  n_old_arg_names = get_func_input_arg_names(proargnames,
487  proargmodes,
488  &old_arg_names);
489  n_new_arg_names = get_func_input_arg_names(parameterNames,
490  parameterModes,
491  &new_arg_names);
492  for (j = 0; j < n_old_arg_names; j++)
493  {
494  if (old_arg_names[j] == NULL)
495  continue;
496  if (j >= n_new_arg_names || new_arg_names[j] == NULL ||
497  strcmp(old_arg_names[j], new_arg_names[j]) != 0)
498  ereport(ERROR,
499  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
500  errmsg("cannot change name of input parameter \"%s\"",
501  old_arg_names[j]),
502  /* translator: first %s is DROP FUNCTION or DROP PROCEDURE */
503  errhint("Use %s %s first.",
504  dropcmd,
505  format_procedure(oldproc->oid))));
506  }
507  }
508 
509  /*
510  * If there are existing defaults, check compatibility: redefinition
511  * must not remove any defaults nor change their types. (Removing a
512  * default might cause a function to fail to satisfy an existing call.
513  * Changing type would only be possible if the associated parameter is
514  * polymorphic, and in such cases a change of default type might alter
515  * the resolved output type of existing calls.)
516  */
517  if (oldproc->pronargdefaults != 0)
518  {
519  Datum proargdefaults;
520  List *oldDefaults;
521  ListCell *oldlc;
522  ListCell *newlc;
523 
524  if (list_length(parameterDefaults) < oldproc->pronargdefaults)
525  ereport(ERROR,
526  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
527  errmsg("cannot remove parameter defaults from existing function"),
528  /* translator: first %s is DROP FUNCTION or DROP PROCEDURE */
529  errhint("Use %s %s first.",
530  dropcmd,
531  format_procedure(oldproc->oid))));
532 
533  proargdefaults = SysCacheGetAttr(PROCNAMEARGSNSP, oldtup,
534  Anum_pg_proc_proargdefaults,
535  &isnull);
536  Assert(!isnull);
537  oldDefaults = castNode(List, stringToNode(TextDatumGetCString(proargdefaults)));
538  Assert(list_length(oldDefaults) == oldproc->pronargdefaults);
539 
540  /* new list can have more defaults than old, advance over 'em */
541  newlc = list_nth_cell(parameterDefaults,
542  list_length(parameterDefaults) -
543  oldproc->pronargdefaults);
544 
545  foreach(oldlc, oldDefaults)
546  {
547  Node *oldDef = (Node *) lfirst(oldlc);
548  Node *newDef = (Node *) lfirst(newlc);
549 
550  if (exprType(oldDef) != exprType(newDef))
551  ereport(ERROR,
552  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
553  errmsg("cannot change data type of existing parameter default value"),
554  /* translator: first %s is DROP FUNCTION or DROP PROCEDURE */
555  errhint("Use %s %s first.",
556  dropcmd,
557  format_procedure(oldproc->oid))));
558  newlc = lnext(parameterDefaults, newlc);
559  }
560  }
561 
562  /*
563  * Do not change existing oid, ownership or permissions, either. Note
564  * dependency-update code below has to agree with this decision.
565  */
566  replaces[Anum_pg_proc_oid - 1] = false;
567  replaces[Anum_pg_proc_proowner - 1] = false;
568  replaces[Anum_pg_proc_proacl - 1] = false;
569 
570  /* Okay, do it... */
571  tup = heap_modify_tuple(oldtup, tupDesc, values, nulls, replaces);
572  CatalogTupleUpdate(rel, &tup->t_self, tup);
573 
574  ReleaseSysCache(oldtup);
575  is_update = true;
576  }
577  else
578  {
579  /* Creating a new procedure */
580  Oid newOid;
581 
582  /* First, get default permissions and set up proacl */
583  proacl = get_user_default_acl(OBJECT_FUNCTION, proowner,
584  procNamespace);
585  if (proacl != NULL)
586  values[Anum_pg_proc_proacl - 1] = PointerGetDatum(proacl);
587  else
588  nulls[Anum_pg_proc_proacl - 1] = true;
589 
591  Anum_pg_proc_oid);
592  values[Anum_pg_proc_oid - 1] = ObjectIdGetDatum(newOid);
593  tup = heap_form_tuple(tupDesc, values, nulls);
594  CatalogTupleInsert(rel, tup);
595  is_update = false;
596  }
597 
598 
599  retval = ((Form_pg_proc) GETSTRUCT(tup))->oid;
600 
601  /*
602  * Create dependencies for the new function. If we are updating an
603  * existing function, first delete any existing pg_depend entries.
604  * (However, since we are not changing ownership or permissions, the
605  * shared dependencies do *not* need to change, and we leave them alone.)
606  */
607  if (is_update)
608  deleteDependencyRecordsFor(ProcedureRelationId, retval, true);
609 
610  myself.classId = ProcedureRelationId;
611  myself.objectId = retval;
612  myself.objectSubId = 0;
613 
614  /* dependency on namespace */
615  referenced.classId = NamespaceRelationId;
616  referenced.objectId = procNamespace;
617  referenced.objectSubId = 0;
618  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
619 
620  /* dependency on implementation language */
621  referenced.classId = LanguageRelationId;
622  referenced.objectId = languageObjectId;
623  referenced.objectSubId = 0;
624  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
625 
626  /* dependency on return type */
627  referenced.classId = TypeRelationId;
628  referenced.objectId = returnType;
629  referenced.objectSubId = 0;
630  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
631 
632  /* dependency on transform used by return type, if any */
633  if ((trfid = get_transform_oid(returnType, languageObjectId, true)))
634  {
635  referenced.classId = TransformRelationId;
636  referenced.objectId = trfid;
637  referenced.objectSubId = 0;
638  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
639  }
640 
641  /* dependency on parameter types */
642  for (i = 0; i < allParamCount; i++)
643  {
644  referenced.classId = TypeRelationId;
645  referenced.objectId = allParams[i];
646  referenced.objectSubId = 0;
647  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
648 
649  /* dependency on transform used by parameter type, if any */
650  if ((trfid = get_transform_oid(allParams[i], languageObjectId, true)))
651  {
652  referenced.classId = TransformRelationId;
653  referenced.objectId = trfid;
654  referenced.objectSubId = 0;
655  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
656  }
657  }
658 
659  /* dependency on parameter default expressions */
660  if (parameterDefaults)
661  recordDependencyOnExpr(&myself, (Node *) parameterDefaults,
663 
664  /* dependency on support function, if any */
665  if (OidIsValid(prosupport))
666  {
667  referenced.classId = ProcedureRelationId;
668  referenced.objectId = prosupport;
669  referenced.objectSubId = 0;
670  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
671  }
672 
673  /* dependency on owner */
674  if (!is_update)
675  recordDependencyOnOwner(ProcedureRelationId, retval, proowner);
676 
677  /* dependency on any roles mentioned in ACL */
678  if (!is_update)
679  recordDependencyOnNewAcl(ProcedureRelationId, retval, 0,
680  proowner, proacl);
681 
682  /* dependency on extension */
683  recordDependencyOnCurrentExtension(&myself, is_update);
684 
685  heap_freetuple(tup);
686 
687  /* Post creation hook for new function */
688  InvokeObjectPostCreateHook(ProcedureRelationId, retval, 0);
689 
691 
692  /* Verify function body */
693  if (OidIsValid(languageValidator))
694  {
695  ArrayType *set_items = NULL;
696  int save_nestlevel = 0;
697 
698  /* Advance command counter so new tuple can be seen by validator */
700 
701  /*
702  * Set per-function configuration parameters so that the validation is
703  * done with the environment the function expects. However, if
704  * check_function_bodies is off, we don't do this, because that would
705  * create dump ordering hazards that pg_dump doesn't know how to deal
706  * with. (For example, a SET clause might refer to a not-yet-created
707  * text search configuration.) This means that the validator
708  * shouldn't complain about anything that might depend on a GUC
709  * parameter when check_function_bodies is off.
710  */
712  {
713  set_items = (ArrayType *) DatumGetPointer(proconfig);
714  if (set_items) /* Need a new GUC nesting level */
715  {
716  save_nestlevel = NewGUCNestLevel();
717  ProcessGUCArray(set_items,
721  }
722  }
723 
724  OidFunctionCall1(languageValidator, ObjectIdGetDatum(retval));
725 
726  if (set_items)
727  AtEOXact_GUC(true, save_nestlevel);
728  }
729 
730  return myself;
731 }
#define NIL
Definition: pg_list.h:65
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:323
#define ProcedureOidIndexId
Definition: indexing.h:213
#define NameGetDatum(X)
Definition: postgres.h:595
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
int errhint(const char *fmt,...)
Definition: elog.c:974
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:145
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:321
static ListCell * list_nth_cell(const List *list, int n)
Definition: pg_list.h:265
#define RelationGetDescr(relation)
Definition: rel.h:442
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:837
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2526
#define PointerGetDatum(X)
Definition: postgres.h:556
TupleDesc build_function_result_tupdesc_d(char prokind, Datum proallargtypes, Datum proargmodes, Datum proargnames)
Definition: funcapi.c:1211
long deleteDependencyRecordsFor(Oid classId, Oid objectId, bool skipExtensionDeps)
Definition: pg_depend.c:190
Definition: nodes.h:525
int errcode(int sqlerrcode)
Definition: elog.c:570
void * stringToNode(const char *str)
Definition: read.c:89
bool superuser(void)
Definition: superuser.c:47
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:43
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
bool check_function_bodies
Definition: guc.c:500
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
int namestrcpy(Name name, const char *str)
Definition: name.c:250
#define OidIsValid(objectId)
Definition: c.h:638
#define FUNC_MAX_ARGS
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:3353
TupleDesc build_function_result_tupdesc_t(HeapTuple procTuple)
Definition: funcapi.c:1161
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:282
Definition: guc.h:75
HeapTuple SearchSysCache3(int cacheId, Datum key1, Datum key2, Datum key3)
Definition: syscache.c:1146
Datum Float4GetDatum(float4 X)
Definition: fmgr.c:1711
ItemPointerData t_self
Definition: htup.h:65
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:655
Definition: c.h:603
#define ARR_DATA_PTR(a)
Definition: array.h:310
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:596
int dim1
Definition: c.h:594
#define RowExclusiveLock
Definition: lockdefs.h:38
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5729
int errdetail(const char *fmt,...)
Definition: elog.c:860
void recordDependencyOnExpr(const ObjectAddress *depender, Node *expr, List *rtable, DependencyType behavior)
Definition: dependency.c:1586
#define ARR_HASNULL(a)
Definition: array.h:279
#define ereport(elevel, rest)
Definition: elog.h:141
int get_func_input_arg_names(Datum proargnames, Datum proargmodes, char ***arg_names)
Definition: funcapi.c:973
#define TextDatumGetCString(d)
Definition: builtins.h:84
uintptr_t Datum
Definition: postgres.h:367
void CommandCounterIncrement(void)
Definition: xact.c:1003
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1385
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:134
#define BoolGetDatum(X)
Definition: postgres.h:402
#define InvalidOid
Definition: postgres_ext.h:36
void ProcessGUCArray(ArrayType *array, GucContext context, GucSource source, GucAction action)
Definition: guc.c:10316
char * format_procedure(Oid procedure_oid)
Definition: regproc.c:323
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:138
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:224
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:169
#define ARR_NDIM(a)
Definition: array.h:278
#define CharGetDatum(X)
Definition: postgres.h:416
#define DatumGetPointer(X)
Definition: postgres.h:549
void recordDependencyOnNewAcl(Oid classId, Oid objectId, int32 objsubId, Oid ownerId, Acl *acl)
Definition: aclchk.c:5534
static Datum values[MAXATTR]
Definition: bootstrap.c:167
int NewGUCNestLevel(void)
Definition: guc.c:5715
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
int i
#define CStringGetTextDatum(s)
Definition: builtins.h:83
char * nodeToString(const void *obj)
Definition: outfuncs.c:4316
bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
Definition: tupdesc.c:411
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
Definition: pg_list.h:50
#define PointerIsValid(pointer)
Definition: c.h:626
#define ARR_ELEMTYPE(a)
Definition: array.h:280
#define UInt16GetDatum(X)
Definition: postgres.h:465
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:183
bool pg_proc_ownercheck(Oid proc_oid, Oid roleid)
Definition: aclchk.c:4833
Acl * get_user_default_acl(ObjectType objtype, Oid ownerId, Oid nsp_oid)
Definition: aclchk.c:5458

Variable Documentation

◆ BKI_SCHEMA_MACRO

ProcedureRelation_Rowtype_Id BKI_SCHEMA_MACRO
Initial value:
{
Oid oid
unsigned int Oid
Definition: postgres_ext.h:31

Definition at line 32 of file pg_proc.h.

◆ FormData_pg_proc

FormData_pg_proc

Definition at line 127 of file pg_proc.h.

◆ proname

◆ pronargs