29#include "utils/fmgroids.h"
66#include "plerrcodes.h"
180 char *proc_signature;
192 int num_out_args = 0;
196 int *in_arg_varnos = NULL;
215 plerrcontext.arg = &cbarg;
258 else if (is_event_trigger)
263 function->fn_prokind = procStruct->prokind;
266 function->requires_procedure_resowner =
false;
267 function->has_exception_block =
false;
294 &argtypes, &argnames, &argmodes);
301 in_arg_varnos = (
int *)
palloc(numargs *
sizeof(
int));
309 for (
i = 0;
i < numargs;
i++)
312 Oid argtypeid = argtypes[
i];
313 char argmode = argmodes ? argmodes[
i] : PROARGMODE_IN;
332 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
333 errmsg(
"PL/pgSQL functions cannot accept type %s",
341 argnames[
i][0] !=
'\0') ?
356 if (argmode == PROARGMODE_IN ||
357 argmode == PROARGMODE_INOUT ||
358 argmode == PROARGMODE_VARIADIC)
359 in_arg_varnos[num_in_args++] = argvariable->
dno;
360 if (argmode == PROARGMODE_OUT ||
361 argmode == PROARGMODE_INOUT ||
362 argmode == PROARGMODE_TABLE)
363 out_arg_variables[num_out_args++] = argvariable;
369 if (argnames && argnames[
i][0] !=
'\0')
380 if (num_out_args > 1 ||
381 (num_out_args == 1 &&
function->fn_prokind == PROKIND_PROCEDURE))
389 else if (num_out_args == 1)
390 function->out_param_varno = out_arg_variables[0]->
dno;
402 rettypeid = procStruct->prorettype;
403 if (IsPolymorphicType(rettypeid))
407 if (rettypeid == ANYARRAYOID ||
408 rettypeid == ANYCOMPATIBLEARRAYOID)
409 rettypeid = INT4ARRAYOID;
410 else if (rettypeid == ANYRANGEOID ||
411 rettypeid == ANYCOMPATIBLERANGEOID)
412 rettypeid = INT4RANGEOID;
413 else if (rettypeid == ANYMULTIRANGEOID)
414 rettypeid = INT4MULTIRANGEOID;
424 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
425 errmsg(
"could not determine actual return type "
426 "for polymorphic function \"%s\"",
435 function->fn_retset = procStruct->proretset;
442 elog(
ERROR,
"cache lookup failed for type %u", rettypeid);
447 if (typeStruct->typtype == TYPTYPE_PSEUDO)
449 if (rettypeid == VOIDOID ||
450 rettypeid == RECORDOID)
452 else if (rettypeid == TRIGGEROID || rettypeid == EVENT_TRIGGEROID)
454 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
455 errmsg(
"trigger functions can only be called as triggers")));
458 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
459 errmsg(
"PL/pgSQL functions cannot return type %s",
464 function->fn_retisdomain = (typeStruct->typtype == TYPTYPE_DOMAIN);
465 function->fn_retbyval = typeStruct->typbyval;
466 function->fn_rettyplen = typeStruct->typlen;
473 if (IsPolymorphicType(procStruct->prorettype) &&
496 if (procStruct->pronargs != 0)
498 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
499 errmsg(
"trigger functions cannot have declared arguments"),
500 errhint(
"The arguments of the trigger can be accessed through TG_NARGS and TG_ARGV instead.")));
630 if (procStruct->pronargs != 0)
632 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
633 errmsg(
"event trigger functions cannot have declared arguments")));
660 elog(
ERROR,
"unrecognized function typecode: %d",
666 function->fn_readonly = (procStruct->provolatile != PROVOLATILE_VOLATILE);
684 elog(
ERROR,
"plpgsql parser returned %d", parse_rc);
695 if (num_out_args > 0 ||
function->fn_rettype == VOIDOID ||
702 function->fn_nargs = procStruct->pronargs;
746 char *func_name =
"inline_code_block";
767 plerrcontext.arg = &cbarg;
784 "PL/pgSQL inline code context",
804 function->requires_procedure_resowner =
false;
805 function->has_exception_block =
false;
817 function->fn_prokind = PROKIND_FUNCTION;
844 elog(
ERROR,
"plpgsql parser returned %d", parse_rc);
852 if (
function->fn_rettype == VOIDOID)
910 errcontext(
"compilation of PL/pgSQL function \"%s\" near line %d",
932 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
933 errmsg(
"parameter name \"%s\" used more than once",
952 if (
function->action->exceptions != NULL ||
959 new->stmtid = ++
function->nstatements;
971 new->stmtid = ++
function->nstatements;
973 new->retvarno =
function->out_param_varno;
1039 if (myvar != NULL && var != NULL)
1046 (
errcode(ERRCODE_AMBIGUOUS_COLUMN),
1047 errmsg(
"column reference \"%s\" is ambiguous",
1049 errdetail(
"It could refer to either a PL/pgSQL variable or a table column."),
1088 ColumnRef *cref,
bool error_if_no_field)
1093 const char *name2 = NULL;
1094 const char *name3 = NULL;
1095 const char *colname = NULL;
1097 int nnames_scalar = 0;
1098 int nnames_wholerow = 0;
1099 int nnames_field = 0;
1126 nnames_wholerow = 1;
1141 nnames_wholerow = 1;
1148 nnames_wholerow = 2;
1166 nnames_wholerow = 2;
1181 name1, name2, name3,
1190 if (nnames == nnames_scalar)
1194 if (nnames == nnames_wholerow)
1196 if (nnames == nnames_field)
1210 if (strcmp(fld->
fieldname, colname) == 0)
1228 if (error_if_no_field)
1230 (
errcode(ERRCODE_SYNTAX_ERROR),
1231 errmsg(
"field name \"%s\" is a reserved key word",
1233 errhint(
"Use double quotes to quote it."),
1259 Assert(dno >= 0 && dno < estate->ndatums);
1260 datum = estate->
datums[dno];
1279 return (
Node *) param;
1329 wdatum->
ident = word1;
1330 wdatum->
quoted = (yytxt[0] ==
'"');
1336 elog(
ERROR,
"unrecognized plpgsql itemtype: %d",
1346 word->ident = word1;
1347 word->quoted = (yytxt[0] ==
'"');
1389 wdatum->
ident = NULL;
1416 wdatum->
ident = NULL;
1460 word1, word2, word3,
1494 wdatum->
ident = NULL;
1549 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1550 errmsg(
"variable \"%s\" does not exist",
ident)));
1570 const char *fldname = NULL;
1638 (
errcode(ERRCODE_UNDEFINED_COLUMN),
1639 errmsg(
"column \"%s\" of relation \"%s\" does not exist",
1646 elog(
ERROR,
"cache lookup failed for type %u", attrStruct->atttypid);
1655 attrStruct->atttypmod,
1656 attrStruct->attcollation,
1697 errmsg(
"relation \"%s\" does not exist",
ident)));
1703 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1704 errmsg(
"relation \"%s\" does not have a composite type",
1745 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
1746 errmsg(
"relation \"%s\" does not have a composite type",
1772 switch (dtype->
ttype)
1812 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1813 errmsg(
"variable \"%s\" has pseudo-type %s",
1864 row->
refname =
"(unnamed row)";
1871 for (
i = 0;
i < numvars;
i++)
1886 typmod = ((
PLpgSQL_var *) var)->datatype->atttypmod;
1887 typcoll = ((
PLpgSQL_var *) var)->datatype->collation;
1937 if (strcmp(fld->
fieldname, fldname) == 0)
1981 elog(
ERROR,
"cache lookup failed for type %u", typeOid);
2001 if (!typeStruct->typisdefined)
2003 (
errcode(ERRCODE_UNDEFINED_OBJECT),
2004 errmsg(
"type \"%s\" is only a shell",
2005 NameStr(typeStruct->typname))));
2010 typ->
typoid = typeStruct->oid;
2011 switch (typeStruct->typtype)
2016 case TYPTYPE_MULTIRANGE:
2019 case TYPTYPE_COMPOSITE:
2022 case TYPTYPE_DOMAIN:
2028 case TYPTYPE_PSEUDO:
2029 if (typ->
typoid == RECORDOID)
2036 (
int) typeStruct->typtype);
2039 typ->
typlen = typeStruct->typlen;
2040 typ->
typbyval = typeStruct->typbyval;
2041 typ->
typtype = typeStruct->typtype;
2042 typ->
collation = typeStruct->typcollation;
2047 if (typeStruct->typtype == TYPTYPE_BASE)
2053 typ->
typisarray = (IsTrueArrayType(typeStruct) &&
2054 typeStruct->typstorage != TYPSTORAGE_PLAIN);
2056 else if (typeStruct->typtype == TYPTYPE_DOMAIN)
2059 typ->
typisarray = (typeStruct->typlen == -1 &&
2060 typeStruct->typstorage != TYPSTORAGE_PLAIN &&
2080 if (typentry->
typtype == TYPTYPE_DOMAIN)
2083 if (typentry->
tupDesc == NULL)
2085 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
2086 errmsg(
"type %s is not composite",
2121 (
errcode(ERRCODE_UNDEFINED_OBJECT),
2122 errmsg(
"could not find array type for data type %s",
2144 if (strlen(condname) == 5 &&
2145 strspn(condname,
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") == 5)
2160 (
errcode(ERRCODE_UNDEFINED_OBJECT),
2161 errmsg(
"unrecognized exception condition \"%s\"",
2185 if (strcmp(condname,
"others") == 0)
2189 new->condname = condname;
2201 new->condname = condname;
2209 (
errcode(ERRCODE_UNDEFINED_OBJECT),
2210 errmsg(
"unrecognized exception condition \"%s\"",
2257 Size copiable_size = 0;
2280 function->copiable_size = copiable_size;
2326 *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 MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
void * repalloc(void *pointer, Size size)
void pfree(void *pointer)
void * palloc0(Size size)
MemoryContext CurrentMemoryContext
MemoryContext CacheMemoryContext
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)