mirror of
https://github.com/postgres/postgres.git
synced 2025-08-05 07:41:25 +03:00
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
This commit is contained in:
@@ -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,
|
static void map_rel_by_id(migratorContext *ctx, Oid oldid, Oid newid,
|
||||||
const char *old_nspname, const char *old_relname,
|
const char *old_nspname, const char *old_relname,
|
||||||
const char *new_nspname, const char *new_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 DbInfo *new_db, const char *olddata,
|
||||||
const char *newdata, FileNameMap *map);
|
const char *newdata, FileNameMap *map);
|
||||||
static RelInfo *relarr_lookup_reloid(migratorContext *ctx,
|
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)
|
const char *newdata, FileNameMap *map)
|
||||||
{
|
{
|
||||||
map_rel_by_id(ctx, oldrel->relfilenode, newrel->relfilenode, oldrel->nspname,
|
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);
|
new_db, olddata, newdata, map);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,7 +154,7 @@ static void
|
|||||||
map_rel_by_id(migratorContext *ctx, Oid oldid, Oid newid,
|
map_rel_by_id(migratorContext *ctx, Oid oldid, Oid newid,
|
||||||
const char *old_nspname, const char *old_relname,
|
const char *old_nspname, const char *old_relname,
|
||||||
const char *new_nspname, const char *new_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 DbInfo *new_db, const char *olddata,
|
||||||
const char *newdata, FileNameMap *map)
|
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_nspname, sizeof(map->new_nspname), "%s", new_nspname);
|
||||||
snprintf(map->new_relname, sizeof(map->new_relname), "%s", new_relname);
|
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)
|
if (strlen(old_tablespace) == 0)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
@@ -173,7 +174,6 @@ map_rel_by_id(migratorContext *ctx, Oid oldid, Oid newid,
|
|||||||
* exist in the data directories.
|
* exist in the data directories.
|
||||||
*/
|
*/
|
||||||
snprintf(map->old_file, sizeof(map->old_file), "%s/base/%u", olddata, old_db->db_oid);
|
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
|
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,
|
snprintf(map->old_file, sizeof(map->old_file), "%s%s/%u", old_tablespace,
|
||||||
ctx->old.tablespace_suffix, old_db->db_oid);
|
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);
|
ctx->new.tablespace_suffix, new_db->db_oid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1303,6 +1303,28 @@ dumpCreateDB(PGconn *conn)
|
|||||||
appendPQExpBuffer(buf, ";\n");
|
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)
|
if (binary_upgrade)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user