57 int cursorpos,
int *newcursorpos);
104 Oid languageObjectId,
105 Oid languageValidator,
110 bool security_definer,
116 Datum allParameterTypes,
117 Datum parameterModes,
118 Datum parameterNames,
119 List *parameterDefaults,
131 char *paramModes = NULL;
137 bool nulls[Natts_pg_proc];
139 bool replaces[Natts_pg_proc];
154 parameterCount = parameterTypes->
dim1;
157 (
errcode(ERRCODE_TOO_MANY_ARGUMENTS),
159 "functions cannot have more than %d arguments",
174 allParamCount =
ARR_DIMS(allParamArray)[0];
176 allParamCount <= 0 ||
179 elog(
ERROR,
"allParameterTypes is not a 1-D Oid array");
181 Assert(allParamCount >= parameterCount);
186 allParamCount = parameterCount;
187 allParams = parameterTypes->
values;
200 ARR_DIMS(modesArray)[0] != allParamCount ||
203 elog(
ERROR,
"parameterModes is not a 1-D char array");
216 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
217 errmsg(
"cannot determine result data type"),
229 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
230 errmsg(
"unsafe use of pseudo-type \"internal\""),
238 for (
i = 0;
i < allParamCount;
i++)
240 if (paramModes == NULL ||
241 paramModes[
i] == PROARGMODE_IN ||
242 paramModes[
i] == PROARGMODE_VARIADIC)
250 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
251 errmsg(
"cannot determine result data type"),
258 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
259 errmsg(
"unsafe use of pseudo-type \"internal\""),
265 if (paramModes != NULL)
272 for (
i = 0;
i < allParamCount;
i++)
274 switch (paramModes[
i])
277 case PROARGMODE_INOUT:
279 elog(
ERROR,
"variadic parameter must be last");
282 if (
OidIsValid(variadicType) && prokind == PROKIND_PROCEDURE)
283 elog(
ERROR,
"variadic parameter must be last");
285 case PROARGMODE_TABLE:
288 case PROARGMODE_VARIADIC:
290 elog(
ERROR,
"variadic parameter must be last");
291 switch (allParams[
i])
294 variadicType = ANYOID;
297 variadicType = ANYELEMENTOID;
299 case ANYCOMPATIBLEARRAYOID:
300 variadicType = ANYCOMPATIBLEOID;
305 elog(
ERROR,
"variadic parameter is not an array");
310 elog(
ERROR,
"invalid parameter mode '%c'", paramModes[
i]);
320 for (
i = 0;
i < Natts_pg_proc; ++
i)
348 values[Anum_pg_proc_proallargtypes - 1] = allParameterTypes;
350 nulls[Anum_pg_proc_proallargtypes - 1] =
true;
352 values[Anum_pg_proc_proargmodes - 1] = parameterModes;
354 nulls[Anum_pg_proc_proargmodes - 1] =
true;
356 values[Anum_pg_proc_proargnames - 1] = parameterNames;
358 nulls[Anum_pg_proc_proargnames - 1] =
true;
359 if (parameterDefaults !=
NIL)
362 nulls[Anum_pg_proc_proargdefaults - 1] =
true;
364 values[Anum_pg_proc_protrftypes - 1] = trftypes;
366 nulls[Anum_pg_proc_protrftypes - 1] =
true;
371 nulls[Anum_pg_proc_probin - 1] =
true;
375 nulls[Anum_pg_proc_prosqlbody - 1] =
true;
377 values[Anum_pg_proc_proconfig - 1] = proconfig;
379 nulls[Anum_pg_proc_proconfig - 1] =
true;
401 (
errcode(ERRCODE_DUPLICATE_FUNCTION),
402 errmsg(
"function \"%s\" already exists with same argument types",
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) :
423 dropcmd = (prokind == PROKIND_PROCEDURE ?
"DROP PROCEDURE" :
424 prokind == PROKIND_AGGREGATE ?
"DROP AGGREGATE" :
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"),
455 if (returnType == RECORDOID)
465 if (olddesc == NULL && newdesc == NULL)
467 else if (olddesc == NULL || newdesc == NULL ||
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."),
485 Anum_pg_proc_proargnames,
490 char **old_arg_names;
491 char **new_arg_names;
497 Anum_pg_proc_proargmodes,
508 for (
j = 0;
j < n_old_arg_names;
j++)
510 if (old_arg_names[
j] == NULL)
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\"",
533 if (oldproc->pronargdefaults != 0)
535 Datum proargdefaults;
540 if (
list_length(parameterDefaults) < oldproc->pronargdefaults)
542 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
543 errmsg(
"cannot remove parameter defaults from existing function"),
550 Anum_pg_proc_proargdefaults);
557 oldproc->pronargdefaults);
559 foreach(oldlc, oldDefaults)
566 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
567 errmsg(
"cannot change data type of existing parameter default value"),
572 newlc =
lnext(parameterDefaults, newlc);
580 replaces[Anum_pg_proc_oid - 1] =
false;
581 replaces[Anum_pg_proc_proowner - 1] =
false;
582 replaces[Anum_pg_proc_proacl - 1] =
false;
602 nulls[Anum_pg_proc_proacl - 1] =
true;
641 for (
i = 0;
i < allParamCount;
i++)
665 if (languageObjectId == SQLlanguageId && prosqlbody)
669 if (parameterDefaults)
696 int save_nestlevel = 0;
763 elog(
ERROR,
"cache lookup failed for function %u", funcoid);
770 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
771 errmsg(
"there is no built-in function named \"%s\"",
809 elog(
ERROR,
"cache lookup failed for function %u", funcoid);
837 List *raw_parsetree_list;
838 List *querytree_list;
853 elog(
ERROR,
"cache lookup failed for function %u", funcoid);
858 if (
get_typtype(proc->prorettype) == TYPTYPE_PSEUDO &&
859 proc->prorettype != RECORDOID &&
860 proc->prorettype != VOIDOID &&
861 !IsPolymorphicType(proc->prorettype))
863 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
864 errmsg(
"SQL functions cannot return type %s",
870 for (
i = 0;
i < proc->pronargs;
i++)
872 if (
get_typtype(proc->proargtypes.values[
i]) == TYPTYPE_PSEUDO)
874 if (IsPolymorphicType(proc->proargtypes.values[
i]))
878 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
879 errmsg(
"SQL functions cannot have arguments of type %s",
894 callback_arg.
prosrc = prosrc;
897 sqlerrcontext.
arg = &callback_arg;
902 tmp =
SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosqlbody, &isnull);
906 List *stored_query_list;
914 querytree_list =
NIL;
915 foreach(lc, stored_query_list)
918 List *querytree_sublist;
928 querytree_list =
lappend(querytree_list, querytree_sublist);
943 querytree_list =
NIL;
956 foreach(lc, raw_parsetree_list)
959 List *querytree_sublist;
966 querytree_list =
lappend(querytree_list,
1025 int origerrposition;
1036 if (origerrposition <= 0)
1039 if (origerrposition <= 0)
1058 newerrposition = -1;
1061 if (newerrposition > 0)
1099 int prosrclen = strlen(prosrc);
1100 int querylen = strlen(queryText);
1105 for (curpos = 0; curpos < querylen - prosrclen; curpos++)
1107 if (queryText[curpos] ==
'$' &&
1108 strncmp(prosrc, &queryText[curpos + 1], prosrclen) == 0 &&
1109 queryText[curpos + 1 + prosrclen] ==
'$')
1121 else if (queryText[curpos] ==
'\'' &&
1123 cursorpos, &newcursorpos))
1149 int cursorpos,
int *newcursorpos)
1151 int newcp = cursorpos;
1170 if (*literal ==
'\\')
1176 else if (*literal ==
'\'')
1178 if (literal[1] !=
'\'')
1185 if (strncmp(prosrc, literal, chlen) != 0)
1191 if (*literal ==
'\'' && literal[1] !=
'\'')
1194 *newcursorpos = newcp;
1200 *newcursorpos = newcp;
1214 for (
i = 0;
i < nelems;
i++)
void recordDependencyOnNewAcl(Oid classId, Oid objectId, int32 objsubId, Oid ownerId, Acl *acl)
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Acl * get_user_default_acl(ObjectType objtype, Oid ownerId, Oid nsp_oid)
#define DatumGetArrayTypeP(X)
void deconstruct_array_builtin(ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
static Datum values[MAXATTR]
#define CStringGetTextDatum(s)
#define TextDatumGetCString(d)
#define PointerIsValid(pointer)
#define OidIsValid(objectId)
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
void record_object_address_dependencies(const ObjectAddress *depender, ObjectAddresses *referenced, DependencyType behavior)
void recordDependencyOnExpr(const ObjectAddress *depender, Node *expr, List *rtable, DependencyType behavior)
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
ObjectAddresses * new_object_addresses(void)
void free_object_addresses(ObjectAddresses *addrs)
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
int getinternalerrposition(void)
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
int internalerrquery(const char *query)
int internalerrposition(int cursorpos)
int errdetail_internal(const char *fmt,...)
int errdetail(const char *fmt,...)
ErrorContextCallback * error_context_stack
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
int errposition(int cursorpos)
#define ereport(elevel,...)
Oid fmgr_internal_function(const char *proname)
bool CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid)
const Pg_finfo_record * fetch_finfo_record(void *filehandle, const char *funcname)
#define OidFunctionCall1(functionId, arg1)
TupleDesc build_function_result_tupdesc_t(HeapTuple procTuple)
TupleDesc build_function_result_tupdesc_d(char prokind, Datum proallargtypes, Datum proargmodes, Datum proargnames)
int get_func_input_arg_names(Datum proargnames, Datum proargmodes, char ***arg_names)
TypeFuncClass get_func_result_type(Oid functionId, Oid *resultTypeId, TupleDesc *resultTupleDesc)
bool check_sql_fn_retval(List *queryTreeLists, Oid rettype, TupleDesc rettupdesc, char prokind, bool insertDroppedCols)
void check_sql_fn_statements(List *queryTreeLists)
void sql_fn_parser_setup(struct ParseState *pstate, SQLFunctionParseInfoPtr pinfo)
SQLFunctionParseInfoPtr prepare_sql_fn_parse_info(HeapTuple procedureTuple, Node *call_expr, Oid inputCollation)
int NewGUCNestLevel(void)
void ProcessGUCArray(ArrayType *array, GucContext context, GucSource source, GucAction action)
void AtEOXact_GUC(bool isCommit, int nestLevel)
bool check_function_bodies
Assert(PointerIsAligned(start, uint64))
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
void heap_freetuple(HeapTuple htup)
#define HeapTupleIsValid(tuple)
static void * GETSTRUCT(const HeapTupleData *tuple)
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
List * lappend(List *list, void *datum)
List * lappend_oid(List *list, Oid datum)
Oid get_element_type(Oid typid)
char get_typtype(Oid typid)
int pg_mbstrlen_with_len(const char *mbstr, int limit)
int pg_mblen(const char *mbstr)
void namestrcpy(Name name, const char *str)
Oid exprType(const Node *expr)
#define IsA(nodeptr, _type_)
#define castNode(_type_, nodeptr)
#define InvokeObjectPostCreateHook(classId, objectId, subId)
#define ObjectAddressSet(addr, class_id, object_id)
char * nodeToString(const void *obj)
void(* ParserSetupHook)(struct ParseState *pstate, void *arg)
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)
long deleteDependencyRecordsFor(Oid classId, Oid objectId, bool skipExtensionDeps)
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
#define lfirst_node(type, lc)
static int list_length(const List *l)
static ListCell * list_nth_cell(const List *list, int n)
#define foreach_oid(var, lst)
static ListCell * lnext(const List *l, const ListCell *c)
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)
static void sql_function_parse_error_callback(void *arg)
Datum fmgr_internal_validator(PG_FUNCTION_ARGS)
Datum fmgr_c_validator(PG_FUNCTION_ARGS)
List * oid_array_to_list(Datum datum)
static bool match_prosrc_to_literal(const char *prosrc, const char *literal, int cursorpos, int *newcursorpos)
bool function_parse_error_transpose(const char *prosrc)
Datum fmgr_sql_validator(PG_FUNCTION_ARGS)
static int match_prosrc_to_query(const char *prosrc, const char *queryText, int cursorpos)
FormData_pg_proc * Form_pg_proc
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
void pgstat_create_function(Oid proid)
List * pg_analyze_and_rewrite_withcb(RawStmt *parsetree, const char *query_string, ParserSetupHook parserSetup, void *parserSetupArg, QueryEnvironment *queryEnv)
List * pg_parse_query(const char *query_string)
List * pg_rewrite_query(Query *query)
static Datum PointerGetDatum(const void *X)
static Datum Float4GetDatum(float4 X)
static Datum UInt16GetDatum(uint16 X)
static Datum BoolGetDatum(bool X)
static Datum ObjectIdGetDatum(Oid X)
static Datum NameGetDatum(const NameData *X)
static Pointer DatumGetPointer(Datum X)
static Datum CharGetDatum(char X)
void * stringToNode(const char *str)
char * format_procedure(Oid procedure_oid)
#define RelationGetDescr(relation)
void AcquireRewriteLocks(Query *parsetree, bool forExecute, bool forUpdatePushedDown)
struct ErrorContextCallback * previous
void(* callback)(void *arg)
Oid values[FLEXIBLE_ARRAY_MEMBER]
void ReleaseSysCache(HeapTuple tuple)
HeapTuple SearchSysCache1(int cacheId, Datum key1)
HeapTuple SearchSysCache3(int cacheId, Datum key1, Datum key2, Datum key3)
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
void table_close(Relation relation, LOCKMODE lockmode)
Relation table_open(Oid relationId, LOCKMODE lockmode)
bool equalRowTypes(TupleDesc tupdesc1, TupleDesc tupdesc2)
void CommandCounterIncrement(void)