summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2007-02-01 19:23:00 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2007-02-01 19:23:00 +0000
commitb7c9821146145688477d9e09bb5a176bf1378d0b (patch)
tree4948655c76ab35ab13257ef87c9f507b25baaebe
parent3c7d9add711297b9f3daae988aa613bf6d7b1b84 (diff)
downloadpostgresql-b7c9821146145688477d9e09bb5a176bf1378d0b.tar.gz
Fix plpgsql so that when a local variable has no initial-value expression,
an error will be thrown correctly if the variable is of a NOT NULL domain. Report and almost-correct fix from Sergiy Vyshnevetskiy (bug #2948).
-rw-r--r--src/pl/plpgsql/src/pl_exec.c21
-rw-r--r--src/test/regress/expected/domain.out16
-rw-r--r--src/test/regress/sql/domain.sql16
3 files changed, 52 insertions, 1 deletions
diff --git a/src/pl/plpgsql/src/pl_exec.c b/src/pl/plpgsql/src/pl_exec.c
index 8f11b1e5d9..efce8004b9 100644
--- a/src/pl/plpgsql/src/pl_exec.c
+++ b/src/pl/plpgsql/src/pl_exec.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.180.2.2 2007/01/30 18:02:28 tgl Exp $
+ * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.180.2.3 2007/02/01 19:23:00 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -861,8 +861,27 @@ exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
{
if (var->default_val == NULL)
{
+ /* Initially it contains a NULL */
var->value = (Datum) 0;
var->isnull = true;
+ /*
+ * If needed, give the datatype a chance to reject
+ * NULLs, by assigning a NULL to the variable.
+ * We claim the value is of type UNKNOWN, not the
+ * var's datatype, else coercion will be skipped.
+ * (Do this before the notnull check to be
+ * consistent with exec_assign_value.)
+ */
+ if (!var->datatype->typinput.fn_strict)
+ {
+ bool valIsNull = true;
+
+ exec_assign_value(estate,
+ (PLpgSQL_datum *) var,
+ (Datum) 0,
+ UNKNOWNOID,
+ &valIsNull);
+ }
if (var->notnull)
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
diff --git a/src/test/regress/expected/domain.out b/src/test/regress/expected/domain.out
index c4cad728b3..e33444c5bc 100644
--- a/src/test/regress/expected/domain.out
+++ b/src/test/regress/expected/domain.out
@@ -383,6 +383,22 @@ ERROR: domain pos_int does not allow null values
create function doubledecrement(p1 pos_int) returns pos_int as $$
declare v pos_int;
begin
+ return p1;
+end$$ language plpgsql;
+select doubledecrement(3); -- fail because of implicit null assignment
+ERROR: domain pos_int does not allow null values
+CONTEXT: PL/pgSQL function "doubledecrement" line 2 at block variables initialization
+create or replace function doubledecrement(p1 pos_int) returns pos_int as $$
+declare v pos_int := 0;
+begin
+ return p1;
+end$$ language plpgsql;
+select doubledecrement(3); -- fail at initialization assignment
+ERROR: value for domain pos_int violates check constraint "pos_int_check"
+CONTEXT: PL/pgSQL function "doubledecrement" line 2 at block variables initialization
+create or replace function doubledecrement(p1 pos_int) returns pos_int as $$
+declare v pos_int := 1;
+begin
v := p1 - 1;
return v - 1;
end$$ language plpgsql;
diff --git a/src/test/regress/sql/domain.sql b/src/test/regress/sql/domain.sql
index 21940e0e61..411d64648a 100644
--- a/src/test/regress/sql/domain.sql
+++ b/src/test/regress/sql/domain.sql
@@ -310,6 +310,22 @@ execute s1(NULL); -- should fail
create function doubledecrement(p1 pos_int) returns pos_int as $$
declare v pos_int;
begin
+ return p1;
+end$$ language plpgsql;
+
+select doubledecrement(3); -- fail because of implicit null assignment
+
+create or replace function doubledecrement(p1 pos_int) returns pos_int as $$
+declare v pos_int := 0;
+begin
+ return p1;
+end$$ language plpgsql;
+
+select doubledecrement(3); -- fail at initialization assignment
+
+create or replace function doubledecrement(p1 pos_int) returns pos_int as $$
+declare v pos_int := 1;
+begin
v := p1 - 1;
return v - 1;
end$$ language plpgsql;