Skip to content

Commit 40353bc

Browse files
committed
Revert "Make pg_dump emit ATTACH PARTITION instead of PARTITION OF"
... and fallout (from branches 10, 11 and master). The change was ill-considered, and it broke a few normal use cases; since we don't have time to fix it, we'll try again after this week's minor releases. Reported-by: Rushabh Lathia Discussion: https://postgr.es/m/CAGPqQf0iQV=PPOv2Btog9J9AwOQp6HmuVd6SbGTR_v3Zp2XT1w@mail.gmail.com
1 parent f4540a9 commit 40353bc

File tree

3 files changed

+78
-75
lines changed

3 files changed

+78
-75
lines changed

doc/src/sgml/release-10.sgml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -441,23 +441,6 @@
441441
</para>
442442
</listitem>
443443

444-
<listitem>
445-
<para>
446-
Make <application>pg_dump</application> recreate table partitions
447-
using <command>ATTACH PARTITION</command> instead
448-
of <command>CREATE TABLE ... PARTITION OF</command> (David Rowley)
449-
</para>
450-
451-
<para>
452-
This avoids various corner-case problems, notably that dump and
453-
restore might unexpectedly alter a partition's column ordering.
454-
It also means that a selective restore of the partition can succeed
455-
even if its parent partitioned table isn't restored.
456-
(The <command>ATTACH PARTITION</command> will fail of course, but
457-
the partition table itself can be created and populated.)
458-
</para>
459-
</listitem>
460-
461444
<listitem>
462445
<para>
463446
Avoid crash in <filename>contrib/postgres_fdw</filename> when a

src/bin/pg_dump/pg_dump.c

Lines changed: 72 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8188,12 +8188,9 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
81888188
* Normally this is always true, but it's false for dropped columns, as well
81898189
* as those that were inherited without any local definition. (If we print
81908190
* such a column it will mistakenly get pg_attribute.attislocal set to true.)
8191-
* For partitions, it's always true, because we want the partitions to be
8192-
* created independently and ATTACH PARTITION used afterwards.
8193-
*
8194-
* In binary_upgrade mode, we must print all columns and fix the attislocal/
8195-
* attisdropped state later, so as to keep control of the physical column
8196-
* order.
8191+
* However, in binary_upgrade mode, we must print all such columns anyway and
8192+
* fix the attislocal/attisdropped state later, so as to keep control of the
8193+
* physical column order.
81978194
*
81988195
* This function exists because there are scattered nonobvious places that
81998196
* must be kept in sync with this decision.
@@ -8203,9 +8200,7 @@ shouldPrintColumn(DumpOptions *dopt, TableInfo *tbinfo, int colno)
82038200
{
82048201
if (dopt->binary_upgrade)
82058202
return true;
8206-
if (tbinfo->attisdropped[colno])
8207-
return false;
8208-
return (tbinfo->attislocal[colno] || tbinfo->ispartition);
8203+
return (tbinfo->attislocal[colno] && !tbinfo->attisdropped[colno]);
82098204
}
82108205

82118206

@@ -14968,6 +14963,27 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1496814963
if (tbinfo->reloftype && !dopt->binary_upgrade)
1496914964
appendPQExpBuffer(q, " OF %s", tbinfo->reloftype);
1497014965

14966+
/*
14967+
* If the table is a partition, dump it as such; except in the case of
14968+
* a binary upgrade, we dump the table normally and attach it to the
14969+
* parent afterward.
14970+
*/
14971+
if (tbinfo->ispartition && !dopt->binary_upgrade)
14972+
{
14973+
TableInfo *parentRel = tbinfo->parents[0];
14974+
14975+
/*
14976+
* With partitions, unlike inheritance, there can only be one
14977+
* parent.
14978+
*/
14979+
if (tbinfo->numParents != 1)
14980+
exit_horribly(NULL, "invalid number of parents %d for table \"%s\"\n",
14981+
tbinfo->numParents, tbinfo->dobj.name);
14982+
14983+
appendPQExpBuffer(q, " PARTITION OF %s",
14984+
fmtQualifiedDumpable(parentRel));
14985+
}
14986+
1497114987
if (tbinfo->relkind != RELKIND_MATVIEW)
1497214988
{
1497314989
/* Dump the attributes */
@@ -14996,9 +15012,12 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1499615012
(!tbinfo->inhNotNull[j] ||
1499715013
dopt->binary_upgrade));
1499815014

14999-
/* Skip column if fully defined by reloftype */
15000-
if (tbinfo->reloftype && !has_default && !has_notnull &&
15001-
!dopt->binary_upgrade)
15015+
/*
15016+
* Skip column if fully defined by reloftype or the
15017+
* partition parent.
15018+
*/
15019+
if ((tbinfo->reloftype || tbinfo->ispartition) &&
15020+
!has_default && !has_notnull && !dopt->binary_upgrade)
1500215021
continue;
1500315022

1500415023
/* Format properly if not first attr */
@@ -15021,16 +15040,20 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1502115040
* clean things up later.
1502215041
*/
1502315042
appendPQExpBufferStr(q, " INTEGER /* dummy */");
15024-
/* and skip to the next column */
15043+
/* Skip all the rest, too */
1502515044
continue;
1502615045
}
1502715046

1502815047
/*
15029-
* Attribute type; print it except when creating a typed
15030-
* table ('OF type_name'), but in binary-upgrade mode,
15031-
* print it in that case too.
15048+
* Attribute type
15049+
*
15050+
* In binary-upgrade mode, we always include the type. If
15051+
* we aren't in binary-upgrade mode, then we skip the type
15052+
* when creating a typed table ('OF type_name') or a
15053+
* partition ('PARTITION OF'), since the type comes from
15054+
* the parent/partitioned table.
1503215055
*/
15033-
if (dopt->binary_upgrade || !tbinfo->reloftype)
15056+
if (dopt->binary_upgrade || (!tbinfo->reloftype && !tbinfo->ispartition))
1503415057
{
1503515058
appendPQExpBuffer(q, " %s",
1503615059
tbinfo->atttypnames[j]);
@@ -15080,20 +15103,25 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1508015103

1508115104
if (actual_atts)
1508215105
appendPQExpBufferStr(q, "\n)");
15083-
else if (!(tbinfo->reloftype && !dopt->binary_upgrade))
15106+
else if (!((tbinfo->reloftype || tbinfo->ispartition) &&
15107+
!dopt->binary_upgrade))
1508415108
{
1508515109
/*
15086-
* No attributes? we must have a parenthesized attribute list,
15087-
* even though empty, when not using the OF TYPE syntax.
15110+
* We must have a parenthesized attribute list, even though
15111+
* empty, when not using the OF TYPE or PARTITION OF syntax.
1508815112
*/
1508915113
appendPQExpBufferStr(q, " (\n)");
1509015114
}
1509115115

15092-
/*
15093-
* Emit the INHERITS clause (not for partitions), except in
15094-
* binary-upgrade mode.
15095-
*/
15096-
if (numParents > 0 && !tbinfo->ispartition &&
15116+
if (tbinfo->ispartition && !dopt->binary_upgrade)
15117+
{
15118+
appendPQExpBufferStr(q, "\n");
15119+
appendPQExpBufferStr(q, tbinfo->partbound);
15120+
}
15121+
15122+
/* Emit the INHERITS clause, except if this is a partition. */
15123+
if (numParents > 0 &&
15124+
!tbinfo->ispartition &&
1509715125
!dopt->binary_upgrade)
1509815126
{
1509915127
appendPQExpBufferStr(q, "\nINHERITS (");
@@ -15243,16 +15271,30 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1524315271
appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
1524415272
}
1524515273

15246-
if (numParents > 0 && !tbinfo->ispartition)
15274+
if (numParents > 0)
1524715275
{
15248-
appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritance this way.\n");
15276+
appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritance and partitioning this way.\n");
1524915277
for (k = 0; k < numParents; k++)
1525015278
{
1525115279
TableInfo *parentRel = parents[k];
1525215280

15253-
appendPQExpBuffer(q, "ALTER TABLE ONLY %s INHERIT %s;\n",
15254-
qualrelname,
15255-
fmtQualifiedDumpable(parentRel));
15281+
/* In the partitioning case, we alter the parent */
15282+
if (tbinfo->ispartition)
15283+
appendPQExpBuffer(q,
15284+
"ALTER TABLE ONLY %s ATTACH PARTITION ",
15285+
fmtQualifiedDumpable(parentRel));
15286+
else
15287+
appendPQExpBuffer(q, "ALTER TABLE ONLY %s INHERIT ",
15288+
qualrelname);
15289+
15290+
/* Partition needs specifying the bounds */
15291+
if (tbinfo->ispartition)
15292+
appendPQExpBuffer(q, "%s %s;\n",
15293+
qualrelname,
15294+
tbinfo->partbound);
15295+
else
15296+
appendPQExpBuffer(q, "%s;\n",
15297+
fmtQualifiedDumpable(parentRel));
1525615298
}
1525715299
}
1525815300

@@ -15265,27 +15307,6 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1526515307
}
1526615308
}
1526715309

15268-
/*
15269-
* For partitioned tables, emit the ATTACH PARTITION clause. Note
15270-
* that we always want to create partitions this way instead of using
15271-
* CREATE TABLE .. PARTITION OF, mainly to preserve a possible column
15272-
* layout discrepancy with the parent, but also to ensure it gets the
15273-
* correct tablespace setting if it differs from the parent's.
15274-
*/
15275-
if (tbinfo->ispartition)
15276-
{
15277-
/* With partitions there can only be one parent */
15278-
if (tbinfo->numParents != 1)
15279-
exit_horribly(NULL, "invalid number of parents %d for table \"%s\"\n",
15280-
tbinfo->numParents, tbinfo->dobj.name);
15281-
15282-
/* Perform ALTER TABLE on the parent */
15283-
appendPQExpBuffer(q,
15284-
"ALTER TABLE ONLY %s ATTACH PARTITION %s %s;\n",
15285-
fmtQualifiedDumpable(parents[0]),
15286-
qualrelname, tbinfo->partbound);
15287-
}
15288-
1528915310
/*
1529015311
* In binary_upgrade mode, arrange to restore the old relfrozenxid and
1529115312
* relminmxid of all vacuumable relations. (While vacuum.c processes

src/bin/pg_dump/t/002_pg_dump.pl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,8 +1047,8 @@
10471047
\QALTER TABLE ONLY dump_test.measurement ATTACH PARTITION dump_test_second_schema.measurement_y2006m2 \E
10481048
\QFOR VALUES FROM ('2006-02-01') TO ('2006-03-01');\E\n
10491049
/xm,
1050-
like => {
1051-
binary_upgrade => 1,
1050+
like => { binary_upgrade => 1, },
1051+
unlike => {
10521052
clean => 1,
10531053
clean_if_exists => 1,
10541054
createdb => 1,
@@ -1057,14 +1057,13 @@
10571057
exclude_test_table => 1,
10581058
exclude_test_table_data => 1,
10591059
no_blobs => 1,
1060-
no_owner => 1,
10611060
no_privs => 1,
1061+
no_owner => 1,
10621062
pg_dumpall_dbprivs => 1,
10631063
role => 1,
10641064
schema_only => 1,
10651065
section_pre_data => 1,
10661066
with_oids => 1,
1067-
}, unlike => {
10681067
only_dump_test_schema => 1,
10691068
only_dump_test_table => 1,
10701069
pg_dumpall_globals => 1,
@@ -4834,8 +4833,7 @@
48344833
\QCREATE TABLE dump_test_second_schema.measurement_y2006m2 PARTITION OF dump_test.measurement\E\n
48354834
\QFOR VALUES FROM ('2006-02-01') TO ('2006-03-01');\E\n
48364835
/xm,
4837-
like => {},
4838-
unlike => {
4836+
like => {
48394837
clean => 1,
48404838
clean_if_exists => 1,
48414839
createdb => 1,
@@ -4850,7 +4848,8 @@
48504848
role => 1,
48514849
schema_only => 1,
48524850
section_pre_data => 1,
4853-
with_oids => 1,
4851+
with_oids => 1, },
4852+
unlike => {
48544853
binary_upgrade => 1,
48554854
only_dump_test_schema => 1,
48564855
only_dump_test_table => 1,

0 commit comments

Comments
 (0)