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

Commitf46bee3

Browse files
committed
Fix dumps of partitioned tables with table AMs
pg_dump/restore failed to properly set the table access method forpartitioned tables, as it relies on SET queries that would changedefault_table_access_method. However, SET affects only tables andmaterialized views, not partitioned tables which would always berestored with their pg_class.relam set to 0, losing their table AM setby either a CREATE TABLE .. USING or by a ALTER TABLE .. SET ACCESSMETHOD.Appending a USING clause to the definition of CREATE TABLE is notpossible as users may specify --no-table-access-method at restore or fora dump, meaning that the table AM portions may have to be skipped.Rather than SET, the solution used by this commit is to generate anextra ALTER TABLE .. SET ACCESS METHOD when restoring a partitionedtable, based on the table AM set in its TOC entry. The choice of usinga SET query or an ALTER TABLE query for a relation requires the additionof the relkind to the TOC entry to be able to choose between one or theother. Note that using ALTER TABLE SET ACCESS METHOD on a relation withphysical storage would require a full rewrite, which would be costly forone. This also creates problems with binary upgrades where the rewritewould not be able to keep the OID of the relation consistent across theupgrade.This commit would normally require a protocol bump, buta45c78e hasalready done one for this release cycle.Regression tests are adjusted with the new expected output, with sometweaks for the table AMs of the partitions to make the output morereadable.Issue introduced by374c7a2, that has added support for table AMsin partitioned tables.Author: Michael PaquierReviewed-by: Álvaro HerreraDiscussion:https://postgr.es/m/Zh4JLSvvtQgBJZkZ@paquier.xyz
1 parenteff6a75 commitf46bee3

File tree

4 files changed

+79
-9
lines changed

4 files changed

+79
-9
lines changed

‎src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include<io.h>
3131
#endif
3232

33+
#include"catalog/pg_class_d.h"
3334
#include"common/string.h"
3435
#include"compress_io.h"
3536
#include"dumputils.h"
@@ -62,6 +63,8 @@ static void _becomeOwner(ArchiveHandle *AH, TocEntry *te);
6263
staticvoid_selectOutputSchema(ArchiveHandle*AH,constchar*schemaName);
6364
staticvoid_selectTablespace(ArchiveHandle*AH,constchar*tablespace);
6465
staticvoid_selectTableAccessMethod(ArchiveHandle*AH,constchar*tableam);
66+
staticvoid_printTableAccessMethodNoStorage(ArchiveHandle*AH,
67+
TocEntry*te);
6568
staticvoidprocessEncodingEntry(ArchiveHandle*AH,TocEntry*te);
6669
staticvoidprocessStdStringsEntry(ArchiveHandle*AH,TocEntry*te);
6770
staticvoidprocessSearchPathEntry(ArchiveHandle*AH,TocEntry*te);
@@ -1222,6 +1225,7 @@ ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId,
12221225
newToc->namespace=opts->namespace ?pg_strdup(opts->namespace) :NULL;
12231226
newToc->tablespace=opts->tablespace ?pg_strdup(opts->tablespace) :NULL;
12241227
newToc->tableam=opts->tableam ?pg_strdup(opts->tableam) :NULL;
1228+
newToc->relkind=opts->relkind;
12251229
newToc->owner=opts->owner ?pg_strdup(opts->owner) :NULL;
12261230
newToc->desc=pg_strdup(opts->description);
12271231
newToc->defn=opts->createStmt ?pg_strdup(opts->createStmt) :NULL;
@@ -2602,6 +2606,7 @@ WriteToc(ArchiveHandle *AH)
26022606
WriteStr(AH,te->namespace);
26032607
WriteStr(AH,te->tablespace);
26042608
WriteStr(AH,te->tableam);
2609+
WriteInt(AH,te->relkind);
26052610
WriteStr(AH,te->owner);
26062611
WriteStr(AH,"false");
26072612

@@ -2707,6 +2712,9 @@ ReadToc(ArchiveHandle *AH)
27072712
if (AH->version >=K_VERS_1_14)
27082713
te->tableam=ReadStr(AH);
27092714

2715+
if (AH->version >=K_VERS_1_16)
2716+
te->relkind=ReadInt(AH);
2717+
27102718
te->owner=ReadStr(AH);
27112719
is_supported= true;
27122720
if (AH->version<K_VERS_1_9)
@@ -3567,6 +3575,51 @@ _selectTableAccessMethod(ArchiveHandle *AH, const char *tableam)
35673575
AH->currTableAm=pg_strdup(want);
35683576
}
35693577

3578+
/*
3579+
* Set the proper default table access method for a table without storage.
3580+
* Currently, this is required only for partitioned tables with a table AM.
3581+
*/
3582+
staticvoid
3583+
_printTableAccessMethodNoStorage(ArchiveHandle*AH,TocEntry*te)
3584+
{
3585+
RestoreOptions*ropt=AH->public.ropt;
3586+
constchar*tableam=te->tableam;
3587+
PQExpBuffercmd;
3588+
3589+
/* do nothing in --no-table-access-method mode */
3590+
if (ropt->noTableAm)
3591+
return;
3592+
3593+
if (!tableam)
3594+
return;
3595+
3596+
Assert(te->relkind==RELKIND_PARTITIONED_TABLE);
3597+
3598+
cmd=createPQExpBuffer();
3599+
3600+
appendPQExpBufferStr(cmd,"ALTER TABLE ");
3601+
appendPQExpBuffer(cmd,"%s ",fmtQualifiedId(te->namespace,te->tag));
3602+
appendPQExpBuffer(cmd,"SET ACCESS METHOD %s;",
3603+
fmtId(tableam));
3604+
3605+
if (RestoringToDB(AH))
3606+
{
3607+
PGresult*res;
3608+
3609+
res=PQexec(AH->connection,cmd->data);
3610+
3611+
if (!res||PQresultStatus(res)!=PGRES_COMMAND_OK)
3612+
warn_or_exit_horribly(AH,
3613+
"could not alter table access method: %s",
3614+
PQerrorMessage(AH->connection));
3615+
PQclear(res);
3616+
}
3617+
else
3618+
ahprintf(AH,"%s\n\n",cmd->data);
3619+
3620+
destroyPQExpBuffer(cmd);
3621+
}
3622+
35703623
/*
35713624
* Extract an object description for a TOC entry, and append it to buf.
35723625
*
@@ -3673,11 +3726,17 @@ _printTocEntry(ArchiveHandle *AH, TocEntry *te, bool isData)
36733726
{
36743727
RestoreOptions*ropt=AH->public.ropt;
36753728

3676-
/* Select owner, schema, tablespace and default AM as necessary */
3729+
/*
3730+
* Select owner, schema, tablespace and default AM as necessary. The
3731+
* default access method for partitioned tables is handled after
3732+
* generating the object definition, as it requires an ALTER command
3733+
* rather than SET.
3734+
*/
36773735
_becomeOwner(AH,te);
36783736
_selectOutputSchema(AH,te->namespace);
36793737
_selectTablespace(AH,te->tablespace);
3680-
_selectTableAccessMethod(AH,te->tableam);
3738+
if (te->relkind!=RELKIND_PARTITIONED_TABLE)
3739+
_selectTableAccessMethod(AH,te->tableam);
36813740

36823741
/* Emit header comment for item */
36833742
if (!AH->noTocComments)
@@ -3812,6 +3871,13 @@ _printTocEntry(ArchiveHandle *AH, TocEntry *te, bool isData)
38123871
}
38133872
}
38143873

3874+
/*
3875+
* Select a partitioned table's default AM, once the table definition has
3876+
* been generated.
3877+
*/
3878+
if (te->relkind==RELKIND_PARTITIONED_TABLE)
3879+
_printTableAccessMethodNoStorage(AH,te);
3880+
38153881
/*
38163882
* If it's an ACL entry, it might contain SET SESSION AUTHORIZATION
38173883
* commands, so we can no longer assume we know the current auth setting.

‎src/bin/pg_dump/pg_backup_archiver.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@
6969
* compression_algorithm
7070
* in header */
7171
#defineK_VERS_1_16 MAKE_ARCHIVE_VERSION(1, 16, 0)/* BLOB METADATA entries
72-
* and multiple BLOBS */
72+
* and multiple BLOBS,
73+
* relkind */
7374

7475
/* Current archive version number (the format we can output) */
7576
#defineK_VERS_MAJOR 1
@@ -353,6 +354,7 @@ struct _tocEntry
353354
char*tablespace;/* null if not in a tablespace; empty string
354355
* means use database default */
355356
char*tableam;/* table access method, only for TABLE tags */
357+
charrelkind;/* relation kind, only for TABLE tags */
356358
char*owner;
357359
char*desc;
358360
char*defn;
@@ -393,6 +395,7 @@ typedef struct _archiveOpts
393395
constchar*namespace;
394396
constchar*tablespace;
395397
constchar*tableam;
398+
charrelkind;
396399
constchar*owner;
397400
constchar*description;
398401
teSectionsection;

‎src/bin/pg_dump/pg_dump.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16758,6 +16758,7 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo)
1675816758
.namespace = tbinfo->dobj.namespace->dobj.name,
1675916759
.tablespace = tablespace,
1676016760
.tableam = tableam,
16761+
.relkind = tbinfo->relkind,
1676116762
.owner = tbinfo->rolname,
1676216763
.description = reltypename,
1676316764
.section = tbinfo->postponed_def ?

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4587,19 +4587,19 @@
45874587
CREATE TABLE dump_test.regress_pg_dump_table_am_parent (id int) PARTITION BY LIST (id);
45884588
ALTER TABLE dump_test.regress_pg_dump_table_am_parent SET ACCESS METHOD regress_table_am;
45894589
CREATE TABLE dump_test.regress_pg_dump_table_am_child_1
4590-
PARTITION OF dump_test.regress_pg_dump_table_am_parent FOR VALUES IN (1) USING heap;
4590+
PARTITION OF dump_test.regress_pg_dump_table_am_parent FOR VALUES IN (1);
45914591
CREATE TABLE dump_test.regress_pg_dump_table_am_child_2
4592-
PARTITION OF dump_test.regress_pg_dump_table_am_parent FOR VALUES IN (2);',
4592+
PARTITION OF dump_test.regress_pg_dump_table_am_parent FOR VALUES IN (2) USING heap;',
45934593
regexp => qr/^
4594-
\QSET default_table_access_method = regress_table_am;\E
4595-
(\n(?!SET[^;]+;)[^\n]*)*
45964594
\n\QCREATE TABLE dump_test.regress_pg_dump_table_am_parent (\E
4595+
(\n(?!SET[^;]+;)[^\n]*)*
4596+
\QALTER TABLE dump_test.regress_pg_dump_table_am_parent SET ACCESS METHOD regress_table_am;\E
45974597
(.*\n)*
4598-
\QSET default_table_access_method =heap;\E
4598+
\QSET default_table_access_method =regress_table_am;\E
45994599
(\n(?!SET[^;]+;)[^\n]*)*
46004600
\n\QCREATE TABLE dump_test.regress_pg_dump_table_am_child_1 (\E
46014601
(.*\n)*
4602-
\QSET default_table_access_method =regress_table_am;\E
4602+
\QSET default_table_access_method =heap;\E
46034603
(\n(?!SET[^;]+;)[^\n]*)*
46044604
\n\QCREATE TABLE dump_test.regress_pg_dump_table_am_child_2 (\E
46054605
(.*\n)*/xm,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp