Re: Fix bug of CHECK constraint enforceability recursion - Mailing list pgsql-hackers

From Chao Li
Subject Re: Fix bug of CHECK constraint enforceability recursion
Date
Msg-id 185FC597-BF26-4075-9D59-E5905ED74E0F@gmail.com
Whole thread
In response to Re: Fix bug of CHECK constraint enforceability recursion  (Zsolt Parragi <zsolt.parragi@percona.com>)
Responses Re: Fix bug of CHECK constraint enforceability recursion
List pgsql-hackers

> On Jun 3, 2026, at 04:56, Zsolt Parragi <zsolt.parragi@percona.com> wrote:
>
> Hello
>
> + * During recursion, another parent outside this ALTER may still enforce
> + * the same constraint. In that case, keep the child constraint ENFORCED
> + * so that its merged enforceability still reflects the remaining enforced
> + * parent.
> + */
> + if (!cmdcon->is_enforced)
> + {
>
> This means once is_enforced is set to true, it will never be
> rechecked. See the following example which showcases an issue with
> this:
>
> create table g(a int constraint k check(a > 0) enforced);
> create table o(a int constraint k check(a > 0) enforced);
> create table s1() inherits(g);
> create table s2() inherits(g, o);
> create table s3() inherits(g);
> alter table g alter constraint k not enforced;
> select conrelid::regclass as tbl, conenforced
> from   pg_constraint
> where  conname = 'k' and contype = 'c'
> order  by conrelid::regclass::text collate "C";
> insert into s1 values (-1);
> insert into s3 values (-1);   -- bug: ERRORs out, but shouldn't
>

Hi Zsolt,

Thanks for your view.

I just tried your test with v6, but didn't see the bug you mentioned:
```
evantest=# create table g(a int constraint k check(a > 0) enforced);
CREATE TABLE
evantest=# create table o(a int constraint k check(a > 0) enforced);
CREATE TABLE
evantest=# create table s1() inherits(g);
CREATE TABLE
evantest=# create table s2() inherits(g, o);
NOTICE:  merging multiple inherited definitions of column "a"
CREATE TABLE
evantest=# create table s3() inherits(g);
CREATE TABLE
evantest=# alter table g alter constraint k not enforced;
ALTER TABLE
evantest=# select conrelid::regclass as tbl, conenforced
evantest-# from   pg_constraint
evantest-# where  conname = 'k';
 tbl | conenforced
-----+-------------
 o   | t
 s2  | t
 g   | f
 s1  | f
 s3  | f
(5 rows)

evantest=# insert into s1 values (-1);
INSERT 0 1
evantest=# insert into s3 values (-1);
INSERT 0 1
```

Here, both s1 and s3 are the direct child of g, thus when g is set to “not enforced”, s1 and s3 is set to “not
enforced"as well. While s2 is a child of both g and o, as o is still enforced, s2 remains enforced. The test result is
rightas expected. 

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/







pgsql-hackers by date:

Previous
From: Richard Guo
Date:
Subject: Re: Eager aggregation, take 3
Next
From: lin teletele
Date:
Subject: Re: Use pg_current_xact_id() instead of deprecated txid_current()