Skip to content

Commit fc8b81a

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 5485c99 commit fc8b81a

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/pl/plpgsql/src/pl_exec.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6300,6 +6300,8 @@ exec_cast_value(PLpgSQL_execstate *estate,
63006300
ExprContext *econtext = estate->eval_econtext;
63016301
MemoryContext oldcontext;
63026302

6303+
SPI_push();
6304+
63036305
oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
63046306

63056307
econtext->caseValue_datum = value;
@@ -6313,6 +6315,8 @@ exec_cast_value(PLpgSQL_execstate *estate,
63136315
cast_entry->cast_in_use = false;
63146316

63156317
MemoryContextSwitchTo(oldcontext);
6318+
6319+
SPI_pop();
63166320
}
63176321
}
63186322

src/test/regress/expected/plpgsql.out

Lines changed: 19 additions & 0 deletions
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

Lines changed: 22 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy