Skip to content

Commit ca44504

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 a7516bb commit ca44504

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

contrib/pg_upgrade/info.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,24 +140,38 @@ 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
strlcpy(map->old_tablespace, old_data, sizeof(map->old_tablespace));
150-
strlcpy(map->new_tablespace, new_data, sizeof(map->new_tablespace));
151151
strlcpy(map->old_tablespace_suffix, "/base", sizeof(map->old_tablespace_suffix));
152-
strlcpy(map->new_tablespace_suffix, "/base", sizeof(map->new_tablespace_suffix));
153152
}
154153
else
155154
{
156155
/* relation belongs to a tablespace, so use the tablespace location */
157156
strlcpy(map->old_tablespace, old_rel->tablespace, sizeof(map->old_tablespace));
158-
strlcpy(map->new_tablespace, new_rel->tablespace, sizeof(map->new_tablespace));
159157
strlcpy(map->old_tablespace_suffix, old_cluster.tablespace_suffix,
160158
sizeof(map->old_tablespace_suffix));
159+
}
160+
161+
/* Do the same for new tablespaces */
162+
if (strlen(new_rel->tablespace) == 0)
163+
{
164+
/*
165+
* relation belongs to the default tablespace, hence relfiles should
166+
* exist in the data directories.
167+
*/
168+
strlcpy(map->new_tablespace, new_data, sizeof(map->new_tablespace));
169+
strlcpy(map->new_tablespace_suffix, "/base", sizeof(map->new_tablespace_suffix));
170+
}
171+
else
172+
{
173+
/* relation belongs to a tablespace, so use the tablespace location */
174+
strlcpy(map->new_tablespace, new_rel->tablespace, sizeof(map->new_tablespace));
161175
strlcpy(map->new_tablespace_suffix, new_cluster.tablespace_suffix,
162176
sizeof(map->new_tablespace_suffix));
163177
}

src/bin/pg_dump/pg_dumpall.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,24 @@ dumpCreateDB(PGconn *conn)
13831383
appendPQExpBuffer(buf, ";\n");
13841384
}
13851385
}
1386+
else if (strcmp(dbtablespace, "pg_default") != 0 && !no_tablespaces)
1387+
{
1388+
/*
1389+
* Cannot change tablespace of the database we're connected to,
1390+
* so to move "postgres" to another tablespace, we connect to
1391+
* "template1", and vice versa.
1392+
*/
1393+
if (strcmp(dbname, "postgres") == 0)
1394+
appendPQExpBuffer(buf, "\\connect template1\n");
1395+
else
1396+
appendPQExpBuffer(buf, "\\connect postgres\n");
1397+
1398+
appendPQExpBuffer(buf, "ALTER DATABASE %s SET TABLESPACE %s;\n",
1399+
fdbname, fmtId(dbtablespace));
1400+
1401+
/* connect to original database */
1402+
appendPQExpBuffer(buf, "\\connect %s\n", fdbname);
1403+
}
13861404

13871405
if (binary_upgrade)
13881406
{

0 commit comments

Comments
 (0)