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 (pg_catalog) BKI_LOOKUP(pg_namespace)
 
Oid proowner BKI_DEFAULT (POSTGRES) BKI_LOOKUP(pg_authid)
 
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)
 
 DECLARE_TOAST (pg_proc, 2836, 2837)
 
 DECLARE_UNIQUE_INDEX_PKEY (pg_proc_oid_index, 2690, ProcedureOidIndexId, on pg_proc using btree(oid oid_ops))
 
 DECLARE_UNIQUE_INDEX (pg_proc_proname_args_nsp_index, 2691, ProcedureNameArgsNspIndexId, on pg_proc using btree(proname name_ops, proargtypes oidvector_ops, pronamespace oid_ops))
 
ObjectAddress ProcedureCreate (const char *procedureName, Oid procNamespace, bool replace, bool returnsSet, Oid returnType, Oid proowner, Oid languageObjectId, Oid languageValidator, const char *prosrc, const char *probin, Node *prosqlbody, char prokind, bool security_definer, bool isLeakProof, bool isStrict, char volatility, char parallel, oidvector *parameterTypes, Datum allParameterTypes, Datum parameterModes, Datum parameterNames, List *parameterDefaults, Datum trftypes, Datum proconfig, Oid prosupport, float4 procost, float4 prorows)
 
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 136 of file pg_proc.h.

Function Documentation

◆ BKI_DEFAULT() [1/9]

Oid pronamespace BKI_DEFAULT ( pg_catalog  )

◆ BKI_DEFAULT() [2/9]

Oid proowner BKI_DEFAULT ( POSTGRES  )

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

oidvector proargtypes BKI_LOOKUP ( pg_type  )

◆ CATALOG()

CATALOG ( pg_proc  ,
1255  ,
ProcedureRelationId   
)

◆ DECLARE_TOAST()

DECLARE_TOAST ( pg_proc  ,
2836  ,
2837   
)

◆ DECLARE_UNIQUE_INDEX()

DECLARE_UNIQUE_INDEX ( pg_proc_proname_args_nsp_index  ,
2691  ,
ProcedureNameArgsNspIndexId  ,
on pg_proc using   btreeproname name_ops, proargtypes oidvector_ops, pronamespace oid_ops 
)

◆ DECLARE_UNIQUE_INDEX_PKEY()

DECLARE_UNIQUE_INDEX_PKEY ( pg_proc_oid_index  ,
2690  ,
ProcedureOidIndexId  ,
on pg_proc using   btreeoid oid_ops 
)

◆ function_parse_error_transpose()

bool function_parse_error_transpose ( const char *  prosrc)

Definition at line 1011 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().

1012 {
1013  int origerrposition;
1014  int newerrposition;
1015  const char *queryText;
1016 
1017  /*
1018  * Nothing to do unless we are dealing with a syntax error that has a
1019  * cursor position.
1020  *
1021  * Some PLs may prefer to report the error position as an internal error
1022  * to begin with, so check that too.
1023  */
1024  origerrposition = geterrposition();
1025  if (origerrposition <= 0)
1026  {
1027  origerrposition = getinternalerrposition();
1028  if (origerrposition <= 0)
1029  return false;
1030  }
1031 
1032  /* We can get the original query text from the active portal (hack...) */
1034  queryText = ActivePortal->sourceText;
1035 
1036  /* Try to locate the prosrc in the original text */
1037  newerrposition = match_prosrc_to_query(prosrc, queryText, origerrposition);
1038 
1039  if (newerrposition > 0)
1040  {
1041  /* Successful, so fix error position to reference original query */
1042  errposition(newerrposition);
1043  /* Get rid of any report of the error as an "internal query" */
1045  internalerrquery(NULL);
1046  }
1047  else
1048  {
1049  /*
1050  * If unsuccessful, convert the position to an internal position
1051  * marker and give the function text as the internal query.
1052  */
1053  errposition(0);
1054  internalerrposition(origerrposition);
1055  internalerrquery(prosrc);
1056  }
1057 
1058  return true;
1059 }
Portal ActivePortal
Definition: pquery.c:35
int geterrposition(void)
Definition: elog.c:1417
int getinternalerrposition(void)
Definition: elog.c:1434
const char * sourceText
Definition: portal.h:136
int internalerrquery(const char *query)
Definition: elog.c:1321
#define Assert(condition)
Definition: c.h:804
PortalStatus status
Definition: portal.h:151
static int match_prosrc_to_query(const char *prosrc, const char *queryText, int cursorpos)
Definition: pg_proc.c:1068
int errposition(int cursorpos)
Definition: elog.c:1285
int internalerrposition(int cursorpos)
Definition: elog.c:1301

◆ oid_array_to_list()

List* oid_array_to_list ( Datum  datum)

Definition at line 1183 of file pg_proc.c.

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

Referenced by compile_plperl_function(), and PLy_procedure_create().

1184 {
1185  ArrayType *array = DatumGetArrayTypeP(datum);
1186  Datum *values;
1187  int nelems;
1188  int i;
1189  List *result = NIL;
1190 
1191  deconstruct_array(array,
1192  OIDOID,
1193  sizeof(Oid), true, TYPALIGN_INT,
1194  &values, NULL, &nelems);
1195  for (i = 0; i < nelems; i++)
1196  result = lappend_oid(result, values[i]);
1197  return result;
1198 }
#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:372
uintptr_t Datum
Definition: postgres.h:411
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3491
static Datum values[MAXATTR]
Definition: bootstrap.c:156
int i
Definition: pg_list.h:50
#define DatumGetArrayTypeP(X)
Definition: array.h:254

◆ 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,
Node prosqlbody,
char  prokind,
bool  security_definer,
bool  isLeakProof,
bool  isStrict,
char  volatility,
char  parallel,
oidvector parameterTypes,
Datum  allParameterTypes,
Datum  parameterModes,
Datum  parameterNames,
List parameterDefaults,
Datum  trftypes,
Datum  proconfig,
Oid  prosupport,
float4  procost,
float4  prorows 
)

Definition at line 71 of file pg_proc.c.

References aclcheck_error(), ACLCHECK_NOT_OWNER, add_exact_object_address(), 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(), CommandCounterIncrement(), CStringGetTextDatum, DatumGetPointer, deleteDependencyRecordsFor(), DEPENDENCY_NORMAL, oidvector::dim1, elog, equalTupleDescs(), ereport, errcode(), errdetail(), errdetail_internal(), errhint(), errmsg(), errmsg_plural(), ERROR, exprType(), Float4GetDatum(), format_procedure(), free_object_addresses(), 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(), new_object_addresses(), NewGUCNestLevel(), NIL, nodeToString(), OBJECT_FUNCTION, ObjectAddressSet, ObjectIdGetDatum, OidFunctionCall1, OidIsValid, pg_proc_ownercheck(), PGC_S_SESSION, PGC_SUSET, PGC_USERSET, PointerGetDatum, PointerIsValid, ProcessGUCArray(), PROCNAMEARGSNSP, record_object_address_dependencies(), 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(), makeMultirangeConstructors(), and makeRangeConstructors().

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

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

◆ proname

◆ pronargs