Need to do SPI_push/SPI_pop around expression evaluation in plpgsql.
authorTom Lane <[email protected]>
Sun, 6 Nov 2016 17:09:36 +0000 (12:09 -0500)
committerTom Lane <[email protected]>
Sun, 6 Nov 2016 17:09:36 +0000 (12:09 -0500)
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[email protected]>

src/pl/plpgsql/src/pl_exec.c
src/test/regress/expected/plpgsql.out
src/test/regress/sql/plpgsql.sql

index 470cf935df33a473b78f34408c181c811962373a..042b31fd77f831e1e794555ba8c8257349080243 100644 (file)
@@ -6300,6 +6300,8 @@ exec_cast_value(PLpgSQL_execstate *estate,
            ExprContext *econtext = estate->eval_econtext;
            MemoryContext oldcontext;
 
+           SPI_push();
+
            oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
 
            econtext->caseValue_datum = value;
@@ -6313,6 +6315,8 @@ exec_cast_value(PLpgSQL_execstate *estate,
            cast_entry->cast_in_use = false;
 
            MemoryContextSwitchTo(oldcontext);
+
+           SPI_pop();
        }
    }
 
index a2c36e44ba0aee3bff52c4884d855cff15cb7c45..147fb9f2bb82b66bd3d24eb9b0c2a87ef58804e9 100644 (file)
@@ -5643,3 +5643,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
index 776f2292ea59a4f3b3b2a92db50d9ce96902ac1a..49223ff2b92b74c403a9aa39b5b75f11d9545759 100644 (file)
@@ -4428,3 +4428,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;
+$$;