@@ -8617,9 +8617,12 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
86178617 * Normally this is always true, but it's false for dropped columns, as well
86188618 * as those that were inherited without any local definition. (If we print
86198619 * such a column it will mistakenly get pg_attribute.attislocal set to true.)
8620- * However, in binary_upgrade mode, we must print all such columns anyway and
8621- * fix the attislocal/attisdropped state later, so as to keep control of the
8622- * physical column order.
8620+ * For partitions, it's always true, because we want the partitions to be
8621+ * created independently and ATTACH PARTITION used afterwards.
8622+ *
8623+ * In binary_upgrade mode, we must print all columns and fix the attislocal/
8624+ * attisdropped state later, so as to keep control of the physical column
8625+ * order.
86238626 *
86248627 * This function exists because there are scattered nonobvious places that
86258628 * must be kept in sync with this decision.
@@ -8629,7 +8632,9 @@ shouldPrintColumn(DumpOptions *dopt, TableInfo *tbinfo, int colno)
86298632{
86308633if (dopt->binary_upgrade)
86318634return true;
8632- return (tbinfo->attislocal[colno] && !tbinfo->attisdropped[colno]);
8635+ if (tbinfo->attisdropped[colno])
8636+ return false;
8637+ return (tbinfo->attislocal[colno] || tbinfo->ispartition);
86338638}
86348639
86358640
@@ -15538,27 +15543,6 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1553815543if (tbinfo->reloftype && !dopt->binary_upgrade)
1553915544appendPQExpBuffer(q, " OF %s", tbinfo->reloftype);
1554015545
15541- /*
15542- * If the table is a partition, dump it as such; except in the case of
15543- * a binary upgrade, we dump the table normally and attach it to the
15544- * parent afterward.
15545- */
15546- if (tbinfo->ispartition && !dopt->binary_upgrade)
15547- {
15548- TableInfo *parentRel = tbinfo->parents[0];
15549-
15550- /*
15551- * With partitions, unlike inheritance, there can only be one
15552- * parent.
15553- */
15554- if (tbinfo->numParents != 1)
15555- exit_horribly(NULL, "invalid number of parents %d for table \"%s\"\n",
15556- tbinfo->numParents, tbinfo->dobj.name);
15557-
15558- appendPQExpBuffer(q, " PARTITION OF %s",
15559- fmtQualifiedDumpable(parentRel));
15560- }
15561-
1556215546if (tbinfo->relkind != RELKIND_MATVIEW)
1556315547{
1556415548/* Dump the attributes */
@@ -15587,12 +15571,9 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1558715571 (!tbinfo->inhNotNull[j] ||
1558815572dopt->binary_upgrade));
1558915573
15590- /*
15591- * Skip column if fully defined by reloftype or the
15592- * partition parent.
15593- */
15594- if ((tbinfo->reloftype || tbinfo->ispartition) &&
15595- !has_default && !has_notnull && !dopt->binary_upgrade)
15574+ /* Skip column if fully defined by reloftype */
15575+ if (tbinfo->reloftype && !has_default && !has_notnull &&
15576+ !dopt->binary_upgrade)
1559615577continue;
1559715578
1559815579/* Format properly if not first attr */
@@ -15615,20 +15596,16 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1561515596 * clean things up later.
1561615597 */
1561715598appendPQExpBufferStr(q, " INTEGER /* dummy */");
15618- /*Skip all therest, too */
15599+ /*and skip to thenext column */
1561915600continue;
1562015601}
1562115602
1562215603/*
15623- * Attribute type
15624- *
15625- * In binary-upgrade mode, we always include the type. If
15626- * we aren't in binary-upgrade mode, then we skip the type
15627- * when creating a typed table ('OF type_name') or a
15628- * partition ('PARTITION OF'), since the type comes from
15629- * the parent/partitioned table.
15604+ * Attribute type; print it except when creating a typed
15605+ * table ('OF type_name'), but in binary-upgrade mode,
15606+ * print it in that case too.
1563015607 */
15631- if (dopt->binary_upgrade ||( !tbinfo->reloftype && !tbinfo->ispartition) )
15608+ if (dopt->binary_upgrade || !tbinfo->reloftype)
1563215609{
1563315610appendPQExpBuffer(q, " %s",
1563415611 tbinfo->atttypnames[j]);
@@ -15678,25 +15655,20 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1567815655
1567915656if (actual_atts)
1568015657appendPQExpBufferStr(q, "\n)");
15681- else if (!((tbinfo->reloftype || tbinfo->ispartition) &&
15682- !dopt->binary_upgrade))
15658+ else if (!(tbinfo->reloftype && !dopt->binary_upgrade))
1568315659{
1568415660/*
15685- *We must have a parenthesized attribute list, even though
15686- * empty, when not using the OF TYPE or PARTITION OF syntax.
15661+ *No attributes? we must have a parenthesized attribute list,
15662+ *even though empty, when not using the OF TYPE syntax.
1568715663 */
1568815664appendPQExpBufferStr(q, " (\n)");
1568915665}
1569015666
15691- if (tbinfo->ispartition && !dopt->binary_upgrade)
15692- {
15693- appendPQExpBufferChar(q, '\n');
15694- appendPQExpBufferStr(q, tbinfo->partbound);
15695- }
15696-
15697- /* Emit the INHERITS clause, except if this is a partition. */
15698- if (numParents > 0 &&
15699- !tbinfo->ispartition &&
15667+ /*
15668+ * Emit the INHERITS clause (not for partitions), except in
15669+ * binary-upgrade mode.
15670+ */
15671+ if (numParents > 0 && !tbinfo->ispartition &&
1570015672!dopt->binary_upgrade)
1570115673{
1570215674appendPQExpBufferStr(q, "\nINHERITS (");
@@ -15869,30 +15841,16 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1586915841appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
1587015842}
1587115843
15872- if (numParents > 0)
15844+ if (numParents > 0 && !tbinfo->ispartition )
1587315845{
15874- appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritanceand partitioning this way.\n");
15846+ appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritance this way.\n");
1587515847for (k = 0; k < numParents; k++)
1587615848{
1587715849TableInfo *parentRel = parents[k];
1587815850
15879- /* In the partitioning case, we alter the parent */
15880- if (tbinfo->ispartition)
15881- appendPQExpBuffer(q,
15882- "ALTER TABLE ONLY %s ATTACH PARTITION ",
15883- fmtQualifiedDumpable(parentRel));
15884- else
15885- appendPQExpBuffer(q, "ALTER TABLE ONLY %s INHERIT ",
15886- qualrelname);
15887-
15888- /* Partition needs specifying the bounds */
15889- if (tbinfo->ispartition)
15890- appendPQExpBuffer(q, "%s %s;\n",
15891- qualrelname,
15892- tbinfo->partbound);
15893- else
15894- appendPQExpBuffer(q, "%s;\n",
15895- fmtQualifiedDumpable(parentRel));
15851+ appendPQExpBuffer(q, "ALTER TABLE ONLY %s INHERIT %s;\n",
15852+ qualrelname,
15853+ fmtQualifiedDumpable(parentRel));
1589615854}
1589715855}
1589815856
@@ -15905,6 +15863,27 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1590515863}
1590615864}
1590715865
15866+ /*
15867+ * For partitioned tables, emit the ATTACH PARTITION clause. Note
15868+ * that we always want to create partitions this way instead of using
15869+ * CREATE TABLE .. PARTITION OF, mainly to preserve a possible column
15870+ * layout discrepancy with the parent, but also to ensure it gets the
15871+ * correct tablespace setting if it differs from the parent's.
15872+ */
15873+ if (tbinfo->ispartition)
15874+ {
15875+ /* With partitions there can only be one parent */
15876+ if (tbinfo->numParents != 1)
15877+ exit_horribly(NULL, "invalid number of parents %d for table \"%s\"",
15878+ tbinfo->numParents, tbinfo->dobj.name);
15879+
15880+ /* Perform ALTER TABLE on the parent */
15881+ appendPQExpBuffer(q,
15882+ "ALTER TABLE ONLY %s ATTACH PARTITION %s %s;\n",
15883+ fmtQualifiedDumpable(parents[0]),
15884+ qualrelname, tbinfo->partbound);
15885+ }
15886+
1590815887/*
1590915888 * In binary_upgrade mode, arrange to restore the old relfrozenxid and
1591015889 * relminmxid of all vacuumable relations. (While vacuum.c processes