From 239d98991073610b36c5d8611e9c6142c2afd877 Mon Sep 17 00:00:00 2001
From: Sai Pranav <rajasaipranav0@gmail.com>
Date: Thu, 26 Nov 2026 20:57:21 +0530
Subject: [PATCH v1] pg_dump: Restore a child's NOT VALID not-null constraint
 before its parent's

NOT VALID not-null constraints are dumped as separate ALTER TABLE ... ADD
CONSTRAINT commands.  Adding one to a parent table recurses to the
inheritance children, and a child with no not-null constraint on the
column yet gets one with the parent's constraint name.  So if a child has
its own NOT VALID not-null constraint with a different name, and the
parent's is restored first, the child's own command then fails with
"cannot create not-null constraint ... A not-null constraint named ...
already exists for this column".  This breaks pg_dump/restore and
pg_upgrade.  Before the commit "Reject ADD CONSTRAINT NOT NULL if name
mismatches existing constraint", the restore went through, but the child
ended up with the parent's constraint name and conislocal = false.

Fix by making each ancestor's NOT VALID not-null constraints depend on
the child's own, so the child's is restored first and the parent's then
merges with it, as in the original database.

Add a table pair to the constraints regression test that is left in
place, so that the pg_upgrade test covers this.

Reported-by: Alexander Lakhin <exclusion@gmail.com>
---
 src/bin/pg_dump/pg_dump.c                 | 46 +++++++++++++++++++++++
 src/test/regress/expected/constraints.out | 13 +++++++
 src/test/regress/sql/constraints.sql      |  7 ++++
 3 files changed, 66 insertions(+)

diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 0ef3337ce1..ad94337383 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -9709,6 +9709,52 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
 			}
 		}
 		PQclear(res);
+
+		/*
+		 * Adding an invalid not-null constraint to a table recurses to its
+		 * inheritance children, and a child that has no not-null constraint
+		 * on the column yet gets one with the parent's constraint name.  If
+		 * the child has its own invalid not-null constraint with a different
+		 * name, that one must therefore be restored first, or adding it
+		 * would fail.  Make the invalid not-null constraints of all of the
+		 * child's ancestors depend on it.
+		 */
+		for (int j = 0; j < numConstrs; j++)
+		{
+			TableInfo **ancestors;
+			int			nancestors = 0;
+
+			if (!constrs[j].conislocal || constrs[j].contable->numParents == 0)
+				continue;
+
+			ancestors = pg_malloc_array(TableInfo *, numTables);
+			for (int p = 0; p < constrs[j].contable->numParents; p++)
+				ancestors[nancestors++] = constrs[j].contable->parents[p];
+
+			for (int a = 0; a < nancestors; a++)
+			{
+				TableInfo  *anc = ancestors[a];
+
+				for (int k = 0; k < numConstrs; k++)
+				{
+					if (constrs[k].contable == anc)
+						addObjectDependency(&constrs[k].dobj,
+											constrs[j].dobj.dumpId);
+				}
+
+				/* Also visit this ancestor's parents, once each */
+				for (int p = 0; p < anc->numParents; p++)
+				{
+					bool		seen = false;
+
+					for (int s = 0; s < nancestors && !seen; s++)
+						seen = (ancestors[s] == anc->parents[p]);
+					if (!seen)
+						ancestors[nancestors++] = anc->parents[p];
+				}
+			}
+			pg_free(ancestors);
+		}
 	}
 
 	/*
diff --git a/src/test/regress/expected/constraints.out b/src/test/regress/expected/constraints.out
index e619459647..c8cf4c8dc0 100644
--- a/src/test/regress/expected/constraints.out
+++ b/src/test/regress/expected/constraints.out
@@ -1623,6 +1623,19 @@ ORDER BY 1;
  notnull_child_upg  | n       | t            | t
 (2 rows)
 
+-- Child with its own NOT VALID constraint, differently named, for pg_upgrade
+CREATE TABLE notnull_parent2_upg (a int);
+ALTER TABLE notnull_parent2_upg ADD CONSTRAINT nn NOT NULL a NOT VALID;
+CREATE TABLE notnull_child2_upg (a int);
+ALTER TABLE notnull_child2_upg ADD NOT NULL a NOT VALID;
+ALTER TABLE notnull_child2_upg INHERIT notnull_parent2_upg;
+EXECUTE get_nnconstraint_info('{notnull_parent2_upg, notnull_child2_upg}');
+       tabname       |            conname            | convalidated | conislocal | coninhcount 
+---------------------+-------------------------------+--------------+------------+-------------
+ notnull_child2_upg  | notnull_child2_upg_a_not_null | f            | t          |           1
+ notnull_parent2_upg | nn                            | f            | t          |           0
+(2 rows)
+
 -- Partition table test, for pg_upgrade
 CREATE TABLE notnull_part1_upg (a int, b int) PARTITION BY LIST (a);
 ALTER TABLE notnull_part1_upg ADD CONSTRAINT notnull_con NOT NULL a NOT VALID; --ok
diff --git a/src/test/regress/sql/constraints.sql b/src/test/regress/sql/constraints.sql
index 99846e7cc6..6cec0db6e0 100644
--- a/src/test/regress/sql/constraints.sql
+++ b/src/test/regress/sql/constraints.sql
@@ -978,6 +978,13 @@ SELECT conrelid::regclass, contype, convalidated, conislocal
 FROM pg_catalog.pg_constraint
 WHERE conrelid in ('notnull_parent_upg'::regclass, 'notnull_child_upg'::regclass)
 ORDER BY 1;
+-- Child with its own NOT VALID constraint, differently named, for pg_upgrade
+CREATE TABLE notnull_parent2_upg (a int);
+ALTER TABLE notnull_parent2_upg ADD CONSTRAINT nn NOT NULL a NOT VALID;
+CREATE TABLE notnull_child2_upg (a int);
+ALTER TABLE notnull_child2_upg ADD NOT NULL a NOT VALID;
+ALTER TABLE notnull_child2_upg INHERIT notnull_parent2_upg;
+EXECUTE get_nnconstraint_info('{notnull_parent2_upg, notnull_child2_upg}');
 
 -- Partition table test, for pg_upgrade
 CREATE TABLE notnull_part1_upg (a int, b int) PARTITION BY LIST (a);
-- 
2.50.1 (Apple Git-155)

