42#include "utils/fmgroids.h"
126#define get_eval_mcontext(estate) \
127 ((estate)->eval_econtext->ecxt_per_tuple_memory)
128#define eval_mcontext_alloc(estate, sz) \
129 MemoryContextAlloc(get_eval_mcontext(estate), sz)
130#define eval_mcontext_alloc0(estate, sz) \
131 MemoryContextAllocZero(get_eval_mcontext(estate), sz)
203#define LOOP_RC_PROCESSING(looplabel, exit_action) \
204 if (rc == PLPGSQL_RC_RETURN) \
209 else if (rc == PLPGSQL_RC_EXIT) \
211 if (estate->exitlabel == NULL) \
214 rc = PLPGSQL_RC_OK; \
217 else if ((looplabel) != NULL && \
218 strcmp(looplabel, estate->exitlabel) == 0) \
221 estate->exitlabel = NULL; \
222 rc = PLPGSQL_RC_OK; \
231 else if (rc == PLPGSQL_RC_CONTINUE) \
233 if (estate->exitlabel == NULL) \
236 rc = PLPGSQL_RC_OK; \
238 else if ((looplabel) != NULL && \
239 strcmp(looplabel, estate->exitlabel) == 0) \
242 estate->exitlabel = NULL; \
243 rc = PLPGSQL_RC_OK; \
252 Assert(rc == PLPGSQL_RC_OK)
330 EState *simple_eval_estate,
378 Portal portal,
bool prefetch_ok);
382 int paramid,
bool speculative,
386 Datum *resv,
bool *resnull);
439 Datum newvalue,
bool isnull,
bool freeable);
448 const char *portalname,
int cursorOptions);
478 EState *simple_eval_estate,
492 simple_eval_estate, simple_eval_resowner);
500 plerrcontext.
arg = &estate;
613 ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
624 (
errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
625 errmsg(
"control reached end of function without RETURN")));
642 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
643 errmsg(
"set-valued function called in context that cannot accept a set")));
647 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
648 errmsg(
"materialize mode required, but it is not allowed in this context")));
730 elog(
ERROR,
"return type must be a row type");
781 ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
830 gettext_noop(
"returned record type does not match expected record type"));
894 gettext_noop(
"returned record type does not match expected record type"));
940 plerrcontext.
arg = &estate;
999 for (
int i = 0;
i < tupdesc->
natts;
i++)
1000 if (
TupleDescAttr(tupdesc,
i)->attgenerated == ATTRIBUTE_GENERATED_STORED)
1014 elog(
ERROR,
"unrecognized trigger action: not INSERT, DELETE, or UPDATE");
1031 ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
1042 (
errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
1043 errmsg(
"control reached end of trigger procedure without RETURN")));
1050 (
errcode(ERRCODE_DATATYPE_MISMATCH),
1051 errmsg(
"trigger procedure cannot return a set")));
1090 gettext_noop(
"returned row structure does not match the structure of the triggering table"));
1118 gettext_noop(
"returned row structure does not match the structure of the triggering table"));
1135 ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
1175 plerrcontext.
arg = &estate;
1189 ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
1200 (
errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
1201 errmsg(
"control reached end of trigger procedure without RETURN")));
1210 ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
1259 errcontext(
"PL/pgSQL function %s line %d %s",
1275 else if (estate->
err_stmt != NULL && err_lineno > 0)
1278 errcontext(
"PL/pgSQL function %s line %d at %s",
1297 int ndatums = estate->
ndatums;
1313 ws_next = workspace;
1317 outdatums = estate->
datums;
1318 for (
i = 0;
i < ndatums;
i++)
1324 switch (indatum->
dtype)
1356 outdatums[
i] = outdatum;
1389 elog(
ERROR,
"trigger promise is not in a trigger function");
1398 elog(
ERROR,
"trigger promise is not in a trigger function");
1406 elog(
ERROR,
"unrecognized trigger execution time: not BEFORE, AFTER, or INSTEAD OF");
1411 elog(
ERROR,
"trigger promise is not in a trigger function");
1417 elog(
ERROR,
"unrecognized trigger event type: not ROW or STATEMENT");
1422 elog(
ERROR,
"trigger promise is not in a trigger function");
1432 elog(
ERROR,
"unrecognized trigger action: not INSERT, DELETE, UPDATE, or TRUNCATE");
1437 elog(
ERROR,
"trigger promise is not in a trigger function");
1445 elog(
ERROR,
"trigger promise is not in a trigger function");
1454 elog(
ERROR,
"trigger promise is not in a trigger function");
1463 elog(
ERROR,
"trigger promise is not in a trigger function");
1471 elog(
ERROR,
"trigger promise is not in a trigger function");
1485 for (
i = 0;
i < nelems;
i++)
1494 -1,
false, TYPALIGN_INT)),
1505 elog(
ERROR,
"event trigger promise is not in an event trigger function");
1511 elog(
ERROR,
"event trigger promise is not in an event trigger function");
1534 "PLpgSQL per-statement data",
1581 for (; cond != NULL; cond = cond->
next)
1590 if (sqlerrstate == 0)
1592 if (edata->
sqlerrcode != ERRCODE_QUERY_CANCELED &&
1626 ((*plpgsql_plugin_ptr)->stmt_beg) (estate, (
PLpgSQL_stmt *) block);
1634 ((*plpgsql_plugin_ptr)->stmt_end) (estate, (
PLpgSQL_stmt *) block);
1649 volatile int rc = -1;
1674 switch (datum->
dtype)
1808 resTypByVal, resTypLen);
1959 if (block->
label == NULL)
2005 ((*plpgsql_plugin_ptr)->stmt_beg) (estate,
stmt);
2009 switch (
stmt->cmd_type)
2128 ((*plpgsql_plugin_ptr)->stmt_end) (estate,
stmt);
2193 if (expr->
plan == NULL)
2206 if (
stmt->is_call &&
stmt->target == NULL)
2223 options.allow_nonatomic =
true;
2229 elog(
ERROR,
"SPI_execute_plan_extended failed executing query \"%s\": %s",
2234 if (before_lxid != after_lxid)
2254 elog(
ERROR,
"DO statement returned a row");
2259 elog(
ERROR,
"procedure call returned more than one row");
2299 elog(
ERROR,
"query for CALL statement is not a CallStmt");
2303 elog(
ERROR,
"query for CALL statement is not a CallStmt");
2305 funcexpr =
stmt->funcexpr;
2310 elog(
ERROR,
"cache lookup failed for function %u",
2329 row->
refname =
"(unnamed row)";
2341 for (
i = 0;
i < numargs;
i++)
2344 (argmodes[
i] == PROARGMODE_INOUT ||
2345 argmodes[
i] == PROARGMODE_OUT))
2358 row->
varnos[nfields++] = dno;
2363 if (argnames && argnames[
i] && argnames[
i][0])
2365 (
errcode(ERRCODE_SYNTAX_ERROR),
2366 errmsg(
"procedure parameter \"%s\" is an output parameter but corresponding argument is not writable",
2370 (
errcode(ERRCODE_SYNTAX_ERROR),
2371 errmsg(
"procedure parameter %d is an output parameter but corresponding argument is not writable",
2406 (
errcode(ERRCODE_STACKED_DIAGNOSTICS_ACCESSED_WITHOUT_ACTIVE_HANDLER),
2407 errmsg(
"GET STACKED DIAGNOSTICS cannot be used outside an exception handler")));
2409 foreach(lc,
stmt->diag_items)
2414 switch (diag_item->
kind)
2419 false, INT8OID, -1);
2480 char *contextstackstr;
2493 elog(
ERROR,
"unrecognized diagnostic item kind: %d",
2518 if (!isnull &&
value)
2521 foreach(lc,
stmt->elsif_list)
2527 if (!isnull &&
value)
2546 if (
stmt->t_expr != NULL)
2554 &isnull, &t_typoid, &t_typmod);
2585 foreach(l,
stmt->case_when_list)
2592 if (!isnull &&
value)
2610 if (!
stmt->have_else)
2612 (
errcode(ERRCODE_CASE_NOT_FOUND),
2613 errmsg(
"case not found"),
2614 errhint(
"CASE statement is missing ELSE part.")));
2661 if (isnull || !
value)
2699 &isnull, &valtype, &valtypmod);
2706 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2707 errmsg(
"lower bound of FOR loop cannot be null")));
2715 &isnull, &valtype, &valtypmod);
2722 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2723 errmsg(
"upper bound of FOR loop cannot be null")));
2733 &isnull, &valtype, &valtypmod);
2740 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2741 errmsg(
"BY value of FOR loop cannot be null")));
2744 if (step_value <= 0)
2746 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2747 errmsg(
"BY value of FOR loop must be greater than zero")));
2762 if (loop_value < end_value)
2767 if (loop_value > end_value)
2793 loop_value -= step_value;
2799 loop_value += step_value;
2856 char *curname = NULL;
2880 (
errcode(ERRCODE_DUPLICATE_CURSOR),
2881 errmsg(
"cursor \"%s\" already in use", curname)));
2891 if (
stmt->argquery != NULL)
2903 (
errcode(ERRCODE_SYNTAX_ERROR),
2904 errmsg(
"arguments given for cursor without arguments")));
2906 memset(&set_args, 0,
sizeof(set_args));
2910 set_args.
into =
true;
2916 elog(
ERROR,
"open cursor failed during argument processing");
2922 (
errcode(ERRCODE_SYNTAX_ERROR),
2923 errmsg(
"arguments required for cursor")));
2929 if (query->
plan == NULL)
2944 elog(
ERROR,
"could not open cursor: %s",
2951 if (curname == NULL)
2976 if (curname == NULL)
2998 Oid loop_var_elem_type;
3004 Oid iterator_result_type;
3005 int32 iterator_result_typmod;
3013 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3014 errmsg(
"FOREACH expression must not be null")));
3028 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3029 errmsg(
"FOREACH expression must yield an array, not type %s",
3045 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3046 errmsg(
"slice dimension (%d) is out of the valid range 0..%d",
3072 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3073 errmsg(
"FOREACH ... SLICE loop variable must be of an array type")));
3076 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3077 errmsg(
"FOREACH loop variable must not be of an array type")));
3083 if (
stmt->slice > 0)
3086 iterator_result_type = arrtype;
3087 iterator_result_typmod = arrtypmod;
3093 iterator_result_typmod = arrtypmod;
3106 iterator_result_type, iterator_result_typmod);
3109 if (
stmt->slice > 0)
3153 if (
stmt->cond != NULL)
3160 if (isnull ||
value ==
false)
3208 if (
stmt->retvarno >= 0)
3212 switch (retvar->
dtype)
3238 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3239 errmsg(
"cannot return non-composite value from function returning composite type")));
3279 if (
stmt->expr != NULL)
3295 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3296 errmsg(
"cannot return non-composite value from function returning composite type")));
3334 (
errcode(ERRCODE_SYNTAX_ERROR),
3335 errmsg(
"cannot use RETURN NEXT in a non-SETOF function")));
3342 natts = tupdesc->
natts;
3354 if (
stmt->retvarno >= 0)
3358 switch (retvar->
dtype)
3370 bool isNull = var->
isnull;
3375 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3376 errmsg(
"wrong result type supplied in RETURN NEXT")));
3404 if (rec->
erh == NULL)
3414 gettext_noop(
"wrong record type supplied in RETURN NEXT"));
3434 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3435 errmsg(
"wrong record type supplied in RETURN NEXT")));
3446 else if (
stmt->expr)
3470 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3471 errmsg(
"cannot return non-composite value from function returning composite type")));
3478 gettext_noop(
"returned record type does not match expected record type"));
3491 nulldatums = (
Datum *)
3493 nullflags = (
bool *)
3495 memset(nullflags,
true, natts *
sizeof(
bool));
3497 nulldatums, nullflags);
3507 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3508 errmsg(
"wrong result type supplied in RETURN NEXT")));
3526 (
errcode(ERRCODE_SYNTAX_ERROR),
3527 errmsg(
"RETURN NEXT must have a parameter")));
3554 (
errcode(ERRCODE_SYNTAX_ERROR),
3555 errmsg(
"cannot use RETURN QUERY in a non-SETOF function")));
3573 gettext_noop(
"structure of query does not match function result type"));
3576 if (
stmt->query != NULL)
3586 if (expr->
plan == NULL)
3600 options.must_return_tuples =
true;
3605 elog(
ERROR,
"SPI_execute_plan_extended failed executing query \"%s\": %s",
3624 &isnull, &restype, &restypmod);
3627 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3628 errmsg(
"query string argument of EXECUTE is null")));
3643 options.must_return_tuples =
true;
3648 elog(
ERROR,
"SPI_execute_extended failed executing query \"%s\": %s",
3678 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3679 errmsg(
"set-valued function called in context that cannot accept a set")));
3684 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3685 errmsg(
"materialize mode required, but it is not allowed in this context")));
3708#define SET_RAISE_OPTION_TEXT(opt, name) \
3712 (errcode(ERRCODE_SYNTAX_ERROR), \
3713 errmsg("RAISE option already specified: %s", \
3715 opt = MemoryContextStrdup(stmt_mcontext, extval); \
3726 char *condname = NULL;
3727 char *err_message = NULL;
3728 char *err_detail = NULL;
3729 char *err_hint = NULL;
3730 char *err_column = NULL;
3731 char *err_constraint = NULL;
3732 char *err_datatype = NULL;
3733 char *err_table = NULL;
3734 char *err_schema = NULL;
3739 if (
stmt->condname == NULL &&
stmt->message == NULL &&
3746 (
errcode(ERRCODE_STACKED_DIAGNOSTICS_ACCESSED_WITHOUT_ACTIVE_HANDLER),
3747 errmsg(
"RAISE without parameters cannot be used outside an exception handler")));
3773 for (cp =
stmt->message; *cp; cp++)
3795 if (current_param == NULL)
3796 elog(
ERROR,
"unexpected RAISE parameter list length");
3811 current_param =
lnext(
stmt->params, current_param);
3819 if (current_param != NULL)
3820 elog(
ERROR,
"unexpected RAISE parameter list length");
3822 err_message = ds.
data;
3825 foreach(lc,
stmt->options)
3840 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3841 errmsg(
"RAISE statement option cannot be null")));
3850 (
errcode(ERRCODE_SYNTAX_ERROR),
3851 errmsg(
"RAISE option already specified: %s",
3888 if (err_code == 0 &&
stmt->elog_level >=
ERROR)
3889 err_code = ERRCODE_RAISE_EXCEPTION;
3892 if (err_message == NULL)
3896 err_message = condname;
3908 (err_code ?
errcode(err_code) : 0,
3911 (err_hint != NULL) ?
errhint(
"%s", err_hint) : 0,
3912 (err_column != NULL) ?
3914 (err_constraint != NULL) ?
3916 (err_datatype != NULL) ?
3918 (err_table != NULL) ?
3920 (err_schema != NULL) ?
3946 if (isnull || !
value)
3948 char *message = NULL;
3950 if (
stmt->message != NULL)
3957 &isnull, &
typeid, &typmod);
3964 (
errcode(ERRCODE_ASSERT_FAILURE),
3966 errmsg(
"assertion failed")));
3980 EState *simple_eval_estate,
3988 estate->
func = func;
4048 if (simple_eval_estate)
4076 if (simple_eval_resowner)
4121 if ((*plpgsql_plugin_ptr)->func_setup)
4122 ((*plpgsql_plugin_ptr)->func_setup) (estate, func);
4188 options.parserSetupArg = expr;
4190 options.cursorOptions = cursorOptions;
4193 elog(
ERROR,
"SPI_prepare_extended failed for \"%s\": %s",
4219 int too_many_rows_level = 0;
4222 too_many_rows_level =
ERROR;
4224 too_many_rows_level =
WARNING;
4230 if (expr->
plan == NULL)
4233 if (!
stmt->mod_stmt_set)
4237 stmt->mod_stmt =
false;
4249 if (plansource->
commandTag == CMDTAG_INSERT ||
4254 stmt->mod_stmt =
true;
4258 stmt->mod_stmt_set =
true;
4280 if (
stmt->strict ||
stmt->mod_stmt || too_many_rows_level)
4337 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4338 errmsg(
"cannot COPY to/from client in PL/pgSQL")));
4343 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4344 errmsg(
"unsupported transaction command in PL/pgSQL")));
4348 elog(
ERROR,
"SPI_execute_plan_with_paramlist failed executing query \"%s\": %s",
4366 (
errcode(ERRCODE_SYNTAX_ERROR),
4367 errmsg(
"INTO used with a command that cannot return data")));
4389 (
errcode(ERRCODE_NO_DATA_FOUND),
4390 errmsg(
"query returned no rows"),
4398 if (n > 1 && (
stmt->strict ||
stmt->mod_stmt || too_many_rows_level))
4408 errlevel = (
stmt->strict ||
stmt->mod_stmt) ?
ERROR : too_many_rows_level;
4411 (
errcode(ERRCODE_TOO_MANY_ROWS),
4412 errmsg(
"query returned more than one row"),
4414 errhint(
"Make sure the query returns a single row, or use LIMIT 1.")));
4429 (
errcode(ERRCODE_SYNTAX_ERROR),
4430 errmsg(
"query has no destination for result data"),
4431 (rc ==
SPI_OK_SELECT) ?
errhint(
"If you want to discard the results of a SELECT, use PERFORM instead.") : 0));
4464 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4465 errmsg(
"query string argument of EXECUTE is null")));
4519 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4520 errmsg(
"EXECUTE of SELECT ... INTO is not implemented"),
4521 errhint(
"You might want to use EXECUTE ... INTO or EXECUTE CREATE TABLE ... AS instead.")));
4527 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4528 errmsg(
"cannot COPY to/from client in PL/pgSQL")));
4533 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4534 errmsg(
"EXECUTE of transaction commands is not implemented")));
4538 elog(
ERROR,
"SPI_execute_extended failed executing query \"%s\": %s",
4556 (
errcode(ERRCODE_SYNTAX_ERROR),
4557 errmsg(
"INTO used with a command that cannot return data")));
4579 (
errcode(ERRCODE_NO_DATA_FOUND),
4580 errmsg(
"query returned no rows"),
4588 if (n > 1 &&
stmt->strict)
4598 (
errcode(ERRCODE_TOO_MANY_ROWS),
4599 errmsg(
"query returned more than one row"),
4665 char *curname = NULL;
4688 (
errcode(ERRCODE_DUPLICATE_CURSOR),
4689 errmsg(
"cursor \"%s\" already in use", curname)));
4696 if (
stmt->query != NULL)
4705 query =
stmt->query;
4706 if (query->
plan == NULL)
4709 else if (
stmt->dynquery != NULL)
4719 stmt->cursor_options);
4729 if (curname == NULL)
4746 if (
stmt->argquery != NULL)
4758 (
errcode(ERRCODE_SYNTAX_ERROR),
4759 errmsg(
"arguments given for cursor without arguments")));
4761 memset(&set_args, 0,
sizeof(set_args));
4765 set_args.
into =
true;
4771 elog(
ERROR,
"open cursor failed during argument processing");
4777 (
errcode(ERRCODE_SYNTAX_ERROR),
4778 errmsg(
"arguments required for cursor")));
4782 if (query->
plan == NULL)
4798 elog(
ERROR,
"could not open cursor: %s",
4805 if (curname == NULL)
4829 long how_many =
stmt->how_many;
4843 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4844 errmsg(
"cursor variable \"%s\" is null", curvar->
refname)));
4854 (
errcode(ERRCODE_UNDEFINED_CURSOR),
4855 errmsg(
"cursor \"%s\" does not exist", curname)));
4867 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4868 errmsg(
"relative or absolute cursor position is null")));
4931 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4932 errmsg(
"cursor variable \"%s\" is null", curvar->
refname)));
4942 (
errcode(ERRCODE_UNDEFINED_CURSOR),
4943 errmsg(
"cursor \"%s\" does not exist", curname)));
5018 if (expr->
plan == NULL)
5084 switch (target->
dtype)
5105 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
5106 errmsg(
"null value cannot be assigned to variable \"%s\" declared NOT NULL",
5155 if (var->
value != newvalue || var->
isnull || isNull)
5181 (
errcode(ERRCODE_DATATYPE_MISMATCH),
5182 errmsg(
"cannot assign non-composite value to a row variable")));