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

Commit 4de36b1

Browse files
michaelpqCommitfest Bot
authored and
Commitfest Bot
committed
Sequence access methods - dump/restore support
1 parent 165009a commit 4de36b1

File tree

10 files changed

+207
-11
lines changed

10 files changed

+207
-11
lines changed

doc/src/sgml/ref/pg_dump.sgml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,23 @@ PostgreSQL documentation
11621162
</listitem>
11631163
</varlistentry>
11641164

1165+
<varlistentry>
1166+
<term><option>--no-sequence-access-method</option></term>
1167+
<listitem>
1168+
<para>
1169+
Do not output commands to select sequence access methods.
1170+
With this option, all objects will be created with whichever
1171+
sequence access method is the default during restore.
1172+
</para>
1173+
1174+
<para>
1175+
This option is ignored when emitting an archive (non-text) output
1176+
file. For the archive formats, you can specify the option when you
1177+
call <command>pg_restore</command>.
1178+
</para>
1179+
</listitem>
1180+
</varlistentry>
1181+
11651182
<varlistentry>
11661183
<term><option>--no-table-access-method</option></term>
11671184
<listitem>

doc/src/sgml/ref/pg_dumpall.sgml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,17 @@ exclude database <replaceable class="parameter">PATTERN</replaceable>
595595
</listitem>
596596
</varlistentry>
597597

598+
<varlistentry>
599+
<term><option>--no-sequence-access-method</option></term>
600+
<listitem>
601+
<para>
602+
Do not output commands to select sequence access methods.
603+
With this option, all objects will be created with whichever
604+
sequence access method is the default during restore.
605+
</para>
606+
</listitem>
607+
</varlistentry>
608+
598609
<varlistentry>
599610
<term><option>--no-table-access-method</option></term>
600611
<listitem>

doc/src/sgml/ref/pg_restore.sgml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,17 @@ PostgreSQL documentation
820820
</listitem>
821821
</varlistentry>
822822

823+
<varlistentry>
824+
<term><option>--no-sequence-access-method</option></term>
825+
<listitem>
826+
<para>
827+
Do not output commands to select sequence access methods.
828+
With this option, all objects will be created with whichever
829+
sequence access method is the default during restore.
830+
</para>
831+
</listitem>
832+
</varlistentry>
833+
823834
<varlistentry>
824835
<term><option>--no-table-access-method</option></term>
825836
<listitem>

src/bin/pg_dump/pg_backup.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ typedef struct _restoreOptions
9797
{
9898
int createDB; /* Issue commands to create the database */
9999
int noOwner; /* Don't try to match original object owner */
100+
int noSequenceAm; /* Don't issue sequence-AM-related commands */
100101
int noTableAm; /* Don't issue table-AM-related commands */
101102
int noTablespace; /* Don't issue tablespace-related commands */
102103
int disable_triggers; /* disable triggers during data-only
@@ -190,6 +191,7 @@ typedef struct _dumpOptions
190191
int no_unlogged_table_data;
191192
int serializable_deferrable;
192193
int disable_triggers;
194+
int outputNoSequenceAm;
193195
int outputNoTableAm;
194196
int outputNoTablespaces;
195197
int use_setsessauth;

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ dumpOptionsFromRestoreOptions(RestoreOptions *ropt)
181181
dopt->outputSuperuser = ropt->superuser;
182182
dopt->outputCreateDB = ropt->createDB;
183183
dopt->outputNoOwner = ropt->noOwner;
184+
dopt->outputNoSequenceAm = ropt->noSequenceAm;
184185
dopt->outputNoTableAm = ropt->noTableAm;
185186
dopt->outputNoTablespaces = ropt->noTablespace;
186187
dopt->disable_triggers = ropt->disable_triggers;
@@ -1246,6 +1247,7 @@ ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId,
12461247
newToc->tag = pg_strdup(opts->tag);
12471248
newToc->namespace = opts->namespace ? pg_strdup(opts->namespace) : NULL;
12481249
newToc->tablespace = opts->tablespace ? pg_strdup(opts->tablespace) : NULL;
1250+
newToc->sequenceam = opts->sequenceam ? pg_strdup(opts->sequenceam) : NULL;
12491251
newToc->tableam = opts->tableam ? pg_strdup(opts->tableam) : NULL;
12501252
newToc->relkind = opts->relkind;
12511253
newToc->owner = opts->owner ? pg_strdup(opts->owner) : NULL;
@@ -2405,6 +2407,7 @@ _allocAH(const char *FileSpec, const ArchiveFormat fmt,
24052407

24062408
AH->currUser = NULL; /* unknown */
24072409
AH->currSchema = NULL; /* ditto */
2410+
AH->currSequenceAm = NULL; /* ditto */
24082411
AH->currTablespace = NULL; /* ditto */
24092412
AH->currTableAm = NULL; /* ditto */
24102413

@@ -2672,6 +2675,7 @@ WriteToc(ArchiveHandle *AH)
26722675
WriteStr(AH, te->copyStmt);
26732676
WriteStr(AH, te->namespace);
26742677
WriteStr(AH, te->tablespace);
2678+
WriteStr(AH, te->sequenceam);
26752679
WriteStr(AH, te->tableam);
26762680
WriteInt(AH, te->relkind);
26772681
WriteStr(AH, te->owner);
@@ -2776,6 +2780,9 @@ ReadToc(ArchiveHandle *AH)
27762780
if (AH->version >= K_VERS_1_10)
27772781
te->tablespace = ReadStr(AH);
27782782

2783+
if (AH->version >= K_VERS_1_17)
2784+
te->sequenceam = ReadStr(AH);
2785+
27792786
if (AH->version >= K_VERS_1_14)
27802787
te->tableam = ReadStr(AH);
27812788

@@ -3462,6 +3469,9 @@ _reconnectToDB(ArchiveHandle *AH, const char *dbname)
34623469
free(AH->currSchema);
34633470
AH->currSchema = NULL;
34643471

3472+
free(AH->currSequenceAm);
3473+
AH->currSequenceAm = NULL;
3474+
34653475
free(AH->currTableAm);
34663476
AH->currTableAm = NULL;
34673477

@@ -3624,6 +3634,57 @@ _selectTablespace(ArchiveHandle *AH, const char *tablespace)
36243634
destroyPQExpBuffer(qry);
36253635
}
36263636

3637+
/*
3638+
* Set the proper default_sequence_access_method value for the sequence.
3639+
*/
3640+
static void
3641+
_selectSequenceAccessMethod(ArchiveHandle *AH, const char *sequenceam)
3642+
{
3643+
RestoreOptions *ropt = AH->public.ropt;
3644+
PQExpBuffer cmd;
3645+
const char *want,
3646+
*have;
3647+
3648+
/* do nothing in --no-sequence-access-method mode */
3649+
if (ropt->noSequenceAm)
3650+
return;
3651+
3652+
have = AH->currSequenceAm;
3653+
want = sequenceam;
3654+
3655+
if (!want)
3656+
return;
3657+
3658+
if (have && strcmp(want, have) == 0)
3659+
return;
3660+
3661+
cmd = createPQExpBuffer();
3662+
appendPQExpBuffer(cmd,
3663+
"SET default_sequence_access_method = %s;",
3664+
fmtId(want));
3665+
3666+
if (RestoringToDB(AH))
3667+
{
3668+
PGresult *res;
3669+
3670+
res = PQexec(AH->connection, cmd->data);
3671+
3672+
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
3673+
warn_or_exit_horribly(AH,
3674+
"could not set default_sequence_access_method: %s",
3675+
PQerrorMessage(AH->connection));
3676+
3677+
PQclear(res);
3678+
}
3679+
else
3680+
ahprintf(AH, "%s\n\n", cmd->data);
3681+
3682+
destroyPQExpBuffer(cmd);
3683+
3684+
free(AH->currSequenceAm);
3685+
AH->currSequenceAm = pg_strdup(want);
3686+
}
3687+
36273688
/*
36283689
* Set the proper default_table_access_method value for the table.
36293690
*/
@@ -3833,6 +3894,7 @@ _printTocEntry(ArchiveHandle *AH, TocEntry *te, const char *pfx)
38333894
_becomeOwner(AH, te);
38343895
_selectOutputSchema(AH, te->namespace);
38353896
_selectTablespace(AH, te->tablespace);
3897+
_selectSequenceAccessMethod(AH, te->sequenceam);
38363898
if (te->relkind != RELKIND_PARTITIONED_TABLE)
38373899
_selectTableAccessMethod(AH, te->tableam);
38383900

@@ -4389,6 +4451,8 @@ restore_toc_entries_prefork(ArchiveHandle *AH, TocEntry *pending_list)
43894451
AH->currUser = NULL;
43904452
free(AH->currSchema);
43914453
AH->currSchema = NULL;
4454+
free(AH->currSequenceAm);
4455+
AH->currSequenceAm = NULL;
43924456
free(AH->currTablespace);
43934457
AH->currTablespace = NULL;
43944458
free(AH->currTableAm);
@@ -5128,6 +5192,7 @@ CloneArchive(ArchiveHandle *AH)
51285192
clone->connCancel = NULL;
51295193
clone->currUser = NULL;
51305194
clone->currSchema = NULL;
5195+
clone->currSequenceAm = NULL;
51315196
clone->currTableAm = NULL;
51325197
clone->currTablespace = NULL;
51335198

@@ -5187,6 +5252,7 @@ DeCloneArchive(ArchiveHandle *AH)
51875252
/* Clear any connection-local state */
51885253
free(AH->currUser);
51895254
free(AH->currSchema);
5255+
free(AH->currSequenceAm);
51905256
free(AH->currTablespace);
51915257
free(AH->currTableAm);
51925258
free(AH->savedPassword);

src/bin/pg_dump/pg_backup_archiver.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@
7171
#define K_VERS_1_16 MAKE_ARCHIVE_VERSION(1, 16, 0) /* BLOB METADATA entries
7272
* and multiple BLOBS,
7373
* relkind */
74+
#define K_VERS_1_17 MAKE_ARCHIVE_VERSION(1, 17, 0) /* add sequenceam */
7475

7576
/* Current archive version number (the format we can output) */
7677
#define K_VERS_MAJOR 1
77-
#define K_VERS_MINOR 16
78+
#define K_VERS_MINOR 17
7879
#define K_VERS_REV 0
7980
#define K_VERS_SELF MAKE_ARCHIVE_VERSION(K_VERS_MAJOR, K_VERS_MINOR, K_VERS_REV)
8081

@@ -323,6 +324,7 @@ struct _archiveHandle
323324
/* these vars track state to avoid sending redundant SET commands */
324325
char *currUser; /* current username, or NULL if unknown */
325326
char *currSchema; /* current schema, or NULL */
327+
char *currSequenceAm; /* current sequence access method, or NULL */
326328
char *currTablespace; /* current tablespace, or NULL */
327329
char *currTableAm; /* current table access method, or NULL */
328330

@@ -358,6 +360,7 @@ struct _tocEntry
358360
char *namespace; /* null or empty string if not in a schema */
359361
char *tablespace; /* null if not in a tablespace; empty string
360362
* means use database default */
363+
char *sequenceam; /* table access method, only for SEQUENCE tags */
361364
char *tableam; /* table access method, only for TABLE tags */
362365
char relkind; /* relation kind, only for TABLE tags */
363366
char *owner;
@@ -404,6 +407,7 @@ typedef struct _archiveOpts
404407
const char *tag;
405408
const char *namespace;
406409
const char *tablespace;
410+
const char *sequenceam;
407411
const char *tableam;
408412
char relkind;
409413
const char *owner;

src/bin/pg_dump/pg_dump.c

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ typedef struct
134134
int64 cache; /* cache size */
135135
int64 last_value; /* last value of sequence */
136136
bool is_called; /* whether nextval advances before returning */
137+
char *seqam; /* access method of sequence */
137138
} SequenceItem;
138139

139140
typedef enum OidOptions
@@ -495,6 +496,7 @@ main(int argc, char **argv)
495496
{"if-exists", no_argument, &dopt.if_exists, 1},
496497
{"inserts", no_argument, NULL, 9},
497498
{"lock-wait-timeout", required_argument, NULL, 2},
499+
{"no-sequence-access-method", no_argument, &dopt.outputNoSequenceAm, 1},
498500
{"no-table-access-method", no_argument, &dopt.outputNoTableAm, 1},
499501
{"no-tablespaces", no_argument, &dopt.outputNoTablespaces, 1},
500502
{"quote-all-identifiers", no_argument, &quote_all_identifiers, 1},
@@ -1182,6 +1184,7 @@ main(int argc, char **argv)
11821184
ropt->superuser = dopt.outputSuperuser;
11831185
ropt->createDB = dopt.outputCreateDB;
11841186
ropt->noOwner = dopt.outputNoOwner;
1187+
ropt->noSequenceAm = dopt.outputNoSequenceAm;
11851188
ropt->noTableAm = dopt.outputNoTableAm;
11861189
ropt->noTablespace = dopt.outputNoTablespaces;
11871190
ropt->disable_triggers = dopt.disable_triggers;
@@ -1303,6 +1306,7 @@ help(const char *progname)
13031306
printf(_(" --no-security-labels do not dump security label assignments\n"));
13041307
printf(_(" --no-statistics do not dump statistics\n"));
13051308
printf(_(" --no-subscriptions do not dump subscriptions\n"));
1309+
printf(_(" --no-sequence-access-method do not sequence table access methods\n"));
13061310
printf(_(" --no-table-access-method do not dump table access methods\n"));
13071311
printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
13081312
printf(_(" --no-toast-compression do not dump TOAST compression methods\n"));
@@ -14102,6 +14106,9 @@ dumpAccessMethod(Archive *fout, const AccessMethodInfo *aminfo)
1410214106
case AMTYPE_INDEX:
1410314107
appendPQExpBufferStr(q, "TYPE INDEX ");
1410414108
break;
14109+
case AMTYPE_SEQUENCE:
14110+
appendPQExpBufferStr(q, "TYPE SEQUENCE ");
14111+
break;
1410514112
case AMTYPE_TABLE:
1410614113
appendPQExpBufferStr(q, "TYPE TABLE ");
1410714114
break;
@@ -18510,26 +18517,40 @@ collectSequences(Archive *fout)
1851018517
*
1851118518
* Since version 18, we can gather the sequence data in this query with
1851218519
* pg_get_sequence_data(), but we only do so for non-schema-only dumps.
18520+
*
18521+
* Access methods for sequences are supported since version 18.
1851318522
*/
1851418523
if (fout->remoteVersion < 100000)
1851518524
return;
18516-
else if (fout->remoteVersion < 180000 ||
18517-
(!fout->dopt->dumpData && !fout->dopt->sequence_data))
18525+
else if (fout->remoteVersion < 180000)
1851818526
query = "SELECT seqrelid, format_type(seqtypid, NULL), "
1851918527
"seqstart, seqincrement, "
1852018528
"seqmax, seqmin, "
1852118529
"seqcache, seqcycle, "
18522-
"NULL, 'f' "
18530+
"NULL, 'f', NULL "
1852318531
"FROM pg_catalog.pg_sequence "
1852418532
"ORDER BY seqrelid";
18533+
else if (!fout->dopt->dumpData && !fout->dopt->sequence_data)
18534+
query = "SELECT s.seqrelid, format_type(s.seqtypid, NULL), "
18535+
"s.seqstart, s.seqincrement, "
18536+
"s.seqmax, s.seqmin, "
18537+
"s.seqcache, s.seqcycle, "
18538+
"NULL, 'f', a.amname AS seqam "
18539+
"FROM pg_catalog.pg_sequence s "
18540+
"JOIN pg_class c ON (c.oid = s.seqrelid) "
18541+
"JOIN pg_am a ON (a.oid = c.relam) "
18542+
"ORDER BY seqrelid";
1852518543
else
18526-
query = "SELECT seqrelid, format_type(seqtypid, NULL), "
18527-
"seqstart, seqincrement, "
18528-
"seqmax, seqmin, "
18529-
"seqcache, seqcycle, "
18530-
"last_value, is_called "
18531-
"FROM pg_catalog.pg_sequence, "
18532-
"pg_get_sequence_data(seqrelid) "
18544+
query = "SELECT s.seqrelid, format_type(s.seqtypid, NULL), "
18545+
"s.seqstart, s.seqincrement, "
18546+
"s.seqmax, s.seqmin, "
18547+
"s.seqcache, s.seqcycle, "
18548+
"r.last_value, r.is_called, "
18549+
"a.amname AS seqam "
18550+
"FROM pg_catalog.pg_sequence s "
18551+
"JOIN pg_class c ON (c.oid = s.seqrelid) "
18552+
"JOIN pg_am a ON (a.oid = c.relam), "
18553+
"pg_get_sequence_data(s.seqrelid) r "
1853318554
"ORDER BY seqrelid;";
1853418555

1853518556
res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
@@ -18549,6 +18570,10 @@ collectSequences(Archive *fout)
1854918570
sequences[i].cycled = (strcmp(PQgetvalue(res, i, 7), "t") == 0);
1855018571
sequences[i].last_value = strtoi64(PQgetvalue(res, i, 8), NULL, 10);
1855118572
sequences[i].is_called = (strcmp(PQgetvalue(res, i, 9), "t") == 0);
18573+
if (!PQgetisnull(res, i, 10))
18574+
sequences[i].seqam = pg_strdup(PQgetvalue(res, i, 10));
18575+
else
18576+
sequences[i].seqam = NULL;
1855218577
}
1855318578

1855418579
PQclear(res);
@@ -18620,6 +18645,7 @@ dumpSequence(Archive *fout, const TableInfo *tbinfo)
1862018645
seq->minv = strtoi64(PQgetvalue(res, 0, 4), NULL, 10);
1862118646
seq->cache = strtoi64(PQgetvalue(res, 0, 5), NULL, 10);
1862218647
seq->cycled = (strcmp(PQgetvalue(res, 0, 6), "t") == 0);
18648+
seq->seqam = NULL;
1862318649

1862418650
PQclear(res);
1862518651
}
@@ -18742,6 +18768,7 @@ dumpSequence(Archive *fout, const TableInfo *tbinfo)
1874218768
ARCHIVE_OPTS(.tag = tbinfo->dobj.name,
1874318769
.namespace = tbinfo->dobj.namespace->dobj.name,
1874418770
.owner = tbinfo->rolname,
18771+
.sequenceam = seq->seqam,
1874518772
.description = "SEQUENCE",
1874618773
.section = SECTION_PRE_DATA,
1874718774
.createStmt = query->data,

0 commit comments

Comments
 (0)