PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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, pg_proc, btree(oid oid_ops))
 
 DECLARE_UNIQUE_INDEX (pg_proc_proname_args_nsp_index, 2691, ProcedureNameArgsNspIndexId, pg_proc, btree(proname name_ops, proargtypes oidvector_ops, pronamespace oid_ops))
 
 MAKE_SYSCACHE (PROCOID, pg_proc_oid_index, 128)
 
 MAKE_SYSCACHE (PROCNAMEARGSNSP, pg_proc_proname_args_nsp_index, 128)
 
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, List *trfoids, 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]

float4 prorows BKI_DEFAULT ( )

◆ BKI_DEFAULT() [2/9]

float4 procost BKI_DEFAULT ( )

◆ BKI_DEFAULT() [3/9]

char prokind BKI_DEFAULT ( )

◆ BKI_DEFAULT() [4/9]

char provolatile BKI_DEFAULT ( i  )

◆ BKI_DEFAULT() [5/9]

Oid prolang BKI_DEFAULT ( internal  )

◆ BKI_DEFAULT() [6/9]

Oid pronamespace BKI_DEFAULT ( pg_catalog  )

◆ BKI_DEFAULT() [7/9]

Oid proowner BKI_DEFAULT ( POSTGRES  )

◆ BKI_DEFAULT() [8/9]

char proparallel BKI_DEFAULT ( )

◆ BKI_DEFAULT() [9/9]

bool proisstrict 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  ,
pg_proc  ,
btree(proname name_ops, proargtypes oidvector_ops, pronamespace oid_ops)   
)

◆ DECLARE_UNIQUE_INDEX_PKEY()

DECLARE_UNIQUE_INDEX_PKEY ( pg_proc_oid_index  ,
2690  ,
ProcedureOidIndexId  ,
pg_proc  ,
btree(oid oid_ops)   
)

◆ function_parse_error_transpose()

bool function_parse_error_transpose ( const char *  prosrc)

Definition at line 1023 of file pg_proc.c.

1024{
1025 int origerrposition;
1026 int newerrposition;
1027
1028 /*
1029 * Nothing to do unless we are dealing with a syntax error that has a
1030 * cursor position.
1031 *
1032 * Some PLs may prefer to report the error position as an internal error
1033 * to begin with, so check that too.
1034 */
1035 origerrposition = geterrposition();
1036 if (origerrposition <= 0)
1037 {
1038 origerrposition = getinternalerrposition();
1039 if (origerrposition <= 0)
1040 return false;
1041 }
1042
1043 /* We can get the original query text from the active portal (hack...) */
1045 {
1046 const char *queryText = ActivePortal->sourceText;
1047
1048 /* Try to locate the prosrc in the original text */
1049 newerrposition = match_prosrc_to_query(prosrc, queryText,
1050 origerrposition);
1051 }
1052 else
1053 {
1054 /*
1055 * Quietly give up if no ActivePortal. This is an unusual situation
1056 * but it can happen in, e.g., logical replication workers.
1057 */
1058 newerrposition = -1;
1059 }
1060
1061 if (newerrposition > 0)
1062 {
1063 /* Successful, so fix error position to reference original query */
1064 errposition(newerrposition);
1065 /* Get rid of any report of the error as an "internal query" */
1067 internalerrquery(NULL);
1068 }
1069 else
1070 {
1071 /*
1072 * If unsuccessful, convert the position to an internal position
1073 * marker and give the function text as the internal query.
1074 */
1075 errposition(0);
1076 internalerrposition(origerrposition);
1077 internalerrquery(prosrc);
1078 }
1079
1080 return true;
1081}
int getinternalerrposition(void)
Definition: elog.c:1634
int internalerrquery(const char *query)
Definition: elog.c:1504
int internalerrposition(int cursorpos)
Definition: elog.c:1484
int geterrposition(void)
Definition: elog.c:1617
int errposition(int cursorpos)
Definition: elog.c:1468
static int match_prosrc_to_query(const char *prosrc, const char *queryText, int cursorpos)
Definition: pg_proc.c:1090
@ PORTAL_ACTIVE
Definition: portal.h:108
Portal ActivePortal
Definition: pquery.c:37
const char * sourceText
Definition: portal.h:136
PortalStatus status
Definition: portal.h:151

References ActivePortal, 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().

◆ MAKE_SYSCACHE() [1/2]

MAKE_SYSCACHE ( PROCNAMEARGSNSP  ,
pg_proc_proname_args_nsp_index  ,
128   
)

◆ MAKE_SYSCACHE() [2/2]

MAKE_SYSCACHE ( PROCOID  ,
pg_proc_oid_index  ,
128   
)

◆ oid_array_to_list()

List * oid_array_to_list ( Datum  datum)

Definition at line 1205 of file pg_proc.c.

1206{
1207 ArrayType *array = DatumGetArrayTypeP(datum);
1208 Datum *values;
1209 int nelems;
1210 int i;
1211 List *result = NIL;
1212
1213 deconstruct_array_builtin(array, OIDOID, &values, NULL, &nelems);
1214 for (i = 0; i < nelems; i++)
1215 result = lappend_oid(result, values[i]);
1216 return result;
1217}
#define DatumGetArrayTypeP(X)
Definition: array.h:261
void deconstruct_array_builtin(ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3697
static Datum values[MAXATTR]
Definition: bootstrap.c:151
int i
Definition: isn.c:77
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
#define NIL
Definition: pg_list.h:68
uintptr_t Datum
Definition: postgres.h:69
Definition: pg_list.h:54

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

Referenced by compile_plperl_function(), and PLy_procedure_create().

◆ 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,
List trfoids,
Datum  proconfig,
Oid  prosupport,
float4  procost,
float4  prorows 
)

Definition at line 98 of file pg_proc.c.

126{
127 Oid retval;
128 int parameterCount;
129 int allParamCount;
130 Oid *allParams;
131 char *paramModes = NULL;
132 Oid variadicType = InvalidOid;
133 Acl *proacl = NULL;
134 Relation rel;
135 HeapTuple tup;
136 HeapTuple oldtup;
137 bool nulls[Natts_pg_proc];
138 Datum values[Natts_pg_proc];
139 bool replaces[Natts_pg_proc];
140 NameData procname;
141 TupleDesc tupDesc;
142 bool is_update;
143 ObjectAddress myself,
144 referenced;
145 char *detailmsg;
146 int i;
147 ObjectAddresses *addrs;
148
149 /*
150 * sanity checks
151 */
152 Assert(PointerIsValid(prosrc));
153
154 parameterCount = parameterTypes->dim1;
155 if (parameterCount < 0 || parameterCount > FUNC_MAX_ARGS)
157 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
158 errmsg_plural("functions cannot have more than %d argument",
159 "functions cannot have more than %d arguments",
161 FUNC_MAX_ARGS)));
162 /* note: the above is correct, we do NOT count output arguments */
163
164 /* Deconstruct array inputs */
165 if (allParameterTypes != PointerGetDatum(NULL))
166 {
167 /*
168 * We expect the array to be a 1-D OID array; verify that. We don't
169 * need to use deconstruct_array() since the array data is just going
170 * to look like a C array of OID values.
171 */
172 ArrayType *allParamArray = (ArrayType *) DatumGetPointer(allParameterTypes);
173
174 allParamCount = ARR_DIMS(allParamArray)[0];
175 if (ARR_NDIM(allParamArray) != 1 ||
176 allParamCount <= 0 ||
177 ARR_HASNULL(allParamArray) ||
178 ARR_ELEMTYPE(allParamArray) != OIDOID)
179 elog(ERROR, "allParameterTypes is not a 1-D Oid array");
180 allParams = (Oid *) ARR_DATA_PTR(allParamArray);
181 Assert(allParamCount >= parameterCount);
182 /* we assume caller got the contents right */
183 }
184 else
185 {
186 allParamCount = parameterCount;
187 allParams = parameterTypes->values;
188 }
189
190 if (parameterModes != PointerGetDatum(NULL))
191 {
192 /*
193 * We expect the array to be a 1-D CHAR array; verify that. We don't
194 * need to use deconstruct_array() since the array data is just going
195 * to look like a C array of char values.
196 */
197 ArrayType *modesArray = (ArrayType *) DatumGetPointer(parameterModes);
198
199 if (ARR_NDIM(modesArray) != 1 ||
200 ARR_DIMS(modesArray)[0] != allParamCount ||
201 ARR_HASNULL(modesArray) ||
202 ARR_ELEMTYPE(modesArray) != CHAROID)
203 elog(ERROR, "parameterModes is not a 1-D char array");
204 paramModes = (char *) ARR_DATA_PTR(modesArray);
205 }
206
207 /*
208 * Do not allow polymorphic return type unless there is a polymorphic
209 * input argument that we can use to deduce the actual return type.
210 */
211 detailmsg = check_valid_polymorphic_signature(returnType,
212 parameterTypes->values,
213 parameterCount);
214 if (detailmsg)
216 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
217 errmsg("cannot determine result data type"),
218 errdetail_internal("%s", detailmsg)));
219
220 /*
221 * Also, do not allow return type INTERNAL unless at least one input
222 * argument is INTERNAL.
223 */
224 detailmsg = check_valid_internal_signature(returnType,
225 parameterTypes->values,
226 parameterCount);
227 if (detailmsg)
229 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
230 errmsg("unsafe use of pseudo-type \"internal\""),
231 errdetail_internal("%s", detailmsg)));
232
233 /*
234 * Apply the same tests to any OUT arguments.
235 */
236 if (allParameterTypes != PointerGetDatum(NULL))
237 {
238 for (i = 0; i < allParamCount; i++)
239 {
240 if (paramModes == NULL ||
241 paramModes[i] == PROARGMODE_IN ||
242 paramModes[i] == PROARGMODE_VARIADIC)
243 continue; /* ignore input-only params */
244
245 detailmsg = check_valid_polymorphic_signature(allParams[i],
246 parameterTypes->values,
247 parameterCount);
248 if (detailmsg)
250 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
251 errmsg("cannot determine result data type"),
252 errdetail_internal("%s", detailmsg)));
253 detailmsg = check_valid_internal_signature(allParams[i],
254 parameterTypes->values,
255 parameterCount);
256 if (detailmsg)
258 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
259 errmsg("unsafe use of pseudo-type \"internal\""),
260 errdetail_internal("%s", detailmsg)));
261 }
262 }
263
264 /* Identify variadic argument type, if any */
265 if (paramModes != NULL)
266 {
267 /*
268 * Only the last input parameter can be variadic; if it is, save its
269 * element type. Errors here are just elog since caller should have
270 * checked this already.
271 */
272 for (i = 0; i < allParamCount; i++)
273 {
274 switch (paramModes[i])
275 {
276 case PROARGMODE_IN:
277 case PROARGMODE_INOUT:
278 if (OidIsValid(variadicType))
279 elog(ERROR, "variadic parameter must be last");
280 break;
281 case PROARGMODE_OUT:
282 if (OidIsValid(variadicType) && prokind == PROKIND_PROCEDURE)
283 elog(ERROR, "variadic parameter must be last");
284 break;
285 case PROARGMODE_TABLE:
286 /* okay */
287 break;
288 case PROARGMODE_VARIADIC:
289 if (OidIsValid(variadicType))
290 elog(ERROR, "variadic parameter must be last");
291 switch (allParams[i])
292 {
293 case ANYOID:
294 variadicType = ANYOID;
295 break;
296 case ANYARRAYOID:
297 variadicType = ANYELEMENTOID;
298 break;
299 case ANYCOMPATIBLEARRAYOID:
300 variadicType = ANYCOMPATIBLEOID;
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_prosupport - 1] = ObjectIdGetDatum(prosupport);
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 (prosqlbody)
373 values[Anum_pg_proc_prosqlbody - 1] = CStringGetTextDatum(nodeToString(prosqlbody));
374 else
375 nulls[Anum_pg_proc_prosqlbody - 1] = true;
376 if (proconfig != PointerGetDatum(NULL))
377 values[Anum_pg_proc_proconfig - 1] = proconfig;
378 else
379 nulls[Anum_pg_proc_proconfig - 1] = true;
380 /* proacl will be determined later */
381
382 rel = table_open(ProcedureRelationId, RowExclusiveLock);
383 tupDesc = RelationGetDescr(rel);
384
385 /* Check for pre-existing definition */
386 oldtup = SearchSysCache3(PROCNAMEARGSNSP,
387 PointerGetDatum(procedureName),
388 PointerGetDatum(parameterTypes),
389 ObjectIdGetDatum(procNamespace));
390
391 if (HeapTupleIsValid(oldtup))
392 {
393 /* There is one; okay to replace it? */
394 Form_pg_proc oldproc = (Form_pg_proc) GETSTRUCT(oldtup);
395 Datum proargnames;
396 bool isnull;
397 const char *dropcmd;
398
399 if (!replace)
401 (errcode(ERRCODE_DUPLICATE_FUNCTION),
402 errmsg("function \"%s\" already exists with same argument types",
403 procedureName)));
404 if (!object_ownercheck(ProcedureRelationId, oldproc->oid, proowner))
406 procedureName);
407
408 /* Not okay to change routine kind */
409 if (oldproc->prokind != prokind)
411 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
412 errmsg("cannot change routine kind"),
413 (oldproc->prokind == PROKIND_AGGREGATE ?
414 errdetail("\"%s\" is an aggregate function.", procedureName) :
415 oldproc->prokind == PROKIND_FUNCTION ?
416 errdetail("\"%s\" is a function.", procedureName) :
417 oldproc->prokind == PROKIND_PROCEDURE ?
418 errdetail("\"%s\" is a procedure.", procedureName) :
419 oldproc->prokind == PROKIND_WINDOW ?
420 errdetail("\"%s\" is a window function.", procedureName) :
421 0)));
422
423 dropcmd = (prokind == PROKIND_PROCEDURE ? "DROP PROCEDURE" :
424 prokind == PROKIND_AGGREGATE ? "DROP AGGREGATE" :
425 "DROP FUNCTION");
426
427 /*
428 * Not okay to change the return type of the existing proc, since
429 * existing rules, views, etc may depend on the return type.
430 *
431 * In case of a procedure, a changing return type means that whether
432 * the procedure has output parameters was changed. Since there is no
433 * user visible return type, we produce a more specific error message.
434 */
435 if (returnType != oldproc->prorettype ||
436 returnsSet != oldproc->proretset)
438 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
439 prokind == PROKIND_PROCEDURE
440 ? errmsg("cannot change whether a procedure has output parameters")
441 : errmsg("cannot change return type of existing function"),
442
443 /*
444 * translator: first %s is DROP FUNCTION, DROP PROCEDURE, or DROP
445 * AGGREGATE
446 */
447 errhint("Use %s %s first.",
448 dropcmd,
449 format_procedure(oldproc->oid))));
450
451 /*
452 * If it returns RECORD, check for possible change of record type
453 * implied by OUT parameters
454 */
455 if (returnType == RECORDOID)
456 {
457 TupleDesc olddesc;
458 TupleDesc newdesc;
459
460 olddesc = build_function_result_tupdesc_t(oldtup);
461 newdesc = build_function_result_tupdesc_d(prokind,
462 allParameterTypes,
463 parameterModes,
464 parameterNames);
465 if (olddesc == NULL && newdesc == NULL)
466 /* ok, both are runtime-defined RECORDs */ ;
467 else if (olddesc == NULL || newdesc == NULL ||
468 !equalRowTypes(olddesc, newdesc))
470 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
471 errmsg("cannot change return type of existing function"),
472 errdetail("Row type defined by OUT parameters is different."),
473 /* translator: first %s is DROP FUNCTION or DROP PROCEDURE */
474 errhint("Use %s %s first.",
475 dropcmd,
476 format_procedure(oldproc->oid))));
477 }
478
479 /*
480 * If there were any named input parameters, check to make sure the
481 * names have not been changed, as this could break existing calls. We
482 * allow adding names to formerly unnamed parameters, though.
483 */
484 proargnames = SysCacheGetAttr(PROCNAMEARGSNSP, oldtup,
485 Anum_pg_proc_proargnames,
486 &isnull);
487 if (!isnull)
488 {
489 Datum proargmodes;
490 char **old_arg_names;
491 char **new_arg_names;
492 int n_old_arg_names;
493 int n_new_arg_names;
494 int j;
495
496 proargmodes = SysCacheGetAttr(PROCNAMEARGSNSP, oldtup,
497 Anum_pg_proc_proargmodes,
498 &isnull);
499 if (isnull)
500 proargmodes = PointerGetDatum(NULL); /* just to be sure */
501
502 n_old_arg_names = get_func_input_arg_names(proargnames,
503 proargmodes,
504 &old_arg_names);
505 n_new_arg_names = get_func_input_arg_names(parameterNames,
506 parameterModes,
507 &new_arg_names);
508 for (j = 0; j < n_old_arg_names; j++)
509 {
510 if (old_arg_names[j] == NULL)
511 continue;
512 if (j >= n_new_arg_names || new_arg_names[j] == NULL ||
513 strcmp(old_arg_names[j], new_arg_names[j]) != 0)
515 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
516 errmsg("cannot change name of input parameter \"%s\"",
517 old_arg_names[j]),
518 /* translator: first %s is DROP FUNCTION or DROP PROCEDURE */
519 errhint("Use %s %s first.",
520 dropcmd,
521 format_procedure(oldproc->oid))));
522 }
523 }
524
525 /*
526 * If there are existing defaults, check compatibility: redefinition
527 * must not remove any defaults nor change their types. (Removing a
528 * default might cause a function to fail to satisfy an existing call.
529 * Changing type would only be possible if the associated parameter is
530 * polymorphic, and in such cases a change of default type might alter
531 * the resolved output type of existing calls.)
532 */
533 if (oldproc->pronargdefaults != 0)
534 {
535 Datum proargdefaults;
536 List *oldDefaults;
537 ListCell *oldlc;
538 ListCell *newlc;
539
540 if (list_length(parameterDefaults) < oldproc->pronargdefaults)
542 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
543 errmsg("cannot remove parameter defaults from existing function"),
544 /* translator: first %s is DROP FUNCTION or DROP PROCEDURE */
545 errhint("Use %s %s first.",
546 dropcmd,
547 format_procedure(oldproc->oid))));
548
549 proargdefaults = SysCacheGetAttrNotNull(PROCNAMEARGSNSP, oldtup,
550 Anum_pg_proc_proargdefaults);
551 oldDefaults = castNode(List, stringToNode(TextDatumGetCString(proargdefaults)));
552 Assert(list_length(oldDefaults) == oldproc->pronargdefaults);
553
554 /* new list can have more defaults than old, advance over 'em */
555 newlc = list_nth_cell(parameterDefaults,
556 list_length(parameterDefaults) -
557 oldproc->pronargdefaults);
558
559 foreach(oldlc, oldDefaults)
560 {
561 Node *oldDef = (Node *) lfirst(oldlc);
562 Node *newDef = (Node *) lfirst(newlc);
563
564 if (exprType(oldDef) != exprType(newDef))
566 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
567 errmsg("cannot change data type of existing parameter default value"),
568 /* translator: first %s is DROP FUNCTION or DROP PROCEDURE */
569 errhint("Use %s %s first.",
570 dropcmd,
571 format_procedure(oldproc->oid))));
572 newlc = lnext(parameterDefaults, newlc);
573 }
574 }
575
576 /*
577 * Do not change existing oid, ownership or permissions, either. Note
578 * dependency-update code below has to agree with this decision.
579 */
580 replaces[Anum_pg_proc_oid - 1] = false;
581 replaces[Anum_pg_proc_proowner - 1] = false;
582 replaces[Anum_pg_proc_proacl - 1] = false;
583
584 /* Okay, do it... */
585 tup = heap_modify_tuple(oldtup, tupDesc, values, nulls, replaces);
586 CatalogTupleUpdate(rel, &tup->t_self, tup);
587
588 ReleaseSysCache(oldtup);
589 is_update = true;
590 }
591 else
592 {
593 /* Creating a new procedure */
594 Oid newOid;
595
596 /* First, get default permissions and set up proacl */
597 proacl = get_user_default_acl(OBJECT_FUNCTION, proowner,
598 procNamespace);
599 if (proacl != NULL)
600 values[Anum_pg_proc_proacl - 1] = PointerGetDatum(proacl);
601 else
602 nulls[Anum_pg_proc_proacl - 1] = true;
603
604 newOid = GetNewOidWithIndex(rel, ProcedureOidIndexId,
605 Anum_pg_proc_oid);
606 values[Anum_pg_proc_oid - 1] = ObjectIdGetDatum(newOid);
607 tup = heap_form_tuple(tupDesc, values, nulls);
608 CatalogTupleInsert(rel, tup);
609 is_update = false;
610 }
611
612
613 retval = ((Form_pg_proc) GETSTRUCT(tup))->oid;
614
615 /*
616 * Create dependencies for the new function. If we are updating an
617 * existing function, first delete any existing pg_depend entries.
618 * (However, since we are not changing ownership or permissions, the
619 * shared dependencies do *not* need to change, and we leave them alone.)
620 */
621 if (is_update)
622 deleteDependencyRecordsFor(ProcedureRelationId, retval, true);
623
624 addrs = new_object_addresses();
625
626 ObjectAddressSet(myself, ProcedureRelationId, retval);
627
628 /* dependency on namespace */
629 ObjectAddressSet(referenced, NamespaceRelationId, procNamespace);
630 add_exact_object_address(&referenced, addrs);
631
632 /* dependency on implementation language */
633 ObjectAddressSet(referenced, LanguageRelationId, languageObjectId);
634 add_exact_object_address(&referenced, addrs);
635
636 /* dependency on return type */
637 ObjectAddressSet(referenced, TypeRelationId, returnType);
638 add_exact_object_address(&referenced, addrs);
639
640 /* dependency on parameter types */
641 for (i = 0; i < allParamCount; i++)
642 {
643 ObjectAddressSet(referenced, TypeRelationId, allParams[i]);
644 add_exact_object_address(&referenced, addrs);
645 }
646
647 /* dependency on transforms, if any */
648 foreach_oid(transformid, trfoids)
649 {
650 ObjectAddressSet(referenced, TransformRelationId, transformid);
651 add_exact_object_address(&referenced, addrs);
652 }
653
654 /* dependency on support function, if any */
655 if (OidIsValid(prosupport))
656 {
657 ObjectAddressSet(referenced, ProcedureRelationId, prosupport);
658 add_exact_object_address(&referenced, addrs);
659 }
660
663
664 /* dependency on SQL routine body */
665 if (languageObjectId == SQLlanguageId && prosqlbody)
666 recordDependencyOnExpr(&myself, prosqlbody, NIL, DEPENDENCY_NORMAL);
667
668 /* dependency on parameter default expressions */
669 if (parameterDefaults)
670 recordDependencyOnExpr(&myself, (Node *) parameterDefaults,
672
673 /* dependency on owner */
674 if (!is_update)
675 recordDependencyOnOwner(ProcedureRelationId, retval, proowner);
676
677 /* dependency on any roles mentioned in ACL */
678 if (!is_update)
679 recordDependencyOnNewAcl(ProcedureRelationId, retval, 0,
680 proowner, proacl);
681
682 /* dependency on extension */
683 recordDependencyOnCurrentExtension(&myself, is_update);
684
685 heap_freetuple(tup);
686
687 /* Post creation hook for new function */
688 InvokeObjectPostCreateHook(ProcedureRelationId, retval, 0);
689
691
692 /* Verify function body */
693 if (OidIsValid(languageValidator))
694 {
695 ArrayType *set_items = NULL;
696 int save_nestlevel = 0;
697
698 /* Advance command counter so new tuple can be seen by validator */
700
701 /*
702 * Set per-function configuration parameters so that the validation is
703 * done with the environment the function expects. However, if
704 * check_function_bodies is off, we don't do this, because that would
705 * create dump ordering hazards that pg_dump doesn't know how to deal
706 * with. (For example, a SET clause might refer to a not-yet-created
707 * text search configuration.) This means that the validator
708 * shouldn't complain about anything that might depend on a GUC
709 * parameter when check_function_bodies is off.
710 */
712 {
713 set_items = (ArrayType *) DatumGetPointer(proconfig);
714 if (set_items) /* Need a new GUC nesting level */
715 {
716 save_nestlevel = NewGUCNestLevel();
717 ProcessGUCArray(set_items,
721 }
722 }
723
724 OidFunctionCall1(languageValidator, ObjectIdGetDatum(retval));
725
726 if (set_items)
727 AtEOXact_GUC(true, save_nestlevel);
728 }
729
730 /* ensure that stats are dropped if transaction aborts */
731 if (!is_update)
733
734 return myself;
735}
@ ACLCHECK_NOT_OWNER
Definition: acl.h:185
void recordDependencyOnNewAcl(Oid classId, Oid objectId, int32 objsubId, Oid ownerId, Acl *acl)
Definition: aclchk.c:4312
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:2639
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition: aclchk.c:4075
Acl * get_user_default_acl(ObjectType objtype, Oid ownerId, Oid nsp_oid)
Definition: aclchk.c:4232
#define ARR_NDIM(a)
Definition: array.h:290
#define ARR_DATA_PTR(a)
Definition: array.h:322
#define ARR_ELEMTYPE(a)
Definition: array.h:292
#define ARR_DIMS(a)
Definition: array.h:294
#define ARR_HASNULL(a)
Definition: array.h:291
#define CStringGetTextDatum(s)
Definition: builtins.h:97
#define TextDatumGetCString(d)
Definition: builtins.h:98
#define PointerIsValid(pointer)
Definition: c.h:734
#define OidIsValid(objectId)
Definition: c.h:746
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:419
void record_object_address_dependencies(const ObjectAddress *depender, ObjectAddresses *referenced, DependencyType behavior)
Definition: dependency.c:2757
void recordDependencyOnExpr(const ObjectAddress *depender, Node *expr, List *rtable, DependencyType behavior)
Definition: dependency.c:1553
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
Definition: dependency.c:2548
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2502
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2788
@ DEPENDENCY_NORMAL
Definition: dependency.h:33
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1181
int errdetail_internal(const char *fmt,...)
Definition: elog.c:1231
int errdetail(const char *fmt,...)
Definition: elog.c:1204
int errhint(const char *fmt,...)
Definition: elog.c:1318
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define ereport(elevel,...)
Definition: elog.h:149
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:720
TupleDesc build_function_result_tupdesc_t(HeapTuple procTuple)
Definition: funcapi.c:1705
TupleDesc build_function_result_tupdesc_d(char prokind, Datum proallargtypes, Datum proargmodes, Datum proargnames)
Definition: funcapi.c:1751
int get_func_input_arg_names(Datum proargnames, Datum proargmodes, char ***arg_names)
Definition: funcapi.c:1522
int NewGUCNestLevel(void)
Definition: guc.c:2235
void ProcessGUCArray(ArrayType *array, GucContext context, GucSource source, GucAction action)
Definition: guc.c:6457
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:2262
@ GUC_ACTION_SAVE
Definition: guc.h:205
@ PGC_S_SESSION
Definition: guc.h:126
@ PGC_SUSET
Definition: guc.h:78
@ PGC_USERSET
Definition: guc.h:79
bool check_function_bodies
Definition: guc_tables.c:528
Assert(PointerIsAligned(start, uint64))
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
Definition: heaptuple.c:1210
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1435
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:313
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:233
int j
Definition: isn.c:78
#define RowExclusiveLock
Definition: lockdefs.h:38
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2899
void namestrcpy(Name name, const char *str)
Definition: name.c:233
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
#define castNode(_type_, nodeptr)
Definition: nodes.h:182
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:173
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
char * nodeToString(const void *obj)
Definition: outfuncs.c:797
char * check_valid_internal_signature(Oid ret_type, const Oid *declared_arg_types, int nargs)
char * check_valid_polymorphic_signature(Oid ret_type, const Oid *declared_arg_types, int nargs)
@ OBJECT_FUNCTION
Definition: parsenodes.h:2336
#define FUNC_MAX_ARGS
long deleteDependencyRecordsFor(Oid classId, Oid objectId, bool skipExtensionDeps)
Definition: pg_depend.c:301
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:193
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
static ListCell * list_nth_cell(const List *list, int n)
Definition: pg_list.h:277
#define foreach_oid(var, lst)
Definition: pg_list.h:471
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:168
void pgstat_create_function(Oid proid)
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
static Datum Float4GetDatum(float4 X)
Definition: postgres.h:480
static Datum UInt16GetDatum(uint16 X)
Definition: postgres.h:197
static Datum BoolGetDatum(bool X)
Definition: postgres.h:107
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
static Datum NameGetDatum(const NameData *X)
Definition: postgres.h:378
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:317
static Datum CharGetDatum(char X)
Definition: postgres.h:127
#define InvalidOid
Definition: postgres_ext.h:35
unsigned int Oid
Definition: postgres_ext.h:30
void * stringToNode(const char *str)
Definition: read.c:90
char * format_procedure(Oid procedure_oid)
Definition: regproc.c:299
#define RelationGetDescr(relation)
Definition: rel.h:542
ItemPointerData t_self
Definition: htup.h:65
Definition: nodes.h:135
Definition: c.h:712
int dim1
Definition: c.h:702
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:704
bool superuser(void)
Definition: superuser.c:46
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache3(int cacheId, Datum key1, Datum key2, Datum key3)
Definition: syscache.c:243
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:600
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition: syscache.c:631
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
bool equalRowTypes(TupleDesc tupdesc1, TupleDesc tupdesc2)
Definition: tupdesc.c:770
void CommandCounterIncrement(void)
Definition: xact.c:1100

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, equalRowTypes(), ereport, errcode(), errdetail(), errdetail_internal(), errhint(), errmsg(), errmsg_plural(), ERROR, exprType(), Float4GetDatum(), foreach_oid, format_procedure(), free_object_addresses(), FUNC_MAX_ARGS, get_element_type(), get_func_input_arg_names(), get_user_default_acl(), GetNewOidWithIndex(), GETSTRUCT(), GUC_ACTION_SAVE, heap_form_tuple(), heap_freetuple(), heap_modify_tuple(), HeapTupleIsValid, i, InvalidOid, InvokeObjectPostCreateHook, j, lfirst, list_length(), list_nth_cell(), lnext(), NameGetDatum(), namestrcpy(), new_object_addresses(), NewGUCNestLevel(), NIL, nodeToString(), OBJECT_FUNCTION, object_ownercheck(), ObjectAddressSet, ObjectIdGetDatum(), OidFunctionCall1, OidIsValid, PGC_S_SESSION, PGC_SUSET, PGC_USERSET, pgstat_create_function(), PointerGetDatum(), PointerIsValid, ProcessGUCArray(), record_object_address_dependencies(), recordDependencyOnCurrentExtension(), recordDependencyOnExpr(), recordDependencyOnNewAcl(), recordDependencyOnOwner(), RelationGetDescr, ReleaseSysCache(), RowExclusiveLock, SearchSysCache3(), stringToNode(), superuser(), SysCacheGetAttr(), SysCacheGetAttrNotNull(), HeapTupleData::t_self, table_close(), table_open(), TextDatumGetCString, UInt16GetDatum(), oidvector::values, and values.

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

Variable Documentation

◆ BKI_SCHEMA_MACRO

ProcedureRelation_Rowtype_Id BKI_SCHEMA_MACRO
Initial value:
{
Oid oid

Definition at line 30 of file pg_proc.h.

◆ FormData_pg_proc

FormData_pg_proc

Definition at line 129 of file pg_proc.h.

◆ proname

◆ pronargs