]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
pg_{dump,restore}: Refactor handling of conflicting options.
authorNathan Bossart <nathan@postgresql.org>
Mon, 9 Mar 2026 16:37:46 +0000 (11:37 -0500)
committerNathan Bossart <nathan@postgresql.org>
Mon, 9 Mar 2026 16:37:46 +0000 (11:37 -0500)
This commit makes use of the function added by commit b2898baaf7
for these applications' handling of conflicting options.  It
doesn't fix any bugs, but it does trim several lines of code.

Author: Jian He <jian.universality@gmail.com>
Reviewed-by: Steven Niu <niushiji@gmail.com>
Reviewed-by: Zsolt Parragi <zsolt.parragi@percona.com>
Discussion: https://postgr.es/m/CACJufxHDYn%2B3-2jR_kwYB0U7UrNP%2B0EPvAWzBBD5EfUzzr1uiw%40mail.gmail.com

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/001_basic.pl
src/bin/pg_dump/t/002_pg_dump.pl
src/bin/pg_dump/t/007_pg_dumpall.pl

index 8bde1b382defd4a2636e1a79517a96ecaec0bbda..137161aa5e05935ef6c40fa4e8dbd679d2d8b87f 100644 (file)
@@ -826,52 +826,39 @@ 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 and %s cannot be used together",
-                                "-s/--schema-only", "-a/--data-only");
-       if (schema_only && statistics_only)
-               pg_fatal("options %s and %s cannot be used together",
-                                "-s/--schema-only", "--statistics-only");
-       if (data_only && statistics_only)
-               pg_fatal("options %s and %s cannot be used together",
-                                "-a/--data-only", "--statistics-only");
-
-       /* reject conflicting "-only" and "no-" options */
-       if (data_only && no_data)
-               pg_fatal("options %s and %s cannot be used together",
-                                "-a/--data-only", "--no-data");
-       if (schema_only && no_schema)
-               pg_fatal("options %s and %s cannot be used together",
-                                "-s/--schema-only", "--no-schema");
-       if (statistics_only && no_statistics)
-               pg_fatal("options %s and %s cannot be used together",
-                                "--statistics-only", "--no-statistics");
-
-       /* reject conflicting "no-" options */
-       if (with_statistics && no_statistics)
-               pg_fatal("options %s and %s cannot be used together",
-                                "--statistics", "--no-statistics");
-
-       /* reject conflicting "-only" options */
-       if (data_only && with_statistics)
-               pg_fatal("options %s and %s cannot be used together",
-                                "-a/--data-only", "--statistics");
-       if (schema_only && with_statistics)
-               pg_fatal("options %s and %s cannot be used together",
-                                "-s/--schema-only", "--statistics");
-
-       if (schema_only && foreign_servers_include_patterns.head != NULL)
-               pg_fatal("options %s and %s cannot be used together",
-                                "-s/--schema-only", "--include-foreign-data");
+       /* *-only options are incompatible with each other */
+       check_mut_excl_opts(data_only, "-a/--data-only",
+                                               schema_only, "-s/--schema-only",
+                                               statistics_only, "--statistics-only");
+
+       /* --no-* and *-only for same thing are incompatible */
+       check_mut_excl_opts(data_only, "-a/--data-only",
+                                               no_data, "--no-data");
+       check_mut_excl_opts(schema_only, "-s/--schema-only",
+                                               no_schema, "--no-schema");
+       check_mut_excl_opts(statistics_only, "--statistics-only",
+                                               no_statistics, "--no-statistics");
+
+       /* --statistics and --no-statistics are incompatible */
+       check_mut_excl_opts(with_statistics, "--statistics",
+                                               no_statistics, "--no-statistics");
+
+       /* --statistics is incompatible with *-only (except --statistics-only) */
+       check_mut_excl_opts(with_statistics, "--statistics",
+                                               data_only, "-a/--data-only",
+                                               schema_only, "-s/--schema-only");
+
+       /* --include-foreign-data is incompatible with --schema-only */
+       check_mut_excl_opts(foreign_servers_include_patterns.head, "--include-foreign-data",
+                                               schema_only, "-s/--schema-only");
 
        if (numWorkers > 1 && foreign_servers_include_patterns.head != NULL)
                pg_fatal("option %s is not supported with parallel backup",
                                 "--include-foreign-data");
 
-       if (data_only && dopt.outputClean)
-               pg_fatal("options %s and %s cannot be used together",
-                                "-c/--clean", "-a/--data-only");
+       /* --clean is incompatible with --data-only */
+       check_mut_excl_opts(dopt.outputClean, "-c/--clean",
+                                               data_only, "-a/--data-only");
 
        if (dopt.if_exists && !dopt.outputClean)
                pg_fatal("option %s requires option %s",
index 4ded902095288b607eb1cba5436355273dca29f5..b29eaa819388ce41fc72042aa91d077bf32f75c3 100644 (file)
@@ -421,7 +421,7 @@ main(int argc, char *argv[])
                exit_nicely(1);
        }
 
-       /* --exclude_database is incompatible with global *-only options */
+       /* --exclude-database is incompatible with global *-only options */
        check_mut_excl_opts(database_exclude_patterns.head, "--exclude-database",
                                                globals_only, "-g/--globals-only",
                                                roles_only, "-r/--roles-only",
index 752d859e264b854b779e542ab4626a685b2c78c7..fb44c0cfdfe4a0cf1fd6dd9032440c33ae925cd4 100644 (file)
@@ -385,31 +385,20 @@ main(int argc, char **argv)
        if (!opts->cparams.dbname && !opts->filename && !opts->tocSummary)
                pg_fatal("one of -d/--dbname and -f/--file must be specified");
 
-       if (db_exclude_patterns.head != NULL && globals_only)
-       {
-               pg_log_error("option %s cannot be used together with %s",
-                                        "--exclude-database", "-g/--globals-only");
-               pg_log_error_hint("Try \"%s --help\" for more information.", progname);
-               exit_nicely(1);
-       }
+       /* --exclude-database and --globals-only are incompatible */
+       check_mut_excl_opts(db_exclude_patterns.head, "--exclude-database",
+                                               globals_only, "-g/--globals-only");
 
        /* Should get at most one of -d and -f, else user is confused */
-       if (opts->cparams.dbname)
-       {
-               if (opts->filename)
-               {
-                       pg_log_error("options %s and %s cannot be used together",
-                                                "-d/--dbname", "-f/--file");
-                       pg_log_error_hint("Try \"%s --help\" for more information.", progname);
-                       exit_nicely(1);
-               }
+       check_mut_excl_opts(opts->cparams.dbname, "-d/--dbname",
+                                               opts->filename, "-f/--file");
 
-               if (opts->restrict_key)
-                       pg_fatal("options %s and %s cannot be used together",
-                                        "-d/--dbname", "--restrict-key");
+       /* --dbname and --restrict-key are incompatible */
+       check_mut_excl_opts(opts->cparams.dbname, "-d/--dbname",
+                                               opts->restrict_key, "--restrict-key");
 
+       if (opts->cparams.dbname)
                opts->useDB = 1;
-       }
        else
        {
                /*
@@ -423,85 +412,54 @@ main(int argc, char **argv)
                        pg_fatal("invalid restrict key");
        }
 
-       /* reject conflicting "-only" options */
-       if (data_only && schema_only)
-               pg_fatal("options %s and %s cannot be used together",
-                                "-s/--schema-only", "-a/--data-only");
-       if (schema_only && statistics_only)
-               pg_fatal("options %s and %s cannot be used together",
-                                "-s/--schema-only", "--statistics-only");
-       if (data_only && statistics_only)
-               pg_fatal("options %s and %s cannot be used together",
-                                "-a/--data-only", "--statistics-only");
-
-       /* reject conflicting "-only" and "no-" options */
-       if (data_only && no_data)
-               pg_fatal("options %s and %s cannot be used together",
-                                "-a/--data-only", "--no-data");
-       if (schema_only && no_schema)
-               pg_fatal("options %s and %s cannot be used together",
-                                "-s/--schema-only", "--no-schema");
-       if (statistics_only && no_statistics)
-               pg_fatal("options %s and %s cannot be used together",
-                                "--statistics-only", "--no-statistics");
-
-       /* reject conflicting "no-" options */
-       if (with_statistics && no_statistics)
-               pg_fatal("options %s and %s cannot be used together",
-                                "--statistics", "--no-statistics");
-
-       /* reject conflicting "only-" options */
-       if (data_only && with_statistics)
-               pg_fatal("options %s and %s cannot be used together",
-                                "-a/--data-only", "--statistics");
-       if (schema_only && with_statistics)
-               pg_fatal("options %s and %s cannot be used together",
-                                "-s/--schema-only", "--statistics");
-
-       if (data_only && opts->dropSchema)
-               pg_fatal("options %s and %s cannot be used together",
-                                "-c/--clean", "-a/--data-only");
-
-       if (opts->single_txn && opts->txn_size > 0)
-               pg_fatal("options %s and %s cannot be used together",
-                                "-1/--single-transaction", "--transaction-size");
-
-       if (opts->single_txn && globals_only)
-               pg_fatal("options %s and %s cannot be used together when restoring an archive created by pg_dumpall",
-                                "--single-transaction", "-g/--globals-only");
-
-       if (opts->txn_size && globals_only)
-               pg_fatal("options %s and %s cannot be used together when restoring an archive created by pg_dumpall",
-                                "--transaction-size", "-g/--globals-only");
-
-       if (opts->exit_on_error && globals_only)
-               pg_fatal("options %s and %s cannot be used together when restoring an archive created by pg_dumpall",
-                                "--exit-on-error", "-g/--globals-only");
-
-       if (data_only && globals_only)
-               pg_fatal("options %s and %s cannot be used together",
-                                "-a/--data-only", "-g/--globals-only");
-       if (schema_only && globals_only)
-               pg_fatal("options %s and %s cannot be used together",
-                                "-s/--schema-only", "-g/--globals-only");
-       if (statistics_only && globals_only)
-               pg_fatal("options %s and %s cannot be used together",
-                                "--statistics-only", "-g/--globals-only");
-       if (with_statistics && globals_only)
-               pg_fatal("options %s and %s cannot be used together",
-                                "--statistics", "-g/--globals-only");
-
-       if (no_globals && globals_only)
-               pg_fatal("options %s and %s cannot be used together",
-                                "--no-globals", "-g/--globals-only");
+       /* *-only options are incompatible with each other */
+       check_mut_excl_opts(data_only, "-a/--data-only",
+                                               globals_only, "-g/--globals-only",
+                                               schema_only, "-s/--schema-only",
+                                               statistics_only, "--statistics-only");
+
+       /* --no-* and *-only for same thing are incompatible */
+       check_mut_excl_opts(data_only, "-a/--data-only",
+                                               no_data, "--no-data");
+       check_mut_excl_opts(globals_only, "-g/--globals-only",
+                                               no_globals, "--no-globals");
+       check_mut_excl_opts(schema_only, "-s/--schema-only",
+                                               no_schema, "--no-schema");
+       check_mut_excl_opts(statistics_only, "--statistics-only",
+                                               no_statistics, "--no-statistics");
+
+       /* --statistics and --no-statistics are incompatible */
+       check_mut_excl_opts(with_statistics, "--statistics",
+                                               no_statistics, "--no-statistics");
+
+       /* --statistics is incompatible with *-only (except --statistics-only) */
+       check_mut_excl_opts(with_statistics, "--statistics",
+                                               data_only, "-a/--data-only",
+                                               globals_only, "-g/--globals-only",
+                                               schema_only, "-s/--schema-only");
+
+       /* --clean and --data-only are incompatible */
+       check_mut_excl_opts(opts->dropSchema, "-c/--clean",
+                                               data_only, "-a/--data-only");
+
+       /*
+        * --globals-only, --single-transaction, and --transaction-size are
+        * incompatible.
+        */
+       check_mut_excl_opts(globals_only, "-g/--globals-only",
+                                               opts->single_txn, "-1/--single-transaction",
+                                               opts->txn_size, "--transaction-size");
+
+       /* --exit-on-error and --globals-only are incompatible */
+       check_mut_excl_opts(opts->exit_on_error, "--exit-on-error",
+                                               globals_only, "-g/--globals-only");
 
        /*
         * -C is not compatible with -1, because we can't create a database inside
         * a transaction block.
         */
-       if (opts->createDB && opts->single_txn)
-               pg_fatal("options %s and %s cannot be used together",
-                                "-C/--create", "-1/--single-transaction");
+       check_mut_excl_opts(opts->createDB, "-C/--create",
+                                               opts->single_txn, "-1/--single-transaction");
 
        /* Can't do single-txn mode with multiple connections */
        if (opts->single_txn && numWorkers > 1)
index 67131a674f4466c5faf3480cec57815b5fd3fd58..2f5eb48e7b86c4e4ad8dfb3d358855dcd4fcbb14 100644 (file)
@@ -46,8 +46,8 @@ command_fails_like(
 
 command_fails_like(
        [ 'pg_dump', '-s', '-a' ],
-       qr/\Qpg_dump: error: options -s\/--schema-only and -a\/--data-only cannot be used together\E/,
-       'pg_dump: options -s/--schema-only and -a/--data-only cannot be used together'
+       qr/\Qpg_dump: error: options -a\/--data-only and -s\/--schema-only cannot be used together\E/,
+       'pg_dump: options -a/--data-only and -s/--schema-only cannot be used together'
 );
 
 command_fails_like(
@@ -64,8 +64,8 @@ command_fails_like(
 
 command_fails_like(
        [ 'pg_dump', '-s', '--include-foreign-data=xxx' ],
-       qr/\Qpg_dump: error: options -s\/--schema-only and --include-foreign-data cannot be used together\E/,
-       'pg_dump: options -s/--schema-only and --include-foreign-data cannot be used together'
+       qr/\Qpg_dump: error: options --include-foreign-data and -s\/--schema-only cannot be used together\E/,
+       'pg_dump: options --include-foreign-data and -s/--schema-only cannot be used together'
 );
 
 command_fails_like(
@@ -87,8 +87,8 @@ command_fails_like(
 
 command_fails_like(
        [ 'pg_restore', '-s', '-a', '-f -' ],
-       qr/\Qpg_restore: error: options -s\/--schema-only and -a\/--data-only cannot be used together\E/,
-       'pg_restore: options -s/--schema-only and -a/--data-only cannot be used together'
+       qr/\Qpg_restore: error: options -a\/--data-only and -s\/--schema-only cannot be used together\E/,
+       'pg_restore: options -a/--data-only and -s/--schema-only cannot be used together'
 );
 
 command_fails_like(
@@ -300,8 +300,8 @@ command_fails_like(
 
 command_fails_like(
        [ 'pg_restore', '--exclude-database=foo', '--globals-only', '-d', 'xxx' ],
-       qr/\Qpg_restore: error: option --exclude-database cannot be used together with -g\/--globals-only\E/,
-       'pg_restore: option --exclude-database cannot be used together with -g/--globals-only'
+       qr/\Qpg_restore: error: options --exclude-database and -g\/--globals-only cannot be used together\E/,
+       'pg_restore: options --exclude-database and -g/--globals-only cannot be used together'
 );
 
 command_fails_like(
@@ -312,14 +312,14 @@ command_fails_like(
 
 command_fails_like(
        [ 'pg_restore', '--schema-only', '--globals-only', '-d', 'xxx' ],
-       qr/\Qpg_restore: error: options -s\/--schema-only and -g\/--globals-only cannot be used together\E/,
-       'pg_restore: error: options -s/--schema-only and -g/--globals-only cannot be used together'
+       qr/\Qpg_restore: error: options -g\/--globals-only and -s\/--schema-only cannot be used together\E/,
+       'pg_restore: error: options -g/--globals-only and -s/--schema-only cannot be used together'
 );
 
 command_fails_like(
        [ 'pg_restore', '--statistics-only', '--globals-only', '-d', 'xxx' ],
-       qr/\Qpg_restore: error: options --statistics-only and -g\/--globals-only cannot be used together\E/,
-       'pg_restore: error: options --statistics-only and -g/--globals-only cannot be used together'
+       qr/\Qpg_restore: error: options -g\/--globals-only and --statistics-only cannot be used together\E/,
+       'pg_restore: error: options -g/--globals-only and --statistics-only cannot be used together'
 );
 
 command_fails_like(
@@ -339,6 +339,6 @@ command_fails_like(
                'pg_restore', '--globals-only', '--no-globals', '-d', 'xxx',
                'dumpdir'
        ],
-       qr/\Qpg_restore: error: options --no-globals and -g\/--globals-only cannot be used together\E/,
+       qr/\Qpg_restore: error: options -g\/--globals-only and --no-globals cannot be used together\E/,
        'options --no-globals and --globals-only cannot be used together');
 done_testing();
index e7cc998cfbad273005c2d30665ec53c5cbb244e6..6d1d38128fcf77d8f55a0026a2f580545654dd5d 100644 (file)
@@ -5077,8 +5077,8 @@ command_fails_like(
                '--schema-only',
                '--statistics',
        ],
-       qr/\Qpg_dump: error: options -s\/--schema-only and --statistics cannot be used together\E/,
-       'cannot use --schema-only and --statistics together');
+       qr/\Qpg_dump: error: options --statistics and -s\/--schema-only cannot be used together\E/,
+       'cannot use --statistics and --schema-only together');
 
 command_fails_like(
        [
index c16c27d7387c2030a4ae3cd304ad6c918d52245e..22f11a13a9a68736e94e7a15ad02b67c6f5a1d34 100644 (file)
@@ -520,7 +520,7 @@ $node->command_fails_like(
                '--schema-only',
                '--file' => "$tempdir/error_test.sql",
        ],
-       qr/\Qpg_restore: error: options -s\/--schema-only and -g\/--globals-only cannot be used together\E/,
+       qr/\Qpg_restore: error: options -g\/--globals-only and -s\/--schema-only cannot be used together\E/,
        'When --globals-only and --schema-only are used together');
 
 # report an error when --globals-only and --statistics-only are used together
@@ -533,7 +533,7 @@ $node->command_fails_like(
                '--statistics-only',
                '--file' => "$tempdir/error_test.sql",
        ],
-       qr/\Qpg_restore: error: options --statistics-only and -g\/--globals-only cannot be used together\E/,
+       qr/\Qpg_restore: error: options -g\/--globals-only and --statistics-only cannot be used together\E/,
        'When --globals-only and --statistics-only are used together');
 
 # report an error when --globals-only and --statistics are used together
@@ -572,7 +572,7 @@ $node->command_fails_like(
                '--single-transaction',
                '--file' => "$tempdir/error_test.sql",
        ],
-       qr/\Qpg_restore: error: options --single-transaction and -g\/--globals-only cannot be used together\E/,
+       qr/\Qpg_restore: error: options -g\/--globals-only and -1\/--single-transaction cannot be used together\E/,
        'When --globals-only and --single-transaction are used together');
 
 # report an error when --globals-only and --transaction-size are used together
@@ -585,7 +585,7 @@ $node->command_fails_like(
                '--transaction-size' => '100',
                '--file' => "$tempdir/error_test.sql",
        ],
-       qr/\Qpg_restore: error: options --transaction-size and -g\/--globals-only cannot be used together\E/,
+       qr/\Qpg_restore: error: options -g\/--globals-only and --transaction-size cannot be used together\E/,
        'When --globals-only and --transaction-size are used together');
 
 # verify map.dat preamble exists