From befc63e849d24a4d4aacece42937f7600af7967f Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Fri, 11 Sep 2015 15:51:10 -0400 Subject: 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 --- src/bin/pg_dump/pg_dumpall.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src') diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c index 5cb7f442d90..3529c5c0b0a 100644 --- a/src/bin/pg_dump/pg_dumpall.c +++ b/src/bin/pg_dump/pg_dumpall.c @@ -1357,6 +1357,28 @@ dumpCreateDB(PGconn *conn) appendPQExpBuffer(buf, ";\n"); } } + else if (strcmp(dbtablespace, "pg_default") != 0 && !no_tablespaces) + { + /* + * Cannot change tablespace of the database we're connected to, + * so to move "postgres" to another tablespace, we connect to + * "template1", and vice versa. + */ + if (strcmp(dbname, "postgres") == 0) + appendPQExpBuffer(buf, "%s\\connect template1\n", + /* Add a space before \\connect so pg_upgrade can split */ + binary_upgrade ? " " : ""); + else + appendPQExpBuffer(buf, "%s\\connect postgres\n", + binary_upgrade ? " " : ""); + + appendPQExpBuffer(buf, "ALTER DATABASE %s SET TABLESPACE %s;\n", + fdbname, fmtId(dbtablespace)); + + /* connect to original database */ + appendPQExpBuffer(buf, "%s\\connect %s\n", + binary_upgrade ? " " : "", fdbname); + } if (binary_upgrade) { -- cgit v1.2.3