Skip to content

Commit b84ef44

Browse files
jeff-davishari90
authored andcommitted
Add pg_dump --with-{schema|data|statistics} options.
yb conflict resolutions: - src/bin/pg_dump/t/002_pg_dump.pl >@@ @@ -1483,6 +1494,7 @@ my %tests = ( >+ schema_only_with_statistics => 1, Cause: Missing 00d9dcf: 'LO create (with no data)' test was added. Resolution: Skip test since its not in pg15. >@@ -3380,6 +3400,7 @@ my %tests = ( >+ schema_only_with_statistics => 1, Cause: Missing 1a05c1d: 'COPY test_compression_method' test was added. Resolution: Skip test since its not in pg15. <@@ -3552,6 +3573,7 @@ my %tests = ( >+ schema_only_with_statistics => 1, Cause: Missing a563c24: 'COPY measurement' test was added. Resolution: Skip test since its not in pg15. 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> (cherry picked from commit bde2fb7)
1 parent af45bcb commit b84ef44

File tree

7 files changed

+207
-11
lines changed

7 files changed

+207
-11
lines changed

doc/src/sgml/ref/pg_dump.sgml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,33 @@ PostgreSQL documentation
10121012
</listitem>
10131013
</varlistentry>
10141014

1015+
<varlistentry>
1016+
<term><option>--with-data</option></term>
1017+
<listitem>
1018+
<para>
1019+
Dump data. This is the default.
1020+
</para>
1021+
</listitem>
1022+
</varlistentry>
1023+
1024+
<varlistentry>
1025+
<term><option>--with-schema</option></term>
1026+
<listitem>
1027+
<para>
1028+
Dump schema (data definitions). This is the default.
1029+
</para>
1030+
</listitem>
1031+
</varlistentry>
1032+
1033+
<varlistentry>
1034+
<term><option>--with-statistics</option></term>
1035+
<listitem>
1036+
<para>
1037+
Dump statistics. This is the default.
1038+
</para>
1039+
</listitem>
1040+
</varlistentry>
1041+
10151042
<varlistentry>
10161043
<term><option>--on-conflict-do-nothing</option></term>
10171044
<listitem>

doc/src/sgml/ref/pg_dumpall.sgml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,33 @@ PostgreSQL documentation
520520
</listitem>
521521
</varlistentry>
522522

523+
<varlistentry>
524+
<term><option>--with-data</option></term>
525+
<listitem>
526+
<para>
527+
Dump data. This is the default.
528+
</para>
529+
</listitem>
530+
</varlistentry>
531+
532+
<varlistentry>
533+
<term><option>--with-schema</option></term>
534+
<listitem>
535+
<para>
536+
Dump schema (data definitions). This is the default.
537+
</para>
538+
</listitem>
539+
</varlistentry>
540+
541+
<varlistentry>
542+
<term><option>--with-statistics</option></term>
543+
<listitem>
544+
<para>
545+
Dump statistics. This is the default.
546+
</para>
547+
</listitem>
548+
</varlistentry>
549+
523550
<varlistentry>
524551
<term><option>--no-unlogged-table-data</option></term>
525552
<listitem>

doc/src/sgml/ref/pg_restore.sgml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,33 @@ PostgreSQL documentation
715715
</listitem>
716716
</varlistentry>
717717

718+
<varlistentry>
719+
<term><option>--with-data</option></term>
720+
<listitem>
721+
<para>
722+
Dump data. This is the default.
723+
</para>
724+
</listitem>
725+
</varlistentry>
726+
727+
<varlistentry>
728+
<term><option>--with-schema</option></term>
729+
<listitem>
730+
<para>
731+
Dump schema (data definitions). This is the default.
732+
</para>
733+
</listitem>
734+
</varlistentry>
735+
736+
<varlistentry>
737+
<term><option>--with-statistics</option></term>
738+
<listitem>
739+
<para>
740+
Dump statistics. This is the default.
741+
</para>
742+
</listitem>
743+
</varlistentry>
744+
718745
<varlistentry>
719746
<term><option>--section=<replaceable class="parameter">sectionname</replaceable></option></term>
720747
<listitem>

src/bin/pg_dump/pg_dump.c

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,9 @@ main(int argc, char **argv)
349349
bool data_only = false;
350350
bool schema_only = false;
351351
bool statistics_only = false;
352+
bool with_data = false;
353+
bool with_schema = false;
354+
bool with_statistics = false;
352355
bool no_data = false;
353356
bool no_schema = false;
354357
bool no_statistics = false;
@@ -422,6 +425,9 @@ main(int argc, char **argv)
422425
{"no-toast-compression", no_argument, &dopt.no_toast_compression, 1},
423426
{"no-unlogged-table-data", no_argument, &dopt.no_unlogged_table_data, 1},
424427
{"no-sync", no_argument, NULL, 7},
428+
{"with-data", no_argument, NULL, 22},
429+
{"with-schema", no_argument, NULL, 23},
430+
{"with-statistics", no_argument, NULL, 24},
425431
{"on-conflict-do-nothing", no_argument, &dopt.do_nothing, 1},
426432
{"rows-per-insert", required_argument, NULL, 10},
427433
{"include-foreign-data", required_argument, NULL, 11},
@@ -651,6 +657,18 @@ main(int argc, char **argv)
651657
no_statistics = true;
652658
break;
653659

660+
case 22:
661+
with_data = true;
662+
break;
663+
664+
case 23:
665+
with_schema = true;
666+
break;
667+
668+
case 24:
669+
with_statistics = true;
670+
break;
671+
654672
default:
655673
/* getopt_long already emitted a complaint */
656674
pg_log_error_hint("Try \"%s --help\" for more information.", progname);
@@ -686,20 +704,30 @@ main(int argc, char **argv)
686704
if (dopt.binary_upgrade)
687705
dopt.sequence_data = 1;
688706

707+
/* reject conflicting "-only" options */
689708
if (data_only && schema_only)
690709
pg_fatal("options -s/--schema-only and -a/--data-only cannot be used together");
691710
if (schema_only && statistics_only)
692711
pg_fatal("options -s/--schema-only and --statistics-only cannot be used together");
693712
if (data_only && statistics_only)
694713
pg_fatal("options -a/--data-only and --statistics-only cannot be used together");
695714

715+
/* reject conflicting "-only" and "no-" options */
696716
if (data_only && no_data)
697717
pg_fatal("options -a/--data-only and --no-data cannot be used together");
698718
if (schema_only && no_schema)
699719
pg_fatal("options -s/--schema-only and --no-schema cannot be used together");
700720
if (statistics_only && no_statistics)
701721
pg_fatal("options --statistics-only and --no-statistics cannot be used together");
702722

723+
/* reject conflicting "with-" and "no-" options */
724+
if (with_data && no_data)
725+
pg_fatal("options --with-data and --no-data cannot be used together");
726+
if (with_schema && no_schema)
727+
pg_fatal("options --with-schema and --no-schema cannot be used together");
728+
if (with_statistics && no_statistics)
729+
pg_fatal("options --with-statistics and --no-statistics cannot be used together");
730+
703731
if (schema_only && foreign_servers_include_patterns.head != NULL)
704732
pg_fatal("options -s/--schema-only and --include-foreign-data cannot be used together");
705733

@@ -712,10 +740,20 @@ main(int argc, char **argv)
712740
if (dopt.if_exists && !dopt.outputClean)
713741
pg_fatal("option --if-exists requires option -c/--clean");
714742

715-
/* set derivative flags */
716-
dopt.dumpData = data_only || (!schema_only && !statistics_only && !no_data);
717-
dopt.dumpSchema = schema_only || (!data_only && !statistics_only && !no_schema);
718-
dopt.dumpStatistics = statistics_only || (!data_only && !schema_only && !no_statistics);
743+
/*
744+
* Set derivative flags. An "-only" option may be overridden by an
745+
* explicit "with-" option; e.g. "--schema-only --with-statistics" will
746+
* include schema and statistics. Other ambiguous or nonsensical
747+
* combinations, e.g. "--schema-only --no-schema", will have already
748+
* caused an error in one of the checks above.
749+
*/
750+
dopt.dumpData = ((dopt.dumpData && !schema_only && !statistics_only) ||
751+
(data_only || with_data)) && !no_data;
752+
dopt.dumpSchema = ((dopt.dumpSchema && !data_only && !statistics_only) ||
753+
(schema_only || with_schema)) && !no_schema;
754+
dopt.dumpStatistics = ((dopt.dumpStatistics && !schema_only && !data_only) ||
755+
(statistics_only || with_statistics)) && !no_statistics;
756+
719757

720758
/*
721759
* --inserts are already implied above if --column-inserts or
@@ -1102,6 +1140,9 @@ help(const char *progname)
11021140
printf(_(" --use-set-session-authorization\n"
11031141
" use SET SESSION AUTHORIZATION commands instead of\n"
11041142
" ALTER OWNER commands to set ownership\n"));
1143+
printf(_(" --with-data dump the data\n"));
1144+
printf(_(" --with-schema dump the schema\n"));
1145+
printf(_(" --with-statistics dump the statistics\n"));
11051146

11061147
printf(_("\nConnection options:\n"));
11071148
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
@@ -85,6 +85,9 @@ static int no_subscriptions = 0;
8585
static int no_toast_compression = 0;
8686
static int no_unlogged_table_data = 0;
8787
static int no_role_passwords = 0;
88+
static int with_data = 0;
89+
static int with_schema = 0;
90+
static int with_statistics = 0;
8891
static int server_version;
8992
static int load_via_partition_root = 0;
9093
static int on_conflict_do_nothing = 0;
@@ -157,6 +160,9 @@ main(int argc, char *argv[])
157160
{"no-sync", no_argument, NULL, 4},
158161
{"no-toast-compression", no_argument, &no_toast_compression, 1},
159162
{"no-unlogged-table-data", no_argument, &no_unlogged_table_data, 1},
163+
{"with-data", no_argument, &with_data, 1},
164+
{"with-schema", no_argument, &with_schema, 1},
165+
{"with-statistics", no_argument, &with_statistics, 1},
160166
{"on-conflict-do-nothing", no_argument, &on_conflict_do_nothing, 1},
161167
{"rows-per-insert", required_argument, NULL, 7},
162168
{"statistics-only", no_argument, &statistics_only, 1},
@@ -441,6 +447,12 @@ main(int argc, char *argv[])
441447
appendPQExpBufferStr(pgdumpopts, " --no-toast-compression");
442448
if (no_unlogged_table_data)
443449
appendPQExpBufferStr(pgdumpopts, " --no-unlogged-table-data");
450+
if (with_data)
451+
appendPQExpBufferStr(pgdumpopts, " --with-data");
452+
if (with_schema)
453+
appendPQExpBufferStr(pgdumpopts, " --with-schema");
454+
if (with_statistics)
455+
appendPQExpBufferStr(pgdumpopts, " --with-statistics");
444456
if (on_conflict_do_nothing)
445457
appendPQExpBufferStr(pgdumpopts, " --on-conflict-do-nothing");
446458
if (statistics_only)
@@ -668,6 +680,9 @@ help(void)
668680
printf(_(" --use-set-session-authorization\n"
669681
" use SET SESSION AUTHORIZATION commands instead of\n"
670682
" ALTER OWNER commands to set ownership\n"));
683+
printf(_(" --with-data dump the data\n"));
684+
printf(_(" --with-schema dump the schema\n"));
685+
printf(_(" --with-statistics dump the statistics\n"));
671686

672687
printf(_("\nConnection options:\n"));
673688
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
@@ -80,6 +80,9 @@ main(int argc, char **argv)
8080
static int no_subscriptions = 0;
8181
static int strict_names = 0;
8282
static int statistics_only = 0;
83+
static int with_data = 0;
84+
static int with_schema = 0;
85+
static int with_statistics = 0;
8386

8487
struct option cmdopts[] = {
8588
{"clean", 0, NULL, 'c'},
@@ -132,6 +135,9 @@ main(int argc, char **argv)
132135
{"no-security-labels", no_argument, &no_security_labels, 1},
133136
{"no-subscriptions", no_argument, &no_subscriptions, 1},
134137
{"no-statistics", no_argument, &no_statistics, 1},
138+
{"with-data", no_argument, &with_data, 1},
139+
{"with-schema", no_argument, &with_schema, 1},
140+
{"with-statistics", no_argument, &with_statistics, 1},
135141
{"statistics-only", no_argument, &statistics_only, 1},
136142

137143
{NULL, 0, NULL, 0}
@@ -334,12 +340,29 @@ main(int argc, char **argv)
334340
opts->useDB = 1;
335341
}
336342

343+
/* reject conflicting "-only" options */
337344
if (data_only && schema_only)
338345
pg_fatal("options -s/--schema-only and -a/--data-only cannot be used together");
339-
if (data_only && statistics_only)
340-
pg_fatal("options -a/--data-only and --statistics-only cannot be used together");
341346
if (schema_only && statistics_only)
342347
pg_fatal("options -s/--schema-only and --statistics-only cannot be used together");
348+
if (data_only && statistics_only)
349+
pg_fatal("options -a/--data-only and --statistics-only cannot be used together");
350+
351+
/* reject conflicting "-only" and "no-" options */
352+
if (data_only && no_data)
353+
pg_fatal("options -a/--data-only and --no-data cannot be used together");
354+
if (schema_only && no_schema)
355+
pg_fatal("options -s/--schema-only and --no-schema cannot be used together");
356+
if (statistics_only && no_statistics)
357+
pg_fatal("options --statistics-only and --no-statistics cannot be used together");
358+
359+
/* reject conflicting "with-" and "no-" options */
360+
if (with_data && no_data)
361+
pg_fatal("options --with-data and --no-data cannot be used together");
362+
if (with_schema && no_schema)
363+
pg_fatal("options --with-schema and --no-schema cannot be used together");
364+
if (with_statistics && no_statistics)
365+
pg_fatal("options --with-statistics and --no-statistics cannot be used together");
343366

344367
if (data_only && opts->dropSchema)
345368
pg_fatal("options -c/--clean and -a/--data-only cannot be used together");
@@ -355,10 +378,19 @@ main(int argc, char **argv)
355378
if (opts->single_txn && numWorkers > 1)
356379
pg_fatal("cannot specify both --single-transaction and multiple jobs");
357380

358-
/* set derivative flags */
359-
opts->dumpData = data_only || (!no_data && !schema_only && !statistics_only);
360-
opts->dumpSchema = schema_only || (!no_schema && !data_only && !statistics_only);
361-
opts->dumpStatistics = statistics_only || (!no_statistics && !data_only && !schema_only);
381+
/*
382+
* Set derivative flags. An "-only" option may be overridden by an
383+
* explicit "with-" option; e.g. "--schema-only --with-statistics" will
384+
* include schema and statistics. Other ambiguous or nonsensical
385+
* combinations, e.g. "--schema-only --no-schema", will have already
386+
* caused an error in one of the checks above.
387+
*/
388+
opts->dumpData = ((opts->dumpData && !schema_only && !statistics_only) ||
389+
(data_only || with_data)) && !no_data;
390+
opts->dumpSchema = ((opts->dumpSchema && !data_only && !statistics_only) ||
391+
(schema_only || with_schema)) && !no_schema;
392+
opts->dumpStatistics = ((opts->dumpStatistics && !schema_only && !data_only) ||
393+
(statistics_only || with_statistics)) && !no_statistics;
362394

363395
opts->disable_triggers = disable_triggers;
364396
opts->enable_row_security = enable_row_security;
@@ -501,6 +533,9 @@ usage(const char *progname)
501533
printf(_(" --use-set-session-authorization\n"
502534
" use SET SESSION AUTHORIZATION commands instead of\n"
503535
" ALTER OWNER commands to set ownership\n"));
536+
printf(_(" --with-data dump the data\n"));
537+
printf(_(" --with-schema dump the schema\n"));
538+
printf(_(" --with-statistics dump the statistics\n"));
504539

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

0 commit comments

Comments
 (0)