]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
pg_restore: Fix comment handling with --no-publications / --no-subscriptions.
authorFujii Masao <fujii@postgresql.org>
Tue, 16 Sep 2025 01:35:12 +0000 (10:35 +0900)
committerFujii Masao <fujii@postgresql.org>
Tue, 16 Sep 2025 01:36:20 +0000 (10:36 +0900)
Previously, pg_restore did not skip comments on publications or subscriptions
even when --no-publications or --no-subscriptions was specified. As a result,
it could issue COMMENT commands for objects that were never created,
causing those commands to fail.

This commit fixes the issue by ensuring that comments on publications and
subscriptions are also skipped when the corresponding options are used.

Backpatch to all supported versions.

Author: Jian He <jian.universality@gmail.com>
Co-authored-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/CACJufxHCt00pR9h51AVu6+yPD5J7JQn=7dQXxqacj0XyDhc-fA@mail.gmail.com
Backpatch-through: 13

src/bin/pg_dump/pg_backup_archiver.c
src/bin/pg_dump/t/002_pg_dump.pl

index f1ed2d9ff909cf5eb8c89187bd2bae03eda6c617..7299ffa661945182bf2dc3407b350294c34e87db 100644 (file)
@@ -3033,6 +3033,20 @@ _tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH)
                 strcmp(te->desc, "ROW SECURITY") == 0))
                return 0;
 
+       /*
+        * If it's a comment on a publication or a subscription, maybe ignore it.
+        */
+       if (strcmp(te->desc, "COMMENT") == 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.
index a68f132e4490a4194d8722ce1277e3bd2082c6ca..a626d24b5730cd0395cc873f6fadc0d0cb6f24e2 100644 (file)
@@ -650,6 +650,32 @@ my %pgdump_runs = (
                        'postgres',
                ],
        },
+       no_subscriptions => {
+               dump_cmd => [
+                       'pg_dump', '--no-sync',
+                       '--file' => "$tempdir/no_subscriptions.sql",
+                       '--no-subscriptions',
+                       '--statistics',
+                       'postgres',
+               ],
+       },
+       no_subscriptions_restore => {
+               dump_cmd => [
+                       'pg_dump', '--no-sync',
+                       '--format' => 'custom',
+                       '--file' => "$tempdir/no_subscriptions_restore.dump",
+                       '--statistics',
+                       'postgres',
+               ],
+               restore_cmd => [
+                       'pg_restore',
+                       '--format' => 'custom',
+                       '--file' => "$tempdir/no_subscriptions_restore.sql",
+                       '--no-subscriptions',
+                       '--statistics',
+                       "$tempdir/no_subscriptions_restore.dump",
+               ],
+       },
        no_table_access_method => {
                dump_cmd => [
                        'pg_dump', '--no-sync',
@@ -873,6 +899,8 @@ my %full_runs = (
        no_policies => 1,
        no_privs => 1,
        no_statistics => 1,
+       no_subscriptions => 1,
+       no_subscriptions_restore => 1,
        no_table_access_method => 1,
        pg_dumpall_dbprivs => 1,
        pg_dumpall_exclude => 1,
@@ -1844,6 +1872,10 @@ my %tests = (
                regexp =>
                  qr/^COMMENT ON SUBSCRIPTION sub1 IS 'comment on subscription';/m,
                like => { %full_runs, section_post_data => 1, },
+               unlike => {
+                       no_subscriptions => 1,
+                       no_subscriptions_restore => 1,
+               },
        },
 
        'COMMENT ON TEXT SEARCH CONFIGURATION dump_test.alt_ts_conf1' => {
@@ -3361,6 +3393,10 @@ my %tests = (
                        \QCREATE SUBSCRIPTION sub1 CONNECTION 'dbname=doesnotexist' PUBLICATION pub1 WITH (connect = false, slot_name = 'sub1', streaming = parallel);\E
                        /xm,
                like => { %full_runs, section_post_data => 1, },
+               unlike => {
+                       no_subscriptions => 1,
+                       no_subscriptions_restore => 1,
+               },
        },
 
        'CREATE SUBSCRIPTION sub2' => {
@@ -3372,6 +3408,10 @@ my %tests = (
                        \QCREATE SUBSCRIPTION sub2 CONNECTION 'dbname=doesnotexist' PUBLICATION pub1 WITH (connect = false, slot_name = 'sub2', streaming = off, origin = none);\E
                        /xm,
                like => { %full_runs, section_post_data => 1, },
+               unlike => {
+                       no_subscriptions => 1,
+                       no_subscriptions_restore => 1,
+               },
        },
 
        'CREATE SUBSCRIPTION sub3' => {
@@ -3383,6 +3423,10 @@ my %tests = (
                        \QCREATE SUBSCRIPTION sub3 CONNECTION 'dbname=doesnotexist' PUBLICATION pub1 WITH (connect = false, slot_name = 'sub3', streaming = on);\E
                        /xm,
                like => { %full_runs, section_post_data => 1, },
+               unlike => {
+                       no_subscriptions => 1,
+                       no_subscriptions_restore => 1,
+               },
        },