Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
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 2c78f6ed4f5a320af0ec5dd5de8ea75c79acc61d..6998a7b42c9576a3c3c4f6d09ea171150bc3f195 100644 (file)
@@ -92,6 +92,7 @@ create_rel_filename_map(const char *old_data, const char *new_data,
                        const RelInfo *old_rel, const RelInfo *new_rel,
                        FileNameMap *map)
 {
+   /* In case old/new tablespaces don't match, do them separately. */
    if (strlen(old_rel->tablespace) == 0)
    {
        /*
@@ -100,14 +101,27 @@ create_rel_filename_map(const char *old_data, const char *new_data,
         */
        snprintf(map->old_dir, sizeof(map->old_dir), "%s/base/%u", old_data,
                 old_db->db_oid);
-       snprintf(map->new_dir, sizeof(map->new_dir), "%s/base/%u", new_data,
-                new_db->db_oid);
    }
    else
    {
        /* relation belongs to a tablespace, so use the tablespace location */
        snprintf(map->old_dir, sizeof(map->old_dir), "%s%s/%u", old_rel->tablespace,
                 old_cluster.tablespace_suffix, old_db->db_oid);
+   }
+
+   /* Do the same for new tablespaces */
+   if (strlen(new_rel->tablespace) == 0)
+   {
+       /*
+        * relation belongs to the default tablespace, hence relfiles should
+        * exist in the data directories.
+        */
+       snprintf(map->new_dir, sizeof(map->new_dir), "%s/base/%u", new_data,
+                new_db->db_oid);
+   }
+   else
+   {
+       /* relation belongs to a tablespace, so use the tablespace location */
        snprintf(map->new_dir, sizeof(map->new_dir), "%s%s/%u", new_rel->tablespace,
                 new_cluster.tablespace_suffix, new_db->db_oid);
    }
index cb744f5ab337840b8bea4046818a1b6f27bb1f68..e4fde3f1303c124016befda404f33a305ce6d15b 100644 (file)
@@ -1326,6 +1326,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)
        {