Have pg_upgrade properly preserve relfrozenxid in toast tables.
authorBruce Momjian <[email protected]>
Fri, 8 Apr 2011 16:07:48 +0000 (12:07 -0400)
committerBruce Momjian <[email protected]>
Fri, 8 Apr 2011 16:08:06 +0000 (12:08 -0400)
This fixes a pg_upgrade bug that could lead to query errors when clog
files are improperly removed.

src/bin/pg_dump/pg_dump.c
src/bin/pg_dump/pg_dump.h

index 38428956fdb2aafe40babc0fd47b7bd9f2b2094d..c5057f7e47627ab58b66966117a1d19f5ebb9b6d 100644 (file)
@@ -3185,6 +3185,8 @@ getTables(int *numTables)
    int         i_relhasrules;
    int         i_relhasoids;
    int         i_relfrozenxid;
+   int         i_toastoid;
+   int         i_toastfrozenxid;
    int         i_owning_tab;
    int         i_owning_col;
    int         i_reltablespace;
@@ -3226,7 +3228,8 @@ getTables(int *numTables)
                          "(%s c.relowner) AS rolname, "
                          "c.relchecks, c.relhastriggers, "
                          "c.relhasindex, c.relhasrules, c.relhasoids, "
-                         "c.relfrozenxid, "
+                         "c.relfrozenxid, tc.oid AS toid, "
+                         "tc.relfrozenxid AS tfrozenxid, "
                          "d.refobjid AS owning_tab, "
                          "d.refobjsubid AS owning_col, "
                          "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
@@ -3259,6 +3262,8 @@ getTables(int *numTables)
                          "relchecks, (reltriggers <> 0) AS relhastriggers, "
                          "relhasindex, relhasrules, relhasoids, "
                          "relfrozenxid, "
+                         "0 AS toid, "
+                         "0 AS tfrozenxid, "
                          "d.refobjid AS owning_tab, "
                          "d.refobjsubid AS owning_col, "
                          "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
@@ -3290,6 +3295,8 @@ getTables(int *numTables)
                          "relchecks, (reltriggers <> 0) AS relhastriggers, "
                          "relhasindex, relhasrules, relhasoids, "
                          "0 AS relfrozenxid, "
+                         "0 AS toid, "
+                         "0 AS tfrozenxid, "
                          "d.refobjid AS owning_tab, "
                          "d.refobjsubid AS owning_col, "
                          "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
@@ -3321,6 +3328,8 @@ getTables(int *numTables)
                          "relchecks, (reltriggers <> 0) AS relhastriggers, "
                          "relhasindex, relhasrules, relhasoids, "
                          "0 AS relfrozenxid, "
+                         "0 AS toid, "
+                         "0 AS tfrozenxid, "
                          "d.refobjid AS owning_tab, "
                          "d.refobjsubid AS owning_col, "
                          "NULL AS reltablespace, "
@@ -3348,6 +3357,8 @@ getTables(int *numTables)
                          "relchecks, (reltriggers <> 0) AS relhastriggers, "
                          "relhasindex, relhasrules, relhasoids, "
                          "0 AS relfrozenxid, "
+                         "0 AS toid, "
+                         "0 AS tfrozenxid, "
                          "NULL::oid AS owning_tab, "
                          "NULL::int4 AS owning_col, "
                          "NULL AS reltablespace, "
@@ -3370,6 +3381,8 @@ getTables(int *numTables)
                          "relhasindex, relhasrules, "
                          "'t'::bool AS relhasoids, "
                          "0 AS relfrozenxid, "
+                         "0 AS toid, "
+                         "0 AS tfrozenxid, "
                          "NULL::oid AS owning_tab, "
                          "NULL::int4 AS owning_col, "
                          "NULL AS reltablespace, "
@@ -3446,6 +3459,8 @@ getTables(int *numTables)
    i_relhasrules = PQfnumber(res, "relhasrules");
    i_relhasoids = PQfnumber(res, "relhasoids");
    i_relfrozenxid = PQfnumber(res, "relfrozenxid");
+   i_toastoid = PQfnumber(res, "toid");
+   i_toastfrozenxid = PQfnumber(res, "tfrozenxid");
    i_owning_tab = PQfnumber(res, "owning_tab");
    i_owning_col = PQfnumber(res, "owning_col");
    i_reltablespace = PQfnumber(res, "reltablespace");
@@ -3484,6 +3499,8 @@ getTables(int *numTables)
        tblinfo[i].hastriggers = (strcmp(PQgetvalue(res, i, i_relhastriggers), "t") == 0);
        tblinfo[i].hasoids = (strcmp(PQgetvalue(res, i, i_relhasoids), "t") == 0);
        tblinfo[i].frozenxid = atooid(PQgetvalue(res, i, i_relfrozenxid));
+       tblinfo[i].toast_oid = atooid(PQgetvalue(res, i, i_toastoid));
+       tblinfo[i].toast_frozenxid = atooid(PQgetvalue(res, i, i_toastfrozenxid));
        tblinfo[i].ncheck = atoi(PQgetvalue(res, i, i_relchecks));
        if (PQgetisnull(res, i, i_owning_tab))
        {
@@ -10044,13 +10061,23 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
                }
            }
 
-           appendPQExpBuffer(q, "\n-- For binary upgrade, set relfrozenxid.\n");
+           appendPQExpBuffer(q, "\n-- For binary upgrade, set heap's relfrozenxid\n");
            appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
                              "SET relfrozenxid = '%u'\n"
                              "WHERE oid = ",
                              tbinfo->frozenxid);
            appendStringLiteralAH(q, fmtId(tbinfo->dobj.name), fout);
            appendPQExpBuffer(q, "::pg_catalog.regclass;\n");
+
+           if (tbinfo->toast_oid)
+           {
+               /* We preserve the toast oids, so we can use it during restore */
+               appendPQExpBuffer(q, "\n-- For binary upgrade, set toast's relfrozenxid\n");
+               appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
+                                 "SET relfrozenxid = '%u'\n"
+                                 "WHERE oid = '%u';\n",
+                                 tbinfo->toast_frozenxid, tbinfo->toast_oid);
+           }
        }
 
        /* Loop dumping statistics and storage statements */
index 3339bf75623b943ab6e6150faffc5df9275d2cb7..390b20cf1aa6bcea0fe21f5471a30ed63965e53f 100644 (file)
@@ -227,6 +227,8 @@ typedef struct _tableInfo
    bool        hastriggers;    /* does it have any triggers? */
    bool        hasoids;        /* does it have OIDs? */
    uint32      frozenxid;      /* for restore frozen xid */
+   Oid         toast_oid;      /* for restore toast frozen xid */
+   uint32      toast_frozenxid;/* for restore toast frozen xid */
    int         ncheck;         /* # of CHECK expressions */
    /* these two are set only if table is a sequence owned by a column: */
    Oid         owning_tab;     /* OID of table owning sequence */