Skip to content

Commit bde2fb7

Browse files
committed
Add pg_dump --with-{schema|data|statistics} options.
By adding the positive variants of options, in addition to the negative variants that already exist, users can be explicit about what pg_dump should produce. Discussion: https://postgr.es/m/bd0513e4b1ea2b2f2d06f02720c6579711cb62a6.camel@j-davis.com Reviewed-by: Corey Huinker <corey.huinker@gmail.com> Reviewed-by: Andres Freund <andres@anarazel.de>
1 parent 27ee6ed commit bde2fb7

File tree

7 files changed

+210
-11
lines changed

7 files changed

+210
-11
lines changed

doc/src/sgml/ref/pg_dump.sgml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,33 @@ PostgreSQL documentation
12321232
</listitem>
12331233
</varlistentry>
12341234

1235+
<varlistentry>
1236+
<term><option>--with-data</option></term>
1237+
<listitem>
1238+
<para>
1239+
Dump data. This is the default.
1240+
</para>
1241+
</listitem>
1242+
</varlistentry>
1243+
1244+
<varlistentry>
1245+
<term><option>--with-schema</option></term>
1246+
<listitem>
1247+
<para>
1248+
Dump schema (data definitions). This is the default.
1249+
</para>
1250+
</listitem>
1251+
</varlistentry>
1252+
1253+
<varlistentry>
1254+
<term><option>--with-statistics</option></term>
1255+
<listitem>
1256+
<para>
1257+
Dump statistics. This is the default.
1258+
</para>
1259+
</listitem>
1260+
</varlistentry>
1261+
12351262
<varlistentry>
12361263
<term><option>--on-conflict-do-nothing</option></term>
12371264
<listitem>

doc/src/sgml/ref/pg_dumpall.sgml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,33 @@ exclude database <replaceable class="parameter">PATTERN</replaceable>
560560
</listitem>
561561
</varlistentry>
562562

563+
<varlistentry>
564+
<term><option>--with-data</option></term>
565+
<listitem>
566+
<para>
567+
Dump data. This is the default.
568+
</para>
569+
</listitem>
570+
</varlistentry>
571+
572+
<varlistentry>
573+
<term><option>--with-schema</option></term>
574+
<listitem>
575+
<para>
576+
Dump schema (data definitions). This is the default.
577+
</para>
578+
</listitem>
579+
</varlistentry>
580+
581+
<varlistentry>
582+
<term><option>--with-statistics</option></term>
583+
<listitem>
584+
<para>
585+
Dump statistics. This is the default.
586+
</para>
587+
</listitem>
588+
</varlistentry>
589+
563590
<varlistentry>
564591
<term><option>--no-unlogged-table-data</option></term>
565592
<listitem>

doc/src/sgml/ref/pg_restore.sgml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,33 @@ PostgreSQL documentation
805805
</listitem>
806806
</varlistentry>
807807

808+
<varlistentry>
809+
<term><option>--with-data</option></term>
810+
<listitem>
811+
<para>
812+
Dump data. This is the default.
813+
</para>
814+
</listitem>
815+
</varlistentry>
816+
817+
<varlistentry>
818+
<term><option>--with-schema</option></term>
819+
<listitem>
820+
<para>
821+
Dump schema (data definitions). This is the default.
822+
</para>
823+
</listitem>
824+
</varlistentry>
825+
826+
<varlistentry>
827+
<term><option>--with-statistics</option></term>
828+
<listitem>
829+
<para>
830+
Dump statistics. This is the default.
831+
</para>
832+
</listitem>
833+
</varlistentry>
834+
808835
<varlistentry>
809836
<term><option>--section=<replaceable class="parameter">sectionname</replaceable></option></term>
810837
<listitem>

src/bin/pg_dump/pg_dump.c

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,9 @@ main(int argc, char **argv)
433433
bool data_only = false;
434434
bool schema_only = false;
435435
bool statistics_only = false;
436+
bool with_data = false;
437+
bool with_schema = false;
438+
bool with_statistics = false;
436439
bool no_data = false;
437440
bool no_schema = false;
438441
bool no_statistics = false;
@@ -509,6 +512,9 @@ main(int argc, char **argv)
509512
{"no-toast-compression", no_argument, &dopt.no_toast_compression, 1},
510513
{"no-unlogged-table-data", no_argument, &dopt.no_unlogged_table_data, 1},
511514
{"no-sync", no_argument, NULL, 7},
515+
{"with-data", no_argument, NULL, 22},
516+
{"with-schema", no_argument, NULL, 23},
517+
{"with-statistics", no_argument, NULL, 24},
512518
{"on-conflict-do-nothing", no_argument, &dopt.do_nothing, 1},
513519
{"rows-per-insert", required_argument, NULL, 10},
514520
{"include-foreign-data", required_argument, NULL, 11},
@@ -775,6 +781,18 @@ main(int argc, char **argv)
775781
no_statistics = true;
776782
break;
777783

784+
case 22:
785+
with_data = true;
786+
break;
787+
788+
case 23:
789+
with_schema = true;
790+
break;
791+
792+
case 24:
793+
with_statistics = true;
794+
break;
795+
778796
default:
779797
/* getopt_long already emitted a complaint */
780798
pg_log_error_hint("Try \"%s --help\" for more information.", progname);
@@ -802,20 +820,30 @@ main(int argc, char **argv)
802820
if (dopt.column_inserts && dopt.dump_inserts == 0)
803821
dopt.dump_inserts = DUMP_DEFAULT_ROWS_PER_INSERT;
804822

823+
/* reject conflicting "-only" options */
805824
if (data_only && schema_only)
806825
pg_fatal("options -s/--schema-only and -a/--data-only cannot be used together");
807826
if (schema_only && statistics_only)
808827
pg_fatal("options -s/--schema-only and --statistics-only cannot be used together");
809828
if (data_only && statistics_only)
810829
pg_fatal("options -a/--data-only and --statistics-only cannot be used together");
811830

831+
/* reject conflicting "-only" and "no-" options */
812832
if (data_only && no_data)
813833
pg_fatal("options -a/--data-only and --no-data cannot be used together");
814834
if (schema_only && no_schema)
815835
pg_fatal("options -s/--schema-only and --no-schema cannot be used together");
816836
if (statistics_only && no_statistics)
817837
pg_fatal("options --statistics-only and --no-statistics cannot be used together");
818838

839+
/* reject conflicting "with-" and "no-" options */
840+
if (with_data && no_data)
841+
pg_fatal("options --with-data and --no-data cannot be used together");
842+
if (with_schema && no_schema)
843+
pg_fatal("options --with-schema and --no-schema cannot be used together");
844+
if (with_statistics && no_statistics)
845+
pg_fatal("options --with-statistics and --no-statistics cannot be used together");
846+
819847
if (schema_only && foreign_servers_include_patterns.head != NULL)
820848
pg_fatal("options -s/--schema-only and --include-foreign-data cannot be used together");
821849

@@ -828,10 +856,20 @@ main(int argc, char **argv)
828856
if (dopt.if_exists && !dopt.outputClean)
829857
pg_fatal("option --if-exists requires option -c/--clean");
830858

831-
/* set derivative flags */
832-
dopt.dumpData = data_only || (!schema_only && !statistics_only && !no_data);
833-
dopt.dumpSchema = schema_only || (!data_only && !statistics_only && !no_schema);
834-
dopt.dumpStatistics = statistics_only || (!data_only && !schema_only && !no_statistics);
859+
/*
860+
* Set derivative flags. An "-only" option may be overridden by an
861+
* explicit "with-" option; e.g. "--schema-only --with-statistics" will
862+
* include schema and statistics. Other ambiguous or nonsensical
863+
* combinations, e.g. "--schema-only --no-schema", will have already
864+
* caused an error in one of the checks above.
865+
*/
866+
dopt.dumpData = ((dopt.dumpData && !schema_only && !statistics_only) ||
867+
(data_only || with_data)) && !no_data;
868+
dopt.dumpSchema = ((dopt.dumpSchema && !data_only && !statistics_only) ||
869+
(schema_only || with_schema)) && !no_schema;
870+
dopt.dumpStatistics = ((dopt.dumpStatistics && !schema_only && !data_only) ||
871+
(statistics_only || with_statistics)) && !no_statistics;
872+
835873

836874
/*
837875
* --inserts are already implied above if --column-inserts or
@@ -1279,6 +1317,9 @@ help(const char *progname)
12791317
printf(_(" --use-set-session-authorization\n"
12801318
" use SET SESSION AUTHORIZATION commands instead of\n"
12811319
" ALTER OWNER commands to set ownership\n"));
1320+
printf(_(" --with-data dump the data\n"));
1321+
printf(_(" --with-schema dump the schema\n"));
1322+
printf(_(" --with-statistics dump the statistics\n"));
12821323

12831324
printf(_("\nConnection options:\n"));
12841325
printf(_(" -d, --dbname=DBNAME database to dump\n"));

src/bin/pg_dump/pg_dumpall.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ static int no_subscriptions = 0;
111111
static int no_toast_compression = 0;
112112
static int no_unlogged_table_data = 0;
113113
static int no_role_passwords = 0;
114+
static int with_data = 0;
115+
static int with_schema = 0;
116+
static int with_statistics = 0;
114117
static int server_version;
115118
static int load_via_partition_root = 0;
116119
static int on_conflict_do_nothing = 0;
@@ -184,6 +187,9 @@ main(int argc, char *argv[])
184187
{"no-sync", no_argument, NULL, 4},
185188
{"no-toast-compression", no_argument, &no_toast_compression, 1},
186189
{"no-unlogged-table-data", no_argument, &no_unlogged_table_data, 1},
190+
{"with-data", no_argument, &with_data, 1},
191+
{"with-schema", no_argument, &with_schema, 1},
192+
{"with-statistics", no_argument, &with_statistics, 1},
187193
{"on-conflict-do-nothing", no_argument, &on_conflict_do_nothing, 1},
188194
{"rows-per-insert", required_argument, NULL, 7},
189195
{"statistics-only", no_argument, &statistics_only, 1},
@@ -475,6 +481,12 @@ main(int argc, char *argv[])
475481
appendPQExpBufferStr(pgdumpopts, " --no-toast-compression");
476482
if (no_unlogged_table_data)
477483
appendPQExpBufferStr(pgdumpopts, " --no-unlogged-table-data");
484+
if (with_data)
485+
appendPQExpBufferStr(pgdumpopts, " --with-data");
486+
if (with_schema)
487+
appendPQExpBufferStr(pgdumpopts, " --with-schema");
488+
if (with_statistics)
489+
appendPQExpBufferStr(pgdumpopts, " --with-statistics");
478490
if (on_conflict_do_nothing)
479491
appendPQExpBufferStr(pgdumpopts, " --on-conflict-do-nothing");
480492
if (statistics_only)
@@ -704,6 +716,9 @@ help(void)
704716
printf(_(" --use-set-session-authorization\n"
705717
" use SET SESSION AUTHORIZATION commands instead of\n"
706718
" ALTER OWNER commands to set ownership\n"));
719+
printf(_(" --with-data dump the data\n"));
720+
printf(_(" --with-schema dump the schema\n"));
721+
printf(_(" --with-statistics dump the statistics\n"));
707722

708723
printf(_("\nConnection options:\n"));
709724
printf(_(" -d, --dbname=CONNSTR connect using connection string\n"));

src/bin/pg_dump/pg_restore.c

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ main(int argc, char **argv)
8282
static int no_subscriptions = 0;
8383
static int strict_names = 0;
8484
static int statistics_only = 0;
85+
static int with_data = 0;
86+
static int with_schema = 0;
87+
static int with_statistics = 0;
8588

8689
struct option cmdopts[] = {
8790
{"clean", 0, NULL, 'c'},
@@ -136,6 +139,9 @@ main(int argc, char **argv)
136139
{"no-security-labels", no_argument, &no_security_labels, 1},
137140
{"no-subscriptions", no_argument, &no_subscriptions, 1},
138141
{"no-statistics", no_argument, &no_statistics, 1},
142+
{"with-data", no_argument, &with_data, 1},
143+
{"with-schema", no_argument, &with_schema, 1},
144+
{"with-statistics", no_argument, &with_statistics, 1},
139145
{"statistics-only", no_argument, &statistics_only, 1},
140146
{"filter", required_argument, NULL, 4},
141147

@@ -351,12 +357,29 @@ main(int argc, char **argv)
351357
opts->useDB = 1;
352358
}
353359

360+
/* reject conflicting "-only" options */
354361
if (data_only && schema_only)
355362
pg_fatal("options -s/--schema-only and -a/--data-only cannot be used together");
356-
if (data_only && statistics_only)
357-
pg_fatal("options -a/--data-only and --statistics-only cannot be used together");
358363
if (schema_only && statistics_only)
359364
pg_fatal("options -s/--schema-only and --statistics-only cannot be used together");
365+
if (data_only && statistics_only)
366+
pg_fatal("options -a/--data-only and --statistics-only cannot be used together");
367+
368+
/* reject conflicting "-only" and "no-" options */
369+
if (data_only && no_data)
370+
pg_fatal("options -a/--data-only and --no-data cannot be used together");
371+
if (schema_only && no_schema)
372+
pg_fatal("options -s/--schema-only and --no-schema cannot be used together");
373+
if (statistics_only && no_statistics)
374+
pg_fatal("options --statistics-only and --no-statistics cannot be used together");
375+
376+
/* reject conflicting "with-" and "no-" options */
377+
if (with_data && no_data)
378+
pg_fatal("options --with-data and --no-data cannot be used together");
379+
if (with_schema && no_schema)
380+
pg_fatal("options --with-schema and --no-schema cannot be used together");
381+
if (with_statistics && no_statistics)
382+
pg_fatal("options --with-statistics and --no-statistics cannot be used together");
360383

361384
if (data_only && opts->dropSchema)
362385
pg_fatal("options -c/--clean and -a/--data-only cannot be used together");
@@ -375,10 +398,19 @@ main(int argc, char **argv)
375398
if (opts->single_txn && numWorkers > 1)
376399
pg_fatal("cannot specify both --single-transaction and multiple jobs");
377400

378-
/* set derivative flags */
379-
opts->dumpData = data_only || (!no_data && !schema_only && !statistics_only);
380-
opts->dumpSchema = schema_only || (!no_schema && !data_only && !statistics_only);
381-
opts->dumpStatistics = statistics_only || (!no_statistics && !data_only && !schema_only);
401+
/*
402+
* Set derivative flags. An "-only" option may be overridden by an
403+
* explicit "with-" option; e.g. "--schema-only --with-statistics" will
404+
* include schema and statistics. Other ambiguous or nonsensical
405+
* combinations, e.g. "--schema-only --no-schema", will have already
406+
* caused an error in one of the checks above.
407+
*/
408+
opts->dumpData = ((opts->dumpData && !schema_only && !statistics_only) ||
409+
(data_only || with_data)) && !no_data;
410+
opts->dumpSchema = ((opts->dumpSchema && !data_only && !statistics_only) ||
411+
(schema_only || with_schema)) && !no_schema;
412+
opts->dumpStatistics = ((opts->dumpStatistics && !schema_only && !data_only) ||
413+
(statistics_only || with_statistics)) && !no_statistics;
382414

383415
opts->disable_triggers = disable_triggers;
384416
opts->enable_row_security = enable_row_security;
@@ -524,6 +556,9 @@ usage(const char *progname)
524556
printf(_(" --use-set-session-authorization\n"
525557
" use SET SESSION AUTHORIZATION commands instead of\n"
526558
" ALTER OWNER commands to set ownership\n"));
559+
printf(_(" --with-data dump the data\n"));
560+
printf(_(" --with-schema dump the schema\n"));
561+
printf(_(" --with-statistics dump the statistics\n"));
527562

528563
printf(_("\nConnection options:\n"));
529564
printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));

0 commit comments

Comments
 (0)