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 (12)
 
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, 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
 
int16 pronargs
 
 FormData_pg_proc
 

Typedef Documentation

◆ Form_pg_proc

Definition at line 132 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 ( 12  )

◆ 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()

oidvector proargtypes 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 955 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().

956 {
957  int origerrposition;
958  int newerrposition;
959  const char *queryText;
960 
961  /*
962  * Nothing to do unless we are dealing with a syntax error that has a
963  * cursor position.
964  *
965  * Some PLs may prefer to report the error position as an internal error
966  * to begin with, so check that too.
967  */
968  origerrposition = geterrposition();
969  if (origerrposition <= 0)
970  {
971  origerrposition = getinternalerrposition();
972  if (origerrposition <= 0)
973  return false;
974  }
975 
976  /* We can get the original query text from the active portal (hack...) */
978  queryText = ActivePortal->sourceText;
979 
980  /* Try to locate the prosrc in the original text */
981  newerrposition = match_prosrc_to_query(prosrc, queryText, origerrposition);
982 
983  if (newerrposition > 0)
984  {
985  /* Successful, so fix error position to reference original query */
986  errposition(newerrposition);
987  /* Get rid of any report of the error as an "internal query" */
989  internalerrquery(NULL);
990  }
991  else
992  {
993  /*
994  * If unsuccessful, convert the position to an internal position
995  * marker and give the function text as the internal query.
996  */
997  errposition(0);
998  internalerrposition(origerrposition);
999  internalerrquery(prosrc);
1000  }
1001 
1002  return true;
1003 }
Portal ActivePortal
Definition: pquery.c:35
int geterrposition(void)
Definition: elog.c:1257
int getinternalerrposition(void)
Definition: elog.c:1274
const char * sourceText
Definition: portal.h:134
int internalerrquery(const char *query)
Definition: elog.c:1161
#define Assert(condition)
Definition: c.h:699
PortalStatus status
Definition: portal.h:148
static int match_prosrc_to_query(const char *prosrc, const char *queryText, int cursorpos)
Definition: pg_proc.c:1012
int errposition(int cursorpos)
Definition: elog.c:1125
int internalerrposition(int cursorpos)
Definition: elog.c:1141

◆ oid_array_to_list()

List* oid_array_to_list ( Datum  datum)

Definition at line 1127 of file pg_proc.c.

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

Referenced by compile_plperl_function(), and PLy_procedure_create().

1128 {
1129  ArrayType *array = DatumGetArrayTypeP(datum);
1130  Datum *values;
1131  int nelems;
1132  int i;
1133  List *result = NIL;
1134 
1135  deconstruct_array(array,
1136  OIDOID,
1137  sizeof(Oid), true, 'i',
1138  &values, NULL, &nelems);
1139  for (i = 0; i < nelems; i++)
1140  result = lappend_oid(result, values[i]);
1141  return result;
1142 }
#define NIL
Definition: pg_list.h:69
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
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:3449
static Datum values[MAXATTR]
Definition: bootstrap.c:164
int i
Definition: pg_list.h:45
#define DatumGetArrayTypeP(X)
Definition: array.h:246

◆ 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,
float4  procost,
float4  prorows 
)

Definition at line 66 of file pg_proc.c.

References aclcheck_error(), ACLCHECK_NOT_OWNER, aclmembers(), 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(), GETSTRUCT, GUC_ACTION_SAVE, heap_close, heap_form_tuple(), heap_freetuple(), heap_modify_tuple(), heap_open(), HeapTupleGetOid, HeapTupleIsValid, i, InvalidOid, InvokeObjectPostCreateHook, lfirst, list_head(), list_length(), 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, ProcessGUCArray(), PROCNAMEARGSNSP, recordDependencyOn(), recordDependencyOnCurrentExtension(), recordDependencyOnExpr(), recordDependencyOnOwner(), RelationGetDescr, ReleaseSysCache(), RowExclusiveLock, SearchSysCache3(), stringToNode(), superuser(), SysCacheGetAttr(), HeapTupleData::t_self, TextDatumGetCString, UInt16GetDatum, updateAclDependencies(), values, and oidvector::values.

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

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

Variable Documentation

◆ BKI_SCHEMA_MACRO

ProcedureRelation_Rowtype_Id BKI_SCHEMA_MACRO
Initial value:
{
NameData proname
Definition: c.h:570

Definition at line 32 of file pg_proc.h.

◆ FormData_pg_proc

FormData_pg_proc

Definition at line 125 of file pg_proc.h.

◆ pronargs