From 1543c693d709f13acb4f0322dc9181fa2c0c9e5c Mon Sep 17 00:00:00 2001
From: Antoine Violin <a.violin@postgrespro.ru>
Date: Mon, 29 Jun 2026 12:03:00 +0700
Subject: [PATCH] pg_dump: drop column default before adding identity

---
 src/bin/pg_dump/pg_dump.c        | 16 ++++++++++++++++
 src/bin/pg_dump/t/002_pg_dump.pl | 30 ++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 61c572664e7..8b0be39df00 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -19128,6 +19128,22 @@ dumpSequence(Archive *fout, const TableInfo *tbinfo)
 	{
 		owning_tab = findTableByOid(tbinfo->owning_tab);
 
+		/*
+		 * The column may already have a DEFAULT inherited/printed by CREATE TABLE.
+		 * ADD GENERATED AS IDENTITY fails if a default is present, so remove it
+		 * before adding identity.
+		 */
+		if (owning_tab->attrdefs != NULL &&
+			owning_tab->attrdefs[tbinfo->owning_col - 1] != NULL)
+		{
+			appendPQExpBuffer(query,
+							  "ALTER TABLE %s ",
+							  fmtQualifiedDumpable(owning_tab));
+			appendPQExpBuffer(query,
+							  "ALTER COLUMN %s DROP DEFAULT;\n",
+							  fmtId(owning_tab->attnames[tbinfo->owning_col - 1]));
+		}
+
 		appendPQExpBuffer(query,
 						  "ALTER TABLE %s ",
 						  fmtQualifiedDumpable(owning_tab));
diff --git a/src/bin/pg_dump/t/002_pg_dump.pl b/src/bin/pg_dump/t/002_pg_dump.pl
index 8595e0fa93b..1761b0c9539 100644
--- a/src/bin/pg_dump/t/002_pg_dump.pl
+++ b/src/bin/pg_dump/t/002_pg_dump.pl
@@ -1640,6 +1640,36 @@ my %tests = (
 		},
 	},
 
+	'ALTER TABLE inherited identity column DROP DEFAULT' => {
+		create_sql => q{
+			CREATE SEQUENCE dump_test.inherited_identity_column_id_seq AS integer;
+
+			CREATE TABLE dump_test.inherited_identity_column_parent (
+				id integer NOT NULL DEFAULT nextval('dump_test.inherited_identity_column_id_seq'::regclass)
+			);
+
+			CREATE TABLE dump_test.inherited_identity_column_child (
+				id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
+				child_id integer NOT NULL
+			) INHERITS (dump_test.inherited_identity_column_parent);
+		},
+
+		regexp => qr/^
+			\QALTER TABLE dump_test.inherited_identity_column_child ALTER COLUMN id DROP DEFAULT;\E
+			.*?
+			\QALTER TABLE dump_test.inherited_identity_column_child ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY\E
+			/xms,
+		like => {
+			%full_runs,
+			%dump_test_schema_runs,
+			section_pre_data => 1,
+		},
+		unlike => {
+			exclude_dump_test_schema => 1,
+			only_dump_measurement => 1,
+		},
+	},
+
 	'ALTER FOREIGN TABLE foreign_table OWNER TO' => {
 		regexp =>
 		  qr/^\QALTER FOREIGN TABLE dump_test.foreign_table OWNER TO \E.+;/m,
-- 
2.43.0

