]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Prevent setting NO INHERIT on partitioned NOT NULL constraints
authorFujii Masao <fujii@postgresql.org>
Fri, 22 May 2026 14:59:04 +0000 (23:59 +0900)
committerFujii Masao <fujii@postgresql.org>
Fri, 22 May 2026 14:59:04 +0000 (23:59 +0900)
The documentation states that NOT NULL constraints on partitioned tables
are always inherited by all partitions, and therefore cannot be declared
NO INHERIT. While a check already existed to reject creating such
constraints with NO INHERIT, previously the same check was missing for
ALTER TABLE ... ALTER CONSTRAINT ... NO INHERIT.

This commit adds the missing check so that attempting to set NO INHERIT
on a partitioned NOT NULL constraint now fails.

Backpatch to v18, where ALTER TABLE ... ALTER CONSTRAINT ... [NO] INHERIT
was added.

Author: Andreas Karlsson <andreas@proxel.se>
Reviewed-by: Jim Jones <jim.jones@uni-muenster.de>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/ecc985ad-6ec1-4094-a315-317943ca5f3f@proxel.se
Backpatch-through: 18

src/backend/commands/tablecmds.c
src/test/regress/expected/constraints.out
src/test/regress/sql/constraints.sql

index 92b0f38c3532d48ac685c25dc5547ddf3e314a6a..1e0bacf85fc87b46caec0c1ee3568e876ebe1d9d 100644 (file)
@@ -12367,6 +12367,12 @@ ATExecAlterConstraint(List **wqueue, Relation rel, ATAlterConstraint *cmdcon,
                                errcode(ERRCODE_WRONG_OBJECT_TYPE),
                                errmsg("constraint \"%s\" of relation \"%s\" is not a not-null constraint",
                                           cmdcon->conname, RelationGetRelationName(rel)));
+       if (cmdcon->alterInheritability &&
+               cmdcon->noinherit && rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
+               ereport(ERROR,
+                               errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                               errmsg("not-null constraint \"%s\" on partitioned table \"%s\" cannot be NO INHERIT",
+                                          cmdcon->conname, RelationGetRelationName(rel)));
 
        /* Refuse to modify inheritability of inherited constraints */
        if (cmdcon->alterInheritability &&
index 728ef2fd17e0b516e369742a7fa354b0712137f5..e54fec7fb577776a528000e1729631bc62a04e10 100644 (file)
@@ -1130,6 +1130,10 @@ CREATE TABLE ATACC1 (a int NOT NULL NO INHERIT) PARTITION BY LIST (a);
 ERROR:  not-null constraints on partitioned tables cannot be NO INHERIT
 CREATE TABLE ATACC1 (a int, NOT NULL a NO INHERIT) PARTITION BY LIST (a);
 ERROR:  not-null constraints on partitioned tables cannot be NO INHERIT
+CREATE TABLE ATACC1 (a int, CONSTRAINT a_is_not_null NOT NULL a) PARTITION BY LIST (a);
+ALTER TABLE ATACC1 ALTER CONSTRAINT a_is_not_null NO INHERIT;
+ERROR:  not-null constraint "a_is_not_null" on partitioned table "atacc1" cannot be NO INHERIT
+DROP TABLE ATACC1;
 -- it's not possible to override a no-inherit constraint with an inheritable one
 CREATE TABLE ATACC2 (a int, CONSTRAINT a_is_not_null NOT NULL a NO INHERIT);
 CREATE TABLE ATACC1 (a int);
index 483c1e98372de0ae7e1eebfde9dd546e497f7934..dc133b124bbfd81cf4ab7ab61183bf9396f5f6a1 100644 (file)
@@ -757,6 +757,9 @@ DROP TABLE ATACC1, ATACC2, ATACC3;
 -- NOT NULL NO INHERIT is not possible on partitioned tables
 CREATE TABLE ATACC1 (a int NOT NULL NO INHERIT) PARTITION BY LIST (a);
 CREATE TABLE ATACC1 (a int, NOT NULL a NO INHERIT) PARTITION BY LIST (a);
+CREATE TABLE ATACC1 (a int, CONSTRAINT a_is_not_null NOT NULL a) PARTITION BY LIST (a);
+ALTER TABLE ATACC1 ALTER CONSTRAINT a_is_not_null NO INHERIT;
+DROP TABLE ATACC1;
 
 -- it's not possible to override a no-inherit constraint with an inheritable one
 CREATE TABLE ATACC2 (a int, CONSTRAINT a_is_not_null NOT NULL a NO INHERIT);