29#include "utils/fmgroids.h"
66#include "plerrcodes.h"
191 int num_out_args = 0;
195 int *in_arg_varnos = NULL;
214 plerrcontext.arg = &cbarg;
249 else if (is_event_trigger)
254 function->fn_prokind = procStruct->prokind;
257 function->requires_procedure_resowner =
false;
258 function->has_exception_block =
false;
285 &argtypes, &argnames, &argmodes);
292 in_arg_varnos = (
int *)
palloc(numargs *
sizeof(
int));
300 for (
i = 0;
i < numargs;
i++)
303 Oid argtypeid = argtypes[
i];
304 char argmode = argmodes ? argmodes[
i] : PROARGMODE_IN;
323 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
324 errmsg(
"PL/pgSQL functions cannot accept type %s",
332 argnames[
i][0] !=
'\0') ?
347 if (argmode == PROARGMODE_IN ||
348 argmode == PROARGMODE_INOUT ||
349 argmode == PROARGMODE_VARIADIC)
350 in_arg_varnos[num_in_args++] = argvariable->
dno;
351 if (argmode == PROARGMODE_OUT ||
352 argmode == PROARGMODE_INOUT ||
353 argmode == PROARGMODE_TABLE)
354 out_arg_variables[num_out_args++] = argvariable;
360 if (argnames && argnames[
i][0] !=
'\0')
371 if (num_out_args > 1 ||
372 (num_out_args == 1 &&
function->fn_prokind == PROKIND_PROCEDURE))
380 else if (num_out_args == 1)
381 function->out_param_varno = out_arg_variables[0]->
dno;
393 rettypeid = procStruct->prorettype;
394 if (IsPolymorphicType(rettypeid))
398 if (rettypeid == ANYARRAYOID ||
399 rettypeid == ANYCOMPATIBLEARRAYOID)
400 rettypeid = INT4ARRAYOID;
401 else if (rettypeid == ANYRANGEOID ||
402 rettypeid == ANYCOMPATIBLERANGEOID)
403 rettypeid = INT4RANGEOID;
404 else if (rettypeid == ANYMULTIRANGEOID)
405 rettypeid = INT4MULTIRANGEOID;
415 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
416 errmsg(
"could not determine actual return type "
417 "for polymorphic function \"%s\"",
426 function->fn_retset = procStruct->proretset;
433 elog(
ERROR,
"cache lookup failed for type %u", rettypeid);
438 if (typeStruct->typtype == TYPTYPE_PSEUDO)
440 if (rettypeid == VOIDOID ||
441 rettypeid == RECORDOID)
443 else if (rettypeid == TRIGGEROID || rettypeid == EVENT_TRIGGEROID)
445 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
446 errmsg(
"trigger functions can only be called as triggers")));
449 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
450 errmsg(
"PL/pgSQL functions cannot return type %s",
455 function->fn_retisdomain = (typeStruct->typtype == TYPTYPE_DOMAIN);
456 function->fn_retbyval = typeStruct->typbyval;
457 function->fn_rettyplen = typeStruct->typlen;
464 if (IsPolymorphicType(procStruct->prorettype) &&
487 if (procStruct->pronargs != 0)
489 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
490 errmsg(
"trigger functions cannot have declared arguments"),
491 errhint(
"The arguments of the trigger can be accessed through TG_NARGS and TG_ARGV instead.")));
621 if (procStruct->pronargs != 0)
623 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
624 errmsg(
"event trigger functions cannot have declared arguments")));
651 elog(
ERROR,
"unrecognized function typecode: %d",
657 function->fn_readonly = (procStruct->provolatile != PROVOLATILE_VOLATILE);
675 elog(
ERROR,
"plpgsql parser returned %d", parse_rc);
686 if (num_out_args > 0 ||
function->fn_rettype == VOIDOID ||
693 function->fn_nargs = procStruct->pronargs;
732 char *func_name =
"inline_code_block";
753 plerrcontext.arg = &cbarg;
770 "PL/pgSQL inline code context",
790 function->requires_procedure_resowner =
false;
791 function->has_exception_block =
false;
803 function->fn_prokind = PROKIND_FUNCTION;
830 elog(
ERROR,
"plpgsql parser returned %d", parse_rc);
838 if (
function->fn_rettype == VOIDOID)
896 errcontext(
"compilation of PL/pgSQL function \"%s\" near line %d",
918 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
919 errmsg(
"parameter name \"%s\" used more than once",
938 if (
function->action->exceptions != NULL ||
945 new->stmtid = ++
function->nstatements;
957 new->stmtid = ++
function->nstatements;
959 new->retvarno =
function->out_param_varno;
1025 if (myvar != NULL && var != NULL)
1032 (
errcode(ERRCODE_AMBIGUOUS_COLUMN),
1033 errmsg(
"column reference \"%s\" is ambiguous",
1035 errdetail(
"It could refer to either a PL/pgSQL variable or a table column."),
1074 ColumnRef *cref,
bool error_if_no_field)
1079 const char *name2 = NULL;
1080 const char *name3 = NULL;
1081 const char *colname = NULL;
1083 int nnames_scalar = 0;
1084 int nnames_wholerow = 0;
1085 int nnames_field = 0;
1112 nnames_wholerow = 1;
1127 nnames_wholerow = 1;
1134 nnames_wholerow = 2;
1152 nnames_wholerow = 2;
1167 name1, name2, name3,
1176 if (nnames == nnames_scalar)
1180 if (nnames == nnames_wholerow)
1182 if (nnames == nnames_field)
1196 if (strcmp(fld->
fieldname, colname) == 0)
1209 if (error_if_no_field)
1211 (
errcode(ERRCODE_UNDEFINED_COLUMN),
1212 errmsg(
"record \"%s\" has no field \"%s\"",
1213 (nnames_field == 1) ? name1 : name2,
1240 Assert(dno >= 0 && dno < estate->ndatums);
1241 datum = estate->
datums[dno];
1256 ¶m->paramtypmod,
1257 ¶m->paramcollid);
1260 return (
Node *) param;
1310 wdatum->
ident = word1;
1311 wdatum->
quoted = (yytxt[0] ==
'"');
1317 elog(
ERROR,
"unrecognized plpgsql itemtype: %d",
1327 word->ident = word1;
1328 word->quoted = (yytxt[0] ==
'"');
1370 wdatum->
ident = NULL;
1397 wdatum->
ident = NULL;
1441 word1, word2, word3,
1475 wdatum->
ident = NULL;
1530 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1531 errmsg(
"variable \"%s\" does not exist",
ident)));
1551 const char *fldname = NULL;
1619 (
errcode(ERRCODE_UNDEFINED_COLUMN),
1620 errmsg(
"column \"%s\" of relation \"%s\" does not exist",
1627 elog(
ERROR,
"cache lookup failed for type %u", attrStruct->atttypid);
1636 attrStruct->atttypmod,
1637 attrStruct->attcollation,
1673 errmsg(
"relation \"%s\" does not exist",
ident)));
1679 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1680 errmsg(
"relation \"%s\" does not have a composite type",
1717 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1718 errmsg(
"relation \"%s\" does not have a composite type",
1743 switch (dtype->
ttype)
1783 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1784 errmsg(
"variable \"%s\" has pseudo-type %s",
1835 row->
refname =
"(unnamed row)";
1842 for (
i = 0;
i < numvars;
i++)
1857 typmod = ((
PLpgSQL_var *) var)->datatype->atttypmod;
1858 typcoll = ((
PLpgSQL_var *) var)->datatype->collation;
1908 if (strcmp(fld->
fieldname, fldname) == 0)
1950 elog(
ERROR,
"cache lookup failed for type %u", typeOid);
1970 if (!typeStruct->typisdefined)
1972 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1973 errmsg(
"type \"%s\" is only a shell",
1974 NameStr(typeStruct->typname))));
1979 typ->
typoid = typeStruct->oid;
1980 switch (typeStruct->typtype)
1985 case TYPTYPE_MULTIRANGE:
1988 case TYPTYPE_COMPOSITE:
1991 case TYPTYPE_DOMAIN:
1997 case TYPTYPE_PSEUDO:
1998 if (typ->
typoid == RECORDOID)
2005 (
int) typeStruct->typtype);
2008 typ->
typlen = typeStruct->typlen;
2009 typ->
typbyval = typeStruct->typbyval;
2010 typ->
typtype = typeStruct->typtype;
2011 typ->
collation = typeStruct->typcollation;
2016 if (typeStruct->typtype == TYPTYPE_BASE)
2022 typ->
typisarray = (IsTrueArrayType(typeStruct) &&
2023 typeStruct->typstorage != TYPSTORAGE_PLAIN);
2025 else if (typeStruct->typtype == TYPTYPE_DOMAIN)
2028 typ->
typisarray = (typeStruct->typlen == -1 &&
2029 typeStruct->typstorage != TYPSTORAGE_PLAIN &&
2049 if (typentry->
typtype == TYPTYPE_DOMAIN)
2052 if (typentry->
tupDesc == NULL)
2054 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
2055 errmsg(
"type %s is not composite",
2090 (
errcode(ERRCODE_UNDEFINED_OBJECT),
2091 errmsg(
"could not find array type for data type %s",
2113 if (strlen(condname) == 5 &&
2114 strspn(condname,
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") == 5)
2129 (
errcode(ERRCODE_UNDEFINED_OBJECT),
2130 errmsg(
"unrecognized exception condition \"%s\"",
2154 if (strcmp(condname,
"others") == 0)
2158 new->condname = condname;
2170 new->condname = condname;
2178 (
errcode(ERRCODE_UNDEFINED_OBJECT),
2179 errmsg(
"unrecognized exception condition \"%s\"",
2226 Size copiable_size = 0;
2249 function->copiable_size = copiable_size;
2295 *varnos = (
int *)
palloc(
sizeof(
int) * n);
Bitmapset * bms_add_member(Bitmapset *a, int x)
#define TextDatumGetCString(d)
#define OidIsValid(objectId)
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_fn_expr_rettype(FmgrInfo *flinfo)
int get_func_arg_info(HeapTuple procTup, Oid **p_argtypes, char ***p_argnames, char **p_argmodes)
void cfunc_resolve_polymorphic_argtypes(int numargs, Oid *argtypes, char *argmodes, Node *call_expr, bool forValidator, const char *proname)
CachedFunction * cached_function_compile(FunctionCallInfo fcinfo, CachedFunction *function, CachedFunctionCompileCallback ccallback, CachedFunctionDeleteCallback dcallback, Size cacheEntrySize, bool includeResultType, bool forValidator)
bool check_function_bodies
Assert(PointerIsAligned(start, uint64))
#define HeapTupleIsValid(tuple)
static void * GETSTRUCT(const HeapTupleData *tuple)
if(TABLE==NULL||TABLE_index==NULL)
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)
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_)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
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)
int plpgsql_recognize_err_condition(const char *condname, bool allow_sqlstate)
static void plpgsql_compile_error_callback(void *arg)
PLpgSQL_type * plpgsql_parse_cwordrowtype(List *idents)
static Node * plpgsql_param_ref(ParseState *pstate, ParamRef *pref)
static void plpgsql_start_datums(void)
char * plpgsql_error_funcname
bool plpgsql_check_syntax
static const ExceptionLabelMap exception_label_map[]
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 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)
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 Node * plpgsql_post_column_ref(ParseState *pstate, ColumnRef *cref, Node *var)
PLpgSQL_variable * plpgsql_build_variable(const char *refname, int lineno, PLpgSQL_type *dtype, bool add2namespace)
PLpgSQL_function * plpgsql_compile_inline(char *proc_source)
static void plpgsql_compile_callback(FunctionCallInfo fcinfo, HeapTuple procTup, const CachedFunctionHashKey *hashkey, CachedFunction *cfunc, bool forValidator)
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_delete_callback(CachedFunction *cfunc)
void plpgsql_dumptree(PLpgSQL_function *func)
void plpgsql_mark_local_assignment_targets(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
IdentifierLookup plpgsql_IdentifierLookup
void plpgsql_scanner_finish(yyscan_t yyscanner)
yyscan_t plpgsql_scanner_init(const char *str)
int plpgsql_latest_lineno(yyscan_t yyscanner)
@ PLPGSQL_RESOLVE_VARIABLE
@ 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(PLpgSQL_stmt_block **plpgsql_parse_result_p, yyscan_t yyscanner)
static Datum ObjectIdGetDatum(Oid X)
static void word(struct vars *v, int dir, struct state *lp, struct state *rp)
char * format_procedure(Oid procedure_oid)
struct ErrorContextCallback * previous
struct PLpgSQL_nsitem * ns
struct PLpgSQL_function * func
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
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)