From: Bruce Momjian Date: Fri, 11 Sep 2015 19:51:10 +0000 (-0400) Subject: pg_dump, pg_upgrade: allow postgres/template1 tablespace moves X-Git-Tag: REL9_0_23~34 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=52b07779ddd07d5cbe4b833c91061bb8ca9379aa;p=thirdparty%2Fpostgresql.git 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 --- diff --git a/contrib/pg_upgrade/info.c b/contrib/pg_upgrade/info.c index 271c27ce730..5c75d7a18e1 100644 --- a/contrib/pg_upgrade/info.c +++ b/contrib/pg_upgrade/info.c @@ -27,7 +27,7 @@ static void map_rel(migratorContext *ctx, const RelInfo *oldrel, static void map_rel_by_id(migratorContext *ctx, Oid oldid, Oid newid, const char *old_nspname, const char *old_relname, const char *new_nspname, const char *new_relname, - const char *old_tablespace, const DbInfo *old_db, + const char *old_tablespace, const char *new_tablespace, const DbInfo *old_db, const DbInfo *new_db, const char *olddata, const char *newdata, FileNameMap *map); static RelInfo *relarr_lookup_reloid(migratorContext *ctx, @@ -140,7 +140,7 @@ map_rel(migratorContext *ctx, const RelInfo *oldrel, const RelInfo *newrel, const char *newdata, FileNameMap *map) { map_rel_by_id(ctx, oldrel->relfilenode, newrel->relfilenode, oldrel->nspname, - oldrel->relname, newrel->nspname, newrel->relname, oldrel->tablespace, old_db, + oldrel->relname, newrel->nspname, newrel->relname, oldrel->tablespace, newrel->tablespace, old_db, new_db, olddata, newdata, map); } @@ -154,7 +154,7 @@ static void map_rel_by_id(migratorContext *ctx, Oid oldid, Oid newid, const char *old_nspname, const char *old_relname, const char *new_nspname, const char *new_relname, - const char *old_tablespace, const DbInfo *old_db, + const char *old_tablespace, const char *new_tablespace, const DbInfo *old_db, const DbInfo *new_db, const char *olddata, const char *newdata, FileNameMap *map) { @@ -166,6 +166,7 @@ map_rel_by_id(migratorContext *ctx, Oid oldid, Oid newid, snprintf(map->new_nspname, sizeof(map->new_nspname), "%s", new_nspname); snprintf(map->new_relname, sizeof(map->new_relname), "%s", new_relname); + /* In case old/new tablespaces don't match, do them separately. */ if (strlen(old_tablespace) == 0) { /* @@ -173,7 +174,6 @@ map_rel_by_id(migratorContext *ctx, Oid oldid, Oid newid, * exist in the data directories. */ snprintf(map->old_file, sizeof(map->old_file), "%s/base/%u", olddata, old_db->db_oid); - snprintf(map->new_file, sizeof(map->new_file), "%s/base/%u", newdata, new_db->db_oid); } else { @@ -183,7 +183,24 @@ map_rel_by_id(migratorContext *ctx, Oid oldid, Oid newid, */ snprintf(map->old_file, sizeof(map->old_file), "%s%s/%u", old_tablespace, ctx->old.tablespace_suffix, old_db->db_oid); - snprintf(map->new_file, sizeof(map->new_file), "%s%s/%u", old_tablespace, + } + + /* Do the same for new tablespaces */ + if (strlen(new_tablespace) == 0) + { + /* + * relation belongs to the default tablespace, hence relfiles would + * exist in the data directories. + */ + snprintf(map->new_file, sizeof(map->new_file), "%s/base/%u", newdata, new_db->db_oid); + } + else + { + /* + * relation belongs to some tablespace, hence copy its physical + * location + */ + snprintf(map->new_file, sizeof(map->new_file), "%s%s/%u", new_tablespace, ctx->new.tablespace_suffix, new_db->db_oid); } } diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c index aece0bdb6d8..f7df6e3e79c 100644 --- a/src/bin/pg_dump/pg_dumpall.c +++ b/src/bin/pg_dump/pg_dumpall.c @@ -1303,6 +1303,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) {