Skip to content

Commit 6883c54

Browse files
committed
Add pg_dump --binary-upgrade flag to be used by binary upgrade
utilities. The new code allows transfer of dropped column information to the upgraded server.
1 parent 82aea0e commit 6883c54

File tree

5 files changed

+103
-11
lines changed

5 files changed

+103
-11
lines changed

doc/src/sgml/ref/pg_dump.sgml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/pg_dump.sgml,v 1.109 2009/02/10 00:55:21 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/pg_dump.sgml,v 1.110 2009/02/17 15:41:50 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -827,6 +827,11 @@ CREATE DATABASE foo WITH TEMPLATE template0;
827827
editing of the dump file might be required.
828828
</para>
829829

830+
<para>
831+
<application>pg_dump</application> also supports a
832+
<literal>--binary-upgrade</> option for upgrade utility usage.
833+
</para>
834+
830835
</refsect1>
831836

832837
<refsect1 id="pg-dump-examples">

doc/src/sgml/ref/pg_dumpall.sgml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/pg_dumpall.sgml,v 1.75 2009/02/07 14:31:30 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/pg_dumpall.sgml,v 1.76 2009/02/17 15:41:50 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -489,6 +489,11 @@ PostgreSQL documentation
489489
locations.
490490
</para>
491491

492+
<para>
493+
<application>pg_dump</application> also supports a
494+
<literal>--binary-upgrade</> option for upgrade utility usage.
495+
</para>
496+
492497
</refsect1>
493498

494499

src/bin/pg_dump/pg_dump.c

Lines changed: 82 additions & 6 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.521 2009/02/16 23:06:55 momjian Exp $
15+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.522 2009/02/17 15:41:50 momjian Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -99,6 +99,8 @@ static SimpleOidList table_exclude_oids = {NULL, NULL};
9999
/* default, if no "inclusion" switches appear, is to dump everything */
100100
static bool include_everything = true;
101101

102+
static int binary_upgrade = 0;
103+
102104
char g_opaque_type[10]; /* name for the opaque type */
103105

104106
/* placeholders for the delimiters for comments */
@@ -236,7 +238,8 @@ main(int argc, char **argv)
236238
static int outputNoTablespaces = 0;
237239
static int use_setsessauth = 0;
238240

239-
static struct option long_options[] = {
241+
struct option long_options[] = {
242+
{"binary-upgrade", no_argument, &binary_upgrade, 1}, /* not documented */
240243
{"data-only", no_argument, NULL, 'a'},
241244
{"blobs", no_argument, NULL, 'b'},
242245
{"clean", no_argument, NULL, 'c'},
@@ -4611,6 +4614,8 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
46114614
int i_attnotnull;
46124615
int i_atthasdef;
46134616
int i_attisdropped;
4617+
int i_attlen;
4618+
int i_attalign;
46144619
int i_attislocal;
46154620
PGresult *res;
46164621
int ntups;
@@ -4655,7 +4660,7 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
46554660
appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
46564661
"a.attstattarget, a.attstorage, t.typstorage, "
46574662
"a.attnotnull, a.atthasdef, a.attisdropped, "
4658-
"a.attislocal, "
4663+
"a.attlen, a.attalign, a.attislocal, "
46594664
"pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname "
46604665
"FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
46614666
"ON a.atttypid = t.oid "
@@ -4674,7 +4679,8 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
46744679
appendPQExpBuffer(q, "SELECT a.attnum, a.attname, "
46754680
"a.atttypmod, -1 AS attstattarget, a.attstorage, "
46764681
"t.typstorage, a.attnotnull, a.atthasdef, "
4677-
"false AS attisdropped, false AS attislocal, "
4682+
"false AS attisdropped, 0 AS attlen, "
4683+
"' ' AS attalign, false AS attislocal, "
46784684
"format_type(t.oid,a.atttypmod) AS atttypname "
46794685
"FROM pg_attribute a LEFT JOIN pg_type t "
46804686
"ON a.atttypid = t.oid "
@@ -4690,7 +4696,8 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
46904696
"-1 AS attstattarget, attstorage, "
46914697
"attstorage AS typstorage, "
46924698
"attnotnull, atthasdef, false AS attisdropped, "
4693-
"false AS attislocal, "
4699+
"0 AS attlen, ' ' AS attalign, "
4700+
"false AS attislocal, "
46944701
"(SELECT typname FROM pg_type WHERE oid = atttypid) AS atttypname "
46954702
"FROM pg_attribute a "
46964703
"WHERE attrelid = '%u'::oid "
@@ -4714,6 +4721,8 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
47144721
i_attnotnull = PQfnumber(res, "attnotnull");
47154722
i_atthasdef = PQfnumber(res, "atthasdef");
47164723
i_attisdropped = PQfnumber(res, "attisdropped");
4724+
i_attlen = PQfnumber(res, "attlen");
4725+
i_attalign = PQfnumber(res, "attalign");
47174726
i_attislocal = PQfnumber(res, "attislocal");
47184727

47194728
tbinfo->numatts = ntups;
@@ -4724,6 +4733,8 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
47244733
tbinfo->attstorage = (char *) malloc(ntups * sizeof(char));
47254734
tbinfo->typstorage = (char *) malloc(ntups * sizeof(char));
47264735
tbinfo->attisdropped = (bool *) malloc(ntups * sizeof(bool));
4736+
tbinfo->attlen = (int *) malloc(ntups * sizeof(int));
4737+
tbinfo->attalign = (char *) malloc(ntups * sizeof(char));
47274738
tbinfo->attislocal = (bool *) malloc(ntups * sizeof(bool));
47284739
tbinfo->notnull = (bool *) malloc(ntups * sizeof(bool));
47294740
tbinfo->attrdefs = (AttrDefInfo **) malloc(ntups * sizeof(AttrDefInfo *));
@@ -4747,6 +4758,8 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
47474758
tbinfo->attstorage[j] = *(PQgetvalue(res, j, i_attstorage));
47484759
tbinfo->typstorage[j] = *(PQgetvalue(res, j, i_typstorage));
47494760
tbinfo->attisdropped[j] = (PQgetvalue(res, j, i_attisdropped)[0] == 't');
4761+
tbinfo->attlen[j] = atoi(PQgetvalue(res, j, i_attlen));
4762+
tbinfo->attalign[j] = *(PQgetvalue(res, j, i_attalign));
47504763
tbinfo->attislocal[j] = (PQgetvalue(res, j, i_attislocal)[0] == 't');
47514764
tbinfo->notnull[j] = (PQgetvalue(res, j, i_attnotnull)[0] == 't');
47524765
tbinfo->attrdefs[j] = NULL; /* fix below */
@@ -4760,6 +4773,21 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
47604773

47614774
PQclear(res);
47624775

4776+
4777+
/*
4778+
* ALTER TABLE DROP COLUMN clears pg_attribute.atttypid, so we
4779+
* set the column data type to 'TEXT; we will later drop the
4780+
* column.
4781+
*/
4782+
if (binary_upgrade)
4783+
{
4784+
for (j = 0; j < ntups; j++)
4785+
{
4786+
if (tbinfo->attisdropped[j])
4787+
tbinfo->atttypnames[j] = strdup("TEXT");
4788+
}
4789+
}
4790+
47634791
/*
47644792
* Get info about column defaults
47654793
*/
@@ -9680,7 +9708,8 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
96809708
for (j = 0; j < tbinfo->numatts; j++)
96819709
{
96829710
/* Is this one of the table's own attrs, and not dropped ? */
9683-
if (!tbinfo->inhAttrs[j] && !tbinfo->attisdropped[j])
9711+
if (!tbinfo->inhAttrs[j] &&
9712+
(!tbinfo->attisdropped[j] || binary_upgrade))
96849713
{
96859714
/* Format properly if not first attr */
96869715
if (actual_atts > 0)
@@ -9786,6 +9815,53 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
97869815

97879816
appendPQExpBuffer(q, ";\n");
97889817

9818+
/*
9819+
* For binary-compatible heap files, we create dropped columns
9820+
* above and drop them here.
9821+
*/
9822+
if (binary_upgrade)
9823+
{
9824+
for (j = 0; j < tbinfo->numatts; j++)
9825+
{
9826+
if (tbinfo->attisdropped[j])
9827+
{
9828+
appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
9829+
fmtId(tbinfo->dobj.name));
9830+
appendPQExpBuffer(q, "DROP COLUMN %s;\n",
9831+
fmtId(tbinfo->attnames[j]));
9832+
9833+
/*
9834+
* ALTER TABLE DROP COLUMN clears pg_attribute.atttypid,
9835+
* so we have to set pg_attribute.attlen and
9836+
* pg_attribute.attalign values because that is what
9837+
* is used to skip over dropped columns in the heap tuples.
9838+
* We have atttypmod, but it seems impossible to know the
9839+
* correct data type that will yield pg_attribute values
9840+
* that match the old installation.
9841+
* See comment in backend/catalog/heap.c::RemoveAttributeById()
9842+
*/
9843+
appendPQExpBuffer(q, "\n-- For binary upgrade, recreate dropped column's length and alignment.\n");
9844+
appendPQExpBuffer(q, "UPDATE pg_attribute\n"
9845+
"SET attlen = %d, "
9846+
"attalign = '%c'\n"
9847+
"WHERE attname = '%s'\n"
9848+
" AND attrelid = \n"
9849+
" (\n"
9850+
" SELECT oid\n"
9851+
" FROM pg_class\n"
9852+
" WHERE relnamespace = "
9853+
"(SELECT oid FROM pg_namespace "
9854+
"WHERE nspname = CURRENT_SCHEMA)\n"
9855+
" AND relname = '%s'\n"
9856+
" );",
9857+
tbinfo->attlen[j],
9858+
tbinfo->attalign[j],
9859+
tbinfo->attnames[j],
9860+
tbinfo->dobj.name);
9861+
}
9862+
}
9863+
}
9864+
97899865
/* Loop dumping statistics and storage statements */
97909866
for (j = 0; j < tbinfo->numatts; j++)
97919867
{

src/bin/pg_dump/pg_dump.h

Lines changed: 3 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.150 2009/02/02 19:31:39 alvherre Exp $
9+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.h,v 1.151 2009/02/17 15:41:50 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -245,6 +245,8 @@ typedef struct _tableInfo
245245
char *attstorage; /* attribute storage scheme */
246246
char *typstorage; /* type storage scheme */
247247
bool *attisdropped; /* true if attr is dropped; don't dump it */
248+
int *attlen; /* attribute length, used by binary_upgrade */
249+
char *attalign; /* attribute align, used by binary_upgrade */
248250
bool *attislocal; /* true if attr has local definition */
249251

250252
/*

src/bin/pg_dump/pg_dumpall.c

Lines changed: 6 additions & 2 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.113 2009/01/22 20:16:08 tgl Exp $
9+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.114 2009/02/17 15:41:50 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -90,8 +90,10 @@ main(int argc, char *argv[])
9090
const char *std_strings;
9191
int c,
9292
ret;
93+
int binary_upgrade = 0;
9394

94-
static struct option long_options[] = {
95+
struct option long_options[] = {
96+
{"binary-upgrade", no_argument, &binary_upgrade, 1}, /* not documented */
9597
{"data-only", no_argument, NULL, 'a'},
9698
{"clean", no_argument, NULL, 'c'},
9799
{"inserts", no_argument, NULL, 'd'},
@@ -310,6 +312,8 @@ main(int argc, char *argv[])
310312
}
311313

312314
/* Add long options to the pg_dump argument list */
315+
if (binary_upgrade)
316+
appendPQExpBuffer(pgdumpopts, " --binary-upgrade");
313317
if (disable_dollar_quoting)
314318
appendPQExpBuffer(pgdumpopts, " --disable-dollar-quoting");
315319
if (disable_triggers)

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