From af53b60f93b4cde712f924928b92eca5d4a43ac7 Mon Sep 17 00:00:00 2001
From: Sai Pranav <rajasaipranav0@gmail.com>
Date: Mon, 30 Nov 2026 19:11:29 +0530
Subject: [PATCH v4 2/2] pg_dump: Restore comments on inherited NOT VALID
 not-null constraints in order

An inherited NOT VALID not-null constraint is not dumped itself: it is
created when its ancestor's constraint is restored.  But a comment on it
was made to depend on the constraint's own dump ID, which has no TOC
entry, so nothing ordered the COMMENT after the ancestor's ADD
CONSTRAINT.  Depending on the order a plain dump or a parallel
pg_restore happened to use, the comment failed with "constraint ... for
table ... does not exist".

Fix by recording, in flagInhAttrs(), the nearest ancestor that defines a
constraint of the same name locally.  The inherited constraint is made
to depend on it, which orders a plain dump, and the comment depends on
it directly, which orders pg_restore.

Add the case to the constraints regression test.

Reported-by: Alexander Lakhin <exclusion@gmail.com>
---
 src/bin/pg_dump/common.c                  | 36 ++++++++++++++++-------
 src/bin/pg_dump/pg_dump.c                 |  9 +++++-
 src/bin/pg_dump/pg_dump.h                 |  2 ++
 src/test/regress/expected/constraints.out |  5 ++++
 src/test/regress/sql/constraints.sql      |  5 ++++
 5 files changed, 46 insertions(+), 11 deletions(-)

diff --git a/src/bin/pg_dump/common.c b/src/bin/pg_dump/common.c
index b66a85769f..57a540b093 100644
--- a/src/bin/pg_dump/common.c
+++ b/src/bin/pg_dump/common.c
@@ -584,12 +584,11 @@ flagInhAttrs(Archive *fout, DumpOptions *dopt, TableInfo *tblinfo, int numTables
 			}
 
 			/*
-			 * If this table has its own NOT VALID not-null constraint on the
-			 * column, it must be restored before those of its ancestors.
+			 * Order a NOT VALID not-null constraint on this column relative
+			 * to those of its ancestors; see addInvalidNotNullDependencies.
 			 */
 			if (tbinfo->notnull_invalidcon &&
-				tbinfo->notnull_invalidcon[j] &&
-				tbinfo->notnull_invalidcon[j]->conislocal)
+				tbinfo->notnull_invalidcon[j])
 				addInvalidNotNullDependencies(tbinfo, tbinfo->attnames[j],
 											  tbinfo->notnull_invalidcon[j]);
 
@@ -662,16 +661,23 @@ flagInhAttrs(Archive *fout, DumpOptions *dopt, TableInfo *tblinfo, int numTables
 
 /*
  * addInvalidNotNullDependencies
- *	  Make the NOT VALID not-null constraints on column attname of all of
- *	  tbinfo's ancestors depend on childcon, a NOT VALID not-null constraint
- *	  that a descendant of theirs defines locally on that column.
+ *	  Order childcon, a NOT VALID not-null constraint on column attname of
+ *	  tbinfo or of a descendant of it, relative to the NOT VALID not-null
+ *	  constraints on that column of tbinfo's ancestors.
  *
  * Adding a NOT VALID not-null constraint to a table also adds it to its
  * inheritance children, and a child that has no not-null constraint on the
  * column yet gets one with the parent's name.  If the child has its own
  * constraint with a different name and the parent's is restored first,
  * creating the child's then fails.  Restoring the child's first lets the
- * parent's merge into it, as in the original database.
+ * parent's merge into it, as in the original database.  So if childcon is
+ * defined locally, the ancestors' constraints are made to depend on it.
+ *
+ * If childcon is only inherited, it is not dumped itself, but created by the
+ * nearest ancestor that defines a constraint of the same name locally.  Make
+ * childcon depend on that one and record it, so that a comment on childcon
+ * is restored after it, both in a plain dump and by pg_restore (see
+ * dumpTableConstraintComment).
  *
  * Tables in between that have no constraint of their own are walked through
  * too, since adding a constraint to a table recurses to all its descendants.
@@ -722,8 +728,18 @@ addInvalidNotNullDependencies(TableInfo *tbinfo, const char *attname,
 			if (parent->notnull_invalidcon &&
 				parent->notnull_invalidcon[inhAttrInd] &&
 				!parent->notnull_noinh[inhAttrInd])
-				addObjectDependency(&parent->notnull_invalidcon[inhAttrInd]->dobj,
-									childcon->dobj.dumpId);
+			{
+				ConstraintInfo *parentcon = parent->notnull_invalidcon[inhAttrInd];
+
+				if (childcon->conislocal)
+					addObjectDependency(&parentcon->dobj, childcon->dobj.dumpId);
+				else if (childcon->inhcreator == 0 && parentcon->conislocal &&
+						 strcmp(parentcon->dobj.name, childcon->dobj.name) == 0)
+				{
+					childcon->inhcreator = parentcon->dobj.dumpId;
+					addObjectDependency(&childcon->dobj, parentcon->dobj.dumpId);
+				}
+			}
 		}
 	}
 
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 77cca591f8..373d7b5bad 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -9827,6 +9827,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
 				constrs[j].contable = tbinfo;
 				constrs[j].condomain = NULL;
 				constrs[j].contype = 'n';
+				constrs[j].inhcreator = 0;
 				constrs[j].condef = pg_strdup(PQgetvalue(res, j, i_consrc));
 				constrs[j].confrelid = InvalidOid;
 				constrs[j].conindex = 0;
@@ -19240,12 +19241,18 @@ dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo)
 	appendPQExpBuffer(conprefix, "CONSTRAINT %s ON",
 					  fmtId(coninfo->dobj.name));
 
+	/*
+	 * An inherited NOT VALID not-null constraint is not dumped itself, but
+	 * created by its ancestor's, so the comment must follow that one.
+	 */
 	if (coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
 		dumpComment(fout, conprefix->data, qtabname,
 					tbinfo->dobj.namespace->dobj.name,
 					tbinfo->rolname,
 					coninfo->dobj.catId, 0,
-					coninfo->separate ? coninfo->dobj.dumpId : tbinfo->dobj.dumpId);
+					!coninfo->separate ? tbinfo->dobj.dumpId :
+					(coninfo->contype == 'n' && coninfo->inhcreator != 0) ?
+					coninfo->inhcreator : coninfo->dobj.dumpId);
 
 	destroyPQExpBuffer(conprefix);
 	pg_free(qtabname);
diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h
index 7d727bf1f7..37d0e4c27b 100644
--- a/src/bin/pg_dump/pg_dump.h
+++ b/src/bin/pg_dump/pg_dump.h
@@ -530,6 +530,8 @@ typedef struct _constraintInfo
 	bool		conperiod;		/* true if the constraint is WITHOUT OVERLAPS */
 	bool		conislocal;		/* true if constraint has local definition */
 	bool		separate;		/* true if must dump as separate item */
+	DumpId		inhcreator;		/* inherited NOT VALID not-null: the
+								 * ancestor's constraint that creates it, or 0 */
 } ConstraintInfo;
 
 typedef struct _procLangInfo
diff --git a/src/test/regress/expected/constraints.out b/src/test/regress/expected/constraints.out
index 6de42f626b..f092ad34a5 100644
--- a/src/test/regress/expected/constraints.out
+++ b/src/test/regress/expected/constraints.out
@@ -1758,6 +1758,11 @@ EXECUTE get_nnconstraint_info('{notnull_parent2_upg, notnull_child2_upg}');
  notnull_parent2_upg | nn                            | f            | t          |           0
 (2 rows)
 
+-- Comment on a child's inherited NOT VALID constraint, for pg_upgrade
+CREATE TABLE notnull_parent3_upg (a int);
+CREATE TABLE notnull_child3_upg () INHERITS (notnull_parent3_upg);
+ALTER TABLE notnull_parent3_upg ADD NOT NULL a NOT VALID;
+COMMENT ON CONSTRAINT notnull_parent3_upg_a_not_null ON notnull_child3_upg IS 'on the child';
 -- 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 f3eaca30f2..3f7905e865 100644
--- a/src/test/regress/sql/constraints.sql
+++ b/src/test/regress/sql/constraints.sql
@@ -1053,6 +1053,11 @@ 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}');
 
+-- Comment on a child's inherited NOT VALID constraint, for pg_upgrade
+CREATE TABLE notnull_parent3_upg (a int);
+CREATE TABLE notnull_child3_upg () INHERITS (notnull_parent3_upg);
+ALTER TABLE notnull_parent3_upg ADD NOT NULL a NOT VALID;
+COMMENT ON CONSTRAINT notnull_parent3_upg_a_not_null ON notnull_child3_upg IS 'on the child';
 -- 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
-- 
2.50.1 (Apple Git-155)

