1
0
mirror of https://github.com/postgres/postgres.git synced 2025-04-21 12:05:57 +03:00

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>
This commit is contained in:
Jeff Davis 2025-03-25 17:36:38 -07:00
parent 27ee6ede6b
commit bde2fb797a
7 changed files with 210 additions and 11 deletions

View 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>

View 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>

View 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>

View 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"));

View 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"));

View 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"));

View 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,