Skip to content

Commit 1d88d4e

Browse files
committed
Have pg_dump/pg_dumpall --binary-upgrade restore frozenids for relations
and databases.
1 parent b9a3669 commit 1d88d4e

File tree

3 files changed

+64
-18
lines changed

3 files changed

+64
-18
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* by PostgreSQL
1313
*
1414
* IDENTIFICATION
15-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.523 2009/02/17 22:32:54 momjian Exp $
15+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.524 2009/02/18 12:07:07 momjian Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -1585,6 +1585,7 @@ dumpDatabase(Archive *AH)
15851585
i_encoding,
15861586
i_collate,
15871587
i_ctype,
1588+
i_frozenxid,
15881589
i_tablespace;
15891590
CatalogId dbCatId;
15901591
DumpId dbDumpId;
@@ -1594,6 +1595,7 @@ dumpDatabase(Archive *AH)
15941595
*collate,
15951596
*ctype,
15961597
*tablespace;
1598+
uint32 frozenxid;
15971599

15981600
datname = PQdb(g_conn);
15991601

@@ -1609,7 +1611,7 @@ dumpDatabase(Archive *AH)
16091611
appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
16101612
"(%s datdba) AS dba, "
16111613
"pg_encoding_to_char(encoding) AS encoding, "
1612-
"datcollate, datctype, "
1614+
"datcollate, datctype, datfrozenxid, "
16131615
"(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
16141616
"shobj_description(oid, 'pg_database') AS description "
16151617

@@ -1623,7 +1625,7 @@ dumpDatabase(Archive *AH)
16231625
appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
16241626
"(%s datdba) AS dba, "
16251627
"pg_encoding_to_char(encoding) AS encoding, "
1626-
"NULL AS datcollate, NULL AS datctype, "
1628+
"NULL AS datcollate, NULL AS datctype, datfrozenxid, "
16271629
"(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
16281630
"shobj_description(oid, 'pg_database') AS description "
16291631

@@ -1637,7 +1639,7 @@ dumpDatabase(Archive *AH)
16371639
appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
16381640
"(%s datdba) AS dba, "
16391641
"pg_encoding_to_char(encoding) AS encoding, "
1640-
"NULL AS datcollate, NULL AS datctype, "
1642+
"NULL AS datcollate, NULL AS datctype, datfrozenxid, "
16411643
"(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace "
16421644
"FROM pg_database "
16431645
"WHERE datname = ",
@@ -1650,6 +1652,7 @@ dumpDatabase(Archive *AH)
16501652
"(%s datdba) AS dba, "
16511653
"pg_encoding_to_char(encoding) AS encoding, "
16521654
"NULL AS datcollate, NULL AS datctype, "
1655+
"0 AS datfrozenxid, "
16531656
"NULL AS tablespace "
16541657
"FROM pg_database "
16551658
"WHERE datname = ",
@@ -1664,6 +1667,7 @@ dumpDatabase(Archive *AH)
16641667
"(%s datdba) AS dba, "
16651668
"pg_encoding_to_char(encoding) AS encoding, "
16661669
"NULL AS datcollate, NULL AS datctype, "
1670+
"0 AS datfrozenxid, "
16671671
"NULL AS tablespace "
16681672
"FROM pg_database "
16691673
"WHERE datname = ",
@@ -1696,6 +1700,7 @@ dumpDatabase(Archive *AH)
16961700
i_encoding = PQfnumber(res, "encoding");
16971701
i_collate = PQfnumber(res, "datcollate");
16981702
i_ctype = PQfnumber(res, "datctype");
1703+
i_frozenxid = PQfnumber(res, "datfrozenxid");
16991704
i_tablespace = PQfnumber(res, "tablespace");
17001705

17011706
dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid));
@@ -1704,6 +1709,7 @@ dumpDatabase(Archive *AH)
17041709
encoding = PQgetvalue(res, 0, i_encoding);
17051710
collate = PQgetvalue(res, 0, i_collate);
17061711
ctype = PQgetvalue(res, 0, i_ctype);
1712+
frozenxid = atooid(PQgetvalue(res, 0, i_frozenxid));
17071713
tablespace = PQgetvalue(res, 0, i_tablespace);
17081714

17091715
appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
@@ -1728,6 +1734,15 @@ dumpDatabase(Archive *AH)
17281734
fmtId(tablespace));
17291735
appendPQExpBuffer(creaQry, ";\n");
17301736

1737+
if (binary_upgrade)
1738+
{
1739+
appendPQExpBuffer(creaQry, "\n-- For binary upgrade, set datfrozenxid.\n");
1740+
appendPQExpBuffer(creaQry, "UPDATE pg_database\n"
1741+
"SET datfrozenxid = '%u'\n"
1742+
"WHERE datname = '%s';\n",
1743+
frozenxid, datname);
1744+
}
1745+
17311746
appendPQExpBuffer(delQry, "DROP DATABASE %s;\n",
17321747
fmtId(datname));
17331748

@@ -3114,6 +3129,7 @@ getTables(int *numTables)
31143129
int i_relhasindex;
31153130
int i_relhasrules;
31163131
int i_relhasoids;
3132+
int i_relfrozenxid;
31173133
int i_owning_tab;
31183134
int i_owning_col;
31193135
int i_reltablespace;
@@ -3155,6 +3171,7 @@ getTables(int *numTables)
31553171
"(%s c.relowner) AS rolname, "
31563172
"c.relchecks, c.relhastriggers, "
31573173
"c.relhasindex, c.relhasrules, c.relhasoids, "
3174+
"c.relfrozenxid, "
31583175
"d.refobjid AS owning_tab, "
31593176
"d.refobjsubid AS owning_col, "
31603177
"(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
@@ -3186,6 +3203,7 @@ getTables(int *numTables)
31863203
"(%s relowner) AS rolname, "
31873204
"relchecks, (reltriggers <> 0) AS relhastriggers, "
31883205
"relhasindex, relhasrules, relhasoids, "
3206+
"relfrozenxid, "
31893207
"d.refobjid AS owning_tab, "
31903208
"d.refobjsubid AS owning_col, "
31913209
"(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
@@ -3216,6 +3234,7 @@ getTables(int *numTables)
32163234
"(%s relowner) AS rolname, "
32173235
"relchecks, (reltriggers <> 0) AS relhastriggers, "
32183236
"relhasindex, relhasrules, relhasoids, "
3237+
"0 AS relfrozenxid, "
32193238
"d.refobjid AS owning_tab, "
32203239
"d.refobjsubid AS owning_col, "
32213240
"(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
@@ -3246,6 +3265,7 @@ getTables(int *numTables)
32463265
"(%s relowner) AS rolname, "
32473266
"relchecks, (reltriggers <> 0) AS relhastriggers, "
32483267
"relhasindex, relhasrules, relhasoids, "
3268+
"0 AS relfrozenxid, "
32493269
"d.refobjid AS owning_tab, "
32503270
"d.refobjsubid AS owning_col, "
32513271
"NULL AS reltablespace, "
@@ -3272,6 +3292,7 @@ getTables(int *numTables)
32723292
"(%s relowner) AS rolname, "
32733293
"relchecks, (reltriggers <> 0) AS relhastriggers, "
32743294
"relhasindex, relhasrules, relhasoids, "
3295+
"0 AS relfrozenxid, "
32753296
"NULL::oid AS owning_tab, "
32763297
"NULL::int4 AS owning_col, "
32773298
"NULL AS reltablespace, "
@@ -3293,6 +3314,7 @@ getTables(int *numTables)
32933314
"relchecks, (reltriggers <> 0) AS relhastriggers, "
32943315
"relhasindex, relhasrules, "
32953316
"'t'::bool AS relhasoids, "
3317+
"0 AS relfrozenxid, "
32963318
"NULL::oid AS owning_tab, "
32973319
"NULL::int4 AS owning_col, "
32983320
"NULL AS reltablespace, "
@@ -3324,6 +3346,7 @@ getTables(int *numTables)
33243346
"relchecks, (reltriggers <> 0) AS relhastriggers, "
33253347
"relhasindex, relhasrules, "
33263348
"'t'::bool AS relhasoids, "
3349+
"0 as relfrozenxid, "
33273350
"NULL::oid AS owning_tab, "
33283351
"NULL::int4 AS owning_col, "
33293352
"NULL AS reltablespace, "
@@ -3367,6 +3390,7 @@ getTables(int *numTables)
33673390
i_relhasindex = PQfnumber(res, "relhasindex");
33683391
i_relhasrules = PQfnumber(res, "relhasrules");
33693392
i_relhasoids = PQfnumber(res, "relhasoids");
3393+
i_relfrozenxid = PQfnumber(res, "relfrozenxid");
33703394
i_owning_tab = PQfnumber(res, "owning_tab");
33713395
i_owning_col = PQfnumber(res, "owning_col");
33723396
i_reltablespace = PQfnumber(res, "reltablespace");
@@ -3404,6 +3428,7 @@ getTables(int *numTables)
34043428
tblinfo[i].hasrules = (strcmp(PQgetvalue(res, i, i_relhasrules), "t") == 0);
34053429
tblinfo[i].hastriggers = (strcmp(PQgetvalue(res, i, i_relhastriggers), "t") == 0);
34063430
tblinfo[i].hasoids = (strcmp(PQgetvalue(res, i, i_relhasoids), "t") == 0);
3431+
tblinfo[i].frozenxid = atooid(PQgetvalue(res, i, i_relfrozenxid));
34073432
tblinfo[i].ncheck = atoi(PQgetvalue(res, i, i_relchecks));
34083433
if (PQgetisnull(res, i, i_owning_tab))
34093434
{
@@ -9860,6 +9885,15 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
98609885
tbinfo->dobj.name);
98619886
}
98629887
}
9888+
appendPQExpBuffer(q, "\n-- For binary upgrade, set relfrozenxid.\n");
9889+
appendPQExpBuffer(q, "UPDATE pg_class\n"
9890+
"SET relfrozenxid = '%u'\n"
9891+
"WHERE relname = '%s'\n"
9892+
" AND relnamespace = "
9893+
"(SELECT oid FROM pg_namespace "
9894+
"WHERE nspname = CURRENT_SCHEMA);\n",
9895+
tbinfo->frozenxid,
9896+
tbinfo->dobj.name);
98639897
}
98649898

98659899
/* Loop dumping statistics and storage statements */

src/bin/pg_dump/pg_dump.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.h,v 1.151 2009/02/17 15:41:50 momjian Exp $
9+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.h,v 1.152 2009/02/18 12:07:07 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -226,6 +226,7 @@ typedef struct _tableInfo
226226
bool hasrules; /* does it have any rules? */
227227
bool hastriggers; /* does it have any triggers? */
228228
bool hasoids; /* does it have OIDs? */
229+
uint32 frozenxid; /* for restore frozen xid */
229230
int ncheck; /* # of CHECK expressions */
230231
/* these two are set only if table is a sequence owned by a column: */
231232
Oid owning_tab; /* OID of table owning sequence */

src/bin/pg_dump/pg_dumpall.c

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1994, Regents of the University of California
77
*
88
*
9-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.114 2009/02/17 15:41:50 momjian Exp $
9+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.115 2009/02/18 12:07:08 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -27,7 +27,7 @@ int optreset;
2727
#endif
2828

2929
#include "dumputils.h"
30-
30+
#include "pg_backup.h"
3131

3232
/* version string we expect back from pg_dump */
3333
#define PGDUMP_VERSIONSTR "pg_dump (PostgreSQL) " PG_VERSION "\n"
@@ -71,6 +71,8 @@ static int server_version;
7171
static FILE *OPF;
7272
static char *filename = NULL;
7373

74+
static int binary_upgrade = 0;
75+
7476
int
7577
main(int argc, char *argv[])
7678
{
@@ -90,7 +92,6 @@ main(int argc, char *argv[])
9092
const char *std_strings;
9193
int c,
9294
ret;
93-
int binary_upgrade = 0;
9495

9596
struct option long_options[] = {
9697
{"binary-upgrade", no_argument, &binary_upgrade, 1}, /* not documented */
@@ -937,7 +938,7 @@ dumpCreateDB(PGconn *conn)
937938
"SELECT datname, "
938939
"coalesce(rolname, (select rolname from pg_authid where oid=(select datdba from pg_database where datname='template0'))), "
939940
"pg_encoding_to_char(d.encoding), "
940-
"datcollate, datctype, "
941+
"datcollate, datctype, datfrozenxid, "
941942
"datistemplate, datacl, datconnlimit, "
942943
"(SELECT spcname FROM pg_tablespace t WHERE t.oid = d.dattablespace) AS dattablespace "
943944
"FROM pg_database d LEFT JOIN pg_authid u ON (datdba = u.oid) "
@@ -947,7 +948,7 @@ dumpCreateDB(PGconn *conn)
947948
"SELECT datname, "
948949
"coalesce(rolname, (select rolname from pg_authid where oid=(select datdba from pg_database where datname='template0'))), "
949950
"pg_encoding_to_char(d.encoding), "
950-
"null::text AS datcollate, null::text AS datctype, "
951+
"null::text AS datcollate, null::text AS datctype, datfrozenxid, "
951952
"datistemplate, datacl, datconnlimit, "
952953
"(SELECT spcname FROM pg_tablespace t WHERE t.oid = d.dattablespace) AS dattablespace "
953954
"FROM pg_database d LEFT JOIN pg_authid u ON (datdba = u.oid) "
@@ -957,7 +958,7 @@ dumpCreateDB(PGconn *conn)
957958
"SELECT datname, "
958959
"coalesce(usename, (select usename from pg_shadow where usesysid=(select datdba from pg_database where datname='template0'))), "
959960
"pg_encoding_to_char(d.encoding), "
960-
"null::text AS datcollate, null::text AS datctype, "
961+
"null::text AS datcollate, null::text AS datctype, datfrozenxid, "
961962
"datistemplate, datacl, -1 as datconnlimit, "
962963
"(SELECT spcname FROM pg_tablespace t WHERE t.oid = d.dattablespace) AS dattablespace "
963964
"FROM pg_database d LEFT JOIN pg_shadow u ON (datdba = usesysid) "
@@ -967,7 +968,7 @@ dumpCreateDB(PGconn *conn)
967968
"SELECT datname, "
968969
"coalesce(usename, (select usename from pg_shadow where usesysid=(select datdba from pg_database where datname='template0'))), "
969970
"pg_encoding_to_char(d.encoding), "
970-
"null::text AS datcollate, null::text AS datctype, "
971+
"null::text AS datcollate, null::text AS datctype, datfrozenxid, "
971972
"datistemplate, datacl, -1 as datconnlimit, "
972973
"'pg_default' AS dattablespace "
973974
"FROM pg_database d LEFT JOIN pg_shadow u ON (datdba = usesysid) "
@@ -979,7 +980,7 @@ dumpCreateDB(PGconn *conn)
979980
"(select usename from pg_shadow where usesysid=datdba), "
980981
"(select usename from pg_shadow where usesysid=(select datdba from pg_database where datname='template0'))), "
981982
"pg_encoding_to_char(d.encoding), "
982-
"null::text AS datcollate, null::text AS datctype, "
983+
"null::text AS datcollate, null::text AS datctype, 0 AS datfrozenxid"
983984
"datistemplate, '' as datacl, -1 as datconnlimit, "
984985
"'pg_default' AS dattablespace "
985986
"FROM pg_database d "
@@ -994,7 +995,7 @@ dumpCreateDB(PGconn *conn)
994995
"SELECT datname, "
995996
"(select usename from pg_shadow where usesysid=datdba), "
996997
"pg_encoding_to_char(d.encoding), "
997-
"null::text AS datcollate, null::text AS datctype, "
998+
"null::text AS datcollate, null::text AS datctype, 0 AS datfrozenxid"
998999
"'f' as datistemplate, "
9991000
"'' as datacl, -1 as datconnlimit, "
10001001
"'pg_default' AS dattablespace "
@@ -1009,10 +1010,11 @@ dumpCreateDB(PGconn *conn)
10091010
char *dbencoding = PQgetvalue(res, i, 2);
10101011
char *dbcollate = PQgetvalue(res, i, 3);
10111012
char *dbctype = PQgetvalue(res, i, 4);
1012-
char *dbistemplate = PQgetvalue(res, i, 5);
1013-
char *dbacl = PQgetvalue(res, i, 6);
1014-
char *dbconnlimit = PQgetvalue(res, i, 7);
1015-
char *dbtablespace = PQgetvalue(res, i, 8);
1013+
uint32 dbfrozenxid = atooid(PQgetvalue(res, i, 5));
1014+
char *dbistemplate = PQgetvalue(res, i, 6);
1015+
char *dbacl = PQgetvalue(res, i, 7);
1016+
char *dbconnlimit = PQgetvalue(res, i, 8);
1017+
char *dbtablespace = PQgetvalue(res, i, 9);
10161018
char *fdbname;
10171019

10181020
fdbname = strdup(fmtId(dbname));
@@ -1076,6 +1078,15 @@ dumpCreateDB(PGconn *conn)
10761078
appendStringLiteralConn(buf, dbname, conn);
10771079
appendPQExpBuffer(buf, ";\n");
10781080
}
1081+
1082+
if (binary_upgrade)
1083+
{
1084+
appendPQExpBuffer(buf, "\n-- For binary upgrade, set datfrozenxid.\n");
1085+
appendPQExpBuffer(buf, "UPDATE pg_database\n"
1086+
"SET datfrozenxid = '%u'\n"
1087+
"WHERE datname = '%s';\n",
1088+
dbfrozenxid, fdbname);
1089+
}
10791090
}
10801091

10811092
if (!skip_acls &&

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