@@ -8618,9 +8618,12 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
86188618 * Normally this is always true, but it's false for dropped columns, as well
86198619 * as those that were inherited without any local definition. (If we print
86208620 * such a column it will mistakenly get pg_attribute.attislocal set to true.)
8621- * However, in binary_upgrade mode, we must print all such columns anyway and
8622- * fix the attislocal/attisdropped state later, so as to keep control of the
8623- * physical column order.
8621+ * For partitions, it's always true, because we want the partitions to be
8622+ * created independently and ATTACH PARTITION used afterwards.
8623+ *
8624+ * In binary_upgrade mode, we must print all columns and fix the attislocal/
8625+ * attisdropped state later, so as to keep control of the physical column
8626+ * order.
86248627 *
86258628 * This function exists because there are scattered nonobvious places that
86268629 * must be kept in sync with this decision.
@@ -8630,7 +8633,9 @@ shouldPrintColumn(DumpOptions *dopt, TableInfo *tbinfo, int colno)
86308633{
86318634 if (dopt->binary_upgrade)
86328635 return true;
8633- return (tbinfo->attislocal[colno] && !tbinfo->attisdropped[colno]);
8636+ if (tbinfo->attisdropped[colno])
8637+ return false;
8638+ return (tbinfo->attislocal[colno] || tbinfo->ispartition);
86348639}
86358640
86368641
@@ -15599,27 +15604,6 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1559915604 if (tbinfo->reloftype && !dopt->binary_upgrade)
1560015605 appendPQExpBuffer(q, " OF %s", tbinfo->reloftype);
1560115606
15602- /*
15603- * If the table is a partition, dump it as such; except in the case of
15604- * a binary upgrade, we dump the table normally and attach it to the
15605- * parent afterward.
15606- */
15607- if (tbinfo->ispartition && !dopt->binary_upgrade)
15608- {
15609- TableInfo *parentRel = tbinfo->parents[0];
15610-
15611- /*
15612- * With partitions, unlike inheritance, there can only be one
15613- * parent.
15614- */
15615- if (tbinfo->numParents != 1)
15616- fatal("invalid number of parents %d for table \"%s\"",
15617- tbinfo->numParents, tbinfo->dobj.name);
15618-
15619- appendPQExpBuffer(q, " PARTITION OF %s",
15620- fmtQualifiedDumpable(parentRel));
15621- }
15622-
1562315607 if (tbinfo->relkind != RELKIND_MATVIEW)
1562415608 {
1562515609 /* Dump the attributes */
@@ -15648,12 +15632,9 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1564815632 (!tbinfo->inhNotNull[j] ||
1564915633 dopt->binary_upgrade));
1565015634
15651- /*
15652- * Skip column if fully defined by reloftype or the
15653- * partition parent.
15654- */
15655- if ((tbinfo->reloftype || tbinfo->ispartition) &&
15656- !has_default && !has_notnull && !dopt->binary_upgrade)
15635+ /* Skip column if fully defined by reloftype */
15636+ if (tbinfo->reloftype && !has_default && !has_notnull &&
15637+ !dopt->binary_upgrade)
1565715638 continue;
1565815639
1565915640 /* Format properly if not first attr */
@@ -15676,20 +15657,16 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1567615657 * clean things up later.
1567715658 */
1567815659 appendPQExpBufferStr(q, " INTEGER /* dummy */");
15679- /* Skip all the rest, too */
15660+ /* and skip to the next column */
1568015661 continue;
1568115662 }
1568215663
1568315664 /*
15684- * Attribute type
15685- *
15686- * In binary-upgrade mode, we always include the type. If
15687- * we aren't in binary-upgrade mode, then we skip the type
15688- * when creating a typed table ('OF type_name') or a
15689- * partition ('PARTITION OF'), since the type comes from
15690- * the parent/partitioned table.
15665+ * Attribute type; print it except when creating a typed
15666+ * table ('OF type_name'), but in binary-upgrade mode,
15667+ * print it in that case too.
1569115668 */
15692- if (dopt->binary_upgrade || ( !tbinfo->reloftype && !tbinfo->ispartition) )
15669+ if (dopt->binary_upgrade || !tbinfo->reloftype)
1569315670 {
1569415671 appendPQExpBuffer(q, " %s",
1569515672 tbinfo->atttypnames[j]);
@@ -15746,25 +15723,20 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1574615723
1574715724 if (actual_atts)
1574815725 appendPQExpBufferStr(q, "\n)");
15749- else if (!((tbinfo->reloftype || tbinfo->ispartition) &&
15750- !dopt->binary_upgrade))
15726+ else if (!(tbinfo->reloftype && !dopt->binary_upgrade))
1575115727 {
1575215728 /*
15753- * We must have a parenthesized attribute list, even though
15754- * empty, when not using the OF TYPE or PARTITION OF syntax.
15729+ * No attributes? we must have a parenthesized attribute list,
15730+ * even though empty, when not using the OF TYPE syntax.
1575515731 */
1575615732 appendPQExpBufferStr(q, " (\n)");
1575715733 }
1575815734
15759- if (tbinfo->ispartition && !dopt->binary_upgrade)
15760- {
15761- appendPQExpBufferChar(q, '\n');
15762- appendPQExpBufferStr(q, tbinfo->partbound);
15763- }
15764-
15765- /* Emit the INHERITS clause, except if this is a partition. */
15766- if (numParents > 0 &&
15767- !tbinfo->ispartition &&
15735+ /*
15736+ * Emit the INHERITS clause (not for partitions), except in
15737+ * binary-upgrade mode.
15738+ */
15739+ if (numParents > 0 && !tbinfo->ispartition &&
1576815740 !dopt->binary_upgrade)
1576915741 {
1577015742 appendPQExpBufferStr(q, "\nINHERITS (");
@@ -15937,30 +15909,16 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1593715909 appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
1593815910 }
1593915911
15940- if (numParents > 0)
15912+ if (numParents > 0 && !tbinfo->ispartition )
1594115913 {
15942- appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritance and partitioning this way.\n");
15914+ appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritance this way.\n");
1594315915 for (k = 0; k < numParents; k++)
1594415916 {
1594515917 TableInfo *parentRel = parents[k];
1594615918
15947- /* In the partitioning case, we alter the parent */
15948- if (tbinfo->ispartition)
15949- appendPQExpBuffer(q,
15950- "ALTER TABLE ONLY %s ATTACH PARTITION ",
15951- fmtQualifiedDumpable(parentRel));
15952- else
15953- appendPQExpBuffer(q, "ALTER TABLE ONLY %s INHERIT ",
15954- qualrelname);
15955-
15956- /* Partition needs specifying the bounds */
15957- if (tbinfo->ispartition)
15958- appendPQExpBuffer(q, "%s %s;\n",
15959- qualrelname,
15960- tbinfo->partbound);
15961- else
15962- appendPQExpBuffer(q, "%s;\n",
15963- fmtQualifiedDumpable(parentRel));
15919+ appendPQExpBuffer(q, "ALTER TABLE ONLY %s INHERIT %s;\n",
15920+ qualrelname,
15921+ fmtQualifiedDumpable(parentRel));
1596415922 }
1596515923 }
1596615924
@@ -15973,6 +15931,27 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1597315931 }
1597415932 }
1597515933
15934+ /*
15935+ * For partitioned tables, emit the ATTACH PARTITION clause. Note
15936+ * that we always want to create partitions this way instead of using
15937+ * CREATE TABLE .. PARTITION OF, mainly to preserve a possible column
15938+ * layout discrepancy with the parent, but also to ensure it gets the
15939+ * correct tablespace setting if it differs from the parent's.
15940+ */
15941+ if (tbinfo->ispartition)
15942+ {
15943+ /* With partitions there can only be one parent */
15944+ if (tbinfo->numParents != 1)
15945+ fatal("invalid number of parents %d for table \"%s\"",
15946+ tbinfo->numParents, tbinfo->dobj.name);
15947+
15948+ /* Perform ALTER TABLE on the parent */
15949+ appendPQExpBuffer(q,
15950+ "ALTER TABLE ONLY %s ATTACH PARTITION %s %s;\n",
15951+ fmtQualifiedDumpable(parents[0]),
15952+ qualrelname, tbinfo->partbound);
15953+ }
15954+
1597615955 /*
1597715956 * In binary_upgrade mode, arrange to restore the old relfrozenxid and
1597815957 * relminmxid of all vacuumable relations. (While vacuum.c processes
0 commit comments