[PATCH v1] Fix for bug #19770 : Reject NO INHERIT not-null constraints copied by LIKE to partitioned tables

Started by Nitin Motianiabout 1 month ago5 messageshackers
Jump to latest
#1Nitin Motiani
nitinmotiani@google.com

Hi,

It was reported in [1]/messages/by-id/19770-7c6e75fc30df7e06@postgresql.org that creating a partitioned table by using
'CREATE TABLE .. (LIKE ...)' can lead to a not null constraint with
'NO INHERIT' being copied. This is disallowed for partitioned tables
but the LIKE option has accidentally allowed it.

This seems like an unlikely scenario in practice but since it is
simple to fix, I think we should handle it.

I'm attaching a patch which fixes this by explicitly checking for a
not-null NO INHERIT constraint in transformTableLikeClause and
throwing an error.

Please take a look and let me know what you think.

[1]: /messages/by-id/19770-7c6e75fc30df7e06@postgresql.org

Thanks & Regards,
Nitin Motiani
Google

Attachments:

t254300_1
v1-0001-Reject-NO-INHERIT-not-null-constraints-copied-by-.patchapplication/x-patch; name=v1-0001-Reject-NO-INHERIT-not-null-constraints-copied-by-.patchDownload+80-2
#2David Rowley
dgrowleyml@gmail.com
In reply to: Nitin Motiani (#1)
Re: [PATCH v1] Fix for bug #19770 : Reject NO INHERIT not-null constraints copied by LIKE to partitioned tables

On Wed, 21 Oct 2026 at 21:32, Nitin Motiani <nitinmotiani@google.com> wrote:

I'm attaching a patch which fixes this by explicitly checking for a
not-null NO INHERIT constraint in transformTableLikeClause and
throwing an error.

Looking at the test added in cacef1722, we do seem to expect that a NO
INHERIT CHECK constraint will block the creation of a partitioned
table with CREATE TABLE LIKE, so you could argue that it makes sense
to do the same for NOT NULL constraints. If we were to go down that
route, then I think the correct place to put the check would be in
StoreRelNotNull() as the neighbouring StoreConstraints() is where
cacef1722's test hits the error for the CHECK constraint. Putting it
there also helps ensure there's no other place that could bypass this
check.

Having said that, I am wondering if this might just be annoying to
users. If the user wants to bypass copying the CHECK constraints, they
can use CREATE TABLE ... (LIKE ... EXCLUDING CONSTRAINTS), then maybe
they have to manually adjust and add the CHECK constraints into the
new table, but, going by [1]https://www.postgresql.org/docs/devel/sql-createtable.html#SQL-CREATETABLE-PARMS-LIKE:

"The LIKE clause specifies a table from which the new table
automatically copies all column names, their data types, and their
not-null constraints."

and the final sentence of:

"CHECK constraints will be copied. No distinction is made between
column constraints and table constraints. Not-null constraints are
always copied to the new table."

So, it looks like there's no way to skip including the NOT NULL
constraints, meaning the only way to avoid the error would be to
modify the source table. If they're not willing to do that, then
CREATE TABLE LIKE is no good for them.

I've included a patch which puts a new error check in
StoreRelNotNull() and spent some time thinking about the best way to
resolve databases with wrongly set connoinherit flags for partitioned
tables. If those are not fixed then dump/restore will fail as the
CREATE TABLE will include a " NO INHERIT" for the NOT NULL
constraint(s) for the partitioned table. I added some advice in the
commit message which basically gives a query to find these, then goes
on to mention using DROP NOT NULL to get rid of them and ALTER
CONSTRAINT ... INHERIT to fix them.

We could also entertain the idea of forcing these to connoinherit =
false or ignoring no inherit constraints
(RelationGetNotNullConstraints() does have a flag for that), but that
could always be done at a later date when there is more time to
consider any unwanted side-effects of that.

The attached adds the ERROR, but puts that ereport() in the equivalent
place as it is for CHECK constraints. There are arguments to improve
the error message to include the constraint name here, or specify that
we're talking about a NOT NULL constraint. I made it the same text as
for CHECK, as that requires no new translations.

David

[1]: https://www.postgresql.org/docs/devel/sql-createtable.html#SQL-CREATETABLE-PARMS-LIKE

Attachments:

t254300_2
v2-0001-Disallow-NOT-NULL-NO-INHERIT-constraints-on-parti.patchapplication/octet-stream; name=v2-0001-Disallow-NOT-NULL-NO-INHERIT-constraints-on-parti.patchDownload+45-7
#3Chao Li
li.evan.chao@gmail.com
In reply to: David Rowley (#2)
Re: [PATCH v1] Fix for bug #19770 : Reject NO INHERIT not-null constraints copied by LIKE to partitioned tables

On Oct 22, 2026, at 10:55, David Rowley <dgrowleyml@gmail.com> wrote:

On Wed, 21 Oct 2026 at 21:32, Nitin Motiani <nitinmotiani@google.com> wrote:

I'm attaching a patch which fixes this by explicitly checking for a
not-null NO INHERIT constraint in transformTableLikeClause and
throwing an error.

Looking at the test added in cacef1722, we do seem to expect that a NO
INHERIT CHECK constraint will block the creation of a partitioned
table with CREATE TABLE LIKE, so you could argue that it makes sense
to do the same for NOT NULL constraints. If we were to go down that
route, then I think the correct place to put the check would be in
StoreRelNotNull() as the neighbouring StoreConstraints() is where
cacef1722's test hits the error for the CHECK constraint. Putting it
there also helps ensure there's no other place that could bypass this
check.

Having said that, I am wondering if this might just be annoying to
users. If the user wants to bypass copying the CHECK constraints, they
can use CREATE TABLE ... (LIKE ... EXCLUDING CONSTRAINTS), then maybe
they have to manually adjust and add the CHECK constraints into the
new table, but, going by [1]:

"The LIKE clause specifies a table from which the new table
automatically copies all column names, their data types, and their
not-null constraints."

and the final sentence of:

"CHECK constraints will be copied. No distinction is made between
column constraints and table constraints. Not-null constraints are
always copied to the new table."

So, it looks like there's no way to skip including the NOT NULL
constraints, meaning the only way to avoid the error would be to
modify the source table. If they're not willing to do that, then
CREATE TABLE LIKE is no good for them.

I've included a patch which puts a new error check in
StoreRelNotNull() and spent some time thinking about the best way to
resolve databases with wrongly set connoinherit flags for partitioned
tables. If those are not fixed then dump/restore will fail as the
CREATE TABLE will include a " NO INHERIT" for the NOT NULL
constraint(s) for the partitioned table. I added some advice in the
commit message which basically gives a query to find these, then goes
on to mention using DROP NOT NULL to get rid of them and ALTER
CONSTRAINT ... INHERIT to fix them.

We could also entertain the idea of forcing these to connoinherit =
false or ignoring no inherit constraints
(RelationGetNotNullConstraints() does have a flag for that), but that
could always be done at a later date when there is more time to
consider any unwanted side-effects of that.

The attached adds the ERROR, but puts that ereport() in the equivalent
place as it is for CHECK constraints. There are arguments to improve
the error message to include the constraint name here, or specify that
we're talking about a NOT NULL constraint. I made it the same text as
for CHECK, as that requires no new translations.

David

[1] https://www.postgresql.org/docs/devel/sql-createtable.html#SQL-CREATETABLE-PARMS-LIKE
<v2-0001-Disallow-NOT-NULL-NO-INHERIT-constraints-on-parti.patch>

I agree that StoreRelNotNull() is a better place for the check.

When testing the patch, I noticed that the error message differs from the one produced when the constraint is declared directly in CREATE TABLE.

With v2:
```
evantest=# CREATE TABLE p (LIKE template) PARTITION BY LIST (a);
ERROR: cannot add NO INHERIT constraint to partitioned table "p"
```

With a direct constraint declaration:
```
evantest=# CREATE TABLE p1 (a integer, b integer CONSTRAINT badnn NOT NULL NO INHERIT) PARTITION BY LIST (a);
ERROR: not-null constraints on partitioned tables cannot be NO INHERIT
```

Should we keep these error messages consistent as they are actually the same error? Reusing the latter error message should not require new translations. Maybe we could also make the error codes consisten. The existing parser check uses ERRCODE_FEATURE_NOT_SUPPORTED, while v2 uses ERRCODE_INVALID_TABLE_DEFINITION.

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

#4Nitin Motiani
nitinmotiani@google.com
In reply to: Chao Li (#3)
Re: [PATCH v1] Fix for bug #19770 : Reject NO INHERIT not-null constraints copied by LIKE to partitioned tables

On Oct 22, 2026, at 10:55, David Rowley <dgrowleyml@gmail.com> wrote:

Looking at the test added in cacef1722, we do seem to expect that a NO
INHERIT CHECK constraint will block the creation of a partitioned
table with CREATE TABLE LIKE, so you could argue that it makes sense
to do the same for NOT NULL constraints. If we were to go down that
route, then I think the correct place to put the check would be in
StoreRelNotNull() as the neighbouring StoreConstraints() is where
cacef1722's test hits the error for the CHECK constraint. Putting it
there also helps ensure there's no other place that could bypass this
check.

Thanks David for looking into this. StoreRelNotNull looks like the
better spot for this.

I've included a patch which puts a new error check in
StoreRelNotNull() and spent some time thinking about the best way to
resolve databases with wrongly set connoinherit flags for partitioned
tables. If those are not fixed then dump/restore will fail as the
CREATE TABLE will include a " NO INHERIT" for the NOT NULL
constraint(s) for the partitioned table. I added some advice in the
commit message which basically gives a query to find these, then goes
on to mention using DROP NOT NULL to get rid of them and ALTER
CONSTRAINT ... INHERIT to fix them.

Should the release notes also explicitly state that NULLs must be
found and fixed directly on the partitions, because a 'delete ...
where col is null' on the parent table would not delete anything?

We could also entertain the idea of forcing these to connoinherit =
false or ignoring no inherit constraints
(RelationGetNotNullConstraints() does have a flag for that), but that
could always be done at a later date when there is more time to
consider any unwanted side-effects of that.

I had considered that, but it didn't seem right to silently drop a
constraint or change its connoinherit when the semantics of LIKE
suggest that it should be the same as the source. If something like
this is done in the future, it should also give a NOTICE/WARNING to
the user.

The attached adds the ERROR, but puts that ereport() in the equivalent
place as it is for CHECK constraints. There are arguments to improve
the error message to include the constraint name here, or specify that
we're talking about a NOT NULL constraint. I made it the same text as
for CHECK, as that requires no new translations.

The existing test on HEAD copies a table that has both a NO INHERIT
CHECK constraint and a NO INHERIT NOT NULL constraint. With the v2,
that statement now fails on the NOT NULL constraint because the
not-null constraints are stored during DefineRelation() before the
CHECK constraints are added. With VERBOSITY verbose, I got this :

CREATE TABLE x (LIKE noinh_con_copy INCLUDING ALL) PARTITION BY LIST (a);
ERROR: 42P16: cannot add NO INHERIT constraint to partitioned table "x"
LOCATION: StoreRelNotNull, heap.c:2290

The two statements v2 adds cover the NOT NULL alone (fails) and
neither constraint (succeeds), so no test exercises LIKE copying a NO
INHERIT CHECK constraint by itself into a partitioned table any more.
Since both errors currently have the same text, the expected output
doesn't show which check fired.

The v1 had a test for that case (noinh_chk_copy in
create_table_like.sql), and I think it's worth keeping:

-- A NO INHERIT CHECK constraint alone must also be rejected.
CREATE TABLE noinh_chk_copy (a int CHECK (a > 0) NO INHERIT);
CREATE TABLE noinh_chk_copy_parted (LIKE noinh_chk_copy INCLUDING CONSTRAINTS)
PARTITION BY LIST (a); -- fail
DROP TABLE noinh_chk_copy;

Please let me know what you think.

Regards,
Nitin Motiani
Google

#5David Rowley
dgrowleyml@gmail.com
In reply to: Nitin Motiani (#4)
Re: [PATCH v1] Fix for bug #19770 : Reject NO INHERIT not-null constraints copied by LIKE to partitioned tables

On Fri, 23 Oct 2026 at 01:48, Nitin Motiani <nitinmotiani@google.com> wrote:

Should the release notes also explicitly state that NULLs must be
found and fixed directly on the partitions, because a 'delete ...
where col is null' on the parent table would not delete anything?

It's a good point. I'd forgotten that the WHERE IS NOT NULL would
become a gating qual. I added some further wording to the commit
message.

The existing test on HEAD copies a table that has both a NO INHERIT
CHECK constraint and a NO INHERIT NOT NULL constraint. With the v2,
that statement now fails on the NOT NULL constraint because the
not-null constraints are stored during DefineRelation() before the
CHECK constraints are added. With VERBOSITY verbose, I got this :

I swapped the order of those tests so they test each thing correctly.

I pushed the result after swapping the error message over to what Chao
mentioned. I'd debated that at the time, but opted for the nearby one
at the time. It's now the same as for normal CREATE TABLE.

David