From 3abea06eb68bd7b87a46a0821ac0d1c36e591583 Mon Sep 17 00:00:00 2001
From: sai pranav <rajasaipranav0@gmail.com>
Date: Sat, 28 Nov 2026 14:11:44 +0530
Subject: [PATCH v1] Disallow making a primary key column's not-null constraint
 NO INHERIT

A primary key requires the not-null constraints on its columns to be
inheritable: CREATE TABLE and ALTER TABLE ... ADD PRIMARY KEY reject a
NO INHERIT one.  But ALTER TABLE ... ALTER CONSTRAINT ... NO INHERIT
did not check for a primary key, so it could produce that state on an
existing table.  pg_dump then emits the column as NOT NULL NO INHERIT
followed by the primary key, which fails to restore, and so does
pg_upgrade.

Reject it with the same error that ALTER COLUMN ... DROP NOT NULL
reports for a primary key column.

Reported-by: Alexander Lakhin <exclusion@gmail.com>
---
 src/backend/commands/tablecmds.c      | 23 ++++++++++++++++++++---
 src/test/regress/expected/inherit.out |  3 +++
 src/test/regress/sql/inherit.sql      |  2 ++
 3 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 9e363a44c2..778e1f2d6f 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -13711,13 +13711,30 @@ ATExecAlterConstrInheritability(List **wqueue, ATAlterConstraint *cmdcon,
 	if (cmdcon->noinherit == currcon->connoinherit)
 		return false;
 
-	AlterConstrUpdateConstraintEntry(cmdcon, conrel, contuple);
-	CommandCounterIncrement();
-
 	/* Fetch the column number and name */
 	colNum = extractNotNullColumn(contuple);
 	colName = get_attname(currcon->conrelid, colNum, false);
 
+	/*
+	 * A primary key requires the not-null constraints on its columns to be
+	 * inheritable (see verifyNotNullPKCompatible), so don't allow one to be
+	 * made NO INHERIT underneath a primary key.  Otherwise the table could
+	 * not be dumped and restored.
+	 */
+	if (cmdcon->noinherit)
+	{
+		Bitmapset  *pkattrs;
+
+		pkattrs = RelationGetIndexAttrBitmap(rel, INDEX_ATTR_BITMAP_PRIMARY_KEY);
+		if (bms_is_member(colNum - FirstLowInvalidHeapAttributeNumber, pkattrs))
+			ereport(ERROR,
+					errcode(ERRCODE_INVALID_TABLE_DEFINITION),
+					errmsg("column \"%s\" is in a primary key", colName));
+	}
+
+	AlterConstrUpdateConstraintEntry(cmdcon, conrel, contuple);
+	CommandCounterIncrement();
+
 	/*
 	 * Propagate the change to children.  For this subcommand type we don't
 	 * recursively affect children, just the immediate level.
diff --git a/src/test/regress/expected/inherit.out b/src/test/regress/expected/inherit.out
index bde0ed6dd3..32b93ff117 100644
--- a/src/test/regress/expected/inherit.out
+++ b/src/test/regress/expected/inherit.out
@@ -3135,6 +3135,9 @@ ERROR:  constraint "foo" of relation "inh_nn1" does not exist
 create table inh_nn2 () inherits (inh_nn1);
 alter table inh_nn2 alter constraint inh_nn1_f2_not_null no inherit;
 ERROR:  cannot alter inherited constraint "inh_nn1_f2_not_null" on relation "inh_nn2"
+-- Can't make the not-null constraint of a primary key column NO INHERIT
+alter table inh_nn1 alter constraint inh_nn1_f1_not_null no inherit;
+ERROR:  column "f1" is in a primary key
 drop table inh_nn1, inh_nn2;
 --
 -- Mixed ownership inheritance tree
diff --git a/src/test/regress/sql/inherit.sql b/src/test/regress/sql/inherit.sql
index 8590f408e2..2543daf4a9 100644
--- a/src/test/regress/sql/inherit.sql
+++ b/src/test/regress/sql/inherit.sql
@@ -1281,6 +1281,8 @@ alter table inh_nn1 alter constraint foo inherit;
 -- Can't modify inheritability of inherited constraints
 create table inh_nn2 () inherits (inh_nn1);
 alter table inh_nn2 alter constraint inh_nn1_f2_not_null no inherit;
+-- Can't make the not-null constraint of a primary key column NO INHERIT
+alter table inh_nn1 alter constraint inh_nn1_f1_not_null no inherit;
 
 drop table inh_nn1, inh_nn2;
 
-- 
2.50.1 (Apple Git-155)

