Prevent setting NO INHERIT on partitioned not-null constraints

Started by Andreas Karlssonabout 1 month ago6 messageshackers
Jump to latest
#1Andreas Karlsson
andreas.karlsson@percona.com

Hi!

Me and Joel found a bug when working on another patch. We noticed that
you cannot create not-null constraints with NO INHEIRT set on
partitioned tables, but you can actually set it later by using ALTER
CONSTRAINT. This must be an oversight so I have attached a patch which
adds a check to prevent this.

The SQL below should give an error but does not:

CREATE TABLE t (
a int,
CONSTRAINT a_is_not_null NOT NULL a
) PARTITION BY LIST (a);

ALTER TABLE t ALTER CONSTRAINT a_is_not_null NO INHERIT;

--
Andreas Karlsson
Percona

Attachments:

v1-0001-Prevent-setting-NO-INHERIT-on-paritioned-not-null.patchtext/x-patch; charset=UTF-8; name=v1-0001-Prevent-setting-NO-INHERIT-on-paritioned-not-null.patchDownload+13-1
#2Jim Jones
jim.jones@uni-muenster.de
In reply to: Andreas Karlsson (#1)
Re: Prevent setting NO INHERIT on partitioned not-null constraints

Hi Andreas

On 20/05/2026 21:25, Andreas Karlsson wrote:

Me and Joel found a bug when working on another patch. We noticed that
you cannot create not-null constraints with NO INHEIRT set on
partitioned tables, but you can actually set it later by using ALTER
CONSTRAINT. This must be an oversight so I have attached a patch which
adds a check to prevent this.

The SQL below should give an error but does not:

CREATE TABLE t (
  a int,
  CONSTRAINT a_is_not_null NOT NULL a
) PARTITION BY LIST (a);

ALTER TABLE t ALTER CONSTRAINT a_is_not_null NO INHERIT;

+1 for the fix.

postgres=# CREATE TABLE t (
a int,
CONSTRAINT a_is_not_null NOT NULL a
) PARTITION BY LIST (a);
CREATE TABLE
postgres=# ALTER TABLE t ALTER CONSTRAINT a_is_not_null NO INHERIT;
ERROR: not-null constraint "a_is_not_null" on partitioned table "t"
cannot be NO INHERIT

The errcode is most likely wrong:

ERRCODE_WRONG_OBJECT_TYPE -> ERRCODE_FEATURE_NOT_SUPPORTED

At least it is inconsistent with an equivalent check in parse_utilcmd.c:

if (cxt->ispartitioned && constraint->is_no_inherit)
ereport(ERROR,
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("not-null constraints on partitioned tables cannot be
NO INHERIT"));

Thanks!

Best, Jim

#3Andreas Karlsson
andreas.karlsson@percona.com
In reply to: Jim Jones (#2)
Re: Prevent setting NO INHERIT on partitioned not-null constraints

On 5/21/26 5:01 PM, Jim Jones wrote:

The errcode is most likely wrong:

ERRCODE_WRONG_OBJECT_TYPE -> ERRCODE_FEATURE_NOT_SUPPORTED

At least it is inconsistent with an equivalent check in parse_utilcmd.c:

if (cxt->ispartitioned && constraint->is_no_inherit)
ereport(ERROR,
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("not-null constraints on partitioned tables cannot be
NO INHERIT"));

Thanks, that was a copy pasto. Version 2 is attached.

--
Andreas Karlsson
Percona

Attachments:

v2-0001-Prevent-setting-NO-INHERIT-on-paritioned-not-null.patchtext/x-patch; charset=UTF-8; name=v2-0001-Prevent-setting-NO-INHERIT-on-paritioned-not-null.patchDownload+13-1
#4Fujii Masao
masao.fujii@gmail.com
In reply to: Andreas Karlsson (#3)
Re: Prevent setting NO INHERIT on partitioned not-null constraints

On Fri, May 22, 2026 at 6:22 AM Andreas Karlsson <andreas@proxel.se> wrote:

On 5/21/26 5:01 PM, Jim Jones wrote:

The errcode is most likely wrong:

ERRCODE_WRONG_OBJECT_TYPE -> ERRCODE_FEATURE_NOT_SUPPORTED

At least it is inconsistent with an equivalent check in parse_utilcmd.c:

if (cxt->ispartitioned && constraint->is_no_inherit)
ereport(ERROR,
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("not-null constraints on partitioned tables cannot be
NO INHERIT"));

Thanks, that was a copy pasto. Version 2 is attached.

Thanks for the patch! It looks good to me.
Barring any objections, I will commit it.

This should be backpatched to v18, where ALTER TABLE ... ALTER
CONSTRAINT ... [NO] INHERIT
was added. Right?

Regards,

--
Fujii Masao

#5Andreas Karlsson
andreas.karlsson@percona.com
In reply to: Fujii Masao (#4)
Re: Prevent setting NO INHERIT on partitioned not-null constraints

On 5/22/26 3:10 AM, Fujii Masao wrote:

This should be backpatched to v18, where ALTER TABLE ... ALTER
CONSTRAINT ... [NO] INHERIT
was added. Right?

Yes, as this is a bug I think it should be backpatched to 18.

--
Andreas Karlsson
Percona

#6Fujii Masao
masao.fujii@gmail.com
In reply to: Andreas Karlsson (#5)
Re: Prevent setting NO INHERIT on partitioned not-null constraints

On Fri, May 22, 2026 at 5:10 PM Andreas Karlsson <andreas@proxel.se> wrote:

On 5/22/26 3:10 AM, Fujii Masao wrote:

This should be backpatched to v18, where ALTER TABLE ... ALTER
CONSTRAINT ... [NO] INHERIT
was added. Right?

Yes, as this is a bug I think it should be backpatched to 18.

I've pushed the patch. Thanks!

Regards,

--
Fujii Masao