On Sat, Jul 25, 2026 at 11:03:57PM +0900, Fujii Masao wrote: > I'm wondering about backpatching. Patch 0003 fixes undefined behavior > introduced by check_mut_excl_opts(), so it seems appropriate to > backpatch it to v19. Patches 0001 and 0002 are mostly refactoring, > but they restore the v19-era cleanup that was lost when the pg_dumpall > non-text-format support was reverted, so backpatching those to v19 also > seems OK to me. Thoughts?
I've committed (and back-patched) 0003, and I've attached a new version of the remaining patches. At this point, since 19.0 is now released, I don't think we should back-patch these. I plan to commit them within the next couple of days. -- nathan
>From 8725d7b69f82f29899d116d3559f5ad2fc97dce3 Mon Sep 17 00:00:00 2001 From: Nathan Bossart <[email protected]> Date: Tue, 1 Dec 2026 16:28:34 -0600 Subject: [PATCH v3 1/2] pg_dumpall: Fix handling of conflicting options. pg_dumpall is missing checks for some conflicting options, including those passed through to pg_dump. To fix, use check_mut_excl_opts() to check for them, like pg_dump does. This was previously committed as part of commit b2898baaf7, but it was inadvertently reverted by commit 7ca548f23a. As before, no back-patch. Author: Jian He <[email protected]> Co-authored-by: Nathan Bossart <[email protected]> Co-authored-by: Mahendra Singh Thalor <[email protected]> Reviewed-by: Wang Peng <[email protected]> Reviewed-by: Zsolt Parragi <[email protected]> Reviewed-by: Chao Li <[email protected]> Reviewed-by: Fujii Masao <[email protected]> Discussion: https://postgr.es/m/CACJufxFf5%3DwSv2MsuO8iZOvpLZQ1-meAMwhw7JX5gNvWo5PDug%40mail.gmail.com Discussion: https://postgr.es/m/CAKYtNAo-xsBzDQm6bUO_RPPmRkBKFUgnkU0HPhGaeGJHMBx1dA%40mail.gmail.com --- src/bin/pg_dump/pg_dumpall.c | 75 +++++++++++---------- src/bin/pg_dump/t/001_basic.pl | 34 +++++++++- src/bin/pg_dump/t/005_pg_dump_filterfile.pl | 4 +- 3 files changed, 72 insertions(+), 41 deletions(-) diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c index c53e77c2878..aee76bb4761 100644 --- a/src/bin/pg_dump/pg_dumpall.c +++ b/src/bin/pg_dump/pg_dumpall.c @@ -27,6 +27,7 @@ #include "common/string.h" #include "connectdb.h" #include "dumputils.h" +#include "fe_utils/option_utils.h" #include "fe_utils/string_utils.h" #include "filter.h" #include "getopt_long.h" @@ -201,6 +202,7 @@ main(int argc, char *argv[]) bool data_only = false; bool globals_only = false; bool roles_only = false; + bool schema_only = false; bool tablespaces_only = false; PGconn *conn; int encoding; @@ -299,6 +301,7 @@ main(int argc, char *argv[]) break; case 's': + schema_only = true; appendPQExpBufferStr(pgdumpopts, " -s"); break; @@ -395,49 +398,47 @@ main(int argc, char *argv[]) exit_nicely(1); } - if (database_exclude_patterns.head != NULL && - (globals_only || roles_only || tablespaces_only)) - { - pg_log_error("option %s cannot be used together with %s, %s, or %s", - "--exclude-database", - "-g/--globals-only", "-r/--roles-only", "-t/--tablespaces-only"); - pg_log_error_hint("Try \"%s --help\" for more information.", progname); - exit_nicely(1); - } + /* --exclude-database is incompatible with global *-only options */ + check_mut_excl_opts(database_exclude_patterns.head != NULL, "--exclude-database", + globals_only, "-g/--globals-only", + roles_only, "-r/--roles-only", + tablespaces_only, "-t/--tablespaces-only"); - /* Make sure the user hasn't specified a mix of globals-only options */ - if (globals_only && roles_only) - { - pg_log_error("options %s and %s cannot be used together", - "-g/--globals-only", "-r/--roles-only"); - pg_log_error_hint("Try \"%s --help\" for more information.", progname); - exit_nicely(1); - } + /* *-only options are incompatible with each other */ + check_mut_excl_opts(data_only, "-a/--data-only", + globals_only, "-g/--globals-only", + roles_only, "-r/--roles-only", + schema_only, "-s/--schema-only", + statistics_only, "--statistics-only", + tablespaces_only, "-t/--tablespaces-only"); - if (globals_only && tablespaces_only) - { - pg_log_error("options %s and %s cannot be used together", - "-g/--globals-only", "-t/--tablespaces-only"); - pg_log_error_hint("Try \"%s --help\" for more information.", progname); - exit_nicely(1); - } + /* --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"); - if (if_exists && !output_clean) - pg_fatal("option %s requires option %s", - "--if-exists", "-c/--clean"); + /* --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", + roles_only, "-r/--roles-only", + schema_only, "-s/--schema-only", + tablespaces_only, "-t/--tablespaces-only"); /* --clean and --data-only are incompatible */ - if (output_clean && data_only) - pg_fatal("options %s and %s cannot be used together", - "-c/--clean", "-a/--data-only"); + check_mut_excl_opts(output_clean, "-c/--clean", + data_only, "-a/--data-only"); - if (roles_only && tablespaces_only) - { - pg_log_error("options %s and %s cannot be used together", - "-r/--roles-only", "-t/--tablespaces-only"); - pg_log_error_hint("Try \"%s --help\" for more information.", progname); - exit_nicely(1); - } + if (if_exists && !output_clean) + pg_fatal("option %s requires option %s", + "--if-exists", "-c/--clean"); /* * If password values are not required in the dump, switch to using diff --git a/src/bin/pg_dump/t/001_basic.pl b/src/bin/pg_dump/t/001_basic.pl index 95f890e462b..b36fb6d0462 100644 --- a/src/bin/pg_dump/t/001_basic.pl +++ b/src/bin/pg_dump/t/001_basic.pl @@ -251,8 +251,38 @@ command_fails_like( # also fails for -r and -t, but it seems pointless to add more tests for those. command_fails_like( [ 'pg_dumpall', '--exclude-database=foo', '--globals-only' ], - qr/\Qpg_dumpall: error: option --exclude-database cannot be used together with -g\/--globals-only\E/, - 'pg_dumpall: option --exclude-database cannot be used together with -g/--globals-only' + qr/\Qpg_dumpall: error: options --exclude-database and -g\/--globals-only cannot be used together\E/, + 'pg_dumpall: options --exclude-database and -g/--globals-only cannot be used together' +); + +command_fails_like( + [ 'pg_dumpall', '-a', '--no-data' ], + qr/\Qpg_dumpall: error: options -a\/--data-only and --no-data cannot be used together\E/, + 'pg_dumpall: options -a/--data-only and --no-data cannot be used together' +); + +command_fails_like( + [ 'pg_dumpall', '-s', '--no-schema' ], + qr/\Qpg_dumpall: error: options -s\/--schema-only and --no-schema cannot be used together\E/, + 'pg_dumpall: options -s/--schema-only and --no-schema cannot be used together' +); + +command_fails_like( + [ 'pg_dumpall', '--statistics-only', '--no-statistics' ], + qr/\Qpg_dumpall: error: options --statistics-only and --no-statistics cannot be used together\E/, + 'pg_dumpall: options --statistics-only and --no-statistics cannot be used together' +); + +command_fails_like( + [ 'pg_dumpall', '--statistics', '--no-statistics' ], + qr/\Qpg_dumpall: error: options --statistics and --no-statistics cannot be used together\E/, + 'pg_dumpall: options --statistics and --no-statistics cannot be used together' +); + +command_fails_like( + [ 'pg_dumpall', '--statistics', '--tablespaces-only' ], + qr/\Qpg_dumpall: error: options --statistics and -t\/--tablespaces-only cannot be used together\E/, + 'pg_dumpall: options --statistics and -t/--tablespaces-only cannot be used together' ); done_testing(); diff --git a/src/bin/pg_dump/t/005_pg_dump_filterfile.pl b/src/bin/pg_dump/t/005_pg_dump_filterfile.pl index 72a7c90c6af..cecf0442088 100644 --- a/src/bin/pg_dump/t/005_pg_dump_filterfile.pl +++ b/src/bin/pg_dump/t/005_pg_dump_filterfile.pl @@ -570,8 +570,8 @@ command_fails_like( '--filter' => "$tempdir/inputfile.txt", '--globals-only' ], - qr/\Qpg_dumpall: error: option --exclude-database cannot be used together with -g\/--globals-only\E/, - 'pg_dumpall: option --exclude-database cannot be used together with -g/--globals-only' + qr/\Qpg_dumpall: error: options --exclude-database and -g\/--globals-only cannot be used together\E/, + 'pg_dumpall: options --exclude-database and -g/--globals-only cannot be used together' ); # Test invalid filter command -- 2.55.0
>From e9f067a714603eec63a72d34e13c026e61134ffc Mon Sep 17 00:00:00 2001 From: Nathan Bossart <[email protected]> Date: Tue, 1 Dec 2026 16:28:52 -0600 Subject: [PATCH v3 2/2] pg_restore: Refactor handling of conflicting options. This commit makes use of check_mut_excl_opts() for pg_restore's handling of conflicting options. It doesn't fix any bugs, but it does trim several lines of code. This was previously committed as part of commit 7c8280eeb5, but it was reverted by commit 7ca548f23a. Author: Jian He <[email protected]> Co-authored-by: Mahendra Singh Thalor <[email protected]> Reviewed-by: Steven Niu <[email protected]> Reviewed-by: Zsolt Parragi <[email protected]> Reviewed-by: Fujii Masao <[email protected]> Discussion: https://postgr.es/m/CACJufxHDYn%2B3-2jR_kwYB0U7UrNP%2B0EPvAWzBBD5EfUzzr1uiw%40mail.gmail.com Discussion: https://postgr.es/m/CAKYtNAo-xsBzDQm6bUO_RPPmRkBKFUgnkU0HPhGaeGJHMBx1dA%40mail.gmail.com --- src/bin/pg_dump/pg_restore.c | 85 +++++++++++++--------------------- src/bin/pg_dump/t/001_basic.pl | 4 +- 2 files changed, 34 insertions(+), 55 deletions(-) diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index 6e6d2e3c14c..8d8e82ed734 100644 --- a/src/bin/pg_dump/pg_restore.c +++ b/src/bin/pg_dump/pg_restore.c @@ -345,22 +345,15 @@ main(int argc, char **argv) pg_fatal("one of -d/--dbname and -f/--file must be specified"); /* 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 != NULL, "-d/--dbname", + opts->filename != NULL, "-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 != NULL, "-d/--dbname", + opts->restrict_key != NULL, "--restrict-key"); + if (opts->cparams.dbname) opts->useDB = 1; - } else { /* @@ -374,56 +367,42 @@ 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"); + /* *-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"); - /* 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"); + /* --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"); - /* reject conflicting "no-" options */ - if (with_statistics && no_statistics) - pg_fatal("options %s and %s cannot be used together", - "--statistics", "--no-statistics"); + /* --statistics and --no-statistics are incompatible */ + check_mut_excl_opts(with_statistics, "--statistics", + no_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"); + /* --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"); - if (data_only && opts->dropSchema) - pg_fatal("options %s and %s cannot be used together", - "-c/--clean", "-a/--data-only"); + /* --clean and --data-only are incompatible */ + check_mut_excl_opts(opts->dropSchema, "-c/--clean", + data_only, "-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"); + /* --single-transaction and --transaction-size are incompatible */ + check_mut_excl_opts(opts->single_txn, "-1/--single-transaction", + opts->txn_size > 0, "--transaction-size"); /* * -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) diff --git a/src/bin/pg_dump/t/001_basic.pl b/src/bin/pg_dump/t/001_basic.pl index b36fb6d0462..0b82da930d7 100644 --- a/src/bin/pg_dump/t/001_basic.pl +++ b/src/bin/pg_dump/t/001_basic.pl @@ -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( -- 2.55.0
