]> granicus.if.org Git - postgresql/commit
Fix two bugs in merging of inherited CHECK constraints.
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 8 Oct 2016 23:29:27 +0000 (19:29 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 8 Oct 2016 23:29:27 +0000 (19:29 -0400)
commitd1a9f128defa6cb86c3c1274befe46c2e0d381f4
tree27a647118bc977234ef48a8d7ef0f88439bb83f5
parentd1595525bdaffa1bc2812fcda8a151cfbdc81a47
Fix two bugs in merging of inherited CHECK constraints.

Historically, we've allowed users to add a CHECK constraint to a child
table and then add an identical CHECK constraint to the parent.  This
results in "merging" the two constraints so that the pre-existing
child constraint ends up with both conislocal = true and coninhcount > 0.
However, if you tried to do it in the other order, you got a duplicate
constraint error.  This is problematic for pg_dump, which needs to issue
separated ADD CONSTRAINT commands in some cases, but has no good way to
ensure that the constraints will be added in the required order.
And it's more than a bit arbitrary, too.  The goal of complaining about
duplicated ADD CONSTRAINT commands can be served if we reject the case of
adding a constraint when the existing one already has conislocal = true;
but if it has conislocal = false, let's just make the ADD CONSTRAINT set
conislocal = true.  In this way, either order of adding the constraints
has the same end result.

Another problem was that the code allowed creation of a parent constraint
marked convalidated that is merged with a child constraint that is
!convalidated.  In this case, an inheritance scan of the parent table could
emit some rows violating the constraint condition, which would be an
unexpected result given the marking of the parent constraint as validated.
Hence, forbid merging of constraints in this case.  (Note: valid child and
not-valid parent seems fine, so continue to allow that.)

Per report from Benedikt Grundmann.  Back-patch to 9.2 where we introduced
possibly-not-valid check constraints.  The second bug obviously doesn't
apply before that, and I think the first doesn't either, because pg_dump
only gets into this situation when dealing with not-valid constraints.

Report: <CADbMkNPT-Jz5PRSQ4RbUASYAjocV_KHUWapR%2Bg8fNvhUAyRpxA%40mail.gmail.com>
Discussion: <22108.1475874586@sss.pgh.pa.us>
src/backend/catalog/heap.c
src/backend/commands/tablecmds.c
src/test/regress/expected/inherit.out
src/test/regress/expected/sanity_check.out
src/test/regress/sql/inherit.sql