🌐 AI搜索 & 代理 主页
Skip to content

Commit 1cdc07a

Browse files
committed
Sort DO_SUBSCRIPTION_REL dump objects independent of OIDs.
Commit 0decd5e missed DO_SUBSCRIPTION_REL, leading to assertion failures. In the unlikely use case of diffing "pg_dump --binary-upgrade" output, spurious diffs were possible. As part of fixing that, align the DumpableObject naming and sort order with DO_PUBLICATION_REL. The overall effect of this commit is to change sort order from (subname, srsubid) to (rel, subname). Since DO_SUBSCRIPTION_REL is only for --binary-upgrade, accept that larger-than-usual dump order change. Back-patch to v17, where commit 9a17be1 introduced DO_SUBSCRIPTION_REL. Reported-by: vignesh C <vignesh21@gmail.com> Author: vignesh C <vignesh21@gmail.com> Discussion: https://postgr.es/m/CALDaNm2x3rd7C0_HjUpJFbxpAqXgm=QtoKfkEWDVA8h+JFpa_w@mail.gmail.com Backpatch-through: 17
1 parent 4b6d096 commit 1cdc07a

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5057,16 +5057,16 @@ getSubscriptionTables(Archive *fout)
50575057
subrinfo[i].dobj.catId.tableoid = relid;
50585058
subrinfo[i].dobj.catId.oid = cur_srsubid;
50595059
AssignDumpId(&subrinfo[i].dobj);
5060-
subrinfo[i].dobj.name = pg_strdup(subinfo->dobj.name);
5060+
subrinfo[i].dobj.namespace = tblinfo->dobj.namespace;
5061+
subrinfo[i].dobj.name = tblinfo->dobj.name;
5062+
subrinfo[i].subinfo = subinfo;
50615063
subrinfo[i].tblinfo = tblinfo;
50625064
subrinfo[i].srsubstate = PQgetvalue(res, i, i_srsubstate)[0];
50635065
if (PQgetisnull(res, i, i_srsublsn))
50645066
subrinfo[i].srsublsn = NULL;
50655067
else
50665068
subrinfo[i].srsublsn = pg_strdup(PQgetvalue(res, i, i_srsublsn));
50675069

5068-
subrinfo[i].subinfo = subinfo;
5069-
50705070
/* Decide whether we want to dump it */
50715071
selectDumpableObject(&(subrinfo[i].dobj), fout);
50725072
}
@@ -5094,7 +5094,7 @@ dumpSubscriptionTable(Archive *fout, const SubRelInfo *subrinfo)
50945094

50955095
Assert(fout->dopt->binary_upgrade && fout->remoteVersion >= 170000);
50965096

5097-
tag = psprintf("%s %s", subinfo->dobj.name, subrinfo->dobj.name);
5097+
tag = psprintf("%s %s", subinfo->dobj.name, subrinfo->tblinfo->dobj.name);
50985098

50995099
query = createPQExpBuffer();
51005100

@@ -5109,7 +5109,7 @@ dumpSubscriptionTable(Archive *fout, const SubRelInfo *subrinfo)
51095109
"\n-- For binary upgrade, must preserve the subscriber table.\n");
51105110
appendPQExpBufferStr(query,
51115111
"SELECT pg_catalog.binary_upgrade_add_sub_rel_state(");
5112-
appendStringLiteralAH(query, subrinfo->dobj.name, fout);
5112+
appendStringLiteralAH(query, subinfo->dobj.name, fout);
51135113
appendPQExpBuffer(query,
51145114
", %u, '%c'",
51155115
subrinfo->tblinfo->dobj.catId.oid,

src/bin/pg_dump/pg_dump_sort.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,17 @@ DOTypeNameCompare(const void *p1, const void *p2)
453453
if (cmpval != 0)
454454
return cmpval;
455455
}
456+
else if (obj1->objType == DO_SUBSCRIPTION_REL)
457+
{
458+
SubRelInfo *srobj1 = *(SubRelInfo *const *) p1;
459+
SubRelInfo *srobj2 = *(SubRelInfo *const *) p2;
460+
461+
/* Sort by subscription name, since (namespace, name) match the rel */
462+
cmpval = strcmp(srobj1->subinfo->dobj.name,
463+
srobj2->subinfo->dobj.name);
464+
if (cmpval != 0)
465+
return cmpval;
466+
}
456467

457468
/*
458469
* Shouldn't get here except after catalog corruption, but if we do, sort

src/bin/pg_upgrade/t/004_subscription.pl

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,23 +180,27 @@
180180
rmtree($new_sub->data_dir . "/pg_upgrade_output.d");
181181

182182
# Verify that the upgrade should be successful with tables in 'ready'/'init'
183-
# state along with retaining the replication origin's remote lsn, subscription's
184-
# running status, and failover option.
183+
# state along with retaining the replication origin's remote lsn,
184+
# subscription's running status, and failover option. Use multiple tables to
185+
# verify deterministic pg_dump ordering of subscription relations during
186+
# --binary-upgrade.
185187
$publisher->safe_psql(
186188
'postgres', qq[
189+
CREATE TABLE tab_upgraded(id int);
187190
CREATE TABLE tab_upgraded1(id int);
188-
CREATE PUBLICATION regress_pub4 FOR TABLE tab_upgraded1;
191+
CREATE PUBLICATION regress_pub4 FOR TABLE tab_upgraded, tab_upgraded1;
189192
]);
190193

191194
$old_sub->safe_psql(
192195
'postgres', qq[
196+
CREATE TABLE tab_upgraded(id int);
193197
CREATE TABLE tab_upgraded1(id int);
194198
CREATE SUBSCRIPTION regress_sub4 CONNECTION '$connstr' PUBLICATION regress_pub4 WITH (failover = true);
195199
]);
196200

197-
# Wait till the table tab_upgraded1 reaches 'ready' state
201+
# Wait till the tables tab_upgraded and tab_upgraded1 reach 'ready' state
198202
my $synced_query =
199-
"SELECT count(1) = 1 FROM pg_subscription_rel WHERE srsubstate = 'r'";
203+
"SELECT count(1) = 2 FROM pg_subscription_rel WHERE srsubstate = 'r'";
200204
$old_sub->poll_query_until('postgres', $synced_query)
201205
or die "Timed out while waiting for the table to reach ready state";
202206

@@ -234,6 +238,8 @@
234238
# Have the subscription in disabled state before upgrade
235239
$old_sub->safe_psql('postgres', "ALTER SUBSCRIPTION regress_sub5 DISABLE");
236240

241+
my $tab_upgraded_oid = $old_sub->safe_psql('postgres',
242+
"SELECT oid FROM pg_class WHERE relname = 'tab_upgraded'");
237243
my $tab_upgraded1_oid = $old_sub->safe_psql('postgres',
238244
"SELECT oid FROM pg_class WHERE relname = 'tab_upgraded1'");
239245
my $tab_upgraded2_oid = $old_sub->safe_psql('postgres',
@@ -248,9 +254,9 @@
248254

249255
# ------------------------------------------------------
250256
# Check that pg_upgrade is successful when all tables are in ready or in
251-
# init state (tab_upgraded1 table is in ready state and tab_upgraded2 table is
252-
# in init state) along with retaining the replication origin's remote lsn,
253-
# subscription's running status, and failover option.
257+
# init state (tab_upgraded and tab_upgraded1 tables are in ready state and
258+
# tab_upgraded2 table is in init state) along with retaining the replication
259+
# origin's remote lsn, subscription's running status, and failover option.
254260
# ------------------------------------------------------
255261
command_ok(
256262
[
@@ -292,9 +298,10 @@
292298
# Subscription relations should be preserved
293299
$result = $new_sub->safe_psql('postgres',
294300
"SELECT srrelid, srsubstate FROM pg_subscription_rel ORDER BY srrelid");
295-
is( $result, qq($tab_upgraded1_oid|r
301+
is( $result, qq($tab_upgraded_oid|r
302+
$tab_upgraded1_oid|r
296303
$tab_upgraded2_oid|i),
297-
"there should be 2 rows in pg_subscription_rel(representing tab_upgraded1 and tab_upgraded2)"
304+
"there should be 3 rows in pg_subscription_rel(representing tab_upgraded, tab_upgraded1 and tab_upgraded2)"
298305
);
299306

300307
# The replication origin's remote_lsn should be preserved

0 commit comments

Comments
 (0)