Support a --no-tablespaces option in pg_dump/pg_dumpall/pg_restore, so that
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 20 Mar 2008 17:36:58 +0000 (17:36 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 20 Mar 2008 17:36:58 +0000 (17:36 +0000)
dumps can be loaded into databases without the same tablespaces that the
source had.  The option acts by suppressing all "SET default_tablespace"
commands, and also CREATE TABLESPACE commands in pg_dumpall's case.

Gavin Roy, with documentation and minor fixes by me.

doc/src/sgml/ref/pg_dump.sgml
doc/src/sgml/ref/pg_dumpall.sgml
doc/src/sgml/ref/pg_restore.sgml
src/bin/pg_dump/pg_backup.h
src/bin/pg_dump/pg_backup_archiver.c
src/bin/pg_dump/pg_dump.c
src/bin/pg_dump/pg_dumpall.c
src/bin/pg_dump/pg_restore.c

index f89cfc053d25163f3b8a5cf86e0b51eba15a00d3..8c84d6920d4702901eeeb683ddef9768a2a8d00e 100644 (file)
@@ -427,6 +427,23 @@ PostgreSQL documentation
       </listitem>
      </varlistentry>
 
+     <varlistentry>
+      <term><option>--no-tablespaces</option></term>
+      <listitem>
+       <para>
+        Do not output commands to select tablespaces.
+        With this option, all objects will be created in whichever
+        tablespace is the default during restore.
+       </para>
+
+       <para>
+        This option is only meaningful for the plain-text format.  For
+        the archive formats, you can specify the option when you
+        call <command>pg_restore</command>.
+       </para>
+      </listitem>
+     </varlistentry>
+
      <varlistentry>
       <term><option>-s</option></term>
       <term><option>--schema-only</option></term>
index 6f924f2bfb90d9ffb34281f99e2fe9cb06487916..1fef1327820a954057fe76c00895700a5ae0be7b 100644 (file)
@@ -40,7 +40,8 @@ PostgreSQL documentation
    that are common to all databases.
    (<application>pg_dump</application> does not save these objects.)
    This currently includes information about database users and
-   groups, and access permissions that apply to databases as a whole.
+   groups, tablespaces, and properties such as access permissions
+   that apply to databases as a whole.
   </para>
 
   <para>
@@ -204,6 +205,18 @@ PostgreSQL documentation
       </listitem>
      </varlistentry>
 
+     <varlistentry>
+      <term><option>--no-tablespaces</option></term>
+      <listitem>
+       <para>
+        Do not output commands to create tablespaces nor select tablespaces
+        for objects.
+        With this option, all objects will be created in whichever
+        tablespace is the default during restore.
+       </para>
+      </listitem>
+     </varlistentry>
+
      <varlistentry>
       <term><option>-r</option></term>
       <term><option>--roles-only</option></term>
index fb7bd0aea316b947648e23bad61811e9c116223b..502a307d0ba8fabffeb51f9baaf870ce6e6e2f87 100644 (file)
       </listitem>
      </varlistentry>
 
+     <varlistentry>
+      <term><option>--no-tablespaces</option></term>
+      <listitem>
+       <para>
+        Do not output commands to select tablespaces.
+        With this option, all objects will be created in whichever
+        tablespace is the default during restore.
+       </para>
+      </listitem>
+     </varlistentry>
+
      <varlistentry>
       <term><option>-P <replaceable class="parameter">function-name(argtype [, ...])</replaceable></option></term>
       <term><option>--function=<replaceable class="parameter">function-name(argtype [, ...])</replaceable></option></term>
index 07b4e61beda081df5f7d6113d75d28dd579f2898..6cff8e9ecdec4b21bfafc89c1e71f4abaaccf3ed 100644 (file)
@@ -83,6 +83,7 @@ typedef struct _restoreOptions
 {
        int                     create;                 /* Issue commands to create the database */
        int                     noOwner;                /* Don't try to match original object owner */
+       int                     noTablespace;   /* Don't issue tablespace-related commands */
        int                     disable_triggers;               /* disable triggers during data-only
                                                                                 * restore */
        int                     use_setsessauth;/* Use SET SESSION AUTHORIZATION commands
index a4b442e370b2acc5901336959f1667bda07898e8..07f8f64998397bda5d42e319631edc5c8a6d5c9d 100644 (file)
@@ -2378,6 +2378,10 @@ _selectTablespace(ArchiveHandle *AH, const char *tablespace)
        const char *want,
                           *have;
 
+       /* do nothing in --no-tablespaces mode */
+       if (AH->ropt->noTablespace)
+               return;
+
        have = AH->currTablespace;
        want = tablespace;
 
@@ -2578,7 +2582,7 @@ _printTocEntry(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt, bool isDat
                                 pfx, te->tag, te->desc,
                                 te->namespace ? te->namespace : "-",
                                 ropt->noOwner ? "-" : te->owner);
-               if (te->tablespace)
+               if (te->tablespace && !ropt->noTablespace)
                        ahprintf(AH, "; Tablespace: %s", te->tablespace);
                ahprintf(AH, "\n");
 
index ed1b33de07075d9e2710cbc3aa5d8d8e8e2dbd3c..27affc3baa50e75737697402602b59e1fe58e2da 100644 (file)
@@ -223,9 +223,10 @@ main(int argc, char **argv)
        int                     outputCreate = 0;
        bool            outputBlobs = false;
        int                     outputNoOwner = 0;
-       static int      use_setsessauth = 0;
-       static int      disable_triggers = 0;
        char       *outputSuperuser = NULL;
+       static int      disable_triggers = 0;
+       static int  outputNoTablespaces = 0;
+       static int      use_setsessauth = 0;
 
        RestoreOptions *ropt;
 
@@ -266,6 +267,7 @@ main(int argc, char **argv)
                 */
                {"disable-dollar-quoting", no_argument, &disable_dollar_quoting, 1},
                {"disable-triggers", no_argument, &disable_triggers, 1},
+               {"no-tablespaces", no_argument, &outputNoTablespaces, 1},
                {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
 
                {NULL, 0, NULL, 0}
@@ -417,6 +419,8 @@ main(int argc, char **argv)
                                        disable_dollar_quoting = 1;
                                else if (strcmp(optarg, "disable-triggers") == 0)
                                        disable_triggers = 1;
+                               else if (strcmp(optarg, "no-tablespaces") == 0)
+                                       outputNoTablespaces = 1;
                                else if (strcmp(optarg, "use-set-session-authorization") == 0)
                                        use_setsessauth = 1;
                                else
@@ -708,6 +712,7 @@ main(int argc, char **argv)
                ropt->superuser = outputSuperuser;
                ropt->create = outputCreate;
                ropt->noOwner = outputNoOwner;
+               ropt->noTablespace = outputNoTablespaces;
                ropt->disable_triggers = disable_triggers;
                ropt->use_setsessauth = use_setsessauth;
                ropt->dataOnly = dataOnly;
@@ -768,6 +773,7 @@ help(const char *progname)
        printf(_("  -x, --no-privileges         do not dump privileges (grant/revoke)\n"));
        printf(_("  --disable-dollar-quoting    disable dollar quoting, use SQL standard quoting\n"));
        printf(_("  --disable-triggers          disable triggers during data-only restore\n"));
+       printf(_("  --no-tablespaces            do not dump tablespace assignments\n"));
        printf(_("  --use-set-session-authorization\n"
                         "                              use SESSION AUTHORIZATION commands instead of\n"
        "                              ALTER OWNER commands to set ownership\n"));
index 6040dcdf19ce5bea89be65ab1c848cfea126d0f8..e33e617fb222414192b38bbc8578be1b76ef70a5 100644 (file)
@@ -64,6 +64,7 @@ static bool ignoreVersion = false;
 
 static int     disable_dollar_quoting = 0;
 static int     disable_triggers = 0;
+static int     no_tablespaces = 0;
 static int     use_setsessauth = 0;
 static int     server_version;
 
@@ -118,6 +119,7 @@ main(int argc, char *argv[])
                 */
                {"disable-dollar-quoting", no_argument, &disable_dollar_quoting, 1},
                {"disable-triggers", no_argument, &disable_triggers, 1},
+               {"no-tablespaces", no_argument, &no_tablespaces, 1},
                {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
 
                {NULL, 0, NULL, 0}
@@ -285,11 +287,13 @@ main(int argc, char *argv[])
                        case 'X':
                                /* -X is a deprecated alternative to long options */
                                if (strcmp(optarg, "disable-dollar-quoting") == 0)
-                                       appendPQExpBuffer(pgdumpopts, " --disable-dollar-quoting");
+                                       disable_dollar_quoting = 1;
                                else if (strcmp(optarg, "disable-triggers") == 0)
-                                       appendPQExpBuffer(pgdumpopts, " --disable-triggers");
+                                       disable_triggers = 1;
+                               else if (strcmp(optarg, "no-tablespaces") == 0) 
+                                       no_tablespaces = 1;
                                else if (strcmp(optarg, "use-set-session-authorization") == 0)
-                                        /* no-op, still allowed for compatibility */ ;
+                                       use_setsessauth = 1;
                                else
                                {
                                        fprintf(stderr,
@@ -314,6 +318,8 @@ main(int argc, char *argv[])
                appendPQExpBuffer(pgdumpopts, " --disable-dollar-quoting");
        if (disable_triggers)
                appendPQExpBuffer(pgdumpopts, " --disable-triggers");
+       if (no_tablespaces)
+               appendPQExpBuffer(pgdumpopts, " --no-tablespaces");
        if (use_setsessauth)
                appendPQExpBuffer(pgdumpopts, " --use-set-session-authorization");
 
@@ -444,7 +450,7 @@ main(int argc, char *argv[])
                                dumpGroups(conn);
                }
 
-               if (!roles_only)
+               if (!roles_only && !no_tablespaces)
                {
                        /* Dump tablespaces */
                        if (server_version >= 80000)
@@ -502,6 +508,7 @@ help(void)
        printf(_("  --disable-dollar-quoting\n"
                         "                           disable dollar quoting, use SQL standard quoting\n"));
        printf(_("  --disable-triggers       disable triggers during data-only restore\n"));
+       printf(_("  --no-tablespaces         do not dump tablespace assignments\n"));
        printf(_("  --use-set-session-authorization\n"
                         "                           use SESSION AUTHORIZATION commands instead of\n"
                         "                           OWNER TO commands\n"));
index 0e8433f07372c2e7b7c2dc62dabbf5f6a25c3166..7d9e0b9220bec516fc707bf3b038c743fca1e802 100644 (file)
@@ -74,9 +74,10 @@ main(int argc, char **argv)
        char       *inputFileSpec;
        extern int      optind;
        extern char *optarg;
-       static int      use_setsessauth = 0;
        static int      disable_triggers = 0;
        static int      no_data_for_failed_tables = 0;
+       static int  outputNoTablespaces = 0;
+       static int      use_setsessauth = 0;
 
        struct option cmdopts[] = {
                {"clean", 0, NULL, 'c'},
@@ -110,9 +111,10 @@ main(int argc, char **argv)
                /*
                 * the following options don't have an equivalent short option letter
                 */
-               {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
                {"disable-triggers", no_argument, &disable_triggers, 1},
                {"no-data-for-failed-tables", no_argument, &no_data_for_failed_tables, 1},
+               {"no-tablespaces", no_argument, &outputNoTablespaces, 1},
+               {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
 
                {NULL, 0, NULL, 0}
        };
@@ -241,10 +243,14 @@ main(int argc, char **argv)
 
                        case 'X':
                                /* -X is a deprecated alternative to long options */
-                               if (strcmp(optarg, "use-set-session-authorization") == 0)
-                                       use_setsessauth = 1;
-                               else if (strcmp(optarg, "disable-triggers") == 0)
+                               if (strcmp(optarg, "disable-triggers") == 0)
                                        disable_triggers = 1;
+                               else if (strcmp(optarg, "no-data-for-failed-tables") == 0)
+                                       no_data_for_failed_tables = 1;
+                               else if (strcmp(optarg, "no-tablespaces") == 0)
+                                       outputNoTablespaces = 1;
+                               else if (strcmp(optarg, "use-set-session-authorization") == 0)
+                                       use_setsessauth = 1;
                                else
                                {
                                        fprintf(stderr,
@@ -290,8 +296,9 @@ main(int argc, char **argv)
        }
 
        opts->disable_triggers = disable_triggers;
-       opts->use_setsessauth = use_setsessauth;
        opts->noDataForFailedTables = no_data_for_failed_tables;
+       opts->noTablespace = outputNoTablespaces;
+       opts->use_setsessauth = use_setsessauth;
 
        if (opts->formatName)
        {
@@ -395,12 +402,13 @@ usage(const char *progname)
        printf(_("  -T, --trigger=NAME       restore named trigger\n"));
        printf(_("  -x, --no-privileges      skip restoration of access privileges (grant/revoke)\n"));
        printf(_("  --disable-triggers       disable triggers during data-only restore\n"));
-       printf(_("  --use-set-session-authorization\n"
-                        "                           use SESSION AUTHORIZATION commands instead of\n"
-                        "                           OWNER TO commands\n"));
        printf(_("  --no-data-for-failed-tables\n"
                         "                           do not restore data of tables that could not be\n"
                         "                           created\n"));
+       printf(_("  --no-tablespaces         do not dump tablespace assignments\n"));
+       printf(_("  --use-set-session-authorization\n"
+                        "                           use SESSION AUTHORIZATION commands instead of\n"
+                        "                           OWNER TO commands\n"));
        printf(_("  -1, --single-transaction\n"
                         "                           restore as a single transaction\n"));