From: Andrew Dunstan <andrew@dunslane.net>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Reject enforcing a parent CHECK constraint over a NOT VALID chil
Date: Sun, 01 Nov 2026 15:50:02 +0000
Message-ID: <E1xNXoo-00000001fsk-1GRO@gemulon.postgresql.org> (raw)
Reject enforcing a parent CHECK constraint over a NOT VALID child
ALTER TABLE ... ALTER CONSTRAINT ... ENFORCED on a parent's NOT ENFORCED
CHECK constraint marked the parent's constraint validated, but when it
recursed to a child whose matching constraint was already ENFORCED and
NOT VALID, it left that child alone, since its enforceability was
unchanged. The child's existing rows were therefore never checked, and
the parent ended up validated with a child that was not. Constraint
exclusion could then skip the child when querying the parent, and so
miss rows that violate the constraint.
Fix by raising the same error that ALTER TABLE ... INHERIT already
raises when a child has a NOT VALID copy of a parent's constraint, with
a hint to validate the child's constraint first. Validating the child
implicitly instead was considered, but ALTER CONSTRAINT ... ENFORCED
does not validate an ENFORCED NOT VALID constraint when run directly
against the table that has it, so doing so only when reached through a
parent would be inconsistent.
Add regression tests for both regular inheritance and partitioning.
Bug introduced in 342051d73b3.
Reported-by: Shihao Zhong <zhong950419@gmail.com>
Author: Shihao Zhong <zhong950419@gmail.com>
Author: Jian He <jian.universality@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Reviewed-by: Andrew Dunstan <andrew@dunslane.net>
Discussion: https://postgr.es/m/CAGRkXqSGE=fQrjCoNQsyiCZRKfQ4SrB015CAR=3pXYB5tB=ieg@mail.gmail.com
Backpatch-through: 19
Branch
------
REL_19_STABLE
Details
-------
https://git.postgresql.org/pg/commitdiff/8356d8e4dba6dc36c84d4a3f15f10b9c89c2cd83
Modified Files
--------------
src/backend/commands/tablecmds.c | 15 +++++++++++++++
src/test/regress/expected/constraints.out | 11 +++++++++++
src/test/regress/expected/inherit.out | 22 ++++++++++++++++++++++
src/test/regress/sql/constraints.sql | 11 +++++++++++
src/test/regress/sql/inherit.sql | 14 ++++++++++++++
5 files changed, 73 insertions(+)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: andrew@dunslane.net
Subject: Re: pgsql: Reject enforcing a parent CHECK constraint over a NOT VALID chil
In-Reply-To: <E1xNXoo-00000001fsk-1GRO@gemulon.postgresql.org>
References: <E1xNXoo-00000001fsk-1GRO@gemulon.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox