Skip to content

Commit 3243fce

Browse files
committed
pg_dump, pg_upgrade: allow postgres/template1 tablespace moves
Modify pg_dump to restore postgres/template1 databases to non-default tablespaces by switching out of the database to be moved, then switching back. Also, to fix potentially cases where the old/new tablespaces might not match, fix pg_upgrade to process new/old tablespaces separately in all cases. Report by Marti Raudsepp Patch by Marti Raudsepp, me Backpatch through 9.0
1 parent 25b3ddd commit 3243fce

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/bin/pg_dump/pg_dumpall.c

+18
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,24 @@ dumpCreateDB(PGconn *conn)
14121412

14131413
appendPQExpBufferStr(buf, ";\n");
14141414
}
1415+
else if (strcmp(dbtablespace, "pg_default") != 0 && !no_tablespaces)
1416+
{
1417+
/*
1418+
* Cannot change tablespace of the database we're connected to,
1419+
* so to move "postgres" to another tablespace, we connect to
1420+
* "template1", and vice versa.
1421+
*/
1422+
if (strcmp(dbname, "postgres") == 0)
1423+
appendPQExpBuffer(buf, "\\connect template1\n");
1424+
else
1425+
appendPQExpBuffer(buf, "\\connect postgres\n");
1426+
1427+
appendPQExpBuffer(buf, "ALTER DATABASE %s SET TABLESPACE %s;\n",
1428+
fdbname, fmtId(dbtablespace));
1429+
1430+
/* connect to original database */
1431+
appendPQExpBuffer(buf, "\\connect %s\n", fdbname);
1432+
}
14151433

14161434
if (binary_upgrade)
14171435
{

src/bin/pg_upgrade/info.c

+12-3
Original file line numberDiff line numberDiff line change
@@ -140,23 +140,32 @@ create_rel_filename_map(const char *old_data, const char *new_data,
140140
const RelInfo *old_rel, const RelInfo *new_rel,
141141
FileNameMap *map)
142142
{
143+
/* In case old/new tablespaces don't match, do them separately. */
143144
if (strlen(old_rel->tablespace) == 0)
144145
{
145146
/*
146147
* relation belongs to the default tablespace, hence relfiles should
147148
* exist in the data directories.
148149
*/
149150
map->old_tablespace = old_data;
150-
map->new_tablespace = new_data;
151151
map->old_tablespace_suffix = "/base";
152-
map->new_tablespace_suffix = "/base";
153152
}
154153
else
155154
{
156155
/* relation belongs to a tablespace, so use the tablespace location */
157156
map->old_tablespace = old_rel->tablespace;
158-
map->new_tablespace = new_rel->tablespace;
159157
map->old_tablespace_suffix = old_cluster.tablespace_suffix;
158+
}
159+
160+
/* Do the same for new tablespaces */
161+
if (strlen(new_rel->tablespace) == 0)
162+
{
163+
map->new_tablespace = new_data;
164+
map->new_tablespace_suffix = "/base";
165+
}
166+
else
167+
{
168+
map->new_tablespace = new_rel->tablespace;
160169
map->new_tablespace_suffix = new_cluster.tablespace_suffix;
161170
}
162171

0 commit comments

Comments
 (0)