Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 954744f

Browse files
committed
Fix VALIDATE CONSTRAINT to consider NO INHERIT attribute.
Currently, trying to validate a NO INHERIT constraint on the parent will search for the constraint in child tables (where it is not supposed to exist), wrongly causing a "constraint does not exist" error. Amit Langote, per a report from Hans Buschmann. Discussion: http://postgr.es/m/[email protected]
1 parent 8565808 commit 954744f

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
@@ -6262,9 +6262,10 @@ ATExecValidateConstraint(Relation rel, char *constrName, bool recurse,
62626262

62636263
/*
62646264
* If we're recursing, the parent has already done this, so skip
6265-
* it.
6265+
* it. Also, if the constraint is a NO INHERIT constraint, we
6266+
* shouldn't try to look for it in the children.
62666267
*/
6267-
if (!recursing)
6268+
if (!recursing && !con->connoinherit)
62686269
children = find_all_inheritors(RelationGetRelid(rel),
62696270
lockmode, NULL);
62706271

src/test/regress/expected/alter_table.out

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,26 @@ NOTICE: merging constraint "identity" with inherited definition
365365
ALTER TABLE tmp3 VALIDATE CONSTRAINT identity;
366366
NOTICE: boo: 16
367367
NOTICE: boo: 20
368+
-- A NO INHERIT constraint should not be looked for in children during VALIDATE CONSTRAINT
369+
create table parent_noinh_convalid (a int);
370+
create table child_noinh_convalid () inherits (parent_noinh_convalid);
371+
insert into parent_noinh_convalid values (1);
372+
insert into child_noinh_convalid values (1);
373+
alter table parent_noinh_convalid add constraint check_a_is_2 check (a = 2) no inherit not valid;
374+
-- fail, because of the row in parent
375+
alter table parent_noinh_convalid validate constraint check_a_is_2;
376+
ERROR: check constraint "check_a_is_2" is violated by some row
377+
delete from only parent_noinh_convalid;
378+
-- ok (parent itself contains no violating rows)
379+
alter table parent_noinh_convalid validate constraint check_a_is_2;
380+
select convalidated from pg_constraint where conrelid = 'parent_noinh_convalid'::regclass and conname = 'check_a_is_2';
381+
convalidated
382+
--------------
383+
t
384+
(1 row)
385+
386+
-- cleanup
387+
drop table parent_noinh_convalid, child_noinh_convalid;
368388
-- Try (and fail) to create constraint from tmp5(a) to tmp4(a) - unique constraint on
369389
-- tmp4 is a,b
370390
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
@@ -306,6 +306,21 @@ ALTER TABLE tmp7 ADD CONSTRAINT identity CHECK (b = boo(b));
306306
ALTER TABLE tmp3 ADD CONSTRAINT IDENTITY check (b = boo(b)) NOT VALID;
307307
ALTER TABLE tmp3 VALIDATE CONSTRAINT identity;
308308

309+
-- A NO INHERIT constraint should not be looked for in children during VALIDATE CONSTRAINT
310+
create table parent_noinh_convalid (a int);
311+
create table child_noinh_convalid () inherits (parent_noinh_convalid);
312+
insert into parent_noinh_convalid values (1);
313+
insert into child_noinh_convalid values (1);
314+
alter table parent_noinh_convalid add constraint check_a_is_2 check (a = 2) no inherit not valid;
315+
-- fail, because of the row in parent
316+
alter table parent_noinh_convalid validate constraint check_a_is_2;
317+
delete from only parent_noinh_convalid;
318+
-- ok (parent itself contains no violating rows)
319+
alter table parent_noinh_convalid validate constraint check_a_is_2;
320+
select convalidated from pg_constraint where conrelid = 'parent_noinh_convalid'::regclass and conname = 'check_a_is_2';
321+
-- cleanup
322+
drop table parent_noinh_convalid, child_noinh_convalid;
323+
309324
-- Try (and fail) to create constraint from tmp5(a) to tmp4(a) - unique constraint on
310325
-- tmp4 is a,b
311326

0 commit comments

Comments
 (0)