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

Commit6a4dda4

Browse files
committed
Fix VALIDATE CONSTRAINT to consider NO INHERIT attribute.
Currently, trying to validate a NO INHERIT constraint on the parent willsearch for the constraint in child tables (where it is not supposed toexist), wrongly causing a "constraint does not exist" error.Amit Langote, per a report from Hans Buschmann.Discussion:http://postgr.es/m/20170421184012.24362.19@wrigleys.postgresql.org
1 parente4fddfd commit6a4dda4

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

‎src/backend/commands/tablecmds.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7704,9 +7704,10 @@ ATExecValidateConstraint(Relation rel, char *constrName, bool recurse,
77047704

77057705
/*
77067706
* If we're recursing, the parent has already done this, so skip
7707-
* it.
7707+
* it. Also, if the constraint is a NO INHERIT constraint, we
7708+
* shouldn't try to look for it in the children.
77087709
*/
7709-
if (!recursing)
7710+
if (!recursing&& !con->connoinherit)
77107711
children=find_all_inheritors(RelationGetRelid(rel),
77117712
lockmode,NULL);
77127713

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,26 @@ NOTICE: merging constraint "identity" with inherited definition
367367
ALTER TABLE tmp3 VALIDATE CONSTRAINT identity;
368368
NOTICE: boo: 16
369369
NOTICE: boo: 20
370+
-- A NO INHERIT constraint should not be looked for in children during VALIDATE CONSTRAINT
371+
create table parent_noinh_convalid (a int);
372+
create table child_noinh_convalid () inherits (parent_noinh_convalid);
373+
insert into parent_noinh_convalid values (1);
374+
insert into child_noinh_convalid values (1);
375+
alter table parent_noinh_convalid add constraint check_a_is_2 check (a = 2) no inherit not valid;
376+
-- fail, because of the row in parent
377+
alter table parent_noinh_convalid validate constraint check_a_is_2;
378+
ERROR: check constraint "check_a_is_2" is violated by some row
379+
delete from only parent_noinh_convalid;
380+
-- ok (parent itself contains no violating rows)
381+
alter table parent_noinh_convalid validate constraint check_a_is_2;
382+
select convalidated from pg_constraint where conrelid = 'parent_noinh_convalid'::regclass and conname = 'check_a_is_2';
383+
convalidated
384+
--------------
385+
t
386+
(1 row)
387+
388+
-- cleanup
389+
drop table parent_noinh_convalid, child_noinh_convalid;
370390
-- Try (and fail) to create constraint from tmp5(a) to tmp4(a) - unique constraint on
371391
-- tmp4 is a,b
372392
ALTER TABLE tmp5 add constraint tmpconstr foreign key(a) references tmp4(a) match full;

‎src/test/regress/sql/alter_table.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,21 @@ ALTER TABLE tmp7 ADD CONSTRAINT identity CHECK (b = boo(b));
307307
ALTERTABLE tmp3 ADDCONSTRAINT IDENTITYcheck (b= boo(b)) NOT VALID;
308308
ALTERTABLE tmp3 VALIDATECONSTRAINT identity;
309309

310+
-- A NO INHERIT constraint should not be looked for in children during VALIDATE CONSTRAINT
311+
createtableparent_noinh_convalid (aint);
312+
createtablechild_noinh_convalid () inherits (parent_noinh_convalid);
313+
insert into parent_noinh_convalidvalues (1);
314+
insert into child_noinh_convalidvalues (1);
315+
altertable parent_noinh_convalid addconstraint check_a_is_2check (a=2) no inherit not valid;
316+
-- fail, because of the row in parent
317+
altertable parent_noinh_convalid validateconstraint check_a_is_2;
318+
deletefrom only parent_noinh_convalid;
319+
-- ok (parent itself contains no violating rows)
320+
altertable parent_noinh_convalid validateconstraint check_a_is_2;
321+
select convalidatedfrom pg_constraintwhere conrelid='parent_noinh_convalid'::regclassand conname='check_a_is_2';
322+
-- cleanup
323+
droptable parent_noinh_convalid, child_noinh_convalid;
324+
310325
-- Try (and fail) to create constraint from tmp5(a) to tmp4(a) - unique constraint on
311326
-- tmp4 is a,b
312327

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp