• Tom Lane's avatar
    Fix two bugs in merging of inherited CHECK constraints. · e55a946a
    Tom Lane authored
    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>
    e55a946a
tablecmds.c 362 KB