]> 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 15:01:24 +0000 (00:01 +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 cabe22d48be1e2361ffebcfc0620d2a4578ed11b..5307157f0f3a11d012974344cdc093afe325fcb5 100644 (file)
@@ -12262,6 +12262,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 ebc892a2a4280589ed51f3c1189ef77299eb3e66..e619459647c8b5b69f5212ee2c5b4835f083356e 100644 (file)
@@ -1042,6 +1042,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 1e9989698b68669080b5eebcdacc8a12ab1bfeb4..99846e7cc6a644a3a10d988540382bb51c08dd0b 100644 (file)
@@ -702,6 +702,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);