@@ -8617,12 +8617,9 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
8617
8617
* Normally this is always true, but it's false for dropped columns, as well
8618
8618
* as those that were inherited without any local definition. (If we print
8619
8619
* such a column it will mistakenly get pg_attribute.attislocal set to true.)
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.
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.
8626
8623
*
8627
8624
* This function exists because there are scattered nonobvious places that
8628
8625
* must be kept in sync with this decision.
@@ -8632,9 +8629,7 @@ shouldPrintColumn(DumpOptions *dopt, TableInfo *tbinfo, int colno)
8632
8629
{
8633
8630
if (dopt->binary_upgrade)
8634
8631
return true;
8635
- if (tbinfo->attisdropped[colno])
8636
- return false;
8637
- return (tbinfo->attislocal[colno] || tbinfo->ispartition);
8632
+ return (tbinfo->attislocal[colno] && !tbinfo->attisdropped[colno]);
8638
8633
}
8639
8634
8640
8635
@@ -15543,6 +15538,27 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15543
15538
if (tbinfo->reloftype && !dopt->binary_upgrade)
15544
15539
appendPQExpBuffer(q, " OF %s", tbinfo->reloftype);
15545
15540
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
+
15546
15562
if (tbinfo->relkind != RELKIND_MATVIEW)
15547
15563
{
15548
15564
/* Dump the attributes */
@@ -15571,9 +15587,12 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15571
15587
(!tbinfo->inhNotNull[j] ||
15572
15588
dopt->binary_upgrade));
15573
15589
15574
- /* Skip column if fully defined by reloftype */
15575
- if (tbinfo->reloftype && !has_default && !has_notnull &&
15576
- !dopt->binary_upgrade)
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)
15577
15596
continue;
15578
15597
15579
15598
/* Format properly if not first attr */
@@ -15596,16 +15615,20 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15596
15615
* clean things up later.
15597
15616
*/
15598
15617
appendPQExpBufferStr(q, " INTEGER /* dummy */");
15599
- /* and skip to the next column */
15618
+ /* Skip all the rest, too */
15600
15619
continue;
15601
15620
}
15602
15621
15603
15622
/*
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.
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.
15607
15630
*/
15608
- if (dopt->binary_upgrade || !tbinfo->reloftype)
15631
+ if (dopt->binary_upgrade || ( !tbinfo->reloftype && !tbinfo->ispartition) )
15609
15632
{
15610
15633
appendPQExpBuffer(q, " %s",
15611
15634
tbinfo->atttypnames[j]);
@@ -15655,20 +15678,25 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15655
15678
15656
15679
if (actual_atts)
15657
15680
appendPQExpBufferStr(q, "\n)");
15658
- else if (!(tbinfo->reloftype && !dopt->binary_upgrade))
15681
+ else if (!((tbinfo->reloftype || tbinfo->ispartition) &&
15682
+ !dopt->binary_upgrade))
15659
15683
{
15660
15684
/*
15661
- * No attributes? we must have a parenthesized attribute list,
15662
- * even though empty, when not using the OF TYPE syntax.
15685
+ * We must have a parenthesized attribute list, even though
15686
+ * empty, when not using the OF TYPE or PARTITION OF syntax.
15663
15687
*/
15664
15688
appendPQExpBufferStr(q, " (\n)");
15665
15689
}
15666
15690
15667
- /*
15668
- * Emit the INHERITS clause (not for partitions), except in
15669
- * binary-upgrade mode.
15670
- */
15671
- if (numParents > 0 && !tbinfo->ispartition &&
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 &&
15672
15700
!dopt->binary_upgrade)
15673
15701
{
15674
15702
appendPQExpBufferStr(q, "\nINHERITS (");
@@ -15841,16 +15869,30 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15841
15869
appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
15842
15870
}
15843
15871
15844
- if (numParents > 0 && !tbinfo->ispartition )
15872
+ if (numParents > 0)
15845
15873
{
15846
- appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritance this way.\n");
15874
+ appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritance and partitioning this way.\n");
15847
15875
for (k = 0; k < numParents; k++)
15848
15876
{
15849
15877
TableInfo *parentRel = parents[k];
15850
15878
15851
- appendPQExpBuffer(q, "ALTER TABLE ONLY %s INHERIT %s;\n",
15852
- qualrelname,
15853
- fmtQualifiedDumpable(parentRel));
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));
15854
15896
}
15855
15897
}
15856
15898
@@ -15863,27 +15905,6 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15863
15905
}
15864
15906
}
15865
15907
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\"\n",
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
-
15887
15908
/*
15888
15909
* In binary_upgrade mode, arrange to restore the old relfrozenxid and
15889
15910
* relminmxid of all vacuumable relations. (While vacuum.c processes
0 commit comments