@@ -8602,12 +8602,9 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
86028602 * Normally this is always true, but it's false for dropped columns, as well
86038603 * as those that were inherited without any local definition. (If we print
86048604 * such a column it will mistakenly get pg_attribute.attislocal set to true.)
8605- * For partitions, it's always true, because we want the partitions to be
8606- * created independently and ATTACH PARTITION used afterwards.
8607- *
8608- * In binary_upgrade mode, we must print all columns and fix the attislocal/
8609- * attisdropped state later, so as to keep control of the physical column
8610- * order.
8605+ * However, in binary_upgrade mode, we must print all such columns anyway and
8606+ * fix the attislocal/attisdropped state later, so as to keep control of the
8607+ * physical column order.
86118608 *
86128609 * This function exists because there are scattered nonobvious places that
86138610 * must be kept in sync with this decision.
@@ -8617,9 +8614,7 @@ shouldPrintColumn(DumpOptions *dopt, TableInfo *tbinfo, int colno)
86178614{
86188615 if (dopt->binary_upgrade)
86198616 return true;
8620- if (tbinfo->attisdropped[colno])
8621- return false;
8622- return (tbinfo->attislocal[colno] || tbinfo->ispartition);
8617+ return (tbinfo->attislocal[colno] && !tbinfo->attisdropped[colno]);
86238618}
86248619
86258620
@@ -15570,6 +15565,27 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1557015565 if (tbinfo->reloftype && !dopt->binary_upgrade)
1557115566 appendPQExpBuffer(q, " OF %s", tbinfo->reloftype);
1557215567
15568+ /*
15569+ * If the table is a partition, dump it as such; except in the case of
15570+ * a binary upgrade, we dump the table normally and attach it to the
15571+ * parent afterward.
15572+ */
15573+ if (tbinfo->ispartition && !dopt->binary_upgrade)
15574+ {
15575+ TableInfo *parentRel = tbinfo->parents[0];
15576+
15577+ /*
15578+ * With partitions, unlike inheritance, there can only be one
15579+ * parent.
15580+ */
15581+ if (tbinfo->numParents != 1)
15582+ fatal("invalid number of parents %d for table \"%s\"",
15583+ tbinfo->numParents, tbinfo->dobj.name);
15584+
15585+ appendPQExpBuffer(q, " PARTITION OF %s",
15586+ fmtQualifiedDumpable(parentRel));
15587+ }
15588+
1557315589 if (tbinfo->relkind != RELKIND_MATVIEW)
1557415590 {
1557515591 /* Dump the attributes */
@@ -15598,9 +15614,12 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1559815614 (!tbinfo->inhNotNull[j] ||
1559915615 dopt->binary_upgrade));
1560015616
15601- /* Skip column if fully defined by reloftype */
15602- if (tbinfo->reloftype && !has_default && !has_notnull &&
15603- !dopt->binary_upgrade)
15617+ /*
15618+ * Skip column if fully defined by reloftype or the
15619+ * partition parent.
15620+ */
15621+ if ((tbinfo->reloftype || tbinfo->ispartition) &&
15622+ !has_default && !has_notnull && !dopt->binary_upgrade)
1560415623 continue;
1560515624
1560615625 /* Format properly if not first attr */
@@ -15623,16 +15642,20 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1562315642 * clean things up later.
1562415643 */
1562515644 appendPQExpBufferStr(q, " INTEGER /* dummy */");
15626- /* and skip to the next column */
15645+ /* Skip all the rest, too */
1562715646 continue;
1562815647 }
1562915648
1563015649 /*
15631- * Attribute type; print it except when creating a typed
15632- * table ('OF type_name'), but in binary-upgrade mode,
15633- * print it in that case too.
15650+ * Attribute type
15651+ *
15652+ * In binary-upgrade mode, we always include the type. If
15653+ * we aren't in binary-upgrade mode, then we skip the type
15654+ * when creating a typed table ('OF type_name') or a
15655+ * partition ('PARTITION OF'), since the type comes from
15656+ * the parent/partitioned table.
1563415657 */
15635- if (dopt->binary_upgrade || !tbinfo->reloftype)
15658+ if (dopt->binary_upgrade || ( !tbinfo->reloftype && !tbinfo->ispartition) )
1563615659 {
1563715660 appendPQExpBuffer(q, " %s",
1563815661 tbinfo->atttypnames[j]);
@@ -15689,20 +15712,25 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1568915712
1569015713 if (actual_atts)
1569115714 appendPQExpBufferStr(q, "\n)");
15692- else if (!(tbinfo->reloftype && !dopt->binary_upgrade))
15715+ else if (!((tbinfo->reloftype || tbinfo->ispartition) &&
15716+ !dopt->binary_upgrade))
1569315717 {
1569415718 /*
15695- * No attributes? we must have a parenthesized attribute list,
15696- * even though empty, when not using the OF TYPE syntax.
15719+ * We must have a parenthesized attribute list, even though
15720+ * empty, when not using the OF TYPE or PARTITION OF syntax.
1569715721 */
1569815722 appendPQExpBufferStr(q, " (\n)");
1569915723 }
1570015724
15701- /*
15702- * Emit the INHERITS clause (not for partitions), except in
15703- * binary-upgrade mode.
15704- */
15705- if (numParents > 0 && !tbinfo->ispartition &&
15725+ if (tbinfo->ispartition && !dopt->binary_upgrade)
15726+ {
15727+ appendPQExpBufferChar(q, '\n');
15728+ appendPQExpBufferStr(q, tbinfo->partbound);
15729+ }
15730+
15731+ /* Emit the INHERITS clause, except if this is a partition. */
15732+ if (numParents > 0 &&
15733+ !tbinfo->ispartition &&
1570615734 !dopt->binary_upgrade)
1570715735 {
1570815736 appendPQExpBufferStr(q, "\nINHERITS (");
@@ -15875,16 +15903,30 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1587515903 appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
1587615904 }
1587715905
15878- if (numParents > 0 && !tbinfo->ispartition )
15906+ if (numParents > 0)
1587915907 {
15880- appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritance this way.\n");
15908+ appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritance and partitioning this way.\n");
1588115909 for (k = 0; k < numParents; k++)
1588215910 {
1588315911 TableInfo *parentRel = parents[k];
1588415912
15885- appendPQExpBuffer(q, "ALTER TABLE ONLY %s INHERIT %s;\n",
15886- qualrelname,
15887- fmtQualifiedDumpable(parentRel));
15913+ /* In the partitioning case, we alter the parent */
15914+ if (tbinfo->ispartition)
15915+ appendPQExpBuffer(q,
15916+ "ALTER TABLE ONLY %s ATTACH PARTITION ",
15917+ fmtQualifiedDumpable(parentRel));
15918+ else
15919+ appendPQExpBuffer(q, "ALTER TABLE ONLY %s INHERIT ",
15920+ qualrelname);
15921+
15922+ /* Partition needs specifying the bounds */
15923+ if (tbinfo->ispartition)
15924+ appendPQExpBuffer(q, "%s %s;\n",
15925+ qualrelname,
15926+ tbinfo->partbound);
15927+ else
15928+ appendPQExpBuffer(q, "%s;\n",
15929+ fmtQualifiedDumpable(parentRel));
1588815930 }
1588915931 }
1589015932
@@ -15897,27 +15939,6 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1589715939 }
1589815940 }
1589915941
15900- /*
15901- * For partitioned tables, emit the ATTACH PARTITION clause. Note
15902- * that we always want to create partitions this way instead of using
15903- * CREATE TABLE .. PARTITION OF, mainly to preserve a possible column
15904- * layout discrepancy with the parent, but also to ensure it gets the
15905- * correct tablespace setting if it differs from the parent's.
15906- */
15907- if (tbinfo->ispartition)
15908- {
15909- /* With partitions there can only be one parent */
15910- if (tbinfo->numParents != 1)
15911- fatal("invalid number of parents %d for table \"%s\"",
15912- tbinfo->numParents, tbinfo->dobj.name);
15913-
15914- /* Perform ALTER TABLE on the parent */
15915- appendPQExpBuffer(q,
15916- "ALTER TABLE ONLY %s ATTACH PARTITION %s %s;\n",
15917- fmtQualifiedDumpable(parents[0]),
15918- qualrelname, tbinfo->partbound);
15919- }
15920-
1592115942 /*
1592215943 * In binary_upgrade mode, arrange to restore the old relfrozenxid and
1592315944 * relminmxid of all vacuumable relations. (While vacuum.c processes
0 commit comments