Skip to content

Commit 9c38bce

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 dca30da commit 9c38bce

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3812,6 +3812,8 @@ getTables(int *numTables)
38123812
int i_relhasrules;
38133813
int i_relhasoids;
38143814
int i_relfrozenxid;
3815+
int i_toastoid;
3816+
int i_toastfrozenxid;
38153817
int i_relpersistence;
38163818
int i_owning_tab;
38173819
int i_owning_col;
@@ -3855,7 +3857,9 @@ getTables(int *numTables)
38553857
"(%s c.relowner) AS rolname, "
38563858
"c.relchecks, c.relhastriggers, "
38573859
"c.relhasindex, c.relhasrules, c.relhasoids, "
3858-
"c.relfrozenxid, c.relpersistence, "
3860+
"c.relfrozenxid, tc.oid AS toid, "
3861+
"tc.relfrozenxid AS tfrozenxid, "
3862+
"c.relpersistence, "
38593863
"CASE WHEN c.reloftype <> 0 THEN c.reloftype::pg_catalog.regtype ELSE NULL END AS reloftype, "
38603864
"d.refobjid AS owning_tab, "
38613865
"d.refobjsubid AS owning_col, "
@@ -3889,7 +3893,9 @@ getTables(int *numTables)
38893893
"(%s c.relowner) AS rolname, "
38903894
"c.relchecks, c.relhastriggers, "
38913895
"c.relhasindex, c.relhasrules, c.relhasoids, "
3892-
"c.relfrozenxid, 'p' AS relpersistence, "
3896+
"c.relfrozenxid, tc.oid AS toid, "
3897+
"tc.relfrozenxid AS tfrozenxid, "
3898+
"'p' AS relpersistence, "
38933899
"CASE WHEN c.reloftype <> 0 THEN c.reloftype::pg_catalog.regtype ELSE NULL END AS reloftype, "
38943900
"d.refobjid AS owning_tab, "
38953901
"d.refobjsubid AS owning_col, "
@@ -3922,7 +3928,9 @@ getTables(int *numTables)
39223928
"(%s c.relowner) AS rolname, "
39233929
"c.relchecks, c.relhastriggers, "
39243930
"c.relhasindex, c.relhasrules, c.relhasoids, "
3925-
"c.relfrozenxid, 'p' AS relpersistence, "
3931+
"c.relfrozenxid, tc.oid AS toid, "
3932+
"tc.relfrozenxid AS tfrozenxid, "
3933+
"'p' AS relpersistence, "
39263934
"NULL AS reloftype, "
39273935
"d.refobjid AS owning_tab, "
39283936
"d.refobjsubid AS owning_col, "
@@ -3955,7 +3963,10 @@ getTables(int *numTables)
39553963
"(%s relowner) AS rolname, "
39563964
"relchecks, (reltriggers <> 0) AS relhastriggers, "
39573965
"relhasindex, relhasrules, relhasoids, "
3958-
"relfrozenxid, 'p' AS relpersistence, "
3966+
"relfrozenxid, "
3967+
"0 AS toid, "
3968+
"0 AS tfrozenxid, "
3969+
"'p' AS relpersistence, "
39593970
"NULL AS reloftype, "
39603971
"d.refobjid AS owning_tab, "
39613972
"d.refobjsubid AS owning_col, "
@@ -3987,7 +3998,10 @@ getTables(int *numTables)
39873998
"(%s relowner) AS rolname, "
39883999
"relchecks, (reltriggers <> 0) AS relhastriggers, "
39894000
"relhasindex, relhasrules, relhasoids, "
3990-
"0 AS relfrozenxid, 'p' AS relpersistence, "
4001+
"0 AS relfrozenxid, "
4002+
"0 AS toid, "
4003+
"0 AS tfrozenxid, "
4004+
"'p' AS relpersistence, "
39914005
"NULL AS reloftype, "
39924006
"d.refobjid AS owning_tab, "
39934007
"d.refobjsubid AS owning_col, "
@@ -4019,7 +4033,10 @@ getTables(int *numTables)
40194033
"(%s relowner) AS rolname, "
40204034
"relchecks, (reltriggers <> 0) AS relhastriggers, "
40214035
"relhasindex, relhasrules, relhasoids, "
4022-
"0 AS relfrozenxid, 'p' AS relpersistence, "
4036+
"0 AS relfrozenxid, "
4037+
"0 AS toid, "
4038+
"0 AS tfrozenxid, "
4039+
"'p' AS relpersistence, "
40234040
"NULL AS reloftype, "
40244041
"d.refobjid AS owning_tab, "
40254042
"d.refobjsubid AS owning_col, "
@@ -4047,7 +4064,10 @@ getTables(int *numTables)
40474064
"(%s relowner) AS rolname, "
40484065
"relchecks, (reltriggers <> 0) AS relhastriggers, "
40494066
"relhasindex, relhasrules, relhasoids, "
4050-
"0 AS relfrozenxid, 'p' AS relpersistence, "
4067+
"0 AS relfrozenxid, "
4068+
"0 AS toid, "
4069+
"0 AS tfrozenxid, "
4070+
"'p' AS relpersistence, "
40514071
"NULL AS reloftype, "
40524072
"NULL::oid AS owning_tab, "
40534073
"NULL::int4 AS owning_col, "
@@ -4070,7 +4090,10 @@ getTables(int *numTables)
40704090
"relchecks, (reltriggers <> 0) AS relhastriggers, "
40714091
"relhasindex, relhasrules, "
40724092
"'t'::bool AS relhasoids, "
4073-
"0 AS relfrozenxid, 'p' AS relpersistence, "
4093+
"0 AS relfrozenxid, "
4094+
"0 AS toid, "
4095+
"0 AS tfrozenxid, "
4096+
"'p' AS relpersistence, "
40744097
"NULL AS reloftype, "
40754098
"NULL::oid AS owning_tab, "
40764099
"NULL::int4 AS owning_col, "
@@ -4103,7 +4126,10 @@ getTables(int *numTables)
41034126
"relchecks, (reltriggers <> 0) AS relhastriggers, "
41044127
"relhasindex, relhasrules, "
41054128
"'t'::bool AS relhasoids, "
4106-
"0 as relfrozenxid, 'p' AS relpersistence, "
4129+
"0 as relfrozenxid, "
4130+
"0 AS toid, "
4131+
"0 AS tfrozenxid, "
4132+
"'p' AS relpersistence, "
41074133
"NULL AS reloftype, "
41084134
"NULL::oid AS owning_tab, "
41094135
"NULL::int4 AS owning_col, "
@@ -4149,6 +4175,8 @@ getTables(int *numTables)
41494175
i_relhasrules = PQfnumber(res, "relhasrules");
41504176
i_relhasoids = PQfnumber(res, "relhasoids");
41514177
i_relfrozenxid = PQfnumber(res, "relfrozenxid");
4178+
i_toastoid = PQfnumber(res, "toid");
4179+
i_toastfrozenxid = PQfnumber(res, "tfrozenxid");
41524180
i_relpersistence = PQfnumber(res, "relpersistence");
41534181
i_owning_tab = PQfnumber(res, "owning_tab");
41544182
i_owning_col = PQfnumber(res, "owning_col");
@@ -4190,6 +4218,8 @@ getTables(int *numTables)
41904218
tblinfo[i].hastriggers = (strcmp(PQgetvalue(res, i, i_relhastriggers), "t") == 0);
41914219
tblinfo[i].hasoids = (strcmp(PQgetvalue(res, i, i_relhasoids), "t") == 0);
41924220
tblinfo[i].frozenxid = atooid(PQgetvalue(res, i, i_relfrozenxid));
4221+
tblinfo[i].toast_oid = atooid(PQgetvalue(res, i, i_toastoid));
4222+
tblinfo[i].toast_frozenxid = atooid(PQgetvalue(res, i, i_toastfrozenxid));
41934223
if (PQgetisnull(res, i, i_reloftype))
41944224
tblinfo[i].reloftype = NULL;
41954225
else
@@ -12221,13 +12251,23 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1222112251
}
1222212252
}
1222312253

12224-
appendPQExpBuffer(q, "\n-- For binary upgrade, set relfrozenxid\n");
12254+
appendPQExpBuffer(q, "\n-- For binary upgrade, set heap's relfrozenxid\n");
1222512255
appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
1222612256
"SET relfrozenxid = '%u'\n"
1222712257
"WHERE oid = ",
1222812258
tbinfo->frozenxid);
1222912259
appendStringLiteralAH(q, fmtId(tbinfo->dobj.name), fout);
1223012260
appendPQExpBuffer(q, "::pg_catalog.regclass;\n");
12261+
12262+
if (tbinfo->toast_oid)
12263+
{
12264+
/* We preserve the toast oids, so we can use it during restore */
12265+
appendPQExpBuffer(q, "\n-- For binary upgrade, set toast's relfrozenxid\n");
12266+
appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
12267+
"SET relfrozenxid = '%u'\n"
12268+
"WHERE oid = '%u';\n",
12269+
tbinfo->toast_frozenxid, tbinfo->toast_oid);
12270+
}
1223112271
}
1223212272

1223312273
/* 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
@@ -248,6 +248,8 @@ typedef struct _tableInfo
248248
bool hastriggers; /* does it have any triggers? */
249249
bool hasoids; /* does it have OIDs? */
250250
uint32 frozenxid; /* for restore frozen xid */
251+
Oid toast_oid; /* for restore toast frozen xid */
252+
uint32 toast_frozenxid;/* for restore toast frozen xid */
251253
int ncheck; /* # of CHECK expressions */
252254
char *reloftype; /* underlying type for typed table */
253255
/* these two are set only if table is a sequence owned by a column: */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy