@@ -8602,12 +8602,9 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
8602
8602
* Normally this is always true, but it's false for dropped columns, as well
8603
8603
* as those that were inherited without any local definition. (If we print
8604
8604
* 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.
8611
8608
*
8612
8609
* This function exists because there are scattered nonobvious places that
8613
8610
* must be kept in sync with this decision.
@@ -8617,9 +8614,7 @@ shouldPrintColumn(DumpOptions *dopt, TableInfo *tbinfo, int colno)
8617
8614
{
8618
8615
if (dopt->binary_upgrade)
8619
8616
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]);
8623
8618
}
8624
8619
8625
8620
@@ -15570,6 +15565,27 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15570
15565
if (tbinfo->reloftype && !dopt->binary_upgrade)
15571
15566
appendPQExpBuffer(q, " OF %s", tbinfo->reloftype);
15572
15567
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
+
15573
15589
if (tbinfo->relkind != RELKIND_MATVIEW)
15574
15590
{
15575
15591
/* Dump the attributes */
@@ -15598,9 +15614,12 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15598
15614
(!tbinfo->inhNotNull[j] ||
15599
15615
dopt->binary_upgrade));
15600
15616
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)
15604
15623
continue;
15605
15624
15606
15625
/* Format properly if not first attr */
@@ -15623,16 +15642,20 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15623
15642
* clean things up later.
15624
15643
*/
15625
15644
appendPQExpBufferStr(q, " INTEGER /* dummy */");
15626
- /* and skip to the next column */
15645
+ /* Skip all the rest, too */
15627
15646
continue;
15628
15647
}
15629
15648
15630
15649
/*
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.
15634
15657
*/
15635
- if (dopt->binary_upgrade || !tbinfo->reloftype)
15658
+ if (dopt->binary_upgrade || ( !tbinfo->reloftype && !tbinfo->ispartition) )
15636
15659
{
15637
15660
appendPQExpBuffer(q, " %s",
15638
15661
tbinfo->atttypnames[j]);
@@ -15689,20 +15712,25 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15689
15712
15690
15713
if (actual_atts)
15691
15714
appendPQExpBufferStr(q, "\n)");
15692
- else if (!(tbinfo->reloftype && !dopt->binary_upgrade))
15715
+ else if (!((tbinfo->reloftype || tbinfo->ispartition) &&
15716
+ !dopt->binary_upgrade))
15693
15717
{
15694
15718
/*
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.
15697
15721
*/
15698
15722
appendPQExpBufferStr(q, " (\n)");
15699
15723
}
15700
15724
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 &&
15706
15734
!dopt->binary_upgrade)
15707
15735
{
15708
15736
appendPQExpBufferStr(q, "\nINHERITS (");
@@ -15875,16 +15903,30 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15875
15903
appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
15876
15904
}
15877
15905
15878
- if (numParents > 0 && !tbinfo->ispartition )
15906
+ if (numParents > 0)
15879
15907
{
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");
15881
15909
for (k = 0; k < numParents; k++)
15882
15910
{
15883
15911
TableInfo *parentRel = parents[k];
15884
15912
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));
15888
15930
}
15889
15931
}
15890
15932
@@ -15897,27 +15939,6 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15897
15939
}
15898
15940
}
15899
15941
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
-
15921
15942
/*
15922
15943
* In binary_upgrade mode, arrange to restore the old relfrozenxid and
15923
15944
* relminmxid of all vacuumable relations. (While vacuum.c processes
0 commit comments