Fix another bug in merging of inherited CHECK constraints.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 13 Oct 2016 21:05:15 +0000 (17:05 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 13 Oct 2016 21:05:15 +0000 (17:05 -0400)
It's not good for an inherited child constraint to be marked connoinherit;
that would result in the constraint not propagating to grandchild tables,
if any are created later.  The code mostly prevented this from happening
but there was one case that was missed.

This is somewhat related to commit e55a946a8, which also tightened checks
on constraint merging.  Hence, back-patch to 9.2 like that one.  This isn't
so much because there's a concrete feature-related reason to stop there,
as to avoid having more distinct behaviors than we have to in this area.

Amit Langote

Discussion: <b28ee774-7009-313d-dd55-5bdd81242c41@lab.ntt.co.jp>

src/backend/catalog/heap.c
src/test/regress/expected/inherit.out
src/test/regress/sql/inherit.sql

index 5f3cf97be4cd9bdae850ead8febff1972d0767d3..978725199c8a8a3979bbf054fcfaa530caa6dabe 100644 (file)
@@ -2465,6 +2465,17 @@ MergeWithExistingConstraint(Relation rel, char *ccname, Node *expr,
                         errmsg("constraint \"%s\" conflicts with non-inherited constraint on relation \"%s\"",
                                ccname, RelationGetRelationName(rel))));
 
+           /*
+            * Must not change an existing inherited constraint to "no
+            * inherit" status.  That's because inherited constraints should
+            * be able to propagate to lower-level children.
+            */
+           if (con->coninhcount > 0 && is_no_inherit)
+               ereport(ERROR,
+                       (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
+                        errmsg("constraint \"%s\" conflicts with inherited constraint on relation \"%s\"",
+                               ccname, RelationGetRelationName(rel))));
+
            /*
             * If the child constraint is "not valid" then cannot merge with a
             * valid parent constraint
index 46442e89860b446642d1ed6672596ca1a68d4966..ab80804af9baa14392287d371be8441931765ee4 100644 (file)
@@ -645,6 +645,9 @@ Check constraints:
     "p2chk" CHECK (ff1 > 10)
 Inherits: p1
 
+-- Test that child does not override inheritable constraints of the parent
+create table c2 (constraint p2chk check (ff1 > 10) no inherit) inherits (p1);  --fails
+ERROR:  constraint "p2chk" conflicts with inherited constraint on relation "c2"
 drop table p1 cascade;
 NOTICE:  drop cascades to table c1
 -- Tests for casting between the rowtypes of parent and child
index a5e6ecdb868287a2700db43af069cef8484682ba..d65839899c1f3d26797e0d0e99609eaa183efc50 100644 (file)
@@ -157,6 +157,9 @@ create table c1 () inherits (p1);
 \d p1
 \d c1
 
+-- Test that child does not override inheritable constraints of the parent
+create table c2 (constraint p2chk check (ff1 > 10) no inherit) inherits (p1);  --fails
+
 drop table p1 cascade;
 
 -- Tests for casting between the rowtypes of parent and child