]> granicus.if.org Git - postgresql/commitdiff
pg_dump, pg_upgrade: allow postgres/template1 tablespace moves
authorBruce Momjian <bruce@momjian.us>
Fri, 11 Sep 2015 19:51:10 +0000 (15:51 -0400)
committerBruce Momjian <bruce@momjian.us>
Fri, 11 Sep 2015 19:51:10 +0000 (15:51 -0400)
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

contrib/pg_upgrade/info.c
src/bin/pg_dump/pg_dumpall.c

index 271c27ce73081f1489a3b55f23504f418f70266c..5c75d7a18e1c462fa6671d9cd536f1561d809a35 100644 (file)
@@ -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);
        }
 }
index aece0bdb6d887c54bdf5a826010d78e9b01851c9..f7df6e3e79cbd69d4e87004590613c22a65a2db4 100644 (file)
@@ -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)
                {