aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2016-11-06 12:09:36 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2016-11-06 12:09:36 -0500
commit674877e93a1b9217c692a5671e1118136959ee74 (patch)
treec2f5a46de324400b77df0c6bfb7dd8f6da377cf3
parent6e377ef0cc88851f9d077ae93f529701af81170d (diff)
downloadpostgresql-674877e93a1b9217c692a5671e1118136959ee74.tar.gz
postgresql-674877e93a1b9217c692a5671e1118136959ee74.zip
Need to do SPI_push/SPI_pop around expression evaluation in plpgsql.
We must do this in case the expression evaluation results in calling another plpgsql function (or, really, anything using SPI). I missed the need for this when I converted exec_cast_value() from doing a simple InputFunctionCall() to doing ExecEvalExpr() in commit 1345cc67b. There is a SPI_push_conditional in InputFunctionCall(), so that there was no bug before that. Per bug #14414 from Marcos Castedo. Add a regression test based on his example, which was that a plpgsql function in a domain check constraint didn't work when assigning to a domain-type variable within plpgsql. Report: <20161106010947.1387.66380@wrigleys.postgresql.org>
-rw-r--r--src/pl/plpgsql/src/pl_exec.c4
-rw-r--r--src/test/regress/expected/plpgsql.out19
-rw-r--r--src/test/regress/sql/plpgsql.sql22
3 files changed, 45 insertions, 0 deletions
diff --git a/src/pl/plpgsql/src/pl_exec.c b/src/pl/plpgsql/src/pl_exec.c
index fe84040097a..9a61f4ceec7 100644
--- a/src/pl/plpgsql/src/pl_exec.c
+++ b/src/pl/plpgsql/src/pl_exec.c
@@ -6015,6 +6015,8 @@ exec_cast_value(PLpgSQL_execstate *estate,
ExprContext *econtext = estate->eval_econtext;
MemoryContext oldcontext;
+ SPI_push();
+
oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
econtext->caseValue_datum = value;
@@ -6028,6 +6030,8 @@ exec_cast_value(PLpgSQL_execstate *estate,
cast_entry->cast_in_use = false;
MemoryContextSwitchTo(oldcontext);
+
+ SPI_pop();
}
}
diff --git a/src/test/regress/expected/plpgsql.out b/src/test/regress/expected/plpgsql.out
index 2b0454559df..98a37733983 100644
--- a/src/test/regress/expected/plpgsql.out
+++ b/src/test/regress/expected/plpgsql.out
@@ -5611,3 +5611,22 @@ end;
$$;
ERROR: unhandled assertion
CONTEXT: PL/pgSQL function inline_code_block line 3 at ASSERT
+-- Test use of plpgsql in a domain check constraint (cf. bug #14414)
+create function plpgsql_domain_check(val int) returns boolean as $$
+begin return val > 0; end
+$$ language plpgsql immutable;
+create domain plpgsql_domain as integer check(plpgsql_domain_check(value));
+do $$
+declare v_test plpgsql_domain;
+begin
+ v_test := 1;
+end;
+$$;
+do $$
+declare v_test plpgsql_domain := 1;
+begin
+ v_test := 0; -- fail
+end;
+$$;
+ERROR: value for domain plpgsql_domain violates check constraint "plpgsql_domain_check"
+CONTEXT: PL/pgSQL function inline_code_block line 4 at assignment
diff --git a/src/test/regress/sql/plpgsql.sql b/src/test/regress/sql/plpgsql.sql
index a375394bbc2..468dfd3111e 100644
--- a/src/test/regress/sql/plpgsql.sql
+++ b/src/test/regress/sql/plpgsql.sql
@@ -4390,3 +4390,25 @@ exception when others then
null; -- do nothing
end;
$$;
+
+-- Test use of plpgsql in a domain check constraint (cf. bug #14414)
+
+create function plpgsql_domain_check(val int) returns boolean as $$
+begin return val > 0; end
+$$ language plpgsql immutable;
+
+create domain plpgsql_domain as integer check(plpgsql_domain_check(value));
+
+do $$
+declare v_test plpgsql_domain;
+begin
+ v_test := 1;
+end;
+$$;
+
+do $$
+declare v_test plpgsql_domain := 1;
+begin
+ v_test := 0; -- fail
+end;
+$$;