47 #include "utils/fmgroids.h"
131 #define get_eval_mcontext(estate) \
132 ((estate)->eval_econtext->ecxt_per_tuple_memory)
133 #define eval_mcontext_alloc(estate, sz) \
134 MemoryContextAlloc(get_eval_mcontext(estate), sz)
135 #define eval_mcontext_alloc0(estate, sz) \
136 MemoryContextAllocZero(get_eval_mcontext(estate), sz)
198 #define LOOP_RC_PROCESSING(looplabel, exit_action) \
199 if (rc == PLPGSQL_RC_RETURN) \
204 else if (rc == PLPGSQL_RC_EXIT) \
206 if (estate->exitlabel == NULL) \
209 rc = PLPGSQL_RC_OK; \
212 else if ((looplabel) != NULL && \
213 strcmp(looplabel, estate->exitlabel) == 0) \
216 estate->exitlabel = NULL; \
217 rc = PLPGSQL_RC_OK; \
226 else if (rc == PLPGSQL_RC_CONTINUE) \
228 if (estate->exitlabel == NULL) \
231 rc = PLPGSQL_RC_OK; \
233 else if ((looplabel) != NULL && \
234 strcmp(looplabel, estate->exitlabel) == 0) \
237 estate->exitlabel = NULL; \
238 rc = PLPGSQL_RC_OK; \
247 Assert(rc == PLPGSQL_RC_OK)
325 EState *simple_eval_estate,
372 Portal portal,
bool prefetch_ok);
376 int paramid,
bool speculative,
380 Datum *resv,
bool *resnull);
433 Datum newvalue,
bool isnull,
bool freeable);
442 const char *portalname,
int cursorOptions);
472 EState *simple_eval_estate,
486 simple_eval_estate, simple_eval_resowner);
494 plerrcontext.
arg = &estate;
606 ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
617 (
errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
618 errmsg(
"control reached end of function without RETURN")));
635 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
636 errmsg(
"set-valued function called in context that cannot accept a set")));
640 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
641 errmsg(
"materialize mode required, but it is not allowed in this context")));
723 elog(
ERROR,
"return type must be a row type");
774 ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
823 gettext_noop(
"returned record type does not match expected record type"));
887 gettext_noop(
"returned record type does not match expected record type"));
933 plerrcontext.
arg = &estate;
992 for (
int i = 0;
i < tupdesc->
natts;
i++)
993 if (
TupleDescAttr(tupdesc,
i)->attgenerated == ATTRIBUTE_GENERATED_STORED)
1007 elog(
ERROR,
"unrecognized trigger action: not INSERT, DELETE, or UPDATE");
1024 ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
1035 (
errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
1036 errmsg(
"control reached end of trigger procedure without RETURN")));
1043 (
errcode(ERRCODE_DATATYPE_MISMATCH),
1044 errmsg(
"trigger procedure cannot return a set")));
1083 gettext_noop(
"returned row structure does not match the structure of the triggering table"));
1111 gettext_noop(
"returned row structure does not match the structure of the triggering table"));
1128 ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
1168 plerrcontext.
arg = &estate;
1182 ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
1193 (
errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
1194 errmsg(
"control reached end of trigger procedure without RETURN")));
1203 ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
1252 errcontext(
"PL/pgSQL function %s line %d %s",
1268 else if (estate->
err_stmt != NULL && err_lineno > 0)
1271 errcontext(
"PL/pgSQL function %s line %d at %s",
1290 int ndatums = estate->
ndatums;
1306 ws_next = workspace;
1310 outdatums = estate->
datums;
1311 for (
i = 0;
i < ndatums;
i++)
1317 switch (indatum->
dtype)
1349 outdatums[
i] = outdatum;
1382 elog(
ERROR,
"trigger promise is not in a trigger function");
1391 elog(
ERROR,
"trigger promise is not in a trigger function");
1399 elog(
ERROR,
"unrecognized trigger execution time: not BEFORE, AFTER, or INSTEAD OF");
1404 elog(
ERROR,
"trigger promise is not in a trigger function");
1410 elog(
ERROR,
"unrecognized trigger event type: not ROW or STATEMENT");
1415 elog(
ERROR,
"trigger promise is not in a trigger function");
1425 elog(
ERROR,
"unrecognized trigger action: not INSERT, DELETE, UPDATE, or TRUNCATE");
1430 elog(
ERROR,
"trigger promise is not in a trigger function");
1438 elog(
ERROR,
"trigger promise is not in a trigger function");
1447 elog(
ERROR,
"trigger promise is not in a trigger function");
1456 elog(
ERROR,
"trigger promise is not in a trigger function");
1464 elog(
ERROR,
"trigger promise is not in a trigger function");
1478 for (
i = 0;
i < nelems;
i++)
1487 -1,
false, TYPALIGN_INT)),
1498 elog(
ERROR,
"event trigger promise is not in an event trigger function");
1504 elog(
ERROR,
"event trigger promise is not in an event trigger function");
1527 "PLpgSQL per-statement data",
1574 for (; cond != NULL; cond = cond->
next)
1583 if (sqlerrstate == 0)
1585 if (edata->
sqlerrcode != ERRCODE_QUERY_CANCELED &&
1619 ((*plpgsql_plugin_ptr)->stmt_beg) (estate, (
PLpgSQL_stmt *) block);
1627 ((*plpgsql_plugin_ptr)->stmt_end) (estate, (
PLpgSQL_stmt *) block);
1642 volatile int rc = -1;
1667 switch (datum->
dtype)
1801 resTypByVal, resTypLen);
1952 if (block->
label == NULL)
1998 ((*plpgsql_plugin_ptr)->stmt_beg) (estate, stmt);
2121 ((*plpgsql_plugin_ptr)->stmt_end) (estate, stmt);
2186 if (expr->
plan == NULL)
2216 options.allow_nonatomic =
true;
2222 elog(
ERROR,
"SPI_execute_plan_extended failed executing query \"%s\": %s",
2227 if (before_lxid != after_lxid)
2247 elog(
ERROR,
"DO statement returned a row");
2252 elog(
ERROR,
"procedure call returned more than one row");
2290 elog(
ERROR,
"query for CALL statement is not a CallStmt");
2293 elog(
ERROR,
"query for CALL statement is not a CallStmt");
2297 elog(
ERROR,
"query for CALL statement is not a CallStmt");
2304 elog(
ERROR,
"cache lookup failed for function %u",
2323 row->
refname =
"(unnamed row)";
2335 for (
i = 0;
i < numargs;
i++)
2338 (argmodes[
i] == PROARGMODE_INOUT ||
2339 argmodes[
i] == PROARGMODE_OUT))
2352 row->
varnos[nfields++] = dno;
2357 if (argnames && argnames[
i] && argnames[
i][0])
2359 (
errcode(ERRCODE_SYNTAX_ERROR),
2360 errmsg(
"procedure parameter \"%s\" is an output parameter but corresponding argument is not writable",
2364 (
errcode(ERRCODE_SYNTAX_ERROR),
2365 errmsg(
"procedure parameter %d is an output parameter but corresponding argument is not writable",
2398 (
errcode(ERRCODE_STACKED_DIAGNOSTICS_ACCESSED_WITHOUT_ACTIVE_HANDLER),
2399 errmsg(
"GET STACKED DIAGNOSTICS cannot be used outside an exception handler")));
2406 switch (diag_item->
kind)
2411 false, INT8OID, -1);
2466 char *contextstackstr;
2479 elog(
ERROR,
"unrecognized diagnostic item kind: %d",
2504 if (!isnull &&
value)
2513 if (!isnull &&
value)
2532 if (stmt->
t_expr != NULL)
2540 &isnull, &t_typoid, &t_typmod);
2578 if (!isnull &&
value)
2598 (
errcode(ERRCODE_CASE_NOT_FOUND),
2599 errmsg(
"case not found"),
2600 errhint(
"CASE statement is missing ELSE part.")));
2647 if (isnull || !
value)
2685 &isnull, &valtype, &valtypmod);
2692 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2693 errmsg(
"lower bound of FOR loop cannot be null")));
2701 &isnull, &valtype, &valtypmod);
2708 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2709 errmsg(
"upper bound of FOR loop cannot be null")));
2719 &isnull, &valtype, &valtypmod);
2726 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2727 errmsg(
"BY value of FOR loop cannot be null")));
2730 if (step_value <= 0)
2732 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2733 errmsg(
"BY value of FOR loop must be greater than zero")));
2748 if (loop_value < end_value)
2753 if (loop_value > end_value)
2779 loop_value -= step_value;
2785 loop_value += step_value;
2842 char *curname = NULL;
2866 (
errcode(ERRCODE_DUPLICATE_CURSOR),
2867 errmsg(
"cursor \"%s\" already in use", curname)));
2889 (
errcode(ERRCODE_SYNTAX_ERROR),
2890 errmsg(
"arguments given for cursor without arguments")));
2892 memset(&set_args, 0,
sizeof(set_args));
2896 set_args.
into =
true;
2902 elog(
ERROR,
"open cursor failed during argument processing");
2908 (
errcode(ERRCODE_SYNTAX_ERROR),
2909 errmsg(
"arguments required for cursor")));
2915 if (query->
plan == NULL)
2930 elog(
ERROR,
"could not open cursor: %s",
2937 if (curname == NULL)
2962 if (curname == NULL)
2984 Oid loop_var_elem_type;
2990 Oid iterator_result_type;
2991 int32 iterator_result_typmod;
2999 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3000 errmsg(
"FOREACH expression must not be null")));
3014 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3015 errmsg(
"FOREACH expression must yield an array, not type %s",
3031 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3032 errmsg(
"slice dimension (%d) is out of the valid range 0..%d",
3058 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3059 errmsg(
"FOREACH ... SLICE loop variable must be of an array type")));
3062 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3063 errmsg(
"FOREACH loop variable must not be of an array type")));
3069 if (stmt->
slice > 0)
3072 iterator_result_type = arrtype;
3073 iterator_result_typmod = arrtypmod;
3079 iterator_result_typmod = arrtypmod;
3092 iterator_result_type, iterator_result_typmod);
3095 if (stmt->
slice > 0)
3139 if (stmt->
cond != NULL)
3146 if (isnull ||
value ==
false)
3198 switch (retvar->
dtype)
3224 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3225 errmsg(
"cannot return non-composite value from function returning composite type")));
3265 if (stmt->
expr != NULL)
3281 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3282 errmsg(
"cannot return non-composite value from function returning composite type")));
3320 (
errcode(ERRCODE_SYNTAX_ERROR),
3321 errmsg(
"cannot use RETURN NEXT in a non-SETOF function")));
3328 natts = tupdesc->
natts;
3344 switch (retvar->
dtype)
3356 bool isNull = var->
isnull;
3361 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3362 errmsg(
"wrong result type supplied in RETURN NEXT")));
3390 if (rec->
erh == NULL)
3400 gettext_noop(
"wrong record type supplied in RETURN NEXT"));
3420 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3421 errmsg(
"wrong record type supplied in RETURN NEXT")));
3432 else if (stmt->
expr)
3456 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3457 errmsg(
"cannot return non-composite value from function returning composite type")));
3464 gettext_noop(
"returned record type does not match expected record type"));
3477 nulldatums = (
Datum *)
3479 nullflags = (
bool *)
3481 memset(nullflags,
true, natts *
sizeof(
bool));
3483 nulldatums, nullflags);
3493 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3494 errmsg(
"wrong result type supplied in RETURN NEXT")));
3512 (
errcode(ERRCODE_SYNTAX_ERROR),
3513 errmsg(
"RETURN NEXT must have a parameter")));
3540 (
errcode(ERRCODE_SYNTAX_ERROR),
3541 errmsg(
"cannot use RETURN QUERY in a non-SETOF function")));
3559 gettext_noop(
"structure of query does not match function result type"));
3562 if (stmt->
query != NULL)
3572 if (expr->
plan == NULL)
3586 options.must_return_tuples =
true;
3591 elog(
ERROR,
"SPI_execute_plan_extended failed executing query \"%s\": %s",
3610 &isnull, &restype, &restypmod);
3613 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3614 errmsg(
"query string argument of EXECUTE is null")));
3629 options.must_return_tuples =
true;
3634 elog(
ERROR,
"SPI_execute_extended failed executing query \"%s\": %s",
3664 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3665 errmsg(
"set-valued function called in context that cannot accept a set")));
3670 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3671 errmsg(
"materialize mode required, but it is not allowed in this context")));
3694 #define SET_RAISE_OPTION_TEXT(opt, name) \
3698 (errcode(ERRCODE_SYNTAX_ERROR), \
3699 errmsg("RAISE option already specified: %s", \
3701 opt = MemoryContextStrdup(stmt_mcontext, extval); \
3712 char *condname = NULL;
3713 char *err_message = NULL;
3714 char *err_detail = NULL;
3715 char *err_hint = NULL;
3716 char *err_column = NULL;
3717 char *err_constraint = NULL;
3718 char *err_datatype = NULL;
3719 char *err_table = NULL;
3720 char *err_schema = NULL;
3732 (
errcode(ERRCODE_STACKED_DIAGNOSTICS_ACCESSED_WITHOUT_ACTIVE_HANDLER),
3733 errmsg(
"RAISE without parameters cannot be used outside an exception handler")));
3759 for (cp = stmt->
message; *cp; cp++)
3781 if (current_param == NULL)
3782 elog(
ERROR,
"unexpected RAISE parameter list length");
3797 current_param =
lnext(stmt->
params, current_param);
3805 if (current_param != NULL)
3806 elog(
ERROR,
"unexpected RAISE parameter list length");
3808 err_message = ds.
data;
3826 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3827 errmsg(
"RAISE statement option cannot be null")));
3836 (
errcode(ERRCODE_SYNTAX_ERROR),
3837 errmsg(
"RAISE option already specified: %s",
3875 err_code = ERRCODE_RAISE_EXCEPTION;
3878 if (err_message == NULL)
3882 err_message = condname;
3894 (err_code ?
errcode(err_code) : 0,
3897 (err_hint != NULL) ?
errhint(
"%s", err_hint) : 0,
3898 (err_column != NULL) ?
3900 (err_constraint != NULL) ?
3902 (err_datatype != NULL) ?
3904 (err_table != NULL) ?
3906 (err_schema != NULL) ?
3932 if (isnull || !
value)
3934 char *message = NULL;
3943 &isnull, &
typeid, &typmod);
3950 (
errcode(ERRCODE_ASSERT_FAILURE),
3952 errmsg(
"assertion failed")));
3966 EState *simple_eval_estate,
3974 estate->
func = func;
4023 if (simple_eval_estate)
4043 "PLpgSQL cast info",
4057 if (simple_eval_resowner)
4102 if ((*plpgsql_plugin_ptr)->func_setup)
4103 ((*plpgsql_plugin_ptr)->func_setup) (estate, func);
4169 options.parserSetupArg = (
void *) expr;
4171 options.cursorOptions = cursorOptions;
4174 elog(
ERROR,
"SPI_prepare_extended failed for \"%s\": %s",
4200 int too_many_rows_level = 0;
4203 too_many_rows_level =
ERROR;
4205 too_many_rows_level =
WARNING;
4211 if (expr->
plan == NULL)
4229 if (plansource->
commandTag == CMDTAG_INSERT ||
4316 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4317 errmsg(
"cannot COPY to/from client in PL/pgSQL")));
4322 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4323 errmsg(
"unsupported transaction command in PL/pgSQL")));
4327 elog(
ERROR,
"SPI_execute_plan_with_paramlist failed executing query \"%s\": %s",
4345 (
errcode(ERRCODE_SYNTAX_ERROR),
4346 errmsg(
"INTO used with a command that cannot return data")));
4368 (
errcode(ERRCODE_NO_DATA_FOUND),
4369 errmsg(
"query returned no rows"),
4377 if (n > 1 && (stmt->
strict || stmt->
mod_stmt || too_many_rows_level))
4390 (
errcode(ERRCODE_TOO_MANY_ROWS),
4391 errmsg(
"query returned more than one row"),
4393 errhint(
"Make sure the query returns a single row, or use LIMIT 1.")));
4408 (
errcode(ERRCODE_SYNTAX_ERROR),
4409 errmsg(
"query has no destination for result data"),
4410 (rc ==
SPI_OK_SELECT) ?
errhint(
"If you want to discard the results of a SELECT, use PERFORM instead.") : 0));
4443 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4444 errmsg(
"query string argument of EXECUTE is null")));
4497 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4498 errmsg(
"EXECUTE of SELECT ... INTO is not implemented"),
4499 errhint(
"You might want to use EXECUTE ... INTO or EXECUTE CREATE TABLE ... AS instead.")));
4505 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4506 errmsg(
"cannot COPY to/from client in PL/pgSQL")));
4511 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4512 errmsg(
"EXECUTE of transaction commands is not implemented")));
4516 elog(
ERROR,
"SPI_execute_extended failed executing query \"%s\": %s",
4534 (
errcode(ERRCODE_SYNTAX_ERROR),
4535 errmsg(
"INTO used with a command that cannot return data")));
4557 (
errcode(ERRCODE_NO_DATA_FOUND),
4558 errmsg(
"query returned no rows"),
4566 if (n > 1 && stmt->
strict)
4576 (
errcode(ERRCODE_TOO_MANY_ROWS),
4577 errmsg(
"query returned more than one row"),
4643 char *curname = NULL;
4666 (
errcode(ERRCODE_DUPLICATE_CURSOR),
4667 errmsg(
"cursor \"%s\" already in use", curname)));
4674 if (stmt->
query != NULL)
4683 query = stmt->
query;
4684 if (query->
plan == NULL)
4707 if (curname == NULL)
4736 (
errcode(ERRCODE_SYNTAX_ERROR),
4737 errmsg(
"arguments given for cursor without arguments")));
4739 memset(&set_args, 0,
sizeof(set_args));
4743 set_args.
into =
true;
4749 elog(
ERROR,
"open cursor failed during argument processing");
4755 (
errcode(ERRCODE_SYNTAX_ERROR),
4756 errmsg(
"arguments required for cursor")));
4760 if (query->
plan == NULL)
4776 elog(
ERROR,
"could not open cursor: %s",
4783 if (curname == NULL)
4821 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4822 errmsg(
"cursor variable \"%s\" is null", curvar->
refname)));
4832 (
errcode(ERRCODE_UNDEFINED_CURSOR),
4833 errmsg(
"cursor \"%s\" does not exist", curname)));
4845 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4846 errmsg(
"relative or absolute cursor position is null")));
4909 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4910 errmsg(
"cursor variable \"%s\" is null", curvar->
refname)));
4920 (
errcode(ERRCODE_UNDEFINED_CURSOR),
4921 errmsg(
"cursor \"%s\" does not exist", curname)));
4996 if (expr->
plan == NULL)
5062 switch (target->
dtype)
5083 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
5084 errmsg(
"null value cannot be assigned to variable \"%s\" declared NOT NULL",
5133 if (var->
value != newvalue || var->
isnull || isNull)
5159 (
errcode(ERRCODE_DATATYPE_MISMATCH),
5160 errmsg(
"cannot assign non-composite value to a row variable")));
5178 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
5179 errmsg(
"null value cannot be assigned to variable \"%s\" declared NOT NULL",
5191 (
errcode(ERRCODE_DATATYPE_MISMATCH),
5192 errmsg(
"cannot assign non-composite value to a record variable")));
5233 (
errcode(ERRCODE_UNDEFINED_COLUMN),
5234 errmsg(
"record \"%s\" has no field \"%s\"",
5242 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5243 errmsg(
"cannot assign to system column \"%s\"",
5293 switch (datum->
dtype)
5319 elog(
ERROR,
"row variable has no tupdesc");
5325 elog(
ERROR,
"row not compatible with its own tupdesc");
5338 if (rec->
erh == NULL)
5406 (
errcode(ERRCODE_UNDEFINED_COLUMN),
5407 errmsg(
"record \"%s\" has no field \"%s\"",
5440 switch (datum->
dtype)
5480 if (rec->
erh == NULL)
5493 (
errcode(ERRCODE_UNDEFINED_COLUMN),
5494 errmsg(
"record \"%s\" has no field \"%s\"",
5524 switch (datum->
dtype)
5571 if (rec->
erh == NULL)
5584 (
errcode(ERRCODE_UNDEFINED_COLUMN),