Skip to content

Commit 35d2fc1

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 7d07128 commit 35d2fc1

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

contrib/pg_upgrade/info.c

Lines changed: 12 additions & 3 deletions
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

src/bin/pg_dump/pg_dumpall.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,24 @@ dumpCreateDB(PGconn *conn)
13991399
appendPQExpBufferStr(buf, ";\n");
14001400
}
14011401
}
1402+
else if (strcmp(dbtablespace, "pg_default") != 0 && !no_tablespaces)
1403+
{
1404+
/*
1405+
* Cannot change tablespace of the database we're connected to,
1406+
* so to move "postgres" to another tablespace, we connect to
1407+
* "template1", and vice versa.
1408+
*/
1409+
if (strcmp(dbname, "postgres") == 0)
1410+
appendPQExpBuffer(buf, "\\connect template1\n");
1411+
else
1412+
appendPQExpBuffer(buf, "\\connect postgres\n");
1413+
1414+
appendPQExpBuffer(buf, "ALTER DATABASE %s SET TABLESPACE %s;\n",
1415+
fdbname, fmtId(dbtablespace));
1416+
1417+
/* connect to original database */
1418+
appendPQExpBuffer(buf, "\\connect %s\n", fdbname);
1419+
}
14021420

14031421
if (binary_upgrade)
14041422
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy