From c632ba872f3e22bd0e662d3f1908723611370da9 Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Tue, 26 Aug 2025 20:11:51 +0900
Subject: [PATCH v3] pg_restore: Fix handling of comments on
 policies/publications/subscriptions.

Previously, when --no-policies, --no-publications, or --no-subscriptions
were specified, pg_restore skipped restoring the corresponding objects
but did not skip their comments incorrectly. This could cause failures
by trying to restore comments on objects that were not created.

This commit fixes the issue by making pg_restore also skip comments on
these objects when the corresponding options are used.

Discussion: https://postgr.es/m/18970-a7d1cfe1f8d5d8d9@postgresql.org
---
 src/bin/pg_dump/pg_backup_archiver.c | 19 ++++++++++++
 src/bin/pg_dump/t/002_pg_dump.pl     | 46 ++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+)

diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index 3c3acbaccdb..c03479484c5 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -3048,6 +3048,25 @@ _tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH)
 		 strcmp(te->desc, "ROW SECURITY") == 0))
 		return 0;
 
+	/*
+	 * If it's a comment on a policy, a publication, or a subscription, maybe
+	 * ignore it.
+	 */
+	if (strcmp(te->desc, "COMMENT") == 0)
+	{
+		if (ropt->no_policies &&
+			strncmp(te->tag, "POLICY", strlen("POLICY")) == 0)
+			return 0;
+
+		if (ropt->no_publications &&
+			strncmp(te->tag, "PUBLICATION", strlen("PUBLICATION")) == 0)
+			return 0;
+
+		if (ropt->no_subscriptions &&
+			strncmp(te->tag, "SUBSCRIPTION", strlen("SUBSCRIPTION")) == 0)
+			return 0;
+	}
+
 	/*
 	 * If it's a publication or a table part of a publication, maybe ignore
 	 * it.
diff --git a/src/bin/pg_dump/t/002_pg_dump.pl b/src/bin/pg_dump/t/002_pg_dump.pl
index e7a2d64f741..8c4e8a8bc73 100644
--- a/src/bin/pg_dump/t/002_pg_dump.pl
+++ b/src/bin/pg_dump/t/002_pg_dump.pl
@@ -632,6 +632,23 @@ my %pgdump_runs = (
 			'postgres',
 		],
 	},
+	no_policies_restore => {
+		dump_cmd => [
+			'pg_dump', '--no-sync',
+			'--format' => 'custom',
+			'--file' => "$tempdir/no_policies_restore.dump",
+			'--statistics',
+			'postgres',
+		],
+		restore_cmd => [
+			'pg_restore',
+			'--format' => 'custom',
+			'--file' => "$tempdir/no_policies_restore.sql",
+			'--no-policies',
+			'--statistics',
+			"$tempdir/no_policies_restore.dump",
+		],
+	},
 	no_privs => {
 		dump_cmd => [
 			'pg_dump', '--no-sync',
@@ -871,6 +888,7 @@ my %full_runs = (
 	no_large_objects => 1,
 	no_owner => 1,
 	no_policies => 1,
+	no_policies_restore => 1,
 	no_privs => 1,
 	no_statistics => 1,
 	no_table_access_method => 1,
@@ -1512,6 +1530,7 @@ my %tests = (
 			exclude_dump_test_schema => 1,
 			exclude_test_table => 1,
 			no_policies => 1,
+			no_policies_restore => 1,
 			only_dump_measurement => 1,
 		},
 	},
@@ -1830,6 +1849,27 @@ my %tests = (
 		},
 	},
 
+	'COMMENT ON POLICY p1' => {
+		create_order => 55,
+		create_sql => 'COMMENT ON POLICY p1 ON dump_test.test_table
+					   IS \'comment on policy\';',
+		regexp =>
+		  qr/^COMMENT ON POLICY p1 ON dump_test.test_table IS 'comment on policy';/m,
+		like => {
+			%full_runs,
+			%dump_test_schema_runs,
+			only_dump_test_table => 1,
+			section_post_data => 1,
+		},
+		unlike => {
+			exclude_dump_test_schema => 1,
+			exclude_test_table => 1,
+			no_policies => 1,
+			no_policies_restore => 1,
+			only_dump_measurement => 1,
+		},
+	},
+
 	'COMMENT ON PUBLICATION pub1' => {
 		create_order => 55,
 		create_sql => 'COMMENT ON PUBLICATION pub1
@@ -3192,6 +3232,7 @@ my %tests = (
 			exclude_dump_test_schema => 1,
 			exclude_test_table => 1,
 			no_policies => 1,
+			no_policies_restore => 1,
 			only_dump_measurement => 1,
 		},
 	},
@@ -3214,6 +3255,7 @@ my %tests = (
 			exclude_dump_test_schema => 1,
 			exclude_test_table => 1,
 			no_policies => 1,
+			no_policies_restore => 1,
 			only_dump_measurement => 1,
 		},
 	},
@@ -3236,6 +3278,7 @@ my %tests = (
 			exclude_dump_test_schema => 1,
 			exclude_test_table => 1,
 			no_policies => 1,
+			no_policies_restore => 1,
 			only_dump_measurement => 1,
 		},
 	},
@@ -3258,6 +3301,7 @@ my %tests = (
 			exclude_dump_test_schema => 1,
 			exclude_test_table => 1,
 			no_policies => 1,
+			no_policies_restore => 1,
 			only_dump_measurement => 1,
 		},
 	},
@@ -3280,6 +3324,7 @@ my %tests = (
 			exclude_dump_test_schema => 1,
 			exclude_test_table => 1,
 			no_policies => 1,
+			no_policies_restore => 1,
 			only_dump_measurement => 1,
 		},
 	},
@@ -3302,6 +3347,7 @@ my %tests = (
 			exclude_dump_test_schema => 1,
 			exclude_test_table => 1,
 			no_policies => 1,
+			no_policies_restore => 1,
 			only_dump_measurement => 1,
 		},
 	},
-- 
2.50.1

