Skip to content

Commit 00bf707

Browse files
committed
Have pg_upgrade properly preserve relfrozenxid in toast tables.
This fixes a pg_upgrade bug that could lead to query errors when clog files are improperly removed.
1 parent a5c629f commit 00bf707

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3409,6 +3409,8 @@ getTables(int *numTables)
34093409
int i_relhasrules;
34103410
int i_relhasoids;
34113411
int i_relfrozenxid;
3412+
int i_toastoid;
3413+
int i_toastfrozenxid;
34123414
int i_owning_tab;
34133415
int i_owning_col;
34143416
int i_reltablespace;
@@ -3451,7 +3453,8 @@ getTables(int *numTables)
34513453
"(%s c.relowner) AS rolname, "
34523454
"c.relchecks, c.relhastriggers, "
34533455
"c.relhasindex, c.relhasrules, c.relhasoids, "
3454-
"c.relfrozenxid, "
3456+
"c.relfrozenxid, tc.oid AS toid, "
3457+
"tc.relfrozenxid AS tfrozenxid, "
34553458
"CASE WHEN c.reloftype <> 0 THEN c.reloftype::pg_catalog.regtype ELSE NULL END AS reloftype, "
34563459
"d.refobjid AS owning_tab, "
34573460
"d.refobjsubid AS owning_col, "
@@ -3484,7 +3487,8 @@ getTables(int *numTables)
34843487
"(%s c.relowner) AS rolname, "
34853488
"c.relchecks, c.relhastriggers, "
34863489
"c.relhasindex, c.relhasrules, c.relhasoids, "
3487-
"c.relfrozenxid, "
3490+
"c.relfrozenxid, tc.oid AS toid, "
3491+
"tc.relfrozenxid AS tfrozenxid, "
34883492
"NULL AS reloftype, "
34893493
"d.refobjid AS owning_tab, "
34903494
"d.refobjsubid AS owning_col, "
@@ -3518,6 +3522,8 @@ getTables(int *numTables)
35183522
"relchecks, (reltriggers <> 0) AS relhastriggers, "
35193523
"relhasindex, relhasrules, relhasoids, "
35203524
"relfrozenxid, "
3525+
"0 AS toid, "
3526+
"0 AS tfrozenxid, "
35213527
"NULL AS reloftype, "
35223528
"d.refobjid AS owning_tab, "
35233529
"d.refobjsubid AS owning_col, "
@@ -3550,6 +3556,8 @@ getTables(int *numTables)
35503556
"relchecks, (reltriggers <> 0) AS relhastriggers, "
35513557
"relhasindex, relhasrules, relhasoids, "
35523558
"0 AS relfrozenxid, "
3559+
"0 AS toid, "
3560+
"0 AS tfrozenxid, "
35533561
"NULL AS reloftype, "
35543562
"d.refobjid AS owning_tab, "
35553563
"d.refobjsubid AS owning_col, "
@@ -3582,6 +3590,8 @@ getTables(int *numTables)
35823590
"relchecks, (reltriggers <> 0) AS relhastriggers, "
35833591
"relhasindex, relhasrules, relhasoids, "
35843592
"0 AS relfrozenxid, "
3593+
"0 AS toid, "
3594+
"0 AS tfrozenxid, "
35853595
"NULL AS reloftype, "
35863596
"d.refobjid AS owning_tab, "
35873597
"d.refobjsubid AS owning_col, "
@@ -3610,6 +3620,8 @@ getTables(int *numTables)
36103620
"relchecks, (reltriggers <> 0) AS relhastriggers, "
36113621
"relhasindex, relhasrules, relhasoids, "
36123622
"0 AS relfrozenxid, "
3623+
"0 AS toid, "
3624+
"0 AS tfrozenxid, "
36133625
"NULL AS reloftype, "
36143626
"NULL::oid AS owning_tab, "
36153627
"NULL::int4 AS owning_col, "
@@ -3633,6 +3645,8 @@ getTables(int *numTables)
36333645
"relhasindex, relhasrules, "
36343646
"'t'::bool AS relhasoids, "
36353647
"0 AS relfrozenxid, "
3648+
"0 AS toid, "
3649+
"0 AS tfrozenxid, "
36363650
"NULL AS reloftype, "
36373651
"NULL::oid AS owning_tab, "
36383652
"NULL::int4 AS owning_col, "
@@ -3666,6 +3680,8 @@ getTables(int *numTables)
36663680
"relhasindex, relhasrules, "
36673681
"'t'::bool AS relhasoids, "
36683682
"0 as relfrozenxid, "
3683+
"0 AS toid, "
3684+
"0 AS tfrozenxid, "
36693685
"NULL AS reloftype, "
36703686
"NULL::oid AS owning_tab, "
36713687
"NULL::int4 AS owning_col, "
@@ -3711,6 +3727,8 @@ getTables(int *numTables)
37113727
i_relhasrules = PQfnumber(res, "relhasrules");
37123728
i_relhasoids = PQfnumber(res, "relhasoids");
37133729
i_relfrozenxid = PQfnumber(res, "relfrozenxid");
3730+
i_toastoid = PQfnumber(res, "toid");
3731+
i_toastfrozenxid = PQfnumber(res, "tfrozenxid");
37143732
i_owning_tab = PQfnumber(res, "owning_tab");
37153733
i_owning_col = PQfnumber(res, "owning_col");
37163734
i_reltablespace = PQfnumber(res, "reltablespace");
@@ -3750,6 +3768,8 @@ getTables(int *numTables)
37503768
tblinfo[i].hastriggers = (strcmp(PQgetvalue(res, i, i_relhastriggers), "t") == 0);
37513769
tblinfo[i].hasoids = (strcmp(PQgetvalue(res, i, i_relhasoids), "t") == 0);
37523770
tblinfo[i].frozenxid = atooid(PQgetvalue(res, i, i_relfrozenxid));
3771+
tblinfo[i].toast_oid = atooid(PQgetvalue(res, i, i_toastoid));
3772+
tblinfo[i].toast_frozenxid = atooid(PQgetvalue(res, i, i_toastfrozenxid));
37533773
if (PQgetisnull(res, i, i_reloftype))
37543774
tblinfo[i].reloftype = NULL;
37553775
else
@@ -10852,13 +10872,23 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1085210872
}
1085310873
}
1085410874

10855-
appendPQExpBuffer(q, "\n-- For binary upgrade, set relfrozenxid.\n");
10875+
appendPQExpBuffer(q, "\n-- For binary upgrade, set heap's relfrozenxid\n");
1085610876
appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
1085710877
"SET relfrozenxid = '%u'\n"
1085810878
"WHERE oid = ",
1085910879
tbinfo->frozenxid);
1086010880
appendStringLiteralAH(q, fmtId(tbinfo->dobj.name), fout);
1086110881
appendPQExpBuffer(q, "::pg_catalog.regclass;\n");
10882+
10883+
if (tbinfo->toast_oid)
10884+
{
10885+
/* We preserve the toast oids, so we can use it during restore */
10886+
appendPQExpBuffer(q, "\n-- For binary upgrade, set toast's relfrozenxid\n");
10887+
appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
10888+
"SET relfrozenxid = '%u'\n"
10889+
"WHERE oid = '%u';\n",
10890+
tbinfo->toast_frozenxid, tbinfo->toast_oid);
10891+
}
1086210892
}
1086310893

1086410894
/* Loop dumping statistics and storage statements */

src/bin/pg_dump/pg_dump.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ typedef struct _tableInfo
228228
bool hastriggers; /* does it have any triggers? */
229229
bool hasoids; /* does it have OIDs? */
230230
uint32 frozenxid; /* for restore frozen xid */
231+
Oid toast_oid; /* for restore toast frozen xid */
232+
uint32 toast_frozenxid;/* for restore toast frozen xid */
231233
int ncheck; /* # of CHECK expressions */
232234
char *reloftype; /* underlying type for typed table */
233235
/* these two are set only if table is a sequence owned by a column: */

0 commit comments

Comments
 (0)