summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane2018-02-21 23:40:24 +0000
committerTom Lane2018-02-21 23:40:24 +0000
commit2d12c559335e29536f558f07fe053574c142883d (patch)
tree63ab64c89e3acffed66ae506cf2a098bde2ee53c /src
parente11b6488e5a70f870bc7069d7f08a9570ccd3fc2 (diff)
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
Diffstat (limited to 'src')
-rw-r--r--src/bin/pg_dump/pg_dump.c27
1 files changed, 25 insertions, 2 deletions
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 17e6f118c53..6f8ef709215 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -13589,6 +13589,13 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
* order. That also means we have to take care about setting
* attislocal correctly, plus fix up any inherited CHECK constraints.
* Analogously, we set up typed tables using ALTER TABLE / OF here.
+ *
+ * We process foreign tables here, even though they lack heap storage,
+ * because they can participate in inheritance relationships and we
+ * want this stuff to be consistent across the inheritance tree. We
+ * exclude indexes, toast tables, sequences and matviews, even though
+ * they have storage, because we don't support altering or dropping
+ * columns in them, nor can they be part of inheritance trees.
*/
if (binary_upgrade && (tbinfo->relkind == RELKIND_RELATION ||
tbinfo->relkind == RELKIND_FOREIGN_TABLE))
@@ -13679,7 +13686,19 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
fmtId(tbinfo->dobj.name),
tbinfo->reloftype);
}
+ }
+ /*
+ * In binary_upgrade mode, arrange to restore the old relfrozenxid and
+ * relminmxid of all vacuumable relations. (While vacuum.c processes
+ * TOAST tables semi-independently, here we see them only as children
+ * of other relations; so this "if" lacks RELKIND_TOASTVALUE, and the
+ * child toast table is handled below.)
+ */
+ if (binary_upgrade &&
+ (tbinfo->relkind == RELKIND_RELATION ||
+ tbinfo->relkind == RELKIND_MATVIEW))
+ {
appendPQExpBufferStr(q, "\n-- For binary upgrade, set heap's relfrozenxid and relminmxid\n");
appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
"SET relfrozenxid = '%u', relminmxid = '%u'\n"
@@ -13690,7 +13709,10 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
if (tbinfo->toast_oid)
{
- /* We preserve the toast oids, so we can use it during restore */
+ /*
+ * The toast table will have the same OID at restore, so we
+ * can safely target it by OID.
+ */
appendPQExpBufferStr(q, "\n-- For binary upgrade, set toast's relfrozenxid and relminmxid\n");
appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
"SET relfrozenxid = '%u', relminmxid = '%u'\n"
@@ -13704,7 +13726,8 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
* In binary_upgrade mode, restore matviews' populated status by
* poking pg_class directly. This is pretty ugly, but we can't use
* REFRESH MATERIALIZED VIEW since it's possible that some underlying
- * matview is not populated even though this matview is.
+ * matview is not populated even though this matview is; in any case,
+ * we want to transfer the matview's heap storage, not run REFRESH.
*/
if (binary_upgrade && tbinfo->relkind == RELKIND_MATVIEW &&
tbinfo->relispopulated)