Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f46bee3

Browse files
committed
Fix dumps of partitioned tables with table AMs
pg_dump/restore failed to properly set the table access method for partitioned tables, as it relies on SET queries that would change default_table_access_method. However, SET affects only tables and materialized views, not partitioned tables which would always be restored with their pg_class.relam set to 0, losing their table AM set by either a CREATE TABLE .. USING or by a ALTER TABLE .. SET ACCESS METHOD. Appending a USING clause to the definition of CREATE TABLE is not possible as users may specify --no-table-access-method at restore or for a dump, meaning that the table AM portions may have to be skipped. Rather than SET, the solution used by this commit is to generate an extra ALTER TABLE .. SET ACCESS METHOD when restoring a partitioned table, based on the table AM set in its TOC entry. The choice of using a SET query or an ALTER TABLE query for a relation requires the addition of the relkind to the TOC entry to be able to choose between one or the other. Note that using ALTER TABLE SET ACCESS METHOD on a relation with physical storage would require a full rewrite, which would be costly for one. This also creates problems with binary upgrades where the rewrite would not be able to keep the OID of the relation consistent across the upgrade. This commit would normally require a protocol bump, but a45c78e has already done one for this release cycle. Regression tests are adjusted with the new expected output, with some tweaks for the table AMs of the partitions to make the output more readable. Issue introduced by 374c7a2, that has added support for table AMs in partitioned tables. Author: Michael Paquier Reviewed-by: Álvaro Herrera Discussion: https://postgr.es/m/Zh4JLSvvtQgBJZkZ@paquier.xyz
1 parent eff6a75 commit f46bee3

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
static void _selectOutputSchema(ArchiveHandle *AH, const char *schemaName);
6364
static void _selectTablespace(ArchiveHandle *AH, const char *tablespace);
6465
static void _selectTableAccessMethod(ArchiveHandle *AH, const char *tableam);
66+
static void _printTableAccessMethodNoStorage(ArchiveHandle *AH,
67+
TocEntry *te);
6568
static void processEncodingEntry(ArchiveHandle *AH, TocEntry *te);
6669
static void processStdStringsEntry(ArchiveHandle *AH, TocEntry *te);
6770
static void processSearchPathEntry(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+
static void
3583+
_printTableAccessMethodNoStorage(ArchiveHandle *AH, TocEntry *te)
3584+
{
3585+
RestoreOptions *ropt = AH->public.ropt;
3586+
const char *tableam = te->tableam;
3587+
PQExpBuffer cmd;
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
#define K_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
#define K_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+
char relkind; /* relation kind, only for TABLE tags */
356358
char *owner;
357359
char *desc;
358360
char *defn;
@@ -393,6 +395,7 @@ typedef struct _archiveOpts
393395
const char *namespace;
394396
const char *tablespace;
395397
const char *tableam;
398+
char relkind;
396399
const char *owner;
397400
const char *description;
398401
teSection section;

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)