pg_restore: Fix comment handling with --no-publications / --no-subscriptions.

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
REL_14_STABLE
Fujii Masao 3 months ago
parent cbd3732cbb
commit db900ec358
  1. 14
      src/bin/pg_dump/pg_backup_archiver.c
  2. 37
      src/bin/pg_dump/t/002_pg_dump.pl

@ -2858,6 +2858,20 @@ _tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH)
if (ropt->no_comments && strcmp(te->desc, "COMMENT") == 0) if (ropt->no_comments && strcmp(te->desc, "COMMENT") == 0)
return 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 * If it's a publication or a table part of a publication, maybe ignore
* it. * it.

@ -268,6 +268,29 @@ my %pgdump_runs = (
'postgres', 'postgres',
], ],
}, },
no_subscriptions => {
dump_cmd => [
'pg_dump', '--no-sync',
'--file' => "$tempdir/no_subscriptions.sql",
'--no-subscriptions',
'postgres',
],
},
no_subscriptions_restore => {
dump_cmd => [
'pg_dump', '--no-sync',
'--format' => 'custom',
'--file' => "$tempdir/no_subscriptions_restore.dump",
'postgres',
],
restore_cmd => [
'pg_restore',
'--format' => 'custom',
'--file' => "$tempdir/no_subscriptions_restore.sql",
'--no-subscriptions',
"$tempdir/no_subscriptions_restore.dump",
],
},
only_dump_test_schema => { only_dump_test_schema => {
dump_cmd => [ dump_cmd => [
'pg_dump', '--no-sync', 'pg_dump', '--no-sync',
@ -420,6 +443,8 @@ my %full_runs = (
no_blobs => 1, no_blobs => 1,
no_owner => 1, no_owner => 1,
no_privs => 1, no_privs => 1,
no_subscriptions => 1,
no_subscriptions_restore => 1,
pg_dumpall_dbprivs => 1, pg_dumpall_dbprivs => 1,
pg_dumpall_exclude => 1, pg_dumpall_exclude => 1,
schema_only => 1,); schema_only => 1,);
@ -1131,6 +1156,10 @@ my %tests = (
regexp => regexp =>
qr/^COMMENT ON SUBSCRIPTION sub1 IS 'comment on subscription';/m, qr/^COMMENT ON SUBSCRIPTION sub1 IS 'comment on subscription';/m,
like => { %full_runs, section_post_data => 1, }, 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' => { 'COMMENT ON TEXT SEARCH CONFIGURATION dump_test.alt_ts_conf1' => {
@ -2401,6 +2430,10 @@ my %tests = (
\QCREATE SUBSCRIPTION sub1 CONNECTION 'dbname=doesnotexist' PUBLICATION pub1 WITH (connect = false, slot_name = 'sub1');\E \QCREATE SUBSCRIPTION sub1 CONNECTION 'dbname=doesnotexist' PUBLICATION pub1 WITH (connect = false, slot_name = 'sub1');\E
/xm, /xm,
like => { %full_runs, section_post_data => 1, }, like => { %full_runs, section_post_data => 1, },
unlike => {
no_subscriptions => 1,
no_subscriptions_restore => 1,
},
}, },
'ALTER PUBLICATION pub1 ADD TABLE test_table' => { 'ALTER PUBLICATION pub1 ADD TABLE test_table' => {
@ -2969,6 +3002,8 @@ my %tests = (
no_blobs => 1, no_blobs => 1,
no_privs => 1, no_privs => 1,
no_owner => 1, no_owner => 1,
no_subscriptions => 1,
no_subscriptions_restore => 1,
only_dump_test_schema => 1, only_dump_test_schema => 1,
pg_dumpall_dbprivs => 1, pg_dumpall_dbprivs => 1,
pg_dumpall_exclude => 1, pg_dumpall_exclude => 1,
@ -3040,6 +3075,8 @@ my %tests = (
no_blobs => 1, no_blobs => 1,
no_privs => 1, no_privs => 1,
no_owner => 1, no_owner => 1,
no_subscriptions => 1,
no_subscriptions_restore => 1,
pg_dumpall_dbprivs => 1, pg_dumpall_dbprivs => 1,
pg_dumpall_exclude => 1, pg_dumpall_exclude => 1,
role => 1, role => 1,

Loading…
Cancel
Save