Skip to content

Commit 8a9c83b

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/20170421184012.24362.19@wrigleys.postgresql.org
1 parent 29e8c88 commit 8a9c83b

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

69046904
/*
69056905
* If we're recursing, the parent has already done this, so skip
6906-
* it.
6906+
* it. Also, if the constraint is a NO INHERIT constraint, we
6907+
* shouldn't try to look for it in the children.
69076908
*/
6908-
if (!recursing)
6909+
if (!recursing && !con->connoinherit)
69096910
children = find_all_inheritors(RelationGetRelid(rel),
69106911
lockmode, NULL);
69116912

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
ALTER TABLE tmp3 ADD CONSTRAINT IDENTITY check (b = boo(b)) NOT VALID;
308308
ALTER TABLE tmp3 VALIDATE CONSTRAINT identity;
309309

310+
-- A NO INHERIT constraint should not be looked for in children during VALIDATE CONSTRAINT
311+
create table parent_noinh_convalid (a int);
312+
create table child_noinh_convalid () inherits (parent_noinh_convalid);
313+
insert into parent_noinh_convalid values (1);
314+
insert into child_noinh_convalid values (1);
315+
alter table parent_noinh_convalid add constraint check_a_is_2 check (a = 2) no inherit not valid;
316+
-- fail, because of the row in parent
317+
alter table parent_noinh_convalid validate constraint check_a_is_2;
318+
delete from only parent_noinh_convalid;
319+
-- ok (parent itself contains no violating rows)
320+
alter table parent_noinh_convalid validate constraint check_a_is_2;
321+
select convalidated from pg_constraint where conrelid = 'parent_noinh_convalid'::regclass and conname = 'check_a_is_2';
322+
-- cleanup
323+
drop table 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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy