]> 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 7dd810a72a8f5bd15a66166f150fe47a8913b820..034aa71add22c33546760aaf3f023cd085ab4f83 100644 (file)
@@ -140,6 +140,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)
        {
                /*
@@ -147,17 +148,30 @@ create_rel_filename_map(const char *old_data, const char *new_data,
                 * exist in the data directories.
                 */
                strlcpy(map->old_tablespace, old_data, sizeof(map->old_tablespace));
-               strlcpy(map->new_tablespace, new_data, sizeof(map->new_tablespace));
                strlcpy(map->old_tablespace_suffix, "/base", sizeof(map->old_tablespace_suffix));
-               strlcpy(map->new_tablespace_suffix, "/base", sizeof(map->new_tablespace_suffix));
        }
        else
        {
                /* relation belongs to a tablespace, so use the tablespace location */
                strlcpy(map->old_tablespace, old_rel->tablespace, sizeof(map->old_tablespace));
-               strlcpy(map->new_tablespace, new_rel->tablespace, sizeof(map->new_tablespace));
                strlcpy(map->old_tablespace_suffix, old_cluster.tablespace_suffix,
                                sizeof(map->old_tablespace_suffix));
+       }
+
+       /* 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.
+                */
+               strlcpy(map->new_tablespace, new_data, sizeof(map->new_tablespace));
+               strlcpy(map->new_tablespace_suffix, "/base", sizeof(map->new_tablespace_suffix));
+       }
+       else
+       {
+               /* relation belongs to a tablespace, so use the tablespace location */
+               strlcpy(map->new_tablespace, new_rel->tablespace, sizeof(map->new_tablespace));
                strlcpy(map->new_tablespace_suffix, new_cluster.tablespace_suffix,
                                sizeof(map->new_tablespace_suffix));
        }
index e988958e1b73d50172d7df7f7be5523f01a681a9..3b8e53508565cb332f742f2481aac3fbf3bdad95 100644 (file)
@@ -1383,6 +1383,24 @@ 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, "\\connect template1\n");
+                       else
+                               appendPQExpBuffer(buf, "\\connect postgres\n");
+
+                       appendPQExpBuffer(buf, "ALTER DATABASE %s SET TABLESPACE %s;\n",
+                                                         fdbname, fmtId(dbtablespace));
+
+                       /* connect to original database */
+                       appendPQExpBuffer(buf, "\\connect %s\n", fdbname);
+               }
 
                if (binary_upgrade)
                {