Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a5b153f

Browse files
committed
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 1345cc6. 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>
1 parent 20559a8 commit a5b153f

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/pl/plpgsql/src/pl_exec.c

+4
Original file line numberDiff line numberDiff line change
@@ -6102,6 +6102,8 @@ exec_cast_value(PLpgSQL_execstate *estate,
61026102
ExprContext *econtext = estate->eval_econtext;
61036103
MemoryContext oldcontext;
61046104

6105+
SPI_push();
6106+
61056107
oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
61066108

61076109
econtext->caseValue_datum = value;
@@ -6115,6 +6117,8 @@ exec_cast_value(PLpgSQL_execstate *estate,
61156117
cast_entry->cast_in_use = false;
61166118

61176119
MemoryContextSwitchTo(oldcontext);
6120+
6121+
SPI_pop();
61186122
}
61196123
}
61206124

src/test/regress/expected/plpgsql.out

+19
Original file line numberDiff line numberDiff line change
@@ -5643,3 +5643,22 @@ end;
56435643
$$;
56445644
ERROR: unhandled assertion
56455645
CONTEXT: PL/pgSQL function inline_code_block line 3 at ASSERT
5646+
-- Test use of plpgsql in a domain check constraint (cf. bug #14414)
5647+
create function plpgsql_domain_check(val int) returns boolean as $$
5648+
begin return val > 0; end
5649+
$$ language plpgsql immutable;
5650+
create domain plpgsql_domain as integer check(plpgsql_domain_check(value));
5651+
do $$
5652+
declare v_test plpgsql_domain;
5653+
begin
5654+
v_test := 1;
5655+
end;
5656+
$$;
5657+
do $$
5658+
declare v_test plpgsql_domain := 1;
5659+
begin
5660+
v_test := 0; -- fail
5661+
end;
5662+
$$;
5663+
ERROR: value for domain plpgsql_domain violates check constraint "plpgsql_domain_check"
5664+
CONTEXT: PL/pgSQL function inline_code_block line 4 at assignment

src/test/regress/sql/plpgsql.sql

+22
Original file line numberDiff line numberDiff line change
@@ -4428,3 +4428,25 @@ exception when others then
44284428
null; -- do nothing
44294429
end;
44304430
$$;
4431+
4432+
-- Test use of plpgsql in a domain check constraint (cf. bug #14414)
4433+
4434+
create function plpgsql_domain_check(val int) returns boolean as $$
4435+
begin return val > 0; end
4436+
$$ language plpgsql immutable;
4437+
4438+
create domain plpgsql_domain as integer check(plpgsql_domain_check(value));
4439+
4440+
do $$
4441+
declare v_test plpgsql_domain;
4442+
begin
4443+
v_test := 1;
4444+
end;
4445+
$$;
4446+
4447+
do $$
4448+
declare v_test plpgsql_domain := 1;
4449+
begin
4450+
v_test := 0; -- fail
4451+
end;
4452+
$$;

0 commit comments

Comments
 (0)