29#include "utils/fmgroids.h"
69#define FUNCS_PER_USER 128
82#include "plerrcodes.h"
103 ColumnRef *cref,
bool error_if_no_field);
115 Oid *argtypes,
char *argmodes,
116 Node *call_expr,
bool forValidator,
142 bool function_valid =
false;
143 bool hashkey_valid =
false;
150 elog(
ERROR,
"cache lookup failed for function %u", funcOid);
164 hashkey_valid =
true;
175 function_valid =
true;
224 &hashkey, forValidator);
284 int num_out_args = 0;
288 int *in_arg_varnos = NULL;
307 plerrcontext.
arg = forValidator ? proc_source : NULL;
359 else if (is_event_trigger)
364 function->fn_prokind = procStruct->prokind;
367 function->requires_procedure_resowner =
false;
394 &argtypes, &argnames, &argmodes);
401 in_arg_varnos = (
int *)
palloc(numargs *
sizeof(
int));
409 for (
i = 0;
i < numargs;
i++)
412 Oid argtypeid = argtypes[
i];
413 char argmode = argmodes ? argmodes[
i] : PROARGMODE_IN;
432 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
433 errmsg(
"PL/pgSQL functions cannot accept type %s",
441 argnames[
i][0] !=
'\0') ?
456 if (argmode == PROARGMODE_IN ||
457 argmode == PROARGMODE_INOUT ||
458 argmode == PROARGMODE_VARIADIC)
459 in_arg_varnos[num_in_args++] = argvariable->
dno;
460 if (argmode == PROARGMODE_OUT ||
461 argmode == PROARGMODE_INOUT ||
462 argmode == PROARGMODE_TABLE)
463 out_arg_variables[num_out_args++] = argvariable;
469 if (argnames && argnames[
i][0] !=
'\0')
480 if (num_out_args > 1 ||
481 (num_out_args == 1 &&
function->fn_prokind == PROKIND_PROCEDURE))
489 else if (num_out_args == 1)
490 function->out_param_varno = out_arg_variables[0]->
dno;
502 rettypeid = procStruct->prorettype;
503 if (IsPolymorphicType(rettypeid))
507 if (rettypeid == ANYARRAYOID ||
508 rettypeid == ANYCOMPATIBLEARRAYOID)
509 rettypeid = INT4ARRAYOID;
510 else if (rettypeid == ANYRANGEOID ||
511 rettypeid == ANYCOMPATIBLERANGEOID)
512 rettypeid = INT4RANGEOID;
513 else if (rettypeid == ANYMULTIRANGEOID)
514 rettypeid = INT4MULTIRANGEOID;
524 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
525 errmsg(
"could not determine actual return type "
526 "for polymorphic function \"%s\"",
535 function->fn_retset = procStruct->proretset;
542 elog(
ERROR,
"cache lookup failed for type %u", rettypeid);
547 if (typeStruct->typtype == TYPTYPE_PSEUDO)
549 if (rettypeid == VOIDOID ||
550 rettypeid == RECORDOID)
552 else if (rettypeid == TRIGGEROID || rettypeid == EVENT_TRIGGEROID)
554 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
555 errmsg(
"trigger functions can only be called as triggers")));
558 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
559 errmsg(
"PL/pgSQL functions cannot return type %s",
564 function->fn_retisdomain = (typeStruct->typtype == TYPTYPE_DOMAIN);
565 function->fn_retbyval = typeStruct->typbyval;
566 function->fn_rettyplen = typeStruct->typlen;
573 if (IsPolymorphicType(procStruct->prorettype) &&
596 if (procStruct->pronargs != 0)
598 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
599 errmsg(
"trigger functions cannot have declared arguments"),
600 errhint(
"The arguments of the trigger can be accessed through TG_NARGS and TG_ARGV instead.")));
730 if (procStruct->pronargs != 0)
732 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
733 errmsg(
"event trigger functions cannot have declared arguments")));
760 elog(
ERROR,
"unrecognized function typecode: %d",
766 function->fn_readonly = (procStruct->provolatile != PROVOLATILE_VOLATILE);
784 elog(
ERROR,
"plpgsql parser returned %d", parse_rc);
796 if (num_out_args > 0 ||
function->fn_rettype == VOIDOID ||
803 function->fn_nargs = procStruct->pronargs;
844 char *func_name =
"inline_code_block";
864 plerrcontext.
arg = proc_source;
881 "PL/pgSQL inline code context",
901 function->requires_procedure_resowner =
false;
913 function->fn_prokind = PROKIND_FUNCTION;
940 elog(
ERROR,
"plpgsql parser returned %d", parse_rc);
949 if (
function->fn_rettype == VOIDOID)
997 errcontext(
"compilation of PL/pgSQL function \"%s\" near line %d",
1019 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1020 errmsg(
"parameter name \"%s\" used more than once",
1039 if (
function->action->exceptions != NULL ||
1046 new->stmtid = ++
function->nstatements;
1058 new->stmtid = ++
function->nstatements;
1060 new->retvarno =
function->out_param_varno;
1126 if (myvar != NULL && var != NULL)
1133 (
errcode(ERRCODE_AMBIGUOUS_COLUMN),
1134 errmsg(
"column reference \"%s\" is ambiguous",
1136 errdetail(
"It could refer to either a PL/pgSQL variable or a table column."),
1175 ColumnRef *cref,
bool error_if_no_field)
1180 const char *name2 = NULL;
1181 const char *name3 = NULL;
1182 const char *colname = NULL;
1184 int nnames_scalar = 0;
1185 int nnames_wholerow = 0;
1186 int nnames_field = 0;
1213 nnames_wholerow = 1;
1228 nnames_wholerow = 1;
1235 nnames_wholerow = 2;
1253 nnames_wholerow = 2;
1268 name1, name2, name3,
1277 if (nnames == nnames_scalar)
1281 if (nnames == nnames_wholerow)
1283 if (nnames == nnames_field)
1297 if (strcmp(fld->
fieldname, colname) == 0)
1310 if (error_if_no_field)
1312 (
errcode(ERRCODE_UNDEFINED_COLUMN),
1313 errmsg(
"record \"%s\" has no field \"%s\"",
1314 (nnames_field == 1) ? name1 : name2,
1341 Assert(dno >= 0 && dno < estate->ndatums);
1342 datum = estate->
datums[dno];
1357 ¶m->paramtypmod,
1358 ¶m->paramcollid);
1361 return (
Node *) param;
1411 wdatum->
ident = word1;
1412 wdatum->
quoted = (yytxt[0] ==
'"');
1418 elog(
ERROR,
"unrecognized plpgsql itemtype: %d",
1428 word->ident = word1;
1429 word->quoted = (yytxt[0] ==
'"');
1471 wdatum->
ident = NULL;
1498 wdatum->
ident = NULL;
1542 word1, word2, word3,
1576 wdatum->
ident = NULL;
1631 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1632 errmsg(
"variable \"%s\" does not exist",
ident)));
1652 const char *fldname = NULL;
1720 (
errcode(ERRCODE_UNDEFINED_COLUMN),
1721 errmsg(
"column \"%s\" of relation \"%s\" does not exist",
1728 elog(
ERROR,
"cache lookup failed for type %u", attrStruct->atttypid);
1737 attrStruct->atttypmod,
1738 attrStruct->attcollation,
1774 errmsg(
"relation \"%s\" does not exist",
ident)));
1780 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1781 errmsg(
"relation \"%s\" does not have a composite type",
1818 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1819 errmsg(
"relation \"%s\" does not have a composite type",
1844 switch (dtype->
ttype)
1884 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1885 errmsg(
"variable \"%s\" has pseudo-type %s",
1936 row->
refname =
"(unnamed row)";
1943 for (
i = 0;
i < numvars;
i++)
1958 typmod = ((
PLpgSQL_var *) var)->datatype->atttypmod;
1959 typcoll = ((
PLpgSQL_var *) var)->datatype->collation;
2009 if (strcmp(fld->
fieldname, fldname) == 0)
2051 elog(
ERROR,
"cache lookup failed for type %u", typeOid);
2071 if (!typeStruct->typisdefined)
2073 (
errcode(ERRCODE_UNDEFINED_OBJECT),
2074 errmsg(
"type \"%s\" is only a shell",
2075 NameStr(typeStruct->typname))));
2080 typ->
typoid = typeStruct->oid;
2081 switch (typeStruct->typtype)
2086 case TYPTYPE_MULTIRANGE:
2089 case TYPTYPE_COMPOSITE:
2092 case TYPTYPE_DOMAIN:
2098 case TYPTYPE_PSEUDO:
2099 if (typ->
typoid == RECORDOID)
2106 (
int) typeStruct->typtype);
2109 typ->
typlen = typeStruct->typlen;
2110 typ->
typbyval = typeStruct->typbyval;
2111 typ->
typtype = typeStruct->typtype;
2112 typ->
collation = typeStruct->typcollation;
2117 if (typeStruct->typtype == TYPTYPE_BASE)
2123 typ->
typisarray = (IsTrueArrayType(typeStruct) &&
2124 typeStruct->typstorage != TYPSTORAGE_PLAIN);
2126 else if (typeStruct->typtype == TYPTYPE_DOMAIN)
2129 typ->
typisarray = (typeStruct->typlen == -1 &&
2130 typeStruct->typstorage != TYPSTORAGE_PLAIN &&
2150 if (typentry->
typtype == TYPTYPE_DOMAIN)
2153 if (typentry->
tupDesc == NULL)
2155 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
2156 errmsg(
"type %s is not composite",
2191 (
errcode(ERRCODE_UNDEFINED_OBJECT),
2192 errmsg(
"could not find array type for data type %s",
2214 if (strlen(condname) == 5 &&
2215 strspn(condname,
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") == 5)
2230 (
errcode(ERRCODE_UNDEFINED_OBJECT),
2231 errmsg(
"unrecognized exception condition \"%s\"",
2259 if (strcmp(condname,
"others") == 0)
2262 new->sqlerrstate = 0;
2263 new->condname = condname;
2275 new->condname = condname;
2283 (
errcode(ERRCODE_UNDEFINED_OBJECT),
2284 errmsg(
"unrecognized exception condition \"%s\"",
2331 Size copiable_size = 0;
2354 function->copiable_size = copiable_size;
2400 *varnos = (
int *)
palloc(
sizeof(
int) * n);
2458 if (hashkey->
isTrigger && !forValidator)
2468 if (procStruct->pronargs > 0)
2471 memcpy(hashkey->
argtypes, procStruct->proargtypes.values,
2472 procStruct->pronargs *
sizeof(
Oid));
2480 NameStr(procStruct->proname));
2498 Oid *argtypes,
char *argmodes,
2499 Node *call_expr,
bool forValidator,
2512 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2513 errmsg(
"could not determine actual argument "
2514 "type for polymorphic function \"%s\"",
2518 for (
i = 0;
i < numargs;
i++)
2520 char argmode = argmodes ? argmodes[
i] : PROARGMODE_IN;
2522 if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
2524 if (argtypes[
i] == RECORDOID || argtypes[
i] == RECORDARRAYOID)
2530 argtypes[
i] = resolvedtype;
2538 for (
i = 0;
i < numargs;
i++)
2540 switch (argtypes[
i])
2543 case ANYNONARRAYOID:
2545 case ANYCOMPATIBLEOID:
2546 case ANYCOMPATIBLENONARRAYOID:
2547 argtypes[
i] = INT4OID;
2550 case ANYCOMPATIBLEARRAYOID:
2551 argtypes[
i] = INT4ARRAYOID;
2554 case ANYCOMPATIBLERANGEOID:
2555 argtypes[
i] = INT4RANGEOID;
2557 case ANYMULTIRANGEOID:
2558 argtypes[
i] = INT4MULTIRANGEOID;
2636 elog(
WARNING,
"trying to insert a function that already exists");
2657 elog(
WARNING,
"trying to delete function that does not exist");
Bitmapset * bms_add_member(Bitmapset *a, int x)
#define TextDatumGetCString(d)
#define Assert(condition)
#define MemSet(start, val, len)
#define OidIsValid(objectId)
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
int errdetail(const char *fmt,...)
ErrorContextCallback * error_context_stack
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define MAKE_SQLSTATE(ch1, ch2, ch3, ch4, ch5)
#define ereport(elevel,...)
#define CALLED_AS_EVENT_TRIGGER(fcinfo)
Oid get_call_expr_argtype(Node *expr, int argnum)
Oid get_fn_expr_rettype(FmgrInfo *flinfo)
bool resolve_polymorphic_argtypes(int numargs, Oid *argtypes, char *argmodes, Node *call_expr)
int get_func_arg_info(HeapTuple procTup, Oid **p_argtypes, char ***p_argnames, char **p_argmodes)
bool check_function_bodies
#define HeapTupleIsValid(tuple)
#define HeapTupleHeaderGetRawXmin(tup)
if(TABLE==NULL||TABLE_index==NULL)
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
List * lappend(List *list, void *datum)
List * list_copy(const List *oldlist)
List * list_delete_last(List *list)
bool type_is_rowtype(Oid typid)
Oid get_rel_type_id(Oid relid)
Oid get_base_element_type(Oid typid)
Oid get_array_type(Oid typid)
TypeName * makeTypeNameFromNameList(List *names)
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
TypeName * makeTypeName(char *typnam)
void * MemoryContextAlloc(MemoryContext context, Size size)
void * MemoryContextAllocZero(MemoryContext context, Size size)
char * pstrdup(const char *in)
void * repalloc(void *pointer, Size size)
void pfree(void *pointer)
void * palloc0(Size size)
MemoryContext TopMemoryContext
MemoryContext CurrentMemoryContext
void MemoryContextSetIdentifier(MemoryContext context, const char *id)
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
char * NameListToString(const List *names)
RangeVar * makeRangeVarFromNameList(const List *names)
Oid RelnameGetRelid(const char *relname)
#define RangeVarGetRelid(relation, lockmode, missing_ok)
#define IsA(nodeptr, _type_)
int parser_errposition(ParseState *pstate, int location)
FormData_pg_attribute * Form_pg_attribute
on_exit_nicely_callback function
static int list_length(const List *l)
#define list_make3(x1, x2, x3)
#define list_make2(x1, x2)
bool function_parse_error_transpose(const char *prosrc)
FormData_pg_proc * Form_pg_proc
FormData_pg_type * Form_pg_type
#define ERRCODE_UNDEFINED_TABLE
static void add_dummy_return(PLpgSQL_function *function)
static PLpgSQL_row * build_row_from_vars(PLpgSQL_variable **vars, int numvars)
PLpgSQL_condition * plpgsql_parse_err_condition(char *condname)
void plpgsql_parser_setup(struct ParseState *pstate, PLpgSQL_expr *expr)
PLpgSQL_recfield * plpgsql_build_recfield(PLpgSQL_rec *rec, const char *fldname)
int plpgsql_add_initdatums(int **varnos)
MemoryContext plpgsql_compile_tmp_cxt
static Node * make_datum_param(PLpgSQL_expr *expr, int dno, int location)
static void plpgsql_HashTableInsert(PLpgSQL_function *function, PLpgSQL_func_hashkey *func_key)
int plpgsql_recognize_err_condition(const char *condname, bool allow_sqlstate)
struct plpgsql_hashent plpgsql_HashEnt
static void plpgsql_compile_error_callback(void *arg)
PLpgSQL_type * plpgsql_parse_cwordrowtype(List *idents)
static void compute_function_hashkey(FunctionCallInfo fcinfo, Form_pg_proc procStruct, PLpgSQL_func_hashkey *hashkey, bool forValidator)
void plpgsql_HashTableInit(void)
static Node * plpgsql_param_ref(ParseState *pstate, ParamRef *pref)
static void plpgsql_start_datums(void)
char * plpgsql_error_funcname
static PLpgSQL_function * plpgsql_HashTableLookup(PLpgSQL_func_hashkey *func_key)
PLpgSQL_stmt_block * plpgsql_parse_result
bool plpgsql_check_syntax
static const ExceptionLabelMap exception_label_map[]
static PLpgSQL_function * do_compile(FunctionCallInfo fcinfo, HeapTuple procTup, PLpgSQL_function *function, PLpgSQL_func_hashkey *hashkey, bool forValidator)
PLpgSQL_type * plpgsql_build_datatype_arrayof(PLpgSQL_type *dtype)
PLpgSQL_type * plpgsql_build_datatype(Oid typeOid, int32 typmod, Oid collation, TypeName *origtypname)
static void plpgsql_finish_datums(PLpgSQL_function *function)
PLpgSQL_function * plpgsql_curr_compile
PLpgSQL_function * plpgsql_compile(FunctionCallInfo fcinfo, bool forValidator)
bool plpgsql_parse_dblword(char *word1, char *word2, PLwdatum *wdatum, PLcword *cword)
PLpgSQL_type * plpgsql_parse_wordtype(char *ident)
void plpgsql_adddatum(PLpgSQL_datum *newdatum)
PLpgSQL_datum ** plpgsql_Datums
bool plpgsql_parse_word(char *word1, const char *yytxt, bool lookup, PLwdatum *wdatum, PLword *word)
bool plpgsql_DumpExecTree
PLpgSQL_type * plpgsql_parse_cwordtype(List *idents)
static void plpgsql_resolve_polymorphic_argtypes(int numargs, Oid *argtypes, char *argmodes, Node *call_expr, bool forValidator, const char *proname)
static Node * resolve_column_ref(ParseState *pstate, PLpgSQL_expr *expr, ColumnRef *cref, bool error_if_no_field)
PLpgSQL_rec * plpgsql_build_record(const char *refname, int lineno, PLpgSQL_type *dtype, Oid rectypeid, bool add2namespace)
static void add_parameter_name(PLpgSQL_nsitem_type itemtype, int itemno, const char *name)
static void plpgsql_HashTableDelete(PLpgSQL_function *function)
bool plpgsql_parse_tripword(char *word1, char *word2, char *word3, PLwdatum *wdatum, PLcword *cword)
static PLpgSQL_type * build_datatype(HeapTuple typeTup, int32 typmod, Oid collation, TypeName *origtypname)
static Node * plpgsql_pre_column_ref(ParseState *pstate, ColumnRef *cref)
PLpgSQL_type * plpgsql_parse_wordrowtype(char *ident)
static void delete_function(PLpgSQL_function *func)
static Node * plpgsql_post_column_ref(ParseState *pstate, ColumnRef *cref, Node *var)
static HTAB * plpgsql_HashTable
PLpgSQL_variable * plpgsql_build_variable(const char *refname, int lineno, PLpgSQL_type *dtype, bool add2namespace)
PLpgSQL_function * plpgsql_compile_inline(char *proc_source)
void plpgsql_exec_get_datum_type_info(PLpgSQL_execstate *estate, PLpgSQL_datum *datum, Oid *typeId, int32 *typMod, Oid *collation)
void plpgsql_ns_init(void)
PLpgSQL_nsitem * plpgsql_ns_lookup(PLpgSQL_nsitem *ns_cur, bool localmode, const char *name1, const char *name2, const char *name3, int *names_used)
void plpgsql_dumptree(PLpgSQL_function *func)
void plpgsql_free_function_memory(PLpgSQL_function *func)
void plpgsql_ns_additem(PLpgSQL_nsitem_type itemtype, int itemno, const char *name)
void plpgsql_ns_push(const char *label, PLpgSQL_label_type label_type)
PLpgSQL_nsitem * plpgsql_ns_top(void)
int plpgsql_variable_conflict
int plpgsql_extra_warnings
bool plpgsql_print_strict_params
void plpgsql_scanner_finish(void)
IdentifierLookup plpgsql_IdentifierLookup
int plpgsql_latest_lineno(void)
void plpgsql_scanner_init(const char *str)
@ PLPGSQL_RESOLVE_VARIABLE
struct PLpgSQL_func_hashkey PLpgSQL_func_hashkey
@ IDENTIFIER_LOOKUP_DECLARE
@ IDENTIFIER_LOOKUP_NORMAL
@ PLPGSQL_PROMISE_TG_RELID
@ PLPGSQL_PROMISE_TG_WHEN
@ PLPGSQL_PROMISE_TG_ARGV
@ PLPGSQL_PROMISE_TG_TABLE_SCHEMA
@ PLPGSQL_PROMISE_TG_EVENT
@ PLPGSQL_PROMISE_TG_TABLE_NAME
@ PLPGSQL_PROMISE_TG_LEVEL
@ PLPGSQL_PROMISE_TG_NARGS
@ PLPGSQL_PROMISE_TG_NAME
int plpgsql_yyparse(void)
static Datum ObjectIdGetDatum(Oid X)
MemoryContextSwitchTo(old_ctx)
static void word(struct vars *v, int dir, struct state *lp, struct state *rp)
char * format_procedure(Oid procedure_oid)
struct ErrorContextCallback * previous
void(* callback)(void *arg)
struct PLpgSQL_nsitem * ns
struct PLpgSQL_function * func
Oid argtypes[FUNC_MAX_ARGS]
struct PLpgSQL_execstate * cur_estate
PLpgSQL_resolve_option resolve_option
PLpgSQL_nsitem_type itemtype
ExpandedRecordHeader * erh
ParseParamRefHook p_paramref_hook
PreParseColumnRefHook p_pre_columnref_hook
PostParseColumnRefHook p_post_columnref_hook
uint64 tupDesc_identifier
PLpgSQL_function * function
void ReleaseSysCache(HeapTuple tuple)
HeapTuple SearchSysCache1(int cacheId, Datum key1)
HeapTuple SearchSysCacheAttName(Oid relid, const char *attname)
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
#define CALLED_AS_TRIGGER(fcinfo)
TupleDesc CreateTemplateTupleDesc(int natts)
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
#define INVALID_TUPLEDESC_IDENTIFIER
#define TYPECACHE_TUPDESC
#define TYPECACHE_DOMAIN_BASE_INFO
String * makeString(char *str)