PostgreSQL Source Code  git master
pg_proc.h File Reference
#include "catalog/genbki.h"
#include "catalog/objectaddress.h"
#include "catalog/pg_proc_d.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 133 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 970 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().

971 {
972  int origerrposition;
973  int newerrposition;
974  const char *queryText;
975 
976  /*
977  * Nothing to do unless we are dealing with a syntax error that has a
978  * cursor position.
979  *
980  * Some PLs may prefer to report the error position as an internal error
981  * to begin with, so check that too.
982  */
983  origerrposition = geterrposition();
984  if (origerrposition <= 0)
985  {
986  origerrposition = getinternalerrposition();
987  if (origerrposition <= 0)
988  return false;
989  }
990 
991  /* We can get the original query text from the active portal (hack...) */
993  queryText = ActivePortal->sourceText;
994 
995  /* Try to locate the prosrc in the original text */
996  newerrposition = match_prosrc_to_query(prosrc, queryText, origerrposition);
997 
998  if (newerrposition > 0)
999  {
1000  /* Successful, so fix error position to reference original query */
1001  errposition(newerrposition);
1002  /* Get rid of any report of the error as an "internal query" */
1004  internalerrquery(NULL);
1005  }
1006  else
1007  {
1008  /*
1009  * If unsuccessful, convert the position to an internal position
1010  * marker and give the function text as the internal query.
1011  */
1012  errposition(0);
1013  internalerrposition(origerrposition);
1014  internalerrquery(prosrc);
1015  }
1016 
1017  return true;
1018 }
Portal ActivePortal
Definition: pquery.c:35
int geterrposition(void)
Definition: elog.c:1331
int getinternalerrposition(void)
Definition: elog.c:1348
const char * sourceText
Definition: portal.h:135
int internalerrquery(const char *query)
Definition: elog.c:1235
#define Assert(condition)
Definition: c.h:738
PortalStatus status
Definition: portal.h:150
static int match_prosrc_to_query(const char *prosrc, const char *queryText, int cursorpos)
Definition: pg_proc.c:1027
int errposition(int cursorpos)
Definition: elog.c:1199
int internalerrposition(int cursorpos)
Definition: elog.c:1215

◆ oid_array_to_list()

List* oid_array_to_list ( Datum  datum)

Definition at line 1142 of file pg_proc.c.

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

Referenced by compile_plperl_function(), and PLy_procedure_create().

1143 {
1144  ArrayType *array = DatumGetArrayTypeP(datum);
1145  Datum *values;
1146  int nelems;
1147  int i;
1148  List *result = NIL;
1149 
1150  deconstruct_array(array,
1151  OIDOID,
1152  sizeof(Oid), true, TYPALIGN_INT,
1153  &values, NULL, &nelems);
1154  for (i = 0; i < nelems; i++)
1155  result = lappend_oid(result, values[i]);
1156  return result;
1157 }
#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:3462
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 69 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, check_valid_internal_signature(), check_valid_polymorphic_signature(), ObjectAddress::classId, CommandCounterIncrement(), CStringGetTextDatum, DatumGetPointer, deleteDependencyRecordsFor(), DEPENDENCY_NORMAL, oidvector::dim1, elog, equalTupleDescs(), ereport, errcode(), errdetail(), errdetail_internal(), 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(), and makeRangeConstructors().

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

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 31 of file pg_proc.h.

◆ FormData_pg_proc

FormData_pg_proc

Definition at line 126 of file pg_proc.h.

◆ proname

◆ pronargs