Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita98c48d

Browse files
committed
Make pg_dump emit ATTACH PARTITION instead of PARTITION OF
Using PARTITION OF can result in column ordering being changed from thedatabase being dumped, if the partition uses a column layout differentfrom the parent's. It's not pg_dump's job to editorialize on tabledefinitions, so this is not acceptable; back-patch all the way back topg10, where partitioned tables where introduced.This change also ensures that partitions end up in the correcttablespace, if different from the parent's; this is an oversight inca41030 (in pg12 only). Partitioned indexes (in pg11) don't havethis problem, because they're already created as independent indexes andattached to their parents afterwards.This change also has the advantage that the partition is restorable fromthe dump (as a standalone table) even if its parent table isn'trestored.Author: David RowleyReviewed-by: Álvaro HerreraDiscussion:https://postgr.es/m/CAKJS1f_1c260nOt_vBJ067AZ3JXptXVRohDVMLEBmudX1YEx-A@mail.gmail.comDiscussion:https://postgr.es/m/20190423185007.GA27954@alvherre.pgsql
1 parentf8642fb commita98c48d

File tree

2 files changed

+60
-75
lines changed

2 files changed

+60
-75
lines changed

‎src/bin/pg_dump/pg_dump.c

Lines changed: 51 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -8617,9 +8617,12 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
86178617
* Normally this is always true, but it's false for dropped columns, as well
86188618
* as those that were inherited without any local definition. (If we print
86198619
* 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.
86238626
*
86248627
* This function exists because there are scattered nonobvious places that
86258628
* must be kept in sync with this decision.
@@ -8629,7 +8632,9 @@ shouldPrintColumn(DumpOptions *dopt, TableInfo *tbinfo, int colno)
86298632
{
86308633
if (dopt->binary_upgrade)
86318634
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);
86338638
}
86348639

86358640

@@ -15538,27 +15543,6 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1553815543
if (tbinfo->reloftype && !dopt->binary_upgrade)
1553915544
appendPQExpBuffer(q, " OF %s", tbinfo->reloftype);
1554015545

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-
1556215546
if (tbinfo->relkind != RELKIND_MATVIEW)
1556315547
{
1556415548
/* Dump the attributes */
@@ -15587,12 +15571,9 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1558715571
(!tbinfo->inhNotNull[j] ||
1558815572
dopt->binary_upgrade));
1558915573

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)
1559615577
continue;
1559715578

1559815579
/* Format properly if not first attr */
@@ -15615,20 +15596,16 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1561515596
* clean things up later.
1561615597
*/
1561715598
appendPQExpBufferStr(q, " INTEGER /* dummy */");
15618-
/*Skip alltherest, too */
15599+
/*and skip tothenext column */
1561915600
continue;
1562015601
}
1562115602

1562215603
/*
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.
1563015607
*/
15631-
if (dopt->binary_upgrade ||(!tbinfo->reloftype && !tbinfo->ispartition))
15608+
if (dopt->binary_upgrade || !tbinfo->reloftype)
1563215609
{
1563315610
appendPQExpBuffer(q, " %s",
1563415611
tbinfo->atttypnames[j]);
@@ -15678,25 +15655,20 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1567815655

1567915656
if (actual_atts)
1568015657
appendPQExpBufferStr(q, "\n)");
15681-
else if (!((tbinfo->reloftype || tbinfo->ispartition) &&
15682-
!dopt->binary_upgrade))
15658+
else if (!(tbinfo->reloftype && !dopt->binary_upgrade))
1568315659
{
1568415660
/*
15685-
*Wemust have a parenthesized attribute list, even though
15686-
* empty, when not using the OF TYPE or PARTITION OF syntax.
15661+
*No attributes? wemust have a parenthesized attribute list,
15662+
*even thoughempty, when not using the OF TYPE syntax.
1568715663
*/
1568815664
appendPQExpBufferStr(q, " (\n)");
1568915665
}
1569015666

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 &&
1570015672
!dopt->binary_upgrade)
1570115673
{
1570215674
appendPQExpBufferStr(q, "\nINHERITS (");
@@ -15869,30 +15841,16 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1586915841
appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
1587015842
}
1587115843

15872-
if (numParents > 0)
15844+
if (numParents > 0 && !tbinfo->ispartition)
1587315845
{
15874-
appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritanceand partitioningthis way.\n");
15846+
appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritance this way.\n");
1587515847
for (k = 0; k < numParents; k++)
1587615848
{
1587715849
TableInfo *parentRel = parents[k];
1587815850

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));
1589615854
}
1589715855
}
1589815856

@@ -15905,6 +15863,27 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1590515863
}
1590615864
}
1590715865

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+
1590815887
/*
1590915888
* In binary_upgrade mode, arrange to restore the old relfrozenxid and
1591015889
* relminmxid of all vacuumable relations. (While vacuum.c processes

‎src/bin/pg_dump/t/002_pg_dump.pl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,12 @@
732732
\QALTER TABLE ONLY dump_test.measurement ATTACH PARTITION dump_test_second_schema.measurement_y2006m2\E
733733
\QFOR VALUES FROM ('2006-02-01') TO ('2006-03-01');\E\n
734734
/xm,
735-
like=> {binary_upgrade=> 1, },
735+
like=> {
736+
%full_runs,
737+
role=> 1,
738+
section_pre_data=> 1,
739+
binary_upgrade=> 1,
740+
},
736741
},
737742

738743
'ALTER TABLE test_table CLUSTER ON test_table_pkey'=> {
@@ -2350,12 +2355,13 @@
23502355
\QCREATE TABLE dump_test_second_schema.measurement_y2006m2 PARTITION OF dump_test.measurement\E\n
23512356
\QFOR VALUES FROM ('2006-02-01') TO ('2006-03-01');\E\n
23522357
/xm,
2353-
like => {
2358+
like => {},
2359+
unlike => {
23542360
%full_runs,
23552361
role => 1,
23562362
section_pre_data => 1,
2363+
binary_upgrade => 1,
23572364
},
2358-
unlike => { binary_upgrade => 1, },
23592365
},
23602366
23612367
'CREATE TABLE test_fourth_table_zero_col' => {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp