From ea9736f5209ec800ec8ff576f1251e5e5bf1a22b Mon Sep 17 00:00:00 2001
From: Mahendra Singh Thalor <mahi6run@gmail.com>
Date: Mon, 20 Jul 2026 15:27:00 +0530
Subject: [PATCH v2] pg_restore: Use check_mut_excl_opts() for
 mutually-exclusive options

Replace the ad hoc pairwise if/pg_fatal() and if/pg_log_error() checks
for conflicting command-line options with calls to the shared
check_mut_excl_opts() helper, matching the style already used in
pg_dump.c. This covers -d/--dbname vs -f/--file, -d/--dbname vs
--restrict-key, the *-only options, their --no-* counterparts,
--statistics vs --no-statistics, --statistics vs *-only, --clean vs
--data-only, --single-transaction vs --transaction-size, and -C vs
--single-transaction.

No behavior change; error message wording and option-reporting
order are preserved exactly.
---
 src/bin/pg_dump/pg_restore.c | 94 ++++++++++++++----------------------
 1 file changed, 36 insertions(+), 58 deletions(-)

diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c
index 84b8d410c9e..c9c358a3657 100644
--- a/src/bin/pg_dump/pg_restore.c
+++ b/src/bin/pg_dump/pg_restore.c
@@ -348,22 +348,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, "-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
 	{
 		/*
@@ -377,56 +370,41 @@ 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");
+	/* *-only options are incompatible with each other */
+	check_mut_excl_opts(schema_only, "-s/--schema-only",
+						data_only, "-a/--data-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(data_only, "-a/--data-only",
+						schema_only, "-s/--schema-only",
+						with_statistics, "--statistics");
+
+	/* --clean is incompatible with --data-only */
+	check_mut_excl_opts(opts->dropSchema, "-c/--clean",
+						data_only, "-a/--data-only");
+
+	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)
-- 
2.52.0

