From 68528d37bbfbb3ae8dc83418f3e1d343c4050f0a Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 20 Mar 2008 17:36:58 +0000 Subject: [PATCH] Support a --no-tablespaces option in pg_dump/pg_dumpall/pg_restore, so that 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 | 19 ++++++++++++++++++- doc/src/sgml/ref/pg_dumpall.sgml | 17 +++++++++++++++-- doc/src/sgml/ref/pg_restore.sgml | 13 ++++++++++++- src/bin/pg_dump/pg_backup.h | 3 ++- src/bin/pg_dump/pg_backup_archiver.c | 8 ++++++-- src/bin/pg_dump/pg_dump.c | 12 +++++++++--- src/bin/pg_dump/pg_dumpall.c | 17 ++++++++++++----- src/bin/pg_dump/pg_restore.c | 28 ++++++++++++++++++---------- 8 files changed, 92 insertions(+), 25 deletions(-) diff --git a/doc/src/sgml/ref/pg_dump.sgml b/doc/src/sgml/ref/pg_dump.sgml index fd0d56d8c0..9767cfbcd7 100644 --- a/doc/src/sgml/ref/pg_dump.sgml +++ b/doc/src/sgml/ref/pg_dump.sgml @@ -1,5 +1,5 @@ @@ -427,6 +427,23 @@ PostgreSQL documentation + + + + + Do not output commands to select tablespaces. + With this option, all objects will be created in whichever + tablespace is the default during restore. + + + + This option is only meaningful for the plain-text format. For + the archive formats, you can specify the option when you + call pg_restore. + + + + diff --git a/doc/src/sgml/ref/pg_dumpall.sgml b/doc/src/sgml/ref/pg_dumpall.sgml index a697cdbb91..e624210bac 100644 --- a/doc/src/sgml/ref/pg_dumpall.sgml +++ b/doc/src/sgml/ref/pg_dumpall.sgml @@ -1,5 +1,5 @@ @@ -40,7 +40,8 @@ PostgreSQL documentation that are common to all databases. (pg_dump 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. @@ -204,6 +205,18 @@ PostgreSQL documentation + + + + + 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. + + + + diff --git a/doc/src/sgml/ref/pg_restore.sgml b/doc/src/sgml/ref/pg_restore.sgml index 17a99e5395..f908d93dca 100644 --- a/doc/src/sgml/ref/pg_restore.sgml +++ b/doc/src/sgml/ref/pg_restore.sgml @@ -1,4 +1,4 @@ - + @@ -273,6 +273,17 @@ + + + + + Do not output commands to select tablespaces. + With this option, all objects will be created in whichever + tablespace is the default during restore. + + + + diff --git a/src/bin/pg_dump/pg_backup.h b/src/bin/pg_dump/pg_backup.h index a870516d7a..c4eb6e22e4 100644 --- a/src/bin/pg_dump/pg_backup.h +++ b/src/bin/pg_dump/pg_backup.h @@ -15,7 +15,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup.h,v 1.45 2007/01/25 03:30:43 momjian Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup.h,v 1.46 2008/03/20 17:36:57 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -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 diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c index af44e85251..7c36aef548 100644 --- a/src/bin/pg_dump/pg_backup_archiver.c +++ b/src/bin/pg_dump/pg_backup_archiver.c @@ -15,7 +15,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.152 2008/01/14 19:27:41 tgl Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.153 2008/03/20 17:36:57 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -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"); diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index c284bbb2da..f9313c5f33 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -12,7 +12,7 @@ * by PostgreSQL * * IDENTIFICATION - * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.482 2008/01/30 18:35:55 tgl Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.483 2008/03/20 17:36:57 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -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")); diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c index 64b7233393..fb93cfa7b1 100644 --- a/src/bin/pg_dump/pg_dumpall.c +++ b/src/bin/pg_dump/pg_dumpall.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * - * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.100 2008/01/01 19:45:55 momjian Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.101 2008/03/20 17:36:57 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -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")); diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index 94f407c45b..dec2b57aac 100644 --- a/src/bin/pg_dump/pg_restore.c +++ b/src/bin/pg_dump/pg_restore.c @@ -34,7 +34,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/bin/pg_dump/pg_restore.c,v 1.85 2007/12/11 19:01:06 tgl Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/pg_restore.c,v 1.86 2008/03/20 17:36:58 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -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"));