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,
382 Portal portal,
bool prefetch_ok);
386 int paramid,
bool speculative,
390 Datum *resv,
bool *resnull);
443 Datum newvalue,
bool isnull,
bool freeable);
452 const char *portalname,
int cursorOptions);
482 EState *simple_eval_estate,
496 simple_eval_estate, simple_eval_resowner);
504 plerrcontext.
arg = &estate;
616 ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
627 (
errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
628 errmsg(
"control reached end of function without RETURN")));
645 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
646 errmsg(
"set-valued function called in context that cannot accept a set")));
650 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
651 errmsg(
"materialize mode required, but it is not allowed in this context")));
733 elog(
ERROR,
"return type must be a row type");
784 ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
833 gettext_noop(
"returned record type does not match expected record type"));
897 gettext_noop(
"returned record type does not match expected record type"));
943 plerrcontext.
arg = &estate;
1002 for (
int i = 0;
i < tupdesc->
natts;
i++)
1003 if (
TupleDescAttr(tupdesc,
i)->attgenerated == ATTRIBUTE_GENERATED_STORED)
1017 elog(
ERROR,
"unrecognized trigger action: not INSERT, DELETE, or UPDATE");
1034 ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
1045 (
errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
1046 errmsg(
"control reached end of trigger procedure without RETURN")));
1053 (
errcode(ERRCODE_DATATYPE_MISMATCH),
1054 errmsg(
"trigger procedure cannot return a set")));
1093 gettext_noop(
"returned row structure does not match the structure of the triggering table"));
1121 gettext_noop(
"returned row structure does not match the structure of the triggering table"));
1138 ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
1178 plerrcontext.
arg = &estate;
1192 ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
1203 (
errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
1204 errmsg(
"control reached end of trigger procedure without RETURN")));
1213 ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
1262 errcontext(
"PL/pgSQL function %s line %d %s",
1278 else if (estate->
err_stmt != NULL && err_lineno > 0)
1281 errcontext(
"PL/pgSQL function %s line %d at %s",
1300 int ndatums = estate->
ndatums;
1316 ws_next = workspace;
1320 outdatums = estate->
datums;
1321 for (
i = 0;
i < ndatums;
i++)
1327 switch (indatum->
dtype)
1359 outdatums[
i] = outdatum;
1392 elog(
ERROR,
"trigger promise is not in a trigger function");
1401 elog(
ERROR,
"trigger promise is not in a trigger function");
1409 elog(
ERROR,
"unrecognized trigger execution time: not BEFORE, AFTER, or INSTEAD OF");
1414 elog(
ERROR,
"trigger promise is not in a trigger function");
1420 elog(
ERROR,
"unrecognized trigger event type: not ROW or STATEMENT");
1425 elog(
ERROR,
"trigger promise is not in a trigger function");
1435 elog(
ERROR,
"unrecognized trigger action: not INSERT, DELETE, UPDATE, or TRUNCATE");
1440 elog(
ERROR,
"trigger promise is not in a trigger function");
1448 elog(
ERROR,
"trigger promise is not in a trigger function");
1457 elog(
ERROR,
"trigger promise is not in a trigger function");
1466 elog(
ERROR,
"trigger promise is not in a trigger function");
1474 elog(
ERROR,
"trigger promise is not in a trigger function");
1488 for (
i = 0;
i < nelems;
i++)
1497 -1,
false, TYPALIGN_INT)),
1508 elog(
ERROR,
"event trigger promise is not in an event trigger function");
1514 elog(
ERROR,
"event trigger promise is not in an event trigger function");
1537 "PLpgSQL per-statement data",
1584 for (; cond != NULL; cond = cond->
next)
1593 if (sqlerrstate == 0)
1595 if (edata->
sqlerrcode != ERRCODE_QUERY_CANCELED &&
1629 ((*plpgsql_plugin_ptr)->stmt_beg) (estate, (
PLpgSQL_stmt *) block);
1637 ((*plpgsql_plugin_ptr)->stmt_end) (estate, (
PLpgSQL_stmt *) block);
1652 volatile int rc = -1;
1677 switch (datum->
dtype)
1811 resTypByVal, resTypLen);
1962 if (block->
label == NULL)
2008 ((*plpgsql_plugin_ptr)->stmt_beg) (estate,
stmt);
2012 switch (
stmt->cmd_type)
2131 ((*plpgsql_plugin_ptr)->stmt_end) (estate,
stmt);
2196 if (expr->
plan == NULL)
2209 if (
stmt->is_call &&
stmt->target == NULL)
2226 options.allow_nonatomic =
true;
2232 elog(
ERROR,
"SPI_execute_plan_extended failed executing query \"%s\": %s",
2237 if (before_lxid != after_lxid)
2257 elog(
ERROR,
"DO statement returned a row");
2262 elog(
ERROR,
"procedure call returned more than one row");
2300 elog(
ERROR,
"query for CALL statement is not a CallStmt");
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",
2408 (
errcode(ERRCODE_STACKED_DIAGNOSTICS_ACCESSED_WITHOUT_ACTIVE_HANDLER),
2409 errmsg(
"GET STACKED DIAGNOSTICS cannot be used outside an exception handler")));
2411 foreach(lc,
stmt->diag_items)
2416 switch (diag_item->
kind)
2421 false, INT8OID, -1);
2482 char *contextstackstr;
2495 elog(
ERROR,
"unrecognized diagnostic item kind: %d",
2520 if (!isnull &&
value)
2523 foreach(lc,
stmt->elsif_list)
2529 if (!isnull &&
value)
2548 if (
stmt->t_expr != NULL)
2556 &isnull, &t_typoid, &t_typmod);
2587 foreach(l,
stmt->case_when_list)
2594 if (!isnull &&
value)
2612 if (!
stmt->have_else)
2614 (
errcode(ERRCODE_CASE_NOT_FOUND),
2615 errmsg(
"case not found"),
2616 errhint(
"CASE statement is missing ELSE part.")));
2663 if (isnull || !
value)
2701 &isnull, &valtype, &valtypmod);
2708 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2709 errmsg(
"lower bound of FOR loop cannot be null")));
2717 &isnull, &valtype, &valtypmod);
2724 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2725 errmsg(
"upper bound of FOR loop cannot be null")));
2735 &isnull, &valtype, &valtypmod);
2742 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2743 errmsg(
"BY value of FOR loop cannot be null")));
2746 if (step_value <= 0)
2748 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2749 errmsg(
"BY value of FOR loop must be greater than zero")));
2764 if (loop_value < end_value)
2769 if (loop_value > end_value)
2795 loop_value -= step_value;
2801 loop_value += step_value;
2858 char *curname = NULL;
2882 (
errcode(ERRCODE_DUPLICATE_CURSOR),
2883 errmsg(
"cursor \"%s\" already in use", curname)));
2893 if (
stmt->argquery != NULL)
2905 (
errcode(ERRCODE_SYNTAX_ERROR),
2906 errmsg(
"arguments given for cursor without arguments")));
2908 memset(&set_args, 0,
sizeof(set_args));
2912 set_args.
into =
true;
2918 elog(
ERROR,
"open cursor failed during argument processing");
2924 (
errcode(ERRCODE_SYNTAX_ERROR),
2925 errmsg(
"arguments required for cursor")));
2931 if (query->
plan == NULL)
2946 elog(
ERROR,
"could not open cursor: %s",
2953 if (curname == NULL)
2978 if (curname == NULL)
3000 Oid loop_var_elem_type;
3006 Oid iterator_result_type;
3007 int32 iterator_result_typmod;
3015 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3016 errmsg(
"FOREACH expression must not be null")));
3030 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3031 errmsg(
"FOREACH expression must yield an array, not type %s",
3047 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3048 errmsg(
"slice dimension (%d) is out of the valid range 0..%d",
3074 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3075 errmsg(
"FOREACH ... SLICE loop variable must be of an array type")));
3078 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3079 errmsg(
"FOREACH loop variable must not be of an array type")));
3085 if (
stmt->slice > 0)
3088 iterator_result_type = arrtype;
3089 iterator_result_typmod = arrtypmod;
3095 iterator_result_typmod = arrtypmod;
3108 iterator_result_type, iterator_result_typmod);
3111 if (
stmt->slice > 0)
3155 if (
stmt->cond != NULL)
3162 if (isnull ||
value ==
false)
3210 if (
stmt->retvarno >= 0)
3214 switch (retvar->
dtype)
3240 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3241 errmsg(
"cannot return non-composite value from function returning composite type")));
3281 if (
stmt->expr != NULL)
3297 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3298 errmsg(
"cannot return non-composite value from function returning composite type")));
3336 (
errcode(ERRCODE_SYNTAX_ERROR),
3337 errmsg(
"cannot use RETURN NEXT in a non-SETOF function")));
3344 natts = tupdesc->
natts;
3356 if (
stmt->retvarno >= 0)
3360 switch (retvar->
dtype)
3372 bool isNull = var->
isnull;
3377 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3378 errmsg(
"wrong result type supplied in RETURN NEXT")));
3406 if (rec->
erh == NULL)
3416 gettext_noop(
"wrong record type supplied in RETURN NEXT"));
3436 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3437 errmsg(
"wrong record type supplied in RETURN NEXT")));
3448 else if (
stmt->expr)
3472 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3473 errmsg(
"cannot return non-composite value from function returning composite type")));
3480 gettext_noop(
"returned record type does not match expected record type"));
3493 nulldatums = (
Datum *)
3495 nullflags = (
bool *)
3497 memset(nullflags,
true, natts *
sizeof(
bool));
3499 nulldatums, nullflags);
3509 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3510 errmsg(
"wrong result type supplied in RETURN NEXT")));
3528 (
errcode(ERRCODE_SYNTAX_ERROR),
3529 errmsg(
"RETURN NEXT must have a parameter")));
3556 (
errcode(ERRCODE_SYNTAX_ERROR),
3557 errmsg(
"cannot use RETURN QUERY in a non-SETOF function")));
3575 gettext_noop(
"structure of query does not match function result type"));
3578 if (
stmt->query != NULL)
3588 if (expr->
plan == NULL)
3602 options.must_return_tuples =
true;
3607 elog(
ERROR,
"SPI_execute_plan_extended failed executing query \"%s\": %s",
3626 &isnull, &restype, &restypmod);
3629 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3630 errmsg(
"query string argument of EXECUTE is null")));
3645 options.must_return_tuples =
true;
3650 elog(
ERROR,
"SPI_execute_extended failed executing query \"%s\": %s",
3680 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3681 errmsg(
"set-valued function called in context that cannot accept a set")));
3686 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3687 errmsg(
"materialize mode required, but it is not allowed in this context")));
3710 #define SET_RAISE_OPTION_TEXT(opt, name) \
3714 (errcode(ERRCODE_SYNTAX_ERROR), \
3715 errmsg("RAISE option already specified: %s", \
3717 opt = MemoryContextStrdup(stmt_mcontext, extval); \
3728 char *condname = NULL;
3729 char *err_message = NULL;
3730 char *err_detail = NULL;
3731 char *err_hint = NULL;
3732 char *err_column = NULL;
3733 char *err_constraint = NULL;
3734 char *err_datatype = NULL;
3735 char *err_table = NULL;
3736 char *err_schema = NULL;
3741 if (
stmt->condname == NULL &&
stmt->message == NULL &&
3748 (
errcode(ERRCODE_STACKED_DIAGNOSTICS_ACCESSED_WITHOUT_ACTIVE_HANDLER),
3749 errmsg(
"RAISE without parameters cannot be used outside an exception handler")));
3775 for (cp =
stmt->message; *cp; cp++)
3797 if (current_param == NULL)
3798 elog(
ERROR,
"unexpected RAISE parameter list length");
3813 current_param =
lnext(
stmt->params, current_param);
3821 if (current_param != NULL)
3822 elog(
ERROR,
"unexpected RAISE parameter list length");
3824 err_message = ds.
data;
3827 foreach(lc,
stmt->options)
3842 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3843 errmsg(
"RAISE statement option cannot be null")));
3852 (
errcode(ERRCODE_SYNTAX_ERROR),
3853 errmsg(
"RAISE option already specified: %s",
3890 if (err_code == 0 &&
stmt->elog_level >=
ERROR)
3891 err_code = ERRCODE_RAISE_EXCEPTION;
3894 if (err_message == NULL)
3898 err_message = condname;
3910 (err_code ?
errcode(err_code) : 0,
3913 (err_hint != NULL) ?
errhint(
"%s", err_hint) : 0,
3914 (err_column != NULL) ?
3916 (err_constraint != NULL) ?
3918 (err_datatype != NULL) ?
3920 (err_table != NULL) ?
3922 (err_schema != NULL) ?
3948 if (isnull || !
value)
3950 char *message = NULL;
3952 if (
stmt->message != NULL)
3959 &isnull, &
typeid, &typmod);
3966 (
errcode(ERRCODE_ASSERT_FAILURE),
3968 errmsg(
"assertion failed")));
3982 EState *simple_eval_estate,
3990 estate->
func = func;
4050 if (simple_eval_estate)
4078 if (simple_eval_resowner)
4123 if ((*plpgsql_plugin_ptr)->func_setup)
4124 ((*plpgsql_plugin_ptr)->func_setup) (estate, func);
4190 options.parserSetupArg = (
void *) expr;
4192 options.cursorOptions = cursorOptions;
4195 elog(
ERROR,
"SPI_prepare_extended failed for \"%s\": %s",
4221 int too_many_rows_level = 0;
4224 too_many_rows_level =
ERROR;
4226 too_many_rows_level =
WARNING;
4232 if (expr->
plan == NULL)
4235 if (!
stmt->mod_stmt_set)
4239 stmt->mod_stmt =
false;
4250 if (plansource->
commandTag == CMDTAG_INSERT ||
4255 stmt->mod_stmt =
true;
4259 stmt->mod_stmt_set =
true;
4281 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")));
4518 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4519 errmsg(
"EXECUTE of SELECT ... INTO is not implemented"),
4520 errhint(
"You might want to use EXECUTE ... INTO or EXECUTE CREATE TABLE ... AS instead.")));
4526 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4527 errmsg(
"cannot COPY to/from client in PL/pgSQL")));
4532 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4533 errmsg(
"EXECUTE of transaction commands is not implemented")));
4537 elog(
ERROR,
"SPI_execute_extended failed executing query \"%s\": %s",
4555 (
errcode(ERRCODE_SYNTAX_ERROR),
4556 errmsg(
"INTO used with a command that cannot return data")));
4578 (
errcode(ERRCODE_NO_DATA_FOUND),
4579 errmsg(
"query returned no rows"),
4587 if (n > 1 &&
stmt->strict)
4597 (
errcode(ERRCODE_TOO_MANY_ROWS),
4598 errmsg(
"query returned more than one row"),
4664 char *curname = NULL;
4687 (
errcode(ERRCODE_DUPLICATE_CURSOR),
4688 errmsg(
"cursor \"%s\" already in use", curname)));
4695 if (
stmt->query != NULL)
4704 query =
stmt->query;
4705 if (query->
plan == NULL)
4708 else if (
stmt->dynquery != NULL)
4718 stmt->cursor_options);
4728 if (curname == NULL)
4745 if (
stmt->argquery != NULL)
4757 (
errcode(ERRCODE_SYNTAX_ERROR),
4758 errmsg(
"arguments given for cursor without arguments")));
4760 memset(&set_args, 0,
sizeof(set_args));
4764 set_args.
into =
true;
4770 elog(
ERROR,
"open cursor failed during argument processing");
4776 (
errcode(ERRCODE_SYNTAX_ERROR),
4777 errmsg(
"arguments required for cursor")));
4781 if (query->
plan == NULL)
4797 elog(
ERROR,
"could not open cursor: %s",
4804 if (curname == NULL)
4828 long how_many =
stmt->how_many;
4842 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4843 errmsg(
"cursor variable \"%s\" is null", curvar->
refname)));
4853 (
errcode(ERRCODE_UNDEFINED_CURSOR),
4854 errmsg(
"cursor \"%s\" does not exist", curname)));
4866 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4867 errmsg(
"relative or absolute cursor position is null")));
4930 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4931 errmsg(
"cursor variable \"%s\" is null", curvar->
refname)));
4941 (
errcode(ERRCODE_UNDEFINED_CURSOR),
4942 errmsg(
"cursor \"%s\" does not exist", curname)));
5017 if (expr->
plan == NULL)
5083 switch (target->
dtype)
5104 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
5105 errmsg(
"null value cannot be assigned to variable \"%s\" declared NOT NULL",
5154 if (var->
value != newvalue || var->
isnull || isNull)
5180 (
errcode(ERRCODE_DATATYPE_MISMATCH),
5181 errmsg(
"cannot assign non-composite value to a row variable")));
5199 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
5200 errmsg(
"null value cannot be assigned to variable \"%s\" declared NOT NULL",
5212 (
errcode(ERRCODE_DATATYPE_MISMATCH),
5213 errmsg(
"cannot assign non-composite value to a record variable")));
5254 (
errcode(ERRCODE_UNDEFINED_COLUMN),
5255 errmsg(
"record \"%s\" has no field \"%s\"",
5263 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5264 errmsg(
"cannot assign to system column \"%s\"",
5314 switch (datum->
dtype)
5340 elog(
ERROR,
"row variable has no tupdesc");
5346 elog(
ERROR,
"row not compatible with its own tupdesc");
5359 if (rec->
erh == NULL)
5427 (
errcode(ERRCODE_UNDEFINED_COLUMN),
5428 errmsg(
"record \"%s\" has no field \"%s\"",
5461 switch (datum->
dtype)
5501 if (rec->
erh == NULL)
5514 (
errcode(ERRCODE_UNDEFINED_COLUMN),
5515 errmsg(
"record \"%s\" has no field \"%s\"",
5545 switch (datum->
dtype)