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

Commit954744f

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 parent8565808 commit954744f

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
ALTERTABLE tmp3 ADDCONSTRAINT IDENTITYcheck (b= boo(b)) NOT VALID;
307307
ALTERTABLE tmp3 VALIDATECONSTRAINT identity;
308308

309+
-- A NO INHERIT constraint should not be looked for in children during VALIDATE CONSTRAINT
310+
createtableparent_noinh_convalid (aint);
311+
createtablechild_noinh_convalid () inherits (parent_noinh_convalid);
312+
insert into parent_noinh_convalidvalues (1);
313+
insert into child_noinh_convalidvalues (1);
314+
altertable parent_noinh_convalid addconstraint check_a_is_2check (a=2) no inherit not valid;
315+
-- fail, because of the row in parent
316+
altertable parent_noinh_convalid validateconstraint check_a_is_2;
317+
deletefrom only parent_noinh_convalid;
318+
-- ok (parent itself contains no violating rows)
319+
altertable parent_noinh_convalid validateconstraint check_a_is_2;
320+
select convalidatedfrom pg_constraintwhere conrelid='parent_noinh_convalid'::regclassand conname='check_a_is_2';
321+
-- cleanup
322+
droptable 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)

[8]ページ先頭

©2009-2025 Movatter.jp