[PATCH] validate inherited check constraints when enabling enforcement

From: Nikolay Samokhvalov <nik(at)postgres(dot)ai>
To: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: [PATCH] validate inherited check constraints when enabling enforcement
Date: 2026-09-13 11:32:37
Message-ID: CAM527d8qCH4q1xV-TftDmm3eBcBie9AxfdTtThPUoGWnYT08gw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I found a case where add constraint marks an inherited not enforced check
as validated without checking the old rows. With psql -X in a fresh
database:

create table p (a int constraint ck check (a > 0) not enforced);
create table c () inherits (p);
insert into c values (-1);
alter table c add constraint ck check (a > 0);
select conenforced, convalidated from pg_constraint
where conrelid = 'c'::regclass and conname = 'ck';

On unpatched pg19, the alter succeeds and the query returns t, t. It
should reject the -1 row. Adding not valid to the alter also leaves
convalidated true, which is wrong too.

MergeWithExistingConstraint() updates the flags, but the merged
constraint isn't returned to the normal validation path. The attached
patch queues that check and carries enforcement through descendants,
without changing inheritance counts. It preserves not valid, only, and
the descendant ownership checks. This replaces the direct-case patch
in my fork.

My AI harness built and tested the attachment on REL_19_STABLE at
6bc236c8a5b (19beta3), with assertions:
the example fails at the alter as expected, and the focused inheritance
test, 240 core regression tests, and 133 isolation tests pass.

The same merge code is present in current pg18 and master. I haven't
adapted this patch for pg18 yet; it lacks the check enforceability
traversal used here.

Nik

Attachment Content-Type Size
0001-validate-inherited-check-promotion.patch application/octet-stream 34.4 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Dean Rasheed 2026-09-13 11:42:42 Re: SSI: ON CONFLICT DO SELECT takes no predicate lock on the returned row
Previous Message JoongHyuk Shin 2026-09-13 10:31:57 Re: Persist slot invalidations before publishing them