@@ -8617,9 +8617,12 @@ 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
- * 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.
8623
8626
*
8624
8627
* This function exists because there are scattered nonobvious places that
8625
8628
* must be kept in sync with this decision.
@@ -8629,7 +8632,9 @@ shouldPrintColumn(DumpOptions *dopt, TableInfo *tbinfo, int colno)
8629
8632
{
8630
8633
if (dopt->binary_upgrade)
8631
8634
return true;
8632
- return (tbinfo->attislocal[colno] && !tbinfo->attisdropped[colno]);
8635
+ if (tbinfo->attisdropped[colno])
8636
+ return false;
8637
+ return (tbinfo->attislocal[colno] || tbinfo->ispartition);
8633
8638
}
8634
8639
8635
8640
@@ -15538,27 +15543,6 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15538
15543
if (tbinfo->reloftype && !dopt->binary_upgrade)
15539
15544
appendPQExpBuffer(q, " OF %s", tbinfo->reloftype);
15540
15545
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
-
15562
15546
if (tbinfo->relkind != RELKIND_MATVIEW)
15563
15547
{
15564
15548
/* Dump the attributes */
@@ -15587,12 +15571,9 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15587
15571
(!tbinfo->inhNotNull[j] ||
15588
15572
dopt->binary_upgrade));
15589
15573
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)
15596
15577
continue;
15597
15578
15598
15579
/* Format properly if not first attr */
@@ -15615,20 +15596,16 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15615
15596
* clean things up later.
15616
15597
*/
15617
15598
appendPQExpBufferStr(q, " INTEGER /* dummy */");
15618
- /*Skip all therest, too */
15599
+ /*and skip to thenext column */
15619
15600
continue;
15620
15601
}
15621
15602
15622
15603
/*
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.
15630
15607
*/
15631
- if (dopt->binary_upgrade ||( !tbinfo->reloftype && !tbinfo->ispartition) )
15608
+ if (dopt->binary_upgrade || !tbinfo->reloftype)
15632
15609
{
15633
15610
appendPQExpBuffer(q, " %s",
15634
15611
tbinfo->atttypnames[j]);
@@ -15678,25 +15655,20 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15678
15655
15679
15656
if (actual_atts)
15680
15657
appendPQExpBufferStr(q, "\n)");
15681
- else if (!((tbinfo->reloftype || tbinfo->ispartition) &&
15682
- !dopt->binary_upgrade))
15658
+ else if (!(tbinfo->reloftype && !dopt->binary_upgrade))
15683
15659
{
15684
15660
/*
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.
15687
15663
*/
15688
15664
appendPQExpBufferStr(q, " (\n)");
15689
15665
}
15690
15666
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 &&
15700
15672
!dopt->binary_upgrade)
15701
15673
{
15702
15674
appendPQExpBufferStr(q, "\nINHERITS (");
@@ -15869,30 +15841,16 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15869
15841
appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
15870
15842
}
15871
15843
15872
- if (numParents > 0)
15844
+ if (numParents > 0 && !tbinfo->ispartition )
15873
15845
{
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");
15875
15847
for (k = 0; k < numParents; k++)
15876
15848
{
15877
15849
TableInfo *parentRel = parents[k];
15878
15850
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));
15896
15854
}
15897
15855
}
15898
15856
@@ -15905,6 +15863,27 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15905
15863
}
15906
15864
}
15907
15865
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
+
15908
15887
/*
15909
15888
* In binary_upgrade mode, arrange to restore the old relfrozenxid and
15910
15889
* relminmxid of all vacuumable relations. (While vacuum.c processes