Skip to content

Commit 5ab5d21

Browse files
committed
Repair pg_upgrade's failure to preserve relfrozenxid for matviews.
This oversight led to data corruption in matviews, manifesting as "could not access status of transaction" before our most recent releases, and "found xmin from before relfrozenxid" errors since then. The proximate cause of the problem seems to have been confusion between the task of preserving dropped-column status and the task of preserving frozenxid status. Those are required for distinct sets of relkinds, and the reasoning was entirely undocumented in the source code. In hopes of forestalling future errors of the same kind, try to improve the commentary in this area. In passing, also improve the remarkably unhelpful comments around pg_upgrade's set_frozenxids(). That's not actually buggy AFAICS, but good luck figuring out what it does from the old comments. Per report from Claudio Freire. It appears that bug #14852 from Alexey Ermakov is an earlier report of the same issue, and there may be other cases that we failed to identify at the time. Patch by me based on analysis by Andres Freund. The bug dates back to the introduction of matviews, so back-patch to all supported branches. Discussion: https://postgr.es/m/CAGTBQpbrY9CdRGGhyBZ9yqY4jWaGC85rUF4X+R7d-aim=mBNsw@mail.gmail.com Discussion: https://postgr.es/m/20171013115320.28049.86457@wrigleys.postgresql.org
1 parent 6753f6c commit 5ab5d21

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6250,7 +6250,8 @@ getTables(Archive *fout, int *numTables)
62506250
* alterations to parent tables.
62516251
*
62526252
* NOTE: it'd be kinda nice to lock other relations too, not only
6253-
* plain tables, but the backend doesn't presently allow that.
6253+
* plain or partitioned tables, but the backend doesn't presently
6254+
* allow that.
62546255
*
62556256
* We only need to lock the table for certain components; see
62566257
* pg_dump.h
@@ -15428,6 +15429,14 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1542815429
* column order. That also means we have to take care about setting
1542915430
* attislocal correctly, plus fix up any inherited CHECK constraints.
1543015431
* Analogously, we set up typed tables using ALTER TABLE / OF here.
15432+
*
15433+
* We process foreign and partitioned tables here, even though they
15434+
* lack heap storage, because they can participate in inheritance
15435+
* relationships and we want this stuff to be consistent across the
15436+
* inheritance tree. We can exclude indexes, toast tables, sequences
15437+
* and matviews, even though they have storage, because we don't
15438+
* support altering or dropping columns in them, nor can they be part
15439+
* of inheritance trees.
1543115440
*/
1543215441
if (dopt->binary_upgrade &&
1543315442
(tbinfo->relkind == RELKIND_RELATION ||
@@ -15539,7 +15548,19 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1553915548
fmtId(tbinfo->dobj.name),
1554015549
tbinfo->reloftype);
1554115550
}
15551+
}
1554215552

15553+
/*
15554+
* In binary_upgrade mode, arrange to restore the old relfrozenxid and
15555+
* relminmxid of all vacuumable relations. (While vacuum.c processes
15556+
* TOAST tables semi-independently, here we see them only as children
15557+
* of other relations; so this "if" lacks RELKIND_TOASTVALUE, and the
15558+
* child toast table is handled below.)
15559+
*/
15560+
if (dopt->binary_upgrade &&
15561+
(tbinfo->relkind == RELKIND_RELATION ||
15562+
tbinfo->relkind == RELKIND_MATVIEW))
15563+
{
1554315564
appendPQExpBufferStr(q, "\n-- For binary upgrade, set heap's relfrozenxid and relminmxid\n");
1554415565
appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
1554515566
"SET relfrozenxid = '%u', relminmxid = '%u'\n"
@@ -15550,7 +15571,10 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1555015571

1555115572
if (tbinfo->toast_oid)
1555215573
{
15553-
/* We preserve the toast oids, so we can use it during restore */
15574+
/*
15575+
* The toast table will have the same OID at restore, so we
15576+
* can safely target it by OID.
15577+
*/
1555415578
appendPQExpBufferStr(q, "\n-- For binary upgrade, set toast's relfrozenxid and relminmxid\n");
1555515579
appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
1555615580
"SET relfrozenxid = '%u', relminmxid = '%u'\n"
@@ -15564,7 +15588,8 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1556415588
* In binary_upgrade mode, restore matviews' populated status by
1556515589
* poking pg_class directly. This is pretty ugly, but we can't use
1556615590
* REFRESH MATERIALIZED VIEW since it's possible that some underlying
15567-
* matview is not populated even though this matview is.
15591+
* matview is not populated even though this matview is; in any case,
15592+
* we want to transfer the matview's heap storage, not run REFRESH.
1556815593
*/
1556915594
if (dopt->binary_upgrade && tbinfo->relkind == RELKIND_MATVIEW &&
1557015595
tbinfo->relispopulated)

src/bin/pg_upgrade/pg_upgrade.c

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,13 @@ static void
278278
prepare_new_databases(void)
279279
{
280280
/*
281-
* We set autovacuum_freeze_max_age to its maximum value so autovacuum
282-
* does not launch here and delete clog files, before the frozen xids are
283-
* set.
281+
* Before we restore anything, set frozenxids of initdb-created tables.
284282
*/
285-
286283
set_frozenxids(false);
287284

285+
/*
286+
* Now restore global objects (roles and tablespaces).
287+
*/
288288
prep_status("Restoring global objects in the new cluster");
289289

290290
/*
@@ -508,14 +508,25 @@ copy_xact_xlog_xid(void)
508508
/*
509509
* set_frozenxids()
510510
*
511-
* We have frozen all xids, so set datfrozenxid, relfrozenxid, and
512-
* relminmxid to be the old cluster's xid counter, which we just set
513-
* in the new cluster. User-table frozenxid and minmxid values will
514-
* be set by pg_dump --binary-upgrade, but objects not set by the pg_dump
515-
* must have proper frozen counters.
511+
* This is called on the new cluster before we restore anything, with
512+
* minmxid_only = false. Its purpose is to ensure that all initdb-created
513+
* vacuumable tables have relfrozenxid/relminmxid matching the old cluster's
514+
* xid/mxid counters. We also initialize the datfrozenxid/datminmxid of the
515+
* built-in databases to match.
516+
*
517+
* As we create user tables later, their relfrozenxid/relminmxid fields will
518+
* be restored properly by the binary-upgrade restore script. Likewise for
519+
* user-database datfrozenxid/datminmxid. However, if we're upgrading from a
520+
* pre-9.3 database, which does not store per-table or per-DB minmxid, then
521+
* the relminmxid/datminmxid values filled in by the restore script will just
522+
* be zeroes.
523+
*
524+
* Hence, with a pre-9.3 source database, a second call occurs after
525+
* everything is restored, with minmxid_only = true. This pass will
526+
* initialize all tables and databases, both those made by initdb and user
527+
* objects, with the desired minmxid value. frozenxid values are left alone.
516528
*/
517-
static
518-
void
529+
static void
519530
set_frozenxids(bool minmxid_only)
520531
{
521532
int dbnum;

0 commit comments

Comments
 (0)