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)
208 #define LOOP_RC_PROCESSING(looplabel, exit_action) \
209 if (rc == PLPGSQL_RC_RETURN) \
214 else if (rc == PLPGSQL_RC_EXIT) \
216 if (estate->exitlabel == NULL) \
219 rc = PLPGSQL_RC_OK; \
222 else if ((looplabel) != NULL && \
223 strcmp(looplabel, estate->exitlabel) == 0) \
226 estate->exitlabel = NULL; \
227 rc = PLPGSQL_RC_OK; \
236 else if (rc == PLPGSQL_RC_CONTINUE) \
238 if (estate->exitlabel == NULL) \
241 rc = PLPGSQL_RC_OK; \
243 else if ((looplabel) != NULL && \
244 strcmp(looplabel, estate->exitlabel) == 0) \
247 estate->exitlabel = NULL; \
248 rc = PLPGSQL_RC_OK; \
257 Assert(rc == PLPGSQL_RC_OK)
335 EState *simple_eval_estate,
383 Portal portal,
bool prefetch_ok);
387 int paramid,
bool speculative,
391 Datum *resv,
bool *resnull);
444 Datum newvalue,
bool isnull,
bool freeable);
453 const char *portalname,
int cursorOptions);
483 EState *simple_eval_estate,
497 simple_eval_estate, simple_eval_resowner);
505 plerrcontext.
arg = &estate;
617 ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
628 (
errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
629 errmsg(
"control reached end of function without RETURN")));
646 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
647 errmsg(
"set-valued function called in context that cannot accept a set")));
651 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
652 errmsg(
"materialize mode required, but it is not allowed in this context")));
734 elog(
ERROR,
"return type must be a row type");
785 ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
834 gettext_noop(
"returned record type does not match expected record type"));
898 gettext_noop(
"returned record type does not match expected record type"));
944 plerrcontext.
arg = &estate;
1003 for (
int i = 0;
i < tupdesc->
natts;
i++)
1004 if (
TupleDescAttr(tupdesc,
i)->attgenerated == ATTRIBUTE_GENERATED_STORED)
1018 elog(
ERROR,
"unrecognized trigger action: not INSERT, DELETE, or UPDATE");
1035 ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
1046 (
errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
1047 errmsg(
"control reached end of trigger procedure without RETURN")));
1054 (
errcode(ERRCODE_DATATYPE_MISMATCH),
1055 errmsg(
"trigger procedure cannot return a set")));
1094 gettext_noop(
"returned row structure does not match the structure of the triggering table"));
1122 gettext_noop(
"returned row structure does not match the structure of the triggering table"));
1139 ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
1179 plerrcontext.
arg = &estate;
1193 ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
1204 (
errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
1205 errmsg(
"control reached end of trigger procedure without RETURN")));
1214 ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
1263 errcontext(
"PL/pgSQL function %s line %d %s",
1279 else if (estate->
err_stmt != NULL && err_lineno > 0)
1282 errcontext(
"PL/pgSQL function %s line %d at %s",
1301 int ndatums = estate->
ndatums;
1317 ws_next = workspace;
1321 outdatums = estate->
datums;
1322 for (
i = 0;
i < ndatums;
i++)
1328 switch (indatum->
dtype)
1360 outdatums[
i] = outdatum;
1393 elog(
ERROR,
"trigger promise is not in a trigger function");
1402 elog(
ERROR,
"trigger promise is not in a trigger function");
1410 elog(
ERROR,
"unrecognized trigger execution time: not BEFORE, AFTER, or INSTEAD OF");
1415 elog(
ERROR,
"trigger promise is not in a trigger function");
1421 elog(
ERROR,
"unrecognized trigger event type: not ROW or STATEMENT");
1426 elog(
ERROR,
"trigger promise is not in a trigger function");
1436 elog(
ERROR,
"unrecognized trigger action: not INSERT, DELETE, UPDATE, or TRUNCATE");
1441 elog(
ERROR,
"trigger promise is not in a trigger function");
1449 elog(
ERROR,
"trigger promise is not in a trigger function");
1458 elog(
ERROR,
"trigger promise is not in a trigger function");
1467 elog(
ERROR,
"trigger promise is not in a trigger function");
1475 elog(
ERROR,
"trigger promise is not in a trigger function");
1489 for (
i = 0;
i < nelems;
i++)
1498 -1,
false, TYPALIGN_INT)),
1509 elog(
ERROR,
"event trigger promise is not in an event trigger function");
1515 elog(
ERROR,
"event trigger promise is not in an event trigger function");
1538 "PLpgSQL per-statement data",
1585 for (; cond != NULL; cond = cond->
next)
1594 if (sqlerrstate == 0)
1596 if (edata->
sqlerrcode != ERRCODE_QUERY_CANCELED &&
1630 ((*plpgsql_plugin_ptr)->stmt_beg) (estate, (
PLpgSQL_stmt *) block);
1638 ((*plpgsql_plugin_ptr)->stmt_end) (estate, (
PLpgSQL_stmt *) block);
1653 volatile int rc = -1;
1678 switch (datum->
dtype)
1812 resTypByVal, resTypLen);
1963 if (block->
label == NULL)
2009 ((*plpgsql_plugin_ptr)->stmt_beg) (estate,
stmt);
2013 switch (
stmt->cmd_type)
2132 ((*plpgsql_plugin_ptr)->stmt_end) (estate,
stmt);
2197 if (expr->
plan == NULL)
2210 if (
stmt->is_call &&
stmt->target == NULL)
2227 options.allow_nonatomic =
true;
2233 elog(
ERROR,
"SPI_execute_plan_extended failed executing query \"%s\": %s",
2238 if (before_lxid != after_lxid)
2258 elog(
ERROR,
"DO statement returned a row");
2263 elog(
ERROR,
"procedure call returned more than one row");
2303 elog(
ERROR,
"query for CALL statement is not a CallStmt");
2307 elog(
ERROR,
"query for CALL statement is not a CallStmt");
2309 funcexpr =
stmt->funcexpr;
2314 elog(
ERROR,
"cache lookup failed for function %u",
2333 row->
refname =
"(unnamed row)";
2345 for (
i = 0;
i < numargs;
i++)
2348 (argmodes[
i] == PROARGMODE_INOUT ||
2349 argmodes[
i] == PROARGMODE_OUT))
2362 row->
varnos[nfields++] = dno;
2367 if (argnames && argnames[
i] && argnames[
i][0])
2369 (
errcode(ERRCODE_SYNTAX_ERROR),
2370 errmsg(
"procedure parameter \"%s\" is an output parameter but corresponding argument is not writable",
2374 (
errcode(ERRCODE_SYNTAX_ERROR),
2375 errmsg(
"procedure parameter %d is an output parameter but corresponding argument is not writable",
2410 (
errcode(ERRCODE_STACKED_DIAGNOSTICS_ACCESSED_WITHOUT_ACTIVE_HANDLER),
2411 errmsg(
"GET STACKED DIAGNOSTICS cannot be used outside an exception handler")));
2413 foreach(lc,
stmt->diag_items)
2418 switch (diag_item->
kind)
2423 false, INT8OID, -1);
2484 char *contextstackstr;
2497 elog(
ERROR,
"unrecognized diagnostic item kind: %d",
2522 if (!isnull &&
value)
2525 foreach(lc,
stmt->elsif_list)
2531 if (!isnull &&
value)
2550 if (
stmt->t_expr != NULL)
2558 &isnull, &t_typoid, &t_typmod);
2589 foreach(l,
stmt->case_when_list)
2596 if (!isnull &&
value)
2614 if (!
stmt->have_else)
2616 (
errcode(ERRCODE_CASE_NOT_FOUND),
2617 errmsg(
"case not found"),
2618 errhint(
"CASE statement is missing ELSE part.")));
2665 if (isnull || !
value)
2703 &isnull, &valtype, &valtypmod);
2710 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2711 errmsg(
"lower bound of FOR loop cannot be null")));
2719 &isnull, &valtype, &valtypmod);
2726 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2727 errmsg(
"upper bound of FOR loop cannot be null")));
2737 &isnull, &valtype, &valtypmod);
2744 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2745 errmsg(
"BY value of FOR loop cannot be null")));
2748 if (step_value <= 0)
2750 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2751 errmsg(
"BY value of FOR loop must be greater than zero")));
2766 if (loop_value < end_value)
2771 if (loop_value > end_value)
2797 loop_value -= step_value;
2803 loop_value += step_value;
2860 char *curname = NULL;
2884 (
errcode(ERRCODE_DUPLICATE_CURSOR),
2885 errmsg(
"cursor \"%s\" already in use", curname)));
2895 if (
stmt->argquery != NULL)
2907 (
errcode(ERRCODE_SYNTAX_ERROR),
2908 errmsg(
"arguments given for cursor without arguments")));
2910 memset(&set_args, 0,
sizeof(set_args));
2914 set_args.
into =
true;
2920 elog(
ERROR,
"open cursor failed during argument processing");
2926 (
errcode(ERRCODE_SYNTAX_ERROR),
2927 errmsg(
"arguments required for cursor")));
2933 if (query->
plan == NULL)
2948 elog(
ERROR,
"could not open cursor: %s",
2955 if (curname == NULL)
2980 if (curname == NULL)
3002 Oid loop_var_elem_type;
3008 Oid iterator_result_type;
3009 int32 iterator_result_typmod;
3017 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3018 errmsg(
"FOREACH expression must not be null")));
3032 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3033 errmsg(
"FOREACH expression must yield an array, not type %s",
3049 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3050 errmsg(
"slice dimension (%d) is out of the valid range 0..%d",
3076 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3077 errmsg(
"FOREACH ... SLICE loop variable must be of an array type")));
3080 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3081 errmsg(
"FOREACH loop variable must not be of an array type")));
3087 if (
stmt->slice > 0)
3090 iterator_result_type = arrtype;
3091 iterator_result_typmod = arrtypmod;
3097 iterator_result_typmod = arrtypmod;
3110 iterator_result_type, iterator_result_typmod);
3113 if (
stmt->slice > 0)
3157 if (
stmt->cond != NULL)
3164 if (isnull ||
value ==
false)
3212 if (
stmt->retvarno >= 0)
3216 switch (retvar->
dtype)
3242 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3243 errmsg(
"cannot return non-composite value from function returning composite type")));
3283 if (
stmt->expr != NULL)
3299 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3300 errmsg(
"cannot return non-composite value from function returning composite type")));
3338 (
errcode(ERRCODE_SYNTAX_ERROR),
3339 errmsg(
"cannot use RETURN NEXT in a non-SETOF function")));
3346 natts = tupdesc->
natts;
3358 if (
stmt->retvarno >= 0)
3362 switch (retvar->
dtype)
3374 bool isNull = var->
isnull;
3379 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3380 errmsg(
"wrong result type supplied in RETURN NEXT")));
3408 if (rec->
erh == NULL)
3418 gettext_noop(
"wrong record type supplied in RETURN NEXT"));
3438 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3439 errmsg(
"wrong record type supplied in RETURN NEXT")));
3450 else if (
stmt->expr)
3474 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3475 errmsg(
"cannot return non-composite value from function returning composite type")));
3482 gettext_noop(
"returned record type does not match expected record type"));
3495 nulldatums = (
Datum *)
3497 nullflags = (
bool *)
3499 memset(nullflags,
true, natts *
sizeof(
bool));
3501 nulldatums, nullflags);
3511 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3512 errmsg(
"wrong result type supplied in RETURN NEXT")));
3530 (
errcode(ERRCODE_SYNTAX_ERROR),
3531 errmsg(
"RETURN NEXT must have a parameter")));
3558 (
errcode(ERRCODE_SYNTAX_ERROR),
3559 errmsg(
"cannot use RETURN QUERY in a non-SETOF function")));
3577 gettext_noop(
"structure of query does not match function result type"));
3580 if (
stmt->query != NULL)
3590 if (expr->
plan == NULL)
3604 options.must_return_tuples =
true;
3609 elog(
ERROR,
"SPI_execute_plan_extended failed executing query \"%s\": %s",
3628 &isnull, &restype, &restypmod);
3631 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3632 errmsg(
"query string argument of EXECUTE is null")));
3647 options.must_return_tuples =
true;
3652 elog(
ERROR,
"SPI_execute_extended failed executing query \"%s\": %s",
3682 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3683 errmsg(
"set-valued function called in context that cannot accept a set")));
3688 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3689 errmsg(
"materialize mode required, but it is not allowed in this context")));
3712 #define SET_RAISE_OPTION_TEXT(opt, name) \
3716 (errcode(ERRCODE_SYNTAX_ERROR), \
3717 errmsg("RAISE option already specified: %s", \
3719 opt = MemoryContextStrdup(stmt_mcontext, extval); \
3730 char *condname = NULL;
3731 char *err_message = NULL;
3732 char *err_detail = NULL;
3733 char *err_hint = NULL;
3734 char *err_column = NULL;
3735 char *err_constraint = NULL;
3736 char *err_datatype = NULL;
3737 char *err_table = NULL;
3738 char *err_schema = NULL;
3743 if (
stmt->condname == NULL &&
stmt->message == NULL &&
3750 (
errcode(ERRCODE_STACKED_DIAGNOSTICS_ACCESSED_WITHOUT_ACTIVE_HANDLER),
3751 errmsg(
"RAISE without parameters cannot be used outside an exception handler")));
3777 for (cp =
stmt->message; *cp; cp++)
3799 if (current_param == NULL)
3800 elog(
ERROR,
"unexpected RAISE parameter list length");
3815 current_param =
lnext(
stmt->params, current_param);
3823 if (current_param != NULL)
3824 elog(
ERROR,
"unexpected RAISE parameter list length");
3826 err_message = ds.
data;
3829 foreach(lc,
stmt->options)
3844 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3845 errmsg(
"RAISE statement option cannot be null")));
3854 (
errcode(ERRCODE_SYNTAX_ERROR),
3855 errmsg(
"RAISE option already specified: %s",
3892 if (err_code == 0 &&
stmt->elog_level >=
ERROR)
3893 err_code = ERRCODE_RAISE_EXCEPTION;
3896 if (err_message == NULL)
3900 err_message = condname;
3912 (err_code ?
errcode(err_code) : 0,
3915 (err_hint != NULL) ?
errhint(
"%s", err_hint) : 0,
3916 (err_column != NULL) ?
3918 (err_constraint != NULL) ?
3920 (err_datatype != NULL) ?
3922 (err_table != NULL) ?
3924 (err_schema != NULL) ?
3950 if (isnull || !
value)
3952 char *message = NULL;
3954 if (
stmt->message != NULL)
3961 &isnull, &
typeid, &typmod);
3968 (
errcode(ERRCODE_ASSERT_FAILURE),
3970 errmsg(
"assertion failed")));
3984 EState *simple_eval_estate,
3992 estate->
func = func;
4052 if (simple_eval_estate)
4080 if (simple_eval_resowner)
4125 if ((*plpgsql_plugin_ptr)->func_setup)
4126 ((*plpgsql_plugin_ptr)->func_setup) (estate, func);
4192 options.parserSetupArg = (
void *) expr;
4194 options.cursorOptions = cursorOptions;
4197 elog(
ERROR,
"SPI_prepare_extended failed for \"%s\": %s",
4223 int too_many_rows_level = 0;
4226 too_many_rows_level =
ERROR;
4228 too_many_rows_level =
WARNING;
4234 if (expr->
plan == NULL)
4237 if (!
stmt->mod_stmt_set)
4241 stmt->mod_stmt =
false;
4253 if (plansource->
commandTag == CMDTAG_INSERT ||
4258 stmt->mod_stmt =
true;
4262 stmt->mod_stmt_set =
true;
4284 if (
stmt->strict ||
stmt->mod_stmt || too_many_rows_level)
4341 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4342 errmsg(
"cannot COPY to/from client in PL/pgSQL")));
4347 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4348 errmsg(
"unsupported transaction command in PL/pgSQL")));
4352 elog(
ERROR,
"SPI_execute_plan_with_paramlist failed executing query \"%s\": %s",
4370 (
errcode(ERRCODE_SYNTAX_ERROR),
4371 errmsg(
"INTO used with a command that cannot return data")));
4393 (
errcode(ERRCODE_NO_DATA_FOUND),
4394 errmsg(
"query returned no rows"),
4402 if (n > 1 && (
stmt->strict ||
stmt->mod_stmt || too_many_rows_level))
4412 errlevel = (
stmt->strict ||
stmt->mod_stmt) ?
ERROR : too_many_rows_level;
4415 (
errcode(ERRCODE_TOO_MANY_ROWS),
4416 errmsg(
"query returned more than one row"),
4418 errhint(
"Make sure the query returns a single row, or use LIMIT 1.")));
4433 (
errcode(ERRCODE_SYNTAX_ERROR),
4434 errmsg(
"query has no destination for result data"),
4435 (rc ==
SPI_OK_SELECT) ?
errhint(
"If you want to discard the results of a SELECT, use PERFORM instead.") : 0));
4468 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4469 errmsg(
"query string argument of EXECUTE is null")));
4523 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4524 errmsg(
"EXECUTE of SELECT ... INTO is not implemented"),
4525 errhint(
"You might want to use EXECUTE ... INTO or EXECUTE CREATE TABLE ... AS instead.")));
4531 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4532 errmsg(
"cannot COPY to/from client in PL/pgSQL")));
4537 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4538 errmsg(
"EXECUTE of transaction commands is not implemented")));
4542 elog(
ERROR,
"SPI_execute_extended failed executing query \"%s\": %s",
4560 (
errcode(ERRCODE_SYNTAX_ERROR),
4561 errmsg(
"INTO used with a command that cannot return data")));
4583 (
errcode(ERRCODE_NO_DATA_FOUND),
4584 errmsg(
"query returned no rows"),
4592 if (n > 1 &&
stmt->strict)
4602 (
errcode(ERRCODE_TOO_MANY_ROWS),
4603 errmsg(
"query returned more than one row"),
4669 char *curname = NULL;
4692 (
errcode(ERRCODE_DUPLICATE_CURSOR),
4693 errmsg(
"cursor \"%s\" already in use", curname)));
4700 if (
stmt->query != NULL)
4709 query =
stmt->query;
4710 if (query->
plan == NULL)
4713 else if (
stmt->dynquery != NULL)
4723 stmt->cursor_options);
4733 if (curname == NULL)
4750 if (
stmt->argquery != NULL)
4762 (
errcode(ERRCODE_SYNTAX_ERROR),
4763 errmsg(
"arguments given for cursor without arguments")));
4765 memset(&set_args, 0,
sizeof(set_args));
4769 set_args.
into =
true;
4775 elog(
ERROR,
"open cursor failed during argument processing");
4781 (
errcode(ERRCODE_SYNTAX_ERROR),
4782 errmsg(
"arguments required for cursor")));
4786 if (query->
plan == NULL)
4802 elog(
ERROR,
"could not open cursor: %s",
4809 if (curname == NULL)
4833 long how_many =
stmt->how_many;
4847 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4848 errmsg(
"cursor variable \"%s\" is null", curvar->
refname)));
4858 (
errcode(ERRCODE_UNDEFINED_CURSOR),
4859 errmsg(
"cursor \"%s\" does not exist", curname)));
4871 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4872 errmsg(
"relative or absolute cursor position is null")));
4935 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4936 errmsg(
"cursor variable \"%s\" is null", curvar->
refname)));
4946 (
errcode(ERRCODE_UNDEFINED_CURSOR),
4947 errmsg(
"cursor \"%s\" does not exist", curname)));
5022 if (expr->
plan == NULL)
5088 switch (target->
dtype)
5109 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
5110 errmsg(
"null value cannot be assigned to variable \"%s\" declared NOT NULL",
5159 if (var->
value != newvalue || var->
isnull || isNull)
5185 (
errcode(ERRCODE_DATATYPE_MISMATCH),
5186 errmsg(
"cannot assign non-composite value to a row variable")));
5204 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
5205 errmsg(
"null value cannot be assigned to variable \"%s\" declared NOT NULL",
5217 (
errcode(ERRCODE_DATATYPE_MISMATCH),
5218 errmsg(
"cannot assign non-composite value to a record variable")));
5259 (
errcode(ERRCODE_UNDEFINED_COLUMN),
5260 errmsg(
"record \"%s\" has no field \"%s\"",
5268 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5269 errmsg(
"cannot assign to system column \"%s\"",
5319 switch (datum->
dtype)
5345 elog(
ERROR,
"row variable has no tupdesc");
5351 elog(
ERROR,
"row not compatible with its own tupdesc");
5364 if (rec->
erh == NULL)
5432 (
errcode(ERRCODE_UNDEFINED_COLUMN),
5433 errmsg(
"record \"%s\" has no field \"%s\"",
5466 switch (datum->
dtype)
5506 if (rec->
erh == NULL)
5519 (
errcode(ERRCODE_UNDEFINED_COLUMN),
5520 errmsg(
"record \"%s\" has no field \"%s\"",
5550 switch (datum->
dtype)