diff --git a/doc/src/sgml/ref/pg_dump.sgml b/doc/src/sgml/ref/pg_dump.sgml index 63cca18711a..bfc1e7b3524 100644 --- a/doc/src/sgml/ref/pg_dump.sgml +++ b/doc/src/sgml/ref/pg_dump.sgml @@ -1232,6 +1232,33 @@ PostgreSQL documentation + + + + + Dump data. This is the default. + + + + + + + + + Dump schema (data definitions). This is the default. + + + + + + + + + Dump statistics. This is the default. + + + + diff --git a/doc/src/sgml/ref/pg_dumpall.sgml b/doc/src/sgml/ref/pg_dumpall.sgml index ae5afb3c7d5..765b30a3a66 100644 --- a/doc/src/sgml/ref/pg_dumpall.sgml +++ b/doc/src/sgml/ref/pg_dumpall.sgml @@ -560,6 +560,33 @@ exclude database PATTERN + + + + + Dump data. This is the default. + + + + + + + + + Dump schema (data definitions). This is the default. + + + + + + + + + Dump statistics. This is the default. + + + + diff --git a/doc/src/sgml/ref/pg_restore.sgml b/doc/src/sgml/ref/pg_restore.sgml index 35140187807..c840a807ae9 100644 --- a/doc/src/sgml/ref/pg_restore.sgml +++ b/doc/src/sgml/ref/pg_restore.sgml @@ -805,6 +805,33 @@ PostgreSQL documentation + + + + + Dump data. This is the default. + + + + + + + + + Dump schema (data definitions). This is the default. + + + + + + + + + Dump statistics. This is the default. + + + + diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 4b2dfa5e0bd..e41e645f649 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -433,6 +433,9 @@ main(int argc, char **argv) bool data_only = false; bool schema_only = false; bool statistics_only = false; + bool with_data = false; + bool with_schema = false; + bool with_statistics = false; bool no_data = false; bool no_schema = false; bool no_statistics = false; @@ -509,6 +512,9 @@ main(int argc, char **argv) {"no-toast-compression", no_argument, &dopt.no_toast_compression, 1}, {"no-unlogged-table-data", no_argument, &dopt.no_unlogged_table_data, 1}, {"no-sync", no_argument, NULL, 7}, + {"with-data", no_argument, NULL, 22}, + {"with-schema", no_argument, NULL, 23}, + {"with-statistics", no_argument, NULL, 24}, {"on-conflict-do-nothing", no_argument, &dopt.do_nothing, 1}, {"rows-per-insert", required_argument, NULL, 10}, {"include-foreign-data", required_argument, NULL, 11}, @@ -775,6 +781,18 @@ main(int argc, char **argv) no_statistics = true; break; + case 22: + with_data = true; + break; + + case 23: + with_schema = true; + break; + + case 24: + with_statistics = true; + break; + default: /* getopt_long already emitted a complaint */ pg_log_error_hint("Try \"%s --help\" for more information.", progname); @@ -802,6 +820,7 @@ main(int argc, char **argv) if (dopt.column_inserts && dopt.dump_inserts == 0) dopt.dump_inserts = DUMP_DEFAULT_ROWS_PER_INSERT; + /* reject conflicting "-only" options */ if (data_only && schema_only) pg_fatal("options -s/--schema-only and -a/--data-only cannot be used together"); if (schema_only && statistics_only) @@ -809,6 +828,7 @@ main(int argc, char **argv) if (data_only && statistics_only) pg_fatal("options -a/--data-only and --statistics-only cannot be used together"); + /* reject conflicting "-only" and "no-" options */ if (data_only && no_data) pg_fatal("options -a/--data-only and --no-data cannot be used together"); if (schema_only && no_schema) @@ -816,6 +836,14 @@ main(int argc, char **argv) if (statistics_only && no_statistics) pg_fatal("options --statistics-only and --no-statistics cannot be used together"); + /* reject conflicting "with-" and "no-" options */ + if (with_data && no_data) + pg_fatal("options --with-data and --no-data cannot be used together"); + if (with_schema && no_schema) + pg_fatal("options --with-schema and --no-schema cannot be used together"); + if (with_statistics && no_statistics) + pg_fatal("options --with-statistics and --no-statistics cannot be used together"); + if (schema_only && foreign_servers_include_patterns.head != NULL) pg_fatal("options -s/--schema-only and --include-foreign-data cannot be used together"); @@ -828,10 +856,20 @@ main(int argc, char **argv) if (dopt.if_exists && !dopt.outputClean) pg_fatal("option --if-exists requires option -c/--clean"); - /* set derivative flags */ - dopt.dumpData = data_only || (!schema_only && !statistics_only && !no_data); - dopt.dumpSchema = schema_only || (!data_only && !statistics_only && !no_schema); - dopt.dumpStatistics = statistics_only || (!data_only && !schema_only && !no_statistics); + /* + * Set derivative flags. An "-only" option may be overridden by an + * explicit "with-" option; e.g. "--schema-only --with-statistics" will + * include schema and statistics. Other ambiguous or nonsensical + * combinations, e.g. "--schema-only --no-schema", will have already + * caused an error in one of the checks above. + */ + dopt.dumpData = ((dopt.dumpData && !schema_only && !statistics_only) || + (data_only || with_data)) && !no_data; + dopt.dumpSchema = ((dopt.dumpSchema && !data_only && !statistics_only) || + (schema_only || with_schema)) && !no_schema; + dopt.dumpStatistics = ((dopt.dumpStatistics && !schema_only && !data_only) || + (statistics_only || with_statistics)) && !no_statistics; + /* * --inserts are already implied above if --column-inserts or @@ -1279,6 +1317,9 @@ help(const char *progname) printf(_(" --use-set-session-authorization\n" " use SET SESSION AUTHORIZATION commands instead of\n" " ALTER OWNER commands to set ownership\n")); + printf(_(" --with-data dump the data\n")); + printf(_(" --with-schema dump the schema\n")); + printf(_(" --with-statistics dump the statistics\n")); printf(_("\nConnection options:\n")); printf(_(" -d, --dbname=DBNAME database to dump\n")); diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c index 2935cac2c46..2ea574b0f06 100644 --- a/src/bin/pg_dump/pg_dumpall.c +++ b/src/bin/pg_dump/pg_dumpall.c @@ -111,6 +111,9 @@ static int no_subscriptions = 0; static int no_toast_compression = 0; static int no_unlogged_table_data = 0; static int no_role_passwords = 0; +static int with_data = 0; +static int with_schema = 0; +static int with_statistics = 0; static int server_version; static int load_via_partition_root = 0; static int on_conflict_do_nothing = 0; @@ -184,6 +187,9 @@ main(int argc, char *argv[]) {"no-sync", no_argument, NULL, 4}, {"no-toast-compression", no_argument, &no_toast_compression, 1}, {"no-unlogged-table-data", no_argument, &no_unlogged_table_data, 1}, + {"with-data", no_argument, &with_data, 1}, + {"with-schema", no_argument, &with_schema, 1}, + {"with-statistics", no_argument, &with_statistics, 1}, {"on-conflict-do-nothing", no_argument, &on_conflict_do_nothing, 1}, {"rows-per-insert", required_argument, NULL, 7}, {"statistics-only", no_argument, &statistics_only, 1}, @@ -475,6 +481,12 @@ main(int argc, char *argv[]) appendPQExpBufferStr(pgdumpopts, " --no-toast-compression"); if (no_unlogged_table_data) appendPQExpBufferStr(pgdumpopts, " --no-unlogged-table-data"); + if (with_data) + appendPQExpBufferStr(pgdumpopts, " --with-data"); + if (with_schema) + appendPQExpBufferStr(pgdumpopts, " --with-schema"); + if (with_statistics) + appendPQExpBufferStr(pgdumpopts, " --with-statistics"); if (on_conflict_do_nothing) appendPQExpBufferStr(pgdumpopts, " --on-conflict-do-nothing"); if (statistics_only) @@ -704,6 +716,9 @@ help(void) printf(_(" --use-set-session-authorization\n" " use SET SESSION AUTHORIZATION commands instead of\n" " ALTER OWNER commands to set ownership\n")); + printf(_(" --with-data dump the data\n")); + printf(_(" --with-schema dump the schema\n")); + printf(_(" --with-statistics dump the statistics\n")); printf(_("\nConnection options:\n")); printf(_(" -d, --dbname=CONNSTR connect using connection string\n")); diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index 337e64a8a29..47f7b0dd3a1 100644 --- a/src/bin/pg_dump/pg_restore.c +++ b/src/bin/pg_dump/pg_restore.c @@ -82,6 +82,9 @@ main(int argc, char **argv) static int no_subscriptions = 0; static int strict_names = 0; static int statistics_only = 0; + static int with_data = 0; + static int with_schema = 0; + static int with_statistics = 0; struct option cmdopts[] = { {"clean", 0, NULL, 'c'}, @@ -136,6 +139,9 @@ main(int argc, char **argv) {"no-security-labels", no_argument, &no_security_labels, 1}, {"no-subscriptions", no_argument, &no_subscriptions, 1}, {"no-statistics", no_argument, &no_statistics, 1}, + {"with-data", no_argument, &with_data, 1}, + {"with-schema", no_argument, &with_schema, 1}, + {"with-statistics", no_argument, &with_statistics, 1}, {"statistics-only", no_argument, &statistics_only, 1}, {"filter", required_argument, NULL, 4}, @@ -351,12 +357,29 @@ main(int argc, char **argv) opts->useDB = 1; } + /* reject conflicting "-only" options */ if (data_only && schema_only) pg_fatal("options -s/--schema-only and -a/--data-only cannot be used together"); - if (data_only && statistics_only) - pg_fatal("options -a/--data-only and --statistics-only cannot be used together"); if (schema_only && statistics_only) pg_fatal("options -s/--schema-only and --statistics-only cannot be used together"); + if (data_only && statistics_only) + pg_fatal("options -a/--data-only and --statistics-only cannot be used together"); + + /* reject conflicting "-only" and "no-" options */ + if (data_only && no_data) + pg_fatal("options -a/--data-only and --no-data cannot be used together"); + if (schema_only && no_schema) + pg_fatal("options -s/--schema-only and --no-schema cannot be used together"); + if (statistics_only && no_statistics) + pg_fatal("options --statistics-only and --no-statistics cannot be used together"); + + /* reject conflicting "with-" and "no-" options */ + if (with_data && no_data) + pg_fatal("options --with-data and --no-data cannot be used together"); + if (with_schema && no_schema) + pg_fatal("options --with-schema and --no-schema cannot be used together"); + if (with_statistics && no_statistics) + pg_fatal("options --with-statistics and --no-statistics cannot be used together"); if (data_only && opts->dropSchema) pg_fatal("options -c/--clean and -a/--data-only cannot be used together"); @@ -375,10 +398,19 @@ main(int argc, char **argv) if (opts->single_txn && numWorkers > 1) pg_fatal("cannot specify both --single-transaction and multiple jobs"); - /* set derivative flags */ - opts->dumpData = data_only || (!no_data && !schema_only && !statistics_only); - opts->dumpSchema = schema_only || (!no_schema && !data_only && !statistics_only); - opts->dumpStatistics = statistics_only || (!no_statistics && !data_only && !schema_only); + /* + * Set derivative flags. An "-only" option may be overridden by an + * explicit "with-" option; e.g. "--schema-only --with-statistics" will + * include schema and statistics. Other ambiguous or nonsensical + * combinations, e.g. "--schema-only --no-schema", will have already + * caused an error in one of the checks above. + */ + opts->dumpData = ((opts->dumpData && !schema_only && !statistics_only) || + (data_only || with_data)) && !no_data; + opts->dumpSchema = ((opts->dumpSchema && !data_only && !statistics_only) || + (schema_only || with_schema)) && !no_schema; + opts->dumpStatistics = ((opts->dumpStatistics && !schema_only && !data_only) || + (statistics_only || with_statistics)) && !no_statistics; opts->disable_triggers = disable_triggers; opts->enable_row_security = enable_row_security; @@ -524,6 +556,9 @@ usage(const char *progname) printf(_(" --use-set-session-authorization\n" " use SET SESSION AUTHORIZATION commands instead of\n" " ALTER OWNER commands to set ownership\n")); + printf(_(" --with-data dump the data\n")); + printf(_(" --with-schema dump the schema\n")); + printf(_(" --with-statistics dump the statistics\n")); printf(_("\nConnection options:\n")); printf(_(" -h, --host=HOSTNAME database server host or socket directory\n")); diff --git a/src/bin/pg_dump/t/002_pg_dump.pl b/src/bin/pg_dump/t/002_pg_dump.pl index b6c2487c975..51ebf8ad13c 100644 --- a/src/bin/pg_dump/t/002_pg_dump.pl +++ b/src/bin/pg_dump/t/002_pg_dump.pl @@ -741,6 +741,13 @@ my %pgdump_runs = ( 'postgres', ], }, + schema_only_with_statistics => { + dump_cmd => [ + 'pg_dump', '--no-sync', + "--file=$tempdir/schema_only_with_statistics.sql", '--schema-only', + '--with-statistics', 'postgres', + ], + }, no_schema => { dump_cmd => [ 'pg_dump', '--no-sync', @@ -818,7 +825,8 @@ my %full_runs = ( no_table_access_method => 1, pg_dumpall_dbprivs => 1, pg_dumpall_exclude => 1, - schema_only => 1,); + schema_only => 1, + schema_only_with_statistics => 1,); # This is where the actual tests are defined. my %tests = ( @@ -1024,6 +1032,7 @@ my %tests = ( no_large_objects => 1, no_owner => 1, schema_only => 1, + schema_only_with_statistics => 1, }, }, @@ -1437,6 +1446,7 @@ my %tests = ( }, unlike => { schema_only => 1, + schema_only_with_statistics => 1, no_large_objects => 1, }, }, @@ -1461,6 +1471,7 @@ my %tests = ( binary_upgrade => 1, no_large_objects => 1, schema_only => 1, + schema_only_with_statistics => 1, }, }, @@ -1483,6 +1494,7 @@ my %tests = ( binary_upgrade => 1, no_large_objects => 1, schema_only => 1, + schema_only_with_statistics => 1, }, }, @@ -1649,6 +1661,7 @@ my %tests = ( unlike => { no_large_objects => 1, schema_only => 1, + schema_only_with_statistics => 1, }, }, @@ -1806,6 +1819,7 @@ my %tests = ( exclude_test_table => 1, exclude_test_table_data => 1, schema_only => 1, + schema_only_with_statistics => 1, only_dump_measurement => 1, }, }, @@ -1831,6 +1845,7 @@ my %tests = ( binary_upgrade => 1, exclude_dump_test_schema => 1, schema_only => 1, + schema_only_with_statistics => 1, only_dump_measurement => 1, }, }, @@ -1871,6 +1886,7 @@ my %tests = ( binary_upgrade => 1, exclude_dump_test_schema => 1, schema_only => 1, + schema_only_with_statistics => 1, only_dump_measurement => 1, }, }, @@ -1894,6 +1910,7 @@ my %tests = ( binary_upgrade => 1, exclude_dump_test_schema => 1, schema_only => 1, + schema_only_with_statistics => 1, only_dump_measurement => 1, }, }, @@ -1918,6 +1935,7 @@ my %tests = ( binary_upgrade => 1, exclude_dump_test_schema => 1, schema_only => 1, + schema_only_with_statistics => 1, only_dump_measurement => 1, }, }, @@ -1941,6 +1959,7 @@ my %tests = ( binary_upgrade => 1, exclude_dump_test_schema => 1, schema_only => 1, + schema_only_with_statistics => 1, only_dump_measurement => 1, }, }, @@ -1964,6 +1983,7 @@ my %tests = ( binary_upgrade => 1, exclude_dump_test_schema => 1, schema_only => 1, + schema_only_with_statistics => 1, only_dump_measurement => 1, }, }, @@ -3380,6 +3400,7 @@ my %tests = ( binary_upgrade => 1, exclude_dump_test_schema => 1, schema_only => 1, + schema_only_with_statistics => 1, }, }, @@ -3552,6 +3573,7 @@ my %tests = ( unlike => { binary_upgrade => 1, schema_only => 1, + schema_only_with_statistics => 1, exclude_measurement => 1, only_dump_test_schema => 1, test_schema_plus_large_objects => 1, @@ -4436,6 +4458,7 @@ my %tests = ( no_large_objects => 1, no_privs => 1, schema_only => 1, + schema_only_with_statistics => 1, }, }, @@ -4554,6 +4577,7 @@ my %tests = ( binary_upgrade => 1, exclude_dump_test_schema => 1, schema_only => 1, + schema_only_with_statistics => 1, only_dump_measurement => 1, }, }, @@ -4570,6 +4594,7 @@ my %tests = ( binary_upgrade => 1, exclude_dump_test_schema => 1, schema_only => 1, + schema_only_with_statistics => 1, only_dump_measurement => 1, }, }, @@ -4767,6 +4792,7 @@ my %tests = ( no_schema => 1, section_post_data => 1, statistics_only => 1, + schema_only_with_statistics => 1, }, unlike => { exclude_dump_test_schema => 1, @@ -4795,6 +4821,7 @@ my %tests = ( section_data => 1, section_post_data => 1, statistics_only => 1, + schema_only_with_statistics => 1, }, unlike => { no_statistics => 1,