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

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