Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6994d0b

Browse files
committed
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).
1 parentdb047e5 commit6994d0b

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

‎src/pl/plpgsql/src/pl_exec.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.186 2007/01/30 18:02:22 tgl Exp $
11+
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.187 2007/02/01 19:22:07 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -890,8 +890,27 @@ exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
890890
{
891891
if (var->default_val==NULL)
892892
{
893+
/* Initially it contains a NULL */
893894
var->value= (Datum)0;
894895
var->isnull= true;
896+
/*
897+
* If needed, give the datatype a chance to reject
898+
* NULLs, by assigning a NULL to the variable.
899+
* We claim the value is of type UNKNOWN, not the
900+
* var's datatype, else coercion will be skipped.
901+
* (Do this before the notnull check to be
902+
* consistent with exec_assign_value.)
903+
*/
904+
if (!var->datatype->typinput.fn_strict)
905+
{
906+
boolvalIsNull= true;
907+
908+
exec_assign_value(estate,
909+
(PLpgSQL_datum*)var,
910+
(Datum)0,
911+
UNKNOWNOID,
912+
&valIsNull);
913+
}
895914
if (var->notnull)
896915
ereport(ERROR,
897916
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),

‎src/test/regress/expected/domain.out

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,22 @@ ERROR: domain pos_int does not allow null values
382382
-- and local variables are enforced correctly.
383383
create function doubledecrement(p1 pos_int) returns pos_int as $$
384384
declare v pos_int;
385+
begin
386+
return p1;
387+
end$$ language plpgsql;
388+
select doubledecrement(3); -- fail because of implicit null assignment
389+
ERROR: domain pos_int does not allow null values
390+
CONTEXT: PL/pgSQL function "doubledecrement" line 2 during statement block local variable initialization
391+
create or replace function doubledecrement(p1 pos_int) returns pos_int as $$
392+
declare v pos_int := 0;
393+
begin
394+
return p1;
395+
end$$ language plpgsql;
396+
select doubledecrement(3); -- fail at initialization assignment
397+
ERROR: value for domain pos_int violates check constraint "pos_int_check"
398+
CONTEXT: PL/pgSQL function "doubledecrement" line 2 during statement block local variable initialization
399+
create or replace function doubledecrement(p1 pos_int) returns pos_int as $$
400+
declare v pos_int := 1;
385401
begin
386402
v := p1 - 1;
387403
return v - 1;

‎src/test/regress/sql/domain.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,22 @@ execute s1(NULL); -- should fail
309309

310310
createfunctiondoubledecrement(p1 pos_int) returns pos_intas $$
311311
declare v pos_int;
312+
begin
313+
return p1;
314+
end$$ language plpgsql;
315+
316+
select doubledecrement(3);-- fail because of implicit null assignment
317+
318+
create or replacefunctiondoubledecrement(p1 pos_int) returns pos_intas $$
319+
declare v pos_int :=0;
320+
begin
321+
return p1;
322+
end$$ language plpgsql;
323+
324+
select doubledecrement(3);-- fail at initialization assignment
325+
326+
create or replacefunctiondoubledecrement(p1 pos_int) returns pos_intas $$
327+
declare v pos_int :=1;
312328
begin
313329
v := p1-1;
314330
return v-1;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp