From e7b208fbccf74a9ea098b94392edf642712a9c44 Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Wed, 8 May 2024 17:42:19 +0200
Subject: [PATCH 5/6] Use NOT NULL NO INHERIT only in pg_upgrade mode

---
 src/bin/pg_dump/pg_dump.c        |  6 ++++--
 src/bin/pg_dump/t/002_pg_dump.pl | 17 +++++++++++++++--
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 8af9127ba2..3b587499e1 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -9365,7 +9365,8 @@ determineNotNullFlags(Archive *fout, PGresult *res, int r,
 			 * which data is going to be loaded by the dump we produce; thus a
 			 * partitioned table doesn't need it.
 			 */
-			if (tbinfo->relkind != RELKIND_PARTITIONED_TABLE)
+			if (dopt->binary_upgrade &&
+				tbinfo->relkind != RELKIND_PARTITIONED_TABLE)
 				use_throwaway_notnull = true;
 		}
 		else if (!PQgetisnull(res, r, i_notnull_name))
@@ -9408,7 +9409,8 @@ determineNotNullFlags(Archive *fout, PGresult *res, int r,
 		else if (PQgetvalue(res, r, i_notnull_is_pk)[0] == 't')
 		{
 			/* see above */
-			if (tbinfo->relkind != RELKIND_PARTITIONED_TABLE)
+			if (dopt->binary_upgrade &&
+				tbinfo->relkind != RELKIND_PARTITIONED_TABLE)
 				use_throwaway_notnull = true;
 		}
 	}
diff --git a/src/bin/pg_dump/t/002_pg_dump.pl b/src/bin/pg_dump/t/002_pg_dump.pl
index 7085053a2d..52eeb95bb6 100644
--- a/src/bin/pg_dump/t/002_pg_dump.pl
+++ b/src/bin/pg_dump/t/002_pg_dump.pl
@@ -3242,7 +3242,7 @@ my %tests = (
 					   );',
 		regexp => qr/^
 			\QCREATE TABLE dump_test.fk_reference_test_table (\E
-			\n\s+\Qcol1 integer CONSTRAINT \E[a-z0-9_]*\Q NOT NULL NO INHERIT\E
+			\n\s+\Qcol1 integer\E
 			\n\);
 			/xm,
 		like =>
@@ -3250,9 +3250,21 @@ my %tests = (
 		unlike => {
 			exclude_dump_test_schema => 1,
 			only_dump_measurement => 1,
+			binary_upgrade => 1,
 		},
 	},
 
+	# The same table as above when dumped in binary-upgrade mode
+	'binary-upgrade dump for fk_reference_test_table' => {
+		regexp => qr/^
+		\QCREATE TABLE dump_test.fk_reference_test_table (\E
+		\n\s+\Qcol1 integer CONSTRAINT \E[a-z0-9_]*\Q NOT NULL NO INHERIT\E
+		\n\);
+		/xm,
+		like =>
+		{ binary_upgrade => 1 }
+	},
+
 	'CREATE TABLE test_second_table' => {
 		create_order => 6,
 		create_sql => 'CREATE TABLE dump_test.test_second_table (
@@ -3635,7 +3647,7 @@ my %tests = (
 					   );',
 		regexp => qr/^
 			\QCREATE TABLE dump_test.test_table_generated (\E\n
-			\s+\Qcol1 integer CONSTRAINT \E[a-z0-9_]*\Q NOT NULL NO INHERIT,\E\n
+			\s+\Qcol1 integer,\E\n
 			\s+\Qcol2 integer GENERATED ALWAYS AS ((col1 * 2)) STORED\E\n
 			\);
 			/xms,
@@ -3644,6 +3656,7 @@ my %tests = (
 		unlike => {
 			exclude_dump_test_schema => 1,
 			only_dump_measurement => 1,
+			binary_upgrade => 1,
 		},
 	},
 
-- 
2.39.2

