Add pg_dump --with-{schema|data|statistics} options.
authorJeff Davis <jdavis@postgresql.org>
Wed, 26 Mar 2025 00:36:38 +0000 (17:36 -0700)
committerJeff Davis <jdavis@postgresql.org>
Wed, 26 Mar 2025 00:36:38 +0000 (17:36 -0700)
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>
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_dump.c
src/bin/pg_dump/pg_dumpall.c
src/bin/pg_dump/pg_restore.c
src/bin/pg_dump/t/002_pg_dump.pl

index 63cca18711a4121baf68d2c6b33e17a6c781f97d..bfc1e7b352476954170afc3703fee3aca10bc117 100644 (file)
@@ -1232,6 +1232,33 @@ PostgreSQL documentation
       </listitem>
      </varlistentry>
 
+     <varlistentry>
+      <term><option>--with-data</option></term>
+      <listitem>
+       <para>
+        Dump data. This is the default.
+       </para>
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
+      <term><option>--with-schema</option></term>
+      <listitem>
+       <para>
+        Dump schema (data definitions). This is the default.
+       </para>
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
+      <term><option>--with-statistics</option></term>
+      <listitem>
+       <para>
+        Dump statistics. This is the default.
+       </para>
+      </listitem>
+     </varlistentry>
+
      <varlistentry>
       <term><option>--on-conflict-do-nothing</option></term>
       <listitem>
index ae5afb3c7d536683bec2539632641163c15831c2..765b30a3a668eb5a0150224de14db0d40c50a32d 100644 (file)
@@ -560,6 +560,33 @@ exclude database <replaceable class="parameter">PATTERN</replaceable>
       </listitem>
      </varlistentry>
 
+     <varlistentry>
+      <term><option>--with-data</option></term>
+      <listitem>
+       <para>
+        Dump data. This is the default.
+       </para>
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
+      <term><option>--with-schema</option></term>
+      <listitem>
+       <para>
+        Dump schema (data definitions). This is the default.
+       </para>
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
+      <term><option>--with-statistics</option></term>
+      <listitem>
+       <para>
+        Dump statistics. This is the default.
+       </para>
+      </listitem>
+     </varlistentry>
+
      <varlistentry>
       <term><option>--no-unlogged-table-data</option></term>
       <listitem>
index 351401878078f0c953f1719ccd04b2dce74280a6..c840a807ae90f2d854a8aaf2b0a51ba21a0e3627 100644 (file)
@@ -805,6 +805,33 @@ PostgreSQL documentation
       </listitem>
      </varlistentry>
 
+     <varlistentry>
+      <term><option>--with-data</option></term>
+      <listitem>
+       <para>
+        Dump data. This is the default.
+       </para>
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
+      <term><option>--with-schema</option></term>
+      <listitem>
+       <para>
+        Dump schema (data definitions). This is the default.
+       </para>
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
+      <term><option>--with-statistics</option></term>
+      <listitem>
+       <para>
+        Dump statistics. This is the default.
+       </para>
+      </listitem>
+     </varlistentry>
+
      <varlistentry>
        <term><option>--section=<replaceable class="parameter">sectionname</replaceable></option></term>
        <listitem>
index 4b2dfa5e0bd661480c70c30241b4615ce9a72867..e41e645f649c5cb7d9a4dd25063bea4b0f4512fe 100644 (file)
@@ -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"));
index 2935cac2c467c4fcf25ceb4583fb61426521298f..2ea574b0f065d4302c5293bee44a6aa2128bd7f3 100644 (file)
@@ -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"));
index 337e64a8a291ebc3a58b509c6f7e43505011ffba..47f7b0dd3a13aaeb8455a62dc67be2272edb899e 100644 (file)
@@ -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"));
index b6c2487c9750edb446cd5a2caaab281c30e93e98..51ebf8ad13c10c5290405ca2252108a741e86052 100644 (file)
@@ -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,