Skip to content

Commit a7e5457

Browse files
committed
pg_upgrade: Upgrade sequence data via pg_dump
Previously, pg_upgrade migrated sequence data like tables by copying the on-disk file. This does not allow any changes in the on-disk format for sequences. It's simpler to just have pg_dump set the new sequence values as it normally does. To do that, create a hidden submode in pg_dump that dumps sequence data even when a schema-only dump is requested, and trigger that submode in binary upgrade mode. (This new submode could easily be exposed as a command-line option, but it has limited use outside of pg_dump and would probably cause some confusion, so we don't do that at this time.) Reviewed-by: Anastasia Lubennikova <[email protected]> Reviewed-by: Michael Paquier <[email protected]>
1 parent 27d2c12 commit a7e5457

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

src/bin/pg_dump/pg_backup.h

+3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ typedef struct _restoreOptions
118118

119119
bool *idWanted; /* array showing which dump IDs to emit */
120120
int enable_row_security;
121+
int sequence_data; /* dump sequence data even in schema-only mode */
121122
} RestoreOptions;
122123

123124
typedef struct _dumpOptions
@@ -160,6 +161,8 @@ typedef struct _dumpOptions
160161
bool outputBlobs;
161162
int outputNoOwner;
162163
char *outputSuperuser;
164+
165+
int sequence_data; /* dump sequence data even in schema-only mode */
163166
} DumpOptions;
164167

165168
/*

src/bin/pg_dump/pg_backup_archiver.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ dumpOptionsFromRestoreOptions(RestoreOptions *ropt)
171171
dopt->lockWaitTimeout = ropt->lockWaitTimeout;
172172
dopt->include_everything = ropt->include_everything;
173173
dopt->enable_row_security = ropt->enable_row_security;
174+
dopt->sequence_data = ropt->sequence_data;
174175

175176
return dopt;
176177
}
@@ -2855,7 +2856,10 @@ _tocEntryRequired(TocEntry *te, teSection curSection, RestoreOptions *ropt)
28552856

28562857
/* Mask it if we only want schema */
28572858
if (ropt->schemaOnly)
2858-
res = res & REQ_SCHEMA;
2859+
{
2860+
if (!(ropt->sequence_data && strcmp(te->desc, "SEQUENCE SET") == 0))
2861+
res = res & REQ_SCHEMA;
2862+
}
28592863

28602864
/* Mask it if we only want data */
28612865
if (ropt->dataOnly)

src/bin/pg_dump/pg_dump.c

+15-4
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ static void addBoundaryDependencies(DumpableObject **dobjs, int numObjs,
216216
DumpableObject *boundaryObjs);
217217

218218
static void getDomainConstraints(Archive *fout, TypeInfo *tyinfo);
219-
static void getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, bool oids);
219+
static void getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, bool oids, char relkind);
220220
static void makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo, bool oids);
221221
static void buildMatViewRefreshDependencies(Archive *fout);
222222
static void getTableDataFKConstraints(void);
@@ -546,6 +546,12 @@ main(int argc, char **argv)
546546
if (dopt.column_inserts)
547547
dopt.dump_inserts = 1;
548548

549+
/* Binary upgrade mode implies dumping sequence data even in schema-only
550+
* mode. This is not exposed as a separate option, but kept separate
551+
* internally for clarity. */
552+
if (dopt.binary_upgrade)
553+
dopt.sequence_data = 1;
554+
549555
if (dopt.dataOnly && dopt.schemaOnly)
550556
{
551557
write_msg(NULL, "options -s/--schema-only and -a/--data-only cannot be used together\n");
@@ -722,12 +728,15 @@ main(int argc, char **argv)
722728

723729
if (!dopt.schemaOnly)
724730
{
725-
getTableData(&dopt, tblinfo, numTables, dopt.oids);
731+
getTableData(&dopt, tblinfo, numTables, dopt.oids, 0);
726732
buildMatViewRefreshDependencies(fout);
727733
if (dopt.dataOnly)
728734
getTableDataFKConstraints();
729735
}
730736

737+
if (dopt.schemaOnly && dopt.sequence_data)
738+
getTableData(&dopt, tblinfo, numTables, dopt.oids, RELKIND_SEQUENCE);
739+
731740
if (dopt.outputBlobs)
732741
getBlobs(fout);
733742

@@ -806,6 +815,7 @@ main(int argc, char **argv)
806815
ropt->lockWaitTimeout = dopt.lockWaitTimeout;
807816
ropt->include_everything = dopt.include_everything;
808817
ropt->enable_row_security = dopt.enable_row_security;
818+
ropt->sequence_data = dopt.sequence_data;
809819

810820
if (compressLevel == -1)
811821
ropt->compression = 0;
@@ -2039,13 +2049,14 @@ refreshMatViewData(Archive *fout, TableDataInfo *tdinfo)
20392049
* set up dumpable objects representing the contents of tables
20402050
*/
20412051
static void
2042-
getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, bool oids)
2052+
getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, bool oids, char relkind)
20432053
{
20442054
int i;
20452055

20462056
for (i = 0; i < numTables; i++)
20472057
{
2048-
if (tblinfo[i].dobj.dump & DUMP_COMPONENT_DATA)
2058+
if (tblinfo[i].dobj.dump & DUMP_COMPONENT_DATA &&
2059+
(!relkind || tblinfo[i].relkind == relkind))
20492060
makeTableDataInfo(dopt, &(tblinfo[i]), oids);
20502061
}
20512062
}

src/bin/pg_upgrade/info.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
444444
" SELECT c.oid, 0::oid, 0::oid "
445445
" FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
446446
" ON c.relnamespace = n.oid "
447-
" WHERE relkind IN ('r', 'm', 'S') AND "
447+
" WHERE relkind IN ('r', 'm') AND "
448448
/* exclude possible orphaned temp tables */
449449
" ((n.nspname !~ '^pg_temp_' AND "
450450
" n.nspname !~ '^pg_toast_temp_' AND "

0 commit comments

Comments
 (0)