Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit53fa850

Browse files
committed
Fix up pg_dump's --binary-upgrade option so that it behaves properly with
inherited columns and check constraints. Per my recent trouble report.
1 parent7cc514a commit53fa850

File tree

1 file changed

+112
-69
lines changed

1 file changed

+112
-69
lines changed

‎src/bin/pg_dump/pg_dump.c

Lines changed: 112 additions & 69 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.539 2009/06/11 14:49:07 momjian Exp $
15+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.540 2009/07/02 21:34:32 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -233,7 +233,7 @@ main(int argc, char **argv)
233233
staticintoutputNoTablespaces=0;
234234
staticintuse_setsessauth=0;
235235

236-
structoptionlong_options[]= {
236+
staticstructoptionlong_options[]= {
237237
{"data-only",no_argument,NULL,'a'},
238238
{"blobs",no_argument,NULL,'b'},
239239
{"clean",no_argument,NULL,'c'},
@@ -1733,7 +1733,7 @@ dumpDatabase(Archive *AH)
17331733
if (binary_upgrade)
17341734
{
17351735
appendPQExpBuffer(creaQry,"\n-- For binary upgrade, set datfrozenxid.\n");
1736-
appendPQExpBuffer(creaQry,"UPDATE pg_database\n"
1736+
appendPQExpBuffer(creaQry,"UPDATEpg_catalog.pg_database\n"
17371737
"SET datfrozenxid = '%u'\n"
17381738
"WHEREdatname = ",
17391739
frozenxid);
@@ -4712,8 +4712,8 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
47124712
appendPQExpBuffer(q,"SELECT a.attnum, a.attname, "
47134713
"a.atttypmod, -1 AS attstattarget, a.attstorage, "
47144714
"t.typstorage, a.attnotnull, a.atthasdef, "
4715-
"false AS attisdropped,0 ASattlen, "
4716-
"' ' ASattalign, false AS attislocal, "
4715+
"false AS attisdropped,a.attlen, "
4716+
"a.attalign, false AS attislocal, "
47174717
"format_type(t.oid,a.atttypmod) AS atttypname "
47184718
"FROM pg_attribute a LEFT JOIN pg_type t "
47194719
"ON a.atttypid = t.oid "
@@ -4729,7 +4729,7 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
47294729
"-1 AS attstattarget, attstorage, "
47304730
"attstorage AS typstorage, "
47314731
"attnotnull, atthasdef, false AS attisdropped, "
4732-
"0 ASattlen, ' ' AS attalign, "
4732+
"attlen, attalign, "
47334733
"false AS attislocal, "
47344734
"(SELECT typname FROM pg_type WHERE oid = atttypid) AS atttypname "
47354735
"FROM pg_attribute a "
@@ -4806,20 +4806,6 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
48064806

48074807
PQclear(res);
48084808

4809-
4810-
/*
4811-
* ALTER TABLE DROP COLUMN clears pg_attribute.atttypid, so we set the
4812-
* column data type to 'TEXT; we will later drop the column.
4813-
*/
4814-
if (binary_upgrade)
4815-
{
4816-
for (j=0;j<ntups;j++)
4817-
{
4818-
if (tbinfo->attisdropped[j])
4819-
tbinfo->atttypnames[j]=strdup("TEXT");
4820-
}
4821-
}
4822-
48234809
/*
48244810
* Get info about column defaults
48254811
*/
@@ -9783,19 +9769,35 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
97839769
actual_atts=0;
97849770
for (j=0;j<tbinfo->numatts;j++)
97859771
{
9786-
/* Is this one of the table's own attrs, and not dropped ? */
9787-
if (!tbinfo->inhAttrs[j]&&
9788-
(!tbinfo->attisdropped[j]||binary_upgrade))
9772+
/*
9773+
* Normally, dump if it's one of the table's own attrs, and not
9774+
* dropped. But for binary upgrade, dump all the columns.
9775+
*/
9776+
if ((!tbinfo->inhAttrs[j]&& !tbinfo->attisdropped[j])||
9777+
binary_upgrade)
97899778
{
97909779
/* Format properly if not first attr */
97919780
if (actual_atts>0)
97929781
appendPQExpBuffer(q,",");
97939782
appendPQExpBuffer(q,"\n ");
9783+
actual_atts++;
97949784

97959785
/* Attribute name */
97969786
appendPQExpBuffer(q,"%s ",
97979787
fmtId(tbinfo->attnames[j]));
97989788

9789+
if (tbinfo->attisdropped[j])
9790+
{
9791+
/*
9792+
* ALTER TABLE DROP COLUMN clears pg_attribute.atttypid,
9793+
* so we will not have gotten a valid type name; insert
9794+
* INTEGER as a stopgap. We'll clean things up later.
9795+
*/
9796+
appendPQExpBuffer(q,"INTEGER /* dummy */");
9797+
/* Skip all the rest, too */
9798+
continue;
9799+
}
9800+
97999801
/* Attribute type */
98009802
if (g_fout->remoteVersion >=70100)
98019803
{
@@ -9811,22 +9813,23 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
98119813
}
98129814

98139815
/*
9814-
* Default value --- suppress if inherited or to be printed
9815-
* separately.
9816+
* Default value --- suppress if inherited (except in
9817+
* binary-upgrade case, where we're not doing normal
9818+
* inheritance) or if it's to be printed separately.
98169819
*/
98179820
if (tbinfo->attrdefs[j]!=NULL&&
9818-
!tbinfo->inhAttrDef[j]&&
9821+
(!tbinfo->inhAttrDef[j]||binary_upgrade)&&
98199822
!tbinfo->attrdefs[j]->separate)
98209823
appendPQExpBuffer(q," DEFAULT %s",
98219824
tbinfo->attrdefs[j]->adef_expr);
98229825

98239826
/*
9824-
* Not Null constraint --- suppress if inherited
9827+
* Not Null constraint --- suppress if inherited, except
9828+
* in binary-upgrade case.
98259829
*/
9826-
if (tbinfo->notnull[j]&& !tbinfo->inhNotNull[j])
9830+
if (tbinfo->notnull[j]&&
9831+
(!tbinfo->inhNotNull[j]||binary_upgrade))
98279832
appendPQExpBuffer(q," NOT NULL");
9828-
9829-
actual_atts++;
98309833
}
98319834
}
98329835

@@ -9852,7 +9855,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
98529855

98539856
appendPQExpBuffer(q,"\n)");
98549857

9855-
if (numParents>0)
9858+
if (numParents>0&& !binary_upgrade)
98569859
{
98579860
appendPQExpBuffer(q,"\nINHERITS (");
98589861
for (k=0;k<numParents;k++)
@@ -9892,59 +9895,99 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
98929895
appendPQExpBuffer(q,";\n");
98939896

98949897
/*
9895-
* For binary-compatible heap files, we create dropped columns above
9896-
* and drop them here.
9898+
* To create binary-compatible heap files, we have to ensure the
9899+
* same physical column order, including dropped columns, as in the
9900+
* original. Therefore, we create dropped columns above and drop
9901+
* them here, also updating their attlen/attalign values so that
9902+
* the dropped column can be skipped properly. (We do not bother
9903+
* with restoring the original attbyval setting.) Also, inheritance
9904+
* relationships are set up by doing ALTER INHERIT rather than using
9905+
* an INHERITS clause --- the latter would possibly mess up the
9906+
* column order. That also means we have to take care about setting
9907+
* attislocal correctly, plus fix up any inherited CHECK constraints.
98979908
*/
98989909
if (binary_upgrade)
98999910
{
99009911
for (j=0;j<tbinfo->numatts;j++)
99019912
{
99029913
if (tbinfo->attisdropped[j])
99039914
{
9915+
appendPQExpBuffer(q,"\n-- For binary upgrade, recreate dropped column.\n");
9916+
appendPQExpBuffer(q,"UPDATE pg_catalog.pg_attribute\n"
9917+
"SET attlen = %d, "
9918+
"attalign = '%c', attbyval = false\n"
9919+
"WHERE attname = ",
9920+
tbinfo->attlen[j],
9921+
tbinfo->attalign[j]);
9922+
appendStringLiteralAH(q,tbinfo->attnames[j],fout);
9923+
appendPQExpBuffer(q,"\n AND attrelid = ");
9924+
appendStringLiteralAH(q,fmtId(tbinfo->dobj.name),fout);
9925+
appendPQExpBuffer(q,"::pg_catalog.regclass;\n");
9926+
99049927
appendPQExpBuffer(q,"ALTER TABLE ONLY %s ",
99059928
fmtId(tbinfo->dobj.name));
99069929
appendPQExpBuffer(q,"DROP COLUMN %s;\n",
99079930
fmtId(tbinfo->attnames[j]));
9931+
}
9932+
elseif (!tbinfo->attislocal[j])
9933+
{
9934+
appendPQExpBuffer(q,"\n-- For binary upgrade, recreate inherited column.\n");
9935+
appendPQExpBuffer(q,"UPDATE pg_catalog.pg_attribute\n"
9936+
"SET attislocal = false\n"
9937+
"WHERE attname = ");
9938+
appendStringLiteralAH(q,tbinfo->attnames[j],fout);
9939+
appendPQExpBuffer(q,"\n AND attrelid = ");
9940+
appendStringLiteralAH(q,fmtId(tbinfo->dobj.name),fout);
9941+
appendPQExpBuffer(q,"::pg_catalog.regclass;\n");
9942+
}
9943+
}
99089944

9909-
/*
9910-
* ALTER TABLE DROP COLUMN clears pg_attribute.atttypid,
9911-
* so we have to set pg_attribute.attlen and
9912-
* pg_attribute.attalign values because that is what is
9913-
* used to skip over dropped columns in the heap tuples.
9914-
* We have atttypmod, but it seems impossible to know the
9915-
* correct data type that will yield pg_attribute values
9916-
* that match the old installation. See comment in
9917-
* backend/catalog/heap.c::RemoveAttributeById()
9918-
*/
9919-
appendPQExpBuffer(q,"\n-- For binary upgrade, recreate dropped column's length and alignment.\n");
9920-
appendPQExpBuffer(q,"UPDATE pg_attribute\n"
9921-
"SET attlen = %d, "
9922-
"attalign = '%c'\n"
9923-
"WHEREattname = '%s'\n"
9924-
"AND attrelid = \n"
9925-
"(\n"
9926-
"SELECT oid\n"
9927-
"FROM pg_class\n"
9928-
"WHERErelnamespace = "
9929-
"(SELECT oid FROM pg_namespace "
9930-
"WHERE nspname = CURRENT_SCHEMA)\n"
9931-
"AND relname = ",
9932-
tbinfo->attlen[j],
9933-
tbinfo->attalign[j],
9934-
tbinfo->attnames[j]);
9935-
appendStringLiteralAH(q,tbinfo->dobj.name,fout);
9936-
appendPQExpBuffer(q,"\n);\n");
9945+
for (k=0;k<tbinfo->ncheck;k++)
9946+
{
9947+
ConstraintInfo*constr=&(tbinfo->checkexprs[k]);
9948+
9949+
if (constr->separate||constr->conislocal)
9950+
continue;
9951+
9952+
appendPQExpBuffer(q,"\n-- For binary upgrade, set up inherited constraint.\n");
9953+
appendPQExpBuffer(q,"ALTER TABLE ONLY %s ",
9954+
fmtId(tbinfo->dobj.name));
9955+
appendPQExpBuffer(q," ADD CONSTRAINT %s ",
9956+
fmtId(constr->dobj.name));
9957+
appendPQExpBuffer(q,"%s;\n",constr->condef);
9958+
appendPQExpBuffer(q,"UPDATE pg_catalog.pg_constraint\n"
9959+
"SET conislocal = false\n"
9960+
"WHERE contype = 'c' AND conname = ");
9961+
appendStringLiteralAH(q,constr->dobj.name,fout);
9962+
appendPQExpBuffer(q,"\n AND conrelid = ");
9963+
appendStringLiteralAH(q,fmtId(tbinfo->dobj.name),fout);
9964+
appendPQExpBuffer(q,"::pg_catalog.regclass;\n");
9965+
}
9966+
9967+
if (numParents>0)
9968+
{
9969+
appendPQExpBuffer(q,"\n-- For binary upgrade, set up inheritance this way.\n");
9970+
for (k=0;k<numParents;k++)
9971+
{
9972+
TableInfo*parentRel=parents[k];
9973+
9974+
appendPQExpBuffer(q,"ALTER TABLE ONLY %s INHERIT ",
9975+
fmtId(tbinfo->dobj.name));
9976+
if (parentRel->dobj.namespace!=tbinfo->dobj.namespace)
9977+
appendPQExpBuffer(q,"%s.",
9978+
fmtId(parentRel->dobj.namespace->dobj.name));
9979+
appendPQExpBuffer(q,"%s;\n",
9980+
fmtId(parentRel->dobj.name));
99379981
}
99389982
}
9983+
99399984
appendPQExpBuffer(q,"\n-- For binary upgrade, set relfrozenxid.\n");
9940-
appendPQExpBuffer(q,"UPDATE pg_class\n"
9985+
appendPQExpBuffer(q,"UPDATEpg_catalog.pg_class\n"
99419986
"SET relfrozenxid = '%u'\n"
9942-
"WHERErelname = ",
9987+
"WHERE oid = ",
99439988
tbinfo->frozenxid);
9944-
appendStringLiteralAH(q,tbinfo->dobj.name,fout);
9945-
appendPQExpBuffer(q,"\nAND relnamespace = "
9946-
"(SELECT oid FROM pg_namespace "
9947-
"WHERE nspname = CURRENT_SCHEMA);\n");
9989+
appendStringLiteralAH(q,fmtId(tbinfo->dobj.name),fout);
9990+
appendPQExpBuffer(q,"::pg_catalog.regclass;\n");
99489991
}
99499992

99509993
/* Loop dumping statistics and storage statements */
@@ -10051,8 +10094,8 @@ dumpAttrDef(Archive *fout, AttrDefInfo *adinfo)
1005110094
if (!tbinfo->dobj.dump|| !adinfo->separate||dataOnly)
1005210095
return;
1005310096

10054-
/* Don't print inherited defaults, either */
10055-
if (tbinfo->inhAttrDef[adnum-1])
10097+
/* Don't print inherited defaults, either, except for binary upgrade */
10098+
if (tbinfo->inhAttrDef[adnum-1]&& !binary_upgrade)
1005610099
return;
1005710100

1005810101
q=createPQExpBuffer();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp