ALTER COLUMN SET NOT NULL differ from ALTER TABLE ADD CONSTRAINT

Поиск
Список
Период
Сортировка
Искать

ALTER COLUMN SET NOT NULL differ from ALTER TABLE ADD CONSTRAINT

От:
jian he <jian.universality@gmail.com>
Дата:
Hi.

While trying to check
https://git.postgresql.org/cgit/postgresql.git/commit/?id=96e2af605043974137d84edf5c0a24561956919e
potential pg_dump output related bug.

I encountered the following issue first.

I expect ALTER TABLE ADD CONSTRAINT to have the same effect as ALTER COLUMN SET
NOT NULL, meaning that the column associated constraint will be marked as VALID
and ENFORCED across the entire table inheritance hierarchy.

I think it is ok for one form to fail (error out) while the other succeeds.
However, if ALTER COLUMN SET NOT NULL succeeds, all associated NOT NULL
constraints should be marked as VALID and ENFORCED.

drop table if exists tp, tc cascade;
create table tp (a int);
create table tc (a int) inherits (tp);
alter table tc add constraint nn not null a not valid;
alter table tp add constraint nn not null a; -- error
alter table tp alter column a set not null; -- should expect an error

The last ALTER COLUMN SET NOT NULL should either error out or make the column
'a' in table "tc" have a not-null constraint that is ENFORCED and VALID?

The partitioned table has a similar issue.



--
jian
https://www.enterprisedb.com/


Re: ALTER COLUMN SET NOT NULL differ from ALTER TABLE ADD CONSTRAINT

От:
Osama Abdul Qader <osamaabdulqaderpostgres@gmail.com>
Дата:
Hello Jian,

Following up on the issue identified during the investigation of the potential pg_dump output-related bug.

I wanted to highlight one additional concern regarding the behavior of ALTER TABLE ... ALTER COLUMN ... SET NOT NULL when a descendant table has an existing NOT VALID NOT NULL constraint.

In the inheritance example provided earlier, should this operation fail, or should it ensure that the corresponding constraint on the child table is also marked as valid and enforced?

I noticed that a similar issue appears to exist with partitioned tables as well.

Could you please clarify whether this behavior is expected or whether it should be addressed as part of the existing fix?

Thanks!

Osama Abdul Qader


On Mon, 19 Oct 2026 at 19:01, jian he <jian.universality@gmail.com> wrote:
Hi.

While trying to check
https://git.postgresql.org/cgit/postgresql.git/commit/?id=96e2af605043974137d84edf5c0a24561956919e
potential pg_dump output related bug.

I encountered the following issue first.

I expect ALTER TABLE ADD CONSTRAINT to have the same effect as ALTER COLUMN SET
NOT NULL, meaning that the column associated constraint will be marked as VALID
and ENFORCED across the entire table inheritance hierarchy.

I think it is ok for one form to fail (error out) while the other succeeds.
However, if ALTER COLUMN SET NOT NULL succeeds, all associated NOT NULL
constraints should be marked as VALID and ENFORCED.

drop table if exists tp, tc cascade;
create table tp (a int);
create table tc (a int) inherits (tp);
alter table tc add constraint nn not null a not valid;
alter table tp add constraint nn not null a; -- error
alter table tp alter column a set not null; -- should expect an error

The last ALTER COLUMN SET NOT NULL should either error out or make the column
'a' in table "tc" have a not-null constraint that is ENFORCED and VALID?

The partitioned table has a similar issue.



--
jian
https://www.enterprisedb.com/


Re: ALTER COLUMN SET NOT NULL differ from ALTER TABLE ADD CONSTRAINT

От:
Haibo Yan <tristan.yim@gmail.com>
Дата:
Вложения:
On Mon, Oct 19, 2026 at 6:31 AM jian he  wrote:
>
> Hi.
>
> While trying to check
> https://git.postgresql.org/cgit/postgresql.git/commit/?id=96e2af605043974137d84edf5c0a24561956919e
> potential pg_dump output related bug.
>
> I encountered the following issue first.
>
> I expect ALTER TABLE ADD CONSTRAINT to have the same effect as ALTER COLUMN SET
> NOT NULL, meaning that the column associated constraint will be marked as VALID
> and ENFORCED across the entire table inheritance hierarchy.
>
> I think it is ok for one form to fail (error out) while the other succeeds.
> However, if ALTER COLUMN SET NOT NULL succeeds, all associated NOT NULL
> constraints should be marked as VALID and ENFORCED.
>
> drop table if exists tp, tc cascade;
> create table tp (a int);
> create table tc (a int) inherits (tp);
> alter table tc add constraint nn not null a not valid;
> alter table tp add constraint nn not null a; -- error
> alter table tp alter column a set not null; -- should expect an error
>
> The last ALTER COLUMN SET NOT NULL should either error out or make the column
> 'a' in table "tc" have a not-null constraint that is ENFORCED and VALID?
>
> The partitioned table has a similar issue.
>
>
>
> --
> jian
> https://www.enterprisedb.com/
>
>

Hi Jian,

Thanks for the report. I reproduced the issue.

In ATExecSetNotNull(), the recursive path increments coninhcount for
an existing child constraint and returns without checking whether it
is valid. This can leave a NOT VALID child under a valid parent.

The attached patch rejects that combination, as ADD CONSTRAINT does.
It also fixes a related case where SET NOT NULL on an inherited-only
NOT VALID constraint makes it local but skips validation.

The new regression tests pass with the patch and fail on the
unpatched build. The full core regression suite passes (239/239)
on master cf765d6e145, tested on macOS arm64 with assertions enabled.

Please take a look.

Regards,
Haibo

Re: ALTER COLUMN SET NOT NULL differ from ALTER TABLE ADD CONSTRAINT

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Hi Haibo,

Thanks for the patch. I reviewed v1 and the main fix looks right to
me.  I think it needs to go back to 18.

On 19 the wrong results go further. Take jian's case as a partitioned
table, and add o(x int not null) with one row. Then

  select count(*) from o where x not in (select a from p);

returns 1 on 19 and 0 on 18. The bug is from 18, so maybe it belongs
under "Older bugs affecting stable branches" on the 19 open items page.

A few notes.
1. I ran the test before and after I believe it catched the bug, but 1k loc 
feels too much for me.

2. with your change 
create table tp (a int);
create table tc () inherits (tp);
create table tg () inherits (tc);
alter table tg add constraint tg_nn not null a not valid;
alter table tp add constraint tp_nn not null a no inherit not valid;
alter table tp alter constraint tp_nn inherit;
-- ERROR:  incompatible NOT VALID constraint "tg_nn" on relation "tg"

INHERIT adds tp_nn to tc as a valid constraint, and then tg is
rejected. Since tp_nn is NOT VALID, maybe the copy on tc should be
NOT VALID too. ADD CONSTRAINT ... NOT VALID works that way.

Thanks,
Shihao


Re: ALTER COLUMN SET NOT NULL differ from ALTER TABLE ADD CONSTRAINT

От:
Haibo Yan <tristan.yim@gmail.com>
Дата:
Вложения:
Hi Shihao,

Thanks for reviewing the patch!

On Wed, Oct 28, 2026 at 8:02 PM shihao zhong  wrote:
>
> Hi Haibo,
>
> Thanks for the patch. I reviewed v1 and the main fix looks right to
> me.  I think it needs to go back to 18.

Agreed. I'll work on the backpatch after we settle the fix on master.

>
> On 19 the wrong results go further. Take jian's case as a partitioned
> table, and add o(x int not null) with one row. Then
>
>   select count(*) from o where x not in (select a from p);
>
> returns 1 on 19 and 0 on 18. The bug is from 18, so maybe it belongs
> under "Older bugs affecting stable branches" on the 19 open items page.
>

Thanks for the example. I reproduced the wrong result on master and
added a test for it. With the patch, SET NOT NULL fails before it can
leave the parent and partition in the inconsistent state.


> A few notes.
> 1. I ran the test before and after I believe it catched the bug, but 1k loc
> feels too much for me.

Agreed. I've reduced the added test and expected output from 1116
lines to 313 in v2.

>
> 2. with your change
> create table tp (a int);
> create table tc () inherits (tp);
> create table tg () inherits (tc);
> alter table tg add constraint tg_nn not null a not valid;
> alter table tp add constraint tp_nn not null a no inherit not valid;
> alter table tp alter constraint tp_nn inherit;
> -- ERROR:  incompatible NOT VALID constraint "tg_nn" on relation "tg"
>
> INHERIT adds tp_nn to tc as a valid constraint, and then tg is
> rejected. Since tp_nn is NOT VALID, maybe the copy on tc should be
> NOT VALID too. ADD CONSTRAINT ... NOT VALID works that way.

That makes sense. I've changed v2 to create a NOT VALID constraint
on tc, so your example now succeeds. The patch still rejects a
VALID constraint being inherited by a NOT VALID child.

>
> Thanks,
> Shihao
>
>

The full core regression suite passes with v2 on master. Please
find the updated patch attached.

Thanks,
Haibo
FAQ