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

Commit 573e679

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 d77a5f9 commit 573e679

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
@@ -5232,16 +5232,16 @@ getSubscriptionTables(Archive *fout)
52325232
subrinfo[i].dobj.catId.tableoid = relid;
52335233
subrinfo[i].dobj.catId.oid = cur_srsubid;
52345234
AssignDumpId(&subrinfo[i].dobj);
5235-
subrinfo[i].dobj.name = pg_strdup(subinfo->dobj.name);
5235+
subrinfo[i].dobj.namespace = tblinfo->dobj.namespace;
5236+
subrinfo[i].dobj.name = tblinfo->dobj.name;
5237+
subrinfo[i].subinfo = subinfo;
52365238
subrinfo[i].tblinfo = tblinfo;
52375239
subrinfo[i].srsubstate = PQgetvalue(res, i, i_srsubstate)[0];
52385240
if (PQgetisnull(res, i, i_srsublsn))
52395241
subrinfo[i].srsublsn = NULL;
52405242
else
52415243
subrinfo[i].srsublsn = pg_strdup(PQgetvalue(res, i, i_srsublsn));
52425244

5243-
subrinfo[i].subinfo = subinfo;
5244-
52455245
/* Decide whether we want to dump it */
52465246
selectDumpableObject(&(subrinfo[i].dobj), fout);
52475247
}
@@ -5269,7 +5269,7 @@ dumpSubscriptionTable(Archive *fout, const SubRelInfo *subrinfo)
52695269

52705270
Assert(fout->dopt->binary_upgrade && fout->remoteVersion >= 170000);
52715271

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

52745274
query = createPQExpBuffer();
52755275

@@ -5284,7 +5284,7 @@ dumpSubscriptionTable(Archive *fout, const SubRelInfo *subrinfo)
52845284
"\n-- For binary upgrade, must preserve the subscriber table.\n");
52855285
appendPQExpBufferStr(query,
52865286
"SELECT pg_catalog.binary_upgrade_add_sub_rel_state(");
5287-
appendStringLiteralAH(query, subrinfo->dobj.name, fout);
5287+
appendStringLiteralAH(query, subinfo->dobj.name, fout);
52885288
appendPQExpBuffer(query,
52895289
", %u, '%c'",
52905290
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
@@ -454,6 +454,17 @@ DOTypeNameCompare(const void *p1, const void *p2)
454454
if (cmpval != 0)
455455
return cmpval;
456456
}
457+
else if (obj1->objType == DO_SUBSCRIPTION_REL)
458+
{
459+
SubRelInfo *srobj1 = *(SubRelInfo *const *) p1;
460+
SubRelInfo *srobj2 = *(SubRelInfo *const *) p2;
461+
462+
/* Sort by subscription name, since (namespace, name) match the rel */
463+
cmpval = strcmp(srobj1->subinfo->dobj.name,
464+
srobj2->subinfo->dobj.name);
465+
if (cmpval != 0)
466+
return cmpval;
467+
}
457468

458469
/*
459470
* 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
@@ -200,23 +200,27 @@
200200
rmtree($new_sub->data_dir . "/pg_upgrade_output.d");
201201

202202
# Verify that the upgrade should be successful with tables in 'ready'/'init'
203-
# state along with retaining the replication origin's remote lsn, subscription's
204-
# running status, and failover option.
203+
# state along with retaining the replication origin's remote lsn,
204+
# subscription's running status, and failover option. Use multiple tables to
205+
# verify deterministic pg_dump ordering of subscription relations during
206+
# --binary-upgrade.
205207
$publisher->safe_psql(
206208
'postgres', qq[
209+
CREATE TABLE tab_upgraded(id int);
207210
CREATE TABLE tab_upgraded1(id int);
208-
CREATE PUBLICATION regress_pub4 FOR TABLE tab_upgraded1;
211+
CREATE PUBLICATION regress_pub4 FOR TABLE tab_upgraded, tab_upgraded1;
209212
]);
210213

211214
$old_sub->safe_psql(
212215
'postgres', qq[
216+
CREATE TABLE tab_upgraded(id int);
213217
CREATE TABLE tab_upgraded1(id int);
214218
CREATE SUBSCRIPTION regress_sub4 CONNECTION '$connstr' PUBLICATION regress_pub4 WITH (failover = true);
215219
]);
216220

217-
# Wait till the table tab_upgraded1 reaches 'ready' state
221+
# Wait till the tables tab_upgraded and tab_upgraded1 reach 'ready' state
218222
my $synced_query =
219-
"SELECT count(1) = 1 FROM pg_subscription_rel WHERE srsubstate = 'r'";
223+
"SELECT count(1) = 2 FROM pg_subscription_rel WHERE srsubstate = 'r'";
220224
$old_sub->poll_query_until('postgres', $synced_query)
221225
or die "Timed out while waiting for the table to reach ready state";
222226

@@ -254,6 +258,8 @@
254258
# Have the subscription in disabled state before upgrade
255259
$old_sub->safe_psql('postgres', "ALTER SUBSCRIPTION regress_sub5 DISABLE");
256260

261+
my $tab_upgraded_oid = $old_sub->safe_psql('postgres',
262+
"SELECT oid FROM pg_class WHERE relname = 'tab_upgraded'");
257263
my $tab_upgraded1_oid = $old_sub->safe_psql('postgres',
258264
"SELECT oid FROM pg_class WHERE relname = 'tab_upgraded1'");
259265
my $tab_upgraded2_oid = $old_sub->safe_psql('postgres',
@@ -268,9 +274,9 @@
268274

269275
# ------------------------------------------------------
270276
# Check that pg_upgrade is successful when all tables are in ready or in
271-
# init state (tab_upgraded1 table is in ready state and tab_upgraded2 table is
272-
# in init state) along with retaining the replication origin's remote lsn,
273-
# subscription's running status, and failover option.
277+
# init state (tab_upgraded and tab_upgraded1 tables are in ready state and
278+
# tab_upgraded2 table is in init state) along with retaining the replication
279+
# origin's remote lsn, subscription's running status, and failover option.
274280
# ------------------------------------------------------
275281
command_ok(
276282
[
@@ -317,9 +323,10 @@
317323
# Subscription relations should be preserved
318324
$result = $new_sub->safe_psql('postgres',
319325
"SELECT srrelid, srsubstate FROM pg_subscription_rel ORDER BY srrelid");
320-
is( $result, qq($tab_upgraded1_oid|r
326+
is( $result, qq($tab_upgraded_oid|r
327+
$tab_upgraded1_oid|r
321328
$tab_upgraded2_oid|i),
322-
"there should be 2 rows in pg_subscription_rel(representing tab_upgraded1 and tab_upgraded2)"
329+
"there should be 3 rows in pg_subscription_rel(representing tab_upgraded, tab_upgraded1 and tab_upgraded2)"
323330
);
324331

325332
# The replication origin's remote_lsn should be preserved

0 commit comments

Comments
 (0)