@@ -8638,9 +8638,12 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
8638
8638
* Normally this is always true, but it's false for dropped columns, as well
8639
8639
* as those that were inherited without any local definition. (If we print
8640
8640
* such a column it will mistakenly get pg_attribute.attislocal set to true.)
8641
- * However, in binary_upgrade mode, we must print all such columns anyway and
8642
- * fix the attislocal/attisdropped state later, so as to keep control of the
8643
- * physical column order.
8641
+ * For partitions, it's always true, because we want the partitions to be
8642
+ * created independently and ATTACH PARTITION used afterwards.
8643
+ *
8644
+ * In binary_upgrade mode, we must print all columns and fix the attislocal/
8645
+ * attisdropped state later, so as to keep control of the physical column
8646
+ * order.
8644
8647
*
8645
8648
* This function exists because there are scattered nonobvious places that
8646
8649
* must be kept in sync with this decision.
@@ -8650,7 +8653,9 @@ shouldPrintColumn(DumpOptions *dopt, TableInfo *tbinfo, int colno)
8650
8653
{
8651
8654
if (dopt->binary_upgrade)
8652
8655
return true;
8653
- return (tbinfo->attislocal[colno] && !tbinfo->attisdropped[colno]);
8656
+ if (tbinfo->attisdropped[colno])
8657
+ return false;
8658
+ return (tbinfo->attislocal[colno] || tbinfo->ispartition);
8654
8659
}
8655
8660
8656
8661
@@ -15559,27 +15564,6 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15559
15564
if (tbinfo->reloftype && !dopt->binary_upgrade)
15560
15565
appendPQExpBuffer(q, " OF %s", tbinfo->reloftype);
15561
15566
15562
- /*
15563
- * If the table is a partition, dump it as such; except in the case of
15564
- * a binary upgrade, we dump the table normally and attach it to the
15565
- * parent afterward.
15566
- */
15567
- if (tbinfo->ispartition && !dopt->binary_upgrade)
15568
- {
15569
- TableInfo *parentRel = tbinfo->parents[0];
15570
-
15571
- /*
15572
- * With partitions, unlike inheritance, there can only be one
15573
- * parent.
15574
- */
15575
- if (tbinfo->numParents != 1)
15576
- exit_horribly(NULL, "invalid number of parents %d for table \"%s\"\n",
15577
- tbinfo->numParents, tbinfo->dobj.name);
15578
-
15579
- appendPQExpBuffer(q, " PARTITION OF %s",
15580
- fmtQualifiedDumpable(parentRel));
15581
- }
15582
-
15583
15567
if (tbinfo->relkind != RELKIND_MATVIEW)
15584
15568
{
15585
15569
/* Dump the attributes */
@@ -15594,26 +15578,30 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15594
15578
*/
15595
15579
if (shouldPrintColumn(dopt, tbinfo, j))
15596
15580
{
15581
+ bool print_default;
15582
+ bool print_notnull;
15583
+
15597
15584
/*
15598
15585
* Default value --- suppress if to be printed separately.
15599
15586
*/
15600
- bool has_default = (tbinfo->attrdefs[j] != NULL &&
15601
- !tbinfo->attrdefs[j]->separate);
15587
+ print_default = (tbinfo->attrdefs[j] != NULL &&
15588
+ !tbinfo->attrdefs[j]->separate);
15602
15589
15603
15590
/*
15604
15591
* Not Null constraint --- suppress if inherited, except
15605
- * in binary-upgrade case where that won't work.
15592
+ * if partition, or in binary-upgrade case where that
15593
+ * won't work.
15606
15594
*/
15607
- bool has_notnull = (tbinfo->notnull[j] &&
15608
- (!tbinfo->inhNotNull[j] ||
15609
- dopt->binary_upgrade));
15595
+ print_notnull = (tbinfo->notnull[j] &&
15596
+ (!tbinfo->inhNotNull[j] ||
15597
+ tbinfo->ispartition || dopt->binary_upgrade));
15610
15598
15611
15599
/*
15612
- * Skip column if fully defined by reloftype or the
15613
- * partition parent.
15600
+ * Skip column if fully defined by reloftype, except in
15601
+ * binary upgrade
15614
15602
*/
15615
- if (( tbinfo->reloftype || tbinfo->ispartition) &&
15616
- !has_default && !has_notnull && ! dopt->binary_upgrade)
15603
+ if (tbinfo->reloftype && !print_default && !print_notnull &&
15604
+ !dopt->binary_upgrade)
15617
15605
continue;
15618
15606
15619
15607
/* Format properly if not first attr */
@@ -15636,20 +15624,16 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15636
15624
* clean things up later.
15637
15625
*/
15638
15626
appendPQExpBufferStr(q, " INTEGER /* dummy */");
15639
- /* Skip all the rest, too */
15627
+ /* and skip to the next column */
15640
15628
continue;
15641
15629
}
15642
15630
15643
15631
/*
15644
- * Attribute type
15645
- *
15646
- * In binary-upgrade mode, we always include the type. If
15647
- * we aren't in binary-upgrade mode, then we skip the type
15648
- * when creating a typed table ('OF type_name') or a
15649
- * partition ('PARTITION OF'), since the type comes from
15650
- * the parent/partitioned table.
15632
+ * Attribute type; print it except when creating a typed
15633
+ * table ('OF type_name'), but in binary-upgrade mode,
15634
+ * print it in that case too.
15651
15635
*/
15652
- if (dopt->binary_upgrade || ( !tbinfo->reloftype && !tbinfo->ispartition) )
15636
+ if (dopt->binary_upgrade || !tbinfo->reloftype)
15653
15637
{
15654
15638
appendPQExpBuffer(q, " %s",
15655
15639
tbinfo->atttypnames[j]);
@@ -15666,23 +15650,29 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15666
15650
fmtQualifiedDumpable(coll));
15667
15651
}
15668
15652
15669
- if (has_default )
15653
+ if (print_default )
15670
15654
appendPQExpBuffer(q, " DEFAULT %s",
15671
15655
tbinfo->attrdefs[j]->adef_expr);
15672
15656
15673
- if (has_notnull )
15657
+ if (print_notnull )
15674
15658
appendPQExpBufferStr(q, " NOT NULL");
15675
15659
}
15676
15660
}
15677
15661
15678
15662
/*
15679
15663
* Add non-inherited CHECK constraints, if any.
15664
+ *
15665
+ * For partitions, we need to include check constraints even if
15666
+ * they're not defined locally, because the ALTER TABLE ATTACH
15667
+ * PARTITION that we'll emit later expects the constraint to be
15668
+ * there. (No need to fix conislocal: ATTACH PARTITION does that)
15680
15669
*/
15681
15670
for (j = 0; j < tbinfo->ncheck; j++)
15682
15671
{
15683
15672
ConstraintInfo *constr = &(tbinfo->checkexprs[j]);
15684
15673
15685
- if (constr->separate || !constr->conislocal)
15674
+ if (constr->separate ||
15675
+ (!constr->conislocal && !tbinfo->ispartition))
15686
15676
continue;
15687
15677
15688
15678
if (actual_atts == 0)
@@ -15699,25 +15689,20 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15699
15689
15700
15690
if (actual_atts)
15701
15691
appendPQExpBufferStr(q, "\n)");
15702
- else if (!((tbinfo->reloftype || tbinfo->ispartition) &&
15703
- !dopt->binary_upgrade))
15692
+ else if (!(tbinfo->reloftype && !dopt->binary_upgrade))
15704
15693
{
15705
15694
/*
15706
- * We must have a parenthesized attribute list, even though
15707
- * empty, when not using the OF TYPE or PARTITION OF syntax.
15695
+ * No attributes? we must have a parenthesized attribute list,
15696
+ * even though empty, when not using the OF TYPE syntax.
15708
15697
*/
15709
15698
appendPQExpBufferStr(q, " (\n)");
15710
15699
}
15711
15700
15712
- if (tbinfo->ispartition && !dopt->binary_upgrade)
15713
- {
15714
- appendPQExpBufferChar(q, '\n');
15715
- appendPQExpBufferStr(q, tbinfo->partbound);
15716
- }
15717
-
15718
- /* Emit the INHERITS clause, except if this is a partition. */
15719
- if (numParents > 0 &&
15720
- !tbinfo->ispartition &&
15701
+ /*
15702
+ * Emit the INHERITS clause (not for partitions), except in
15703
+ * binary-upgrade mode.
15704
+ */
15705
+ if (numParents > 0 && !tbinfo->ispartition &&
15721
15706
!dopt->binary_upgrade)
15722
15707
{
15723
15708
appendPQExpBufferStr(q, "\nINHERITS (");
@@ -15868,11 +15853,17 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15868
15853
}
15869
15854
}
15870
15855
15856
+ /*
15857
+ * Add inherited CHECK constraints, if any.
15858
+ *
15859
+ * For partitions, they were already dumped, and conislocal
15860
+ * doesn't need fixing.
15861
+ */
15871
15862
for (k = 0; k < tbinfo->ncheck; k++)
15872
15863
{
15873
15864
ConstraintInfo *constr = &(tbinfo->checkexprs[k]);
15874
15865
15875
- if (constr->separate || constr->conislocal)
15866
+ if (constr->separate || constr->conislocal || tbinfo->ispartition )
15876
15867
continue;
15877
15868
15878
15869
appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inherited constraint.\n");
@@ -15890,30 +15881,16 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15890
15881
appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
15891
15882
}
15892
15883
15893
- if (numParents > 0)
15884
+ if (numParents > 0 && !tbinfo->ispartition )
15894
15885
{
15895
- appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritance and partitioning this way.\n");
15886
+ appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritance this way.\n");
15896
15887
for (k = 0; k < numParents; k++)
15897
15888
{
15898
15889
TableInfo *parentRel = parents[k];
15899
15890
15900
- /* In the partitioning case, we alter the parent */
15901
- if (tbinfo->ispartition)
15902
- appendPQExpBuffer(q,
15903
- "ALTER TABLE ONLY %s ATTACH PARTITION ",
15904
- fmtQualifiedDumpable(parentRel));
15905
- else
15906
- appendPQExpBuffer(q, "ALTER TABLE ONLY %s INHERIT ",
15907
- qualrelname);
15908
-
15909
- /* Partition needs specifying the bounds */
15910
- if (tbinfo->ispartition)
15911
- appendPQExpBuffer(q, "%s %s;\n",
15912
- qualrelname,
15913
- tbinfo->partbound);
15914
- else
15915
- appendPQExpBuffer(q, "%s;\n",
15916
- fmtQualifiedDumpable(parentRel));
15891
+ appendPQExpBuffer(q, "ALTER TABLE ONLY %s INHERIT %s;\n",
15892
+ qualrelname,
15893
+ fmtQualifiedDumpable(parentRel));
15917
15894
}
15918
15895
}
15919
15896
@@ -15926,6 +15903,27 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15926
15903
}
15927
15904
}
15928
15905
15906
+ /*
15907
+ * For partitioned tables, emit the ATTACH PARTITION clause. Note
15908
+ * that we always want to create partitions this way instead of using
15909
+ * CREATE TABLE .. PARTITION OF, mainly to preserve a possible column
15910
+ * layout discrepancy with the parent, but also to ensure it gets the
15911
+ * correct tablespace setting if it differs from the parent's.
15912
+ */
15913
+ if (tbinfo->ispartition)
15914
+ {
15915
+ /* With partitions there can only be one parent */
15916
+ if (tbinfo->numParents != 1)
15917
+ exit_horribly(NULL, "invalid number of parents %d for table \"%s\"\n",
15918
+ tbinfo->numParents, tbinfo->dobj.name);
15919
+
15920
+ /* Perform ALTER TABLE on the parent */
15921
+ appendPQExpBuffer(q,
15922
+ "ALTER TABLE ONLY %s ATTACH PARTITION %s %s;\n",
15923
+ fmtQualifiedDumpable(parents[0]),
15924
+ qualrelname, tbinfo->partbound);
15925
+ }
15926
+
15929
15927
/*
15930
15928
* In binary_upgrade mode, arrange to restore the old relfrozenxid and
15931
15929
* relminmxid of all vacuumable relations. (While vacuum.c processes
0 commit comments