On Wed, Feb 25, 2026 at 4:09 AM Zsolt Parragi <[email protected]> wrote:
>
> Shouldn't these also use pg_log_error + pg_log_error_hint + exit_nicely?
>

Sure.

> Otherwise it looks good to me.
While rebasing, I found that I missed the combination: --statistics
and --no-statistics.
Since pg_dump will error out on this combination, pg_dumpall should too.



--
jian
https://www.enterprisedb.com/
From e090f07f37290570eab9c103485e5a00c8a5fcea Mon Sep 17 00:00:00 2001
From: jian he <[email protected]>
Date: Mon, 2 Mar 2026 15:17:52 +0800
Subject: [PATCH v7 1/1] pg_dumpall error out conflict options

--roles-only
--tablespaces-only
--statistics-only
--schema-only
--globals-only
--data-only
--statistics

The only permitted combination is `--statistics --statistics-only`, since
pg_dump supports it as well and the semantics(meaning) of "only" are preserved.

These 4 combinations should fail immediately:
--schema-only       --no-schema
--data-only         --no-data
--statistics-only   --no-statistics
--statistics        --no-statistics

discussion: https://postgr.es/m/CACJufxFf5=wsv2msuo8izovplzq1-meamwhw7jx5gnvwo5p...@mail.gmail.com
commitfest entry: https://commitfest.postgresql.org/patch/6459
---
 src/bin/pg_dump/pg_dumpall.c     | 128 ++++++++++++++++++++++++++-----
 src/bin/pg_dump/t/001_basic.pl   | 126 ++++++++++++++++++++++++++++++
 src/bin/pg_dump/t/002_pg_dump.pl |   2 -
 3 files changed, 235 insertions(+), 21 deletions(-)

diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c
index 65f8e3a41f1..21371a8dcfb 100644
--- a/src/bin/pg_dump/pg_dumpall.c
+++ b/src/bin/pg_dump/pg_dumpall.c
@@ -217,6 +217,7 @@ main(int argc, char *argv[])
 	const char *format_name = "p";
 	trivalue	prompt_password = TRI_DEFAULT;
 	bool		data_only = false;
+	bool		schema_only = false;
 	bool		globals_only = false;
 	bool		roles_only = false;
 	bool		tablespaces_only = false;
@@ -226,6 +227,9 @@ main(int argc, char *argv[])
 				ret;
 	int			optindex;
 	DumpOptions dopt;
+	bool		dump_DBs;
+	bool		dump_roles;
+	bool		dump_tablespaces;
 
 	pg_logging_init(argv[0]);
 	pg_logging_set_level(PG_LOG_WARNING);
@@ -321,6 +325,7 @@ main(int argc, char *argv[])
 				break;
 
 			case 's':
+				schema_only = true;
 				appendPQExpBufferStr(pgdumpopts, " -s");
 				break;
 
@@ -428,19 +433,108 @@ main(int argc, char *argv[])
 		exit_nicely(1);
 	}
 
-	/* Make sure the user hasn't specified a mix of globals-only options */
-	if (globals_only && roles_only)
+	if (data_only && no_data)
 	{
 		pg_log_error("options %s and %s cannot be used together",
-					 "-g/--globals-only", "-r/--roles-only");
+					 "-a/--data-only", "--no-data");
 		pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 		exit_nicely(1);
 	}
 
-	if (globals_only && tablespaces_only)
+	if (schema_only && no_schema)
 	{
 		pg_log_error("options %s and %s cannot be used together",
-					 "-g/--globals-only", "-t/--tablespaces-only");
+					 "-s/--schema-only", "--no-schema");
+
+		pg_log_error_hint("Try \"%s --help\" for more information.", progname);
+		exit_nicely(1);
+	}
+
+	if (statistics_only && no_statistics)
+	{
+		pg_log_error("options %s and %s cannot be used together",
+					 "--statistics-only", "--no-statistics");
+
+		pg_log_error_hint("Try \"%s --help\" for more information.", progname);
+		exit_nicely(1);
+	}
+
+	if (with_statistics && no_statistics)
+	{
+		pg_log_error("options %s and %s cannot be used together",
+					 "--statistics", "--no-statistics");
+
+		pg_log_error_hint("Try \"%s --help\" for more information.", progname);
+		exit_nicely(1);
+	}
+
+	/*
+	 * There must be at most one "only" option. --statistics is compatible
+	 * only with --statistics-only.
+	 */
+	if (globals_only &&
+		(roles_only || tablespaces_only || with_statistics || statistics_only || schema_only || data_only))
+	{
+		pg_log_error("options %s and %s cannot be used together",
+					 "-g/--globals-only",
+					 roles_only ? "-r/--roles-only" :
+					 tablespaces_only ? "-t/--tablespaces-only" :
+					 with_statistics ? "--statistics" :
+					 statistics_only ? "--statistics-only" :
+					 schema_only ? "-s/--schema-only" :
+					 "-a/--data-only");
+
+		pg_log_error_hint("Try \"%s --help\" for more information.", progname);
+		exit_nicely(1);
+	}
+
+	if (roles_only &&
+		(tablespaces_only || schema_only || with_statistics || statistics_only || data_only))
+	{
+		pg_log_error("options %s and %s cannot be used together",
+					 "-r/--roles-only",
+					 tablespaces_only ? "-t/--tablespaces-only" :
+					 schema_only ? "-s/--schema-only" :
+					 with_statistics ? "--statistics" :
+					 statistics_only ? "--statistics-only" :
+					 "-a/--data-only");
+
+		pg_log_error_hint("Try \"%s --help\" for more information.", progname);
+		exit_nicely(1);
+	}
+
+	if (tablespaces_only &&
+		(schema_only || with_statistics || statistics_only || data_only))
+	{
+		pg_log_error("options %s and %s cannot be used together",
+					 "-t/--tablespaces-only",
+					 schema_only ? "-s/--schema-only" :
+					 with_statistics ? "--statistics" :
+					 statistics_only ? "--statistics-only" :
+					 "-a/--data-only");
+
+		pg_log_error_hint("Try \"%s --help\" for more information.", progname);
+		exit_nicely(1);
+	}
+
+	if (data_only && (schema_only || with_statistics || statistics_only))
+	{
+		pg_log_error("options %s and %s cannot be used together",
+					 "-a/--data-only",
+					 schema_only ? "-s/--schema-only" :
+					 statistics_only ? "--statistics-only" :
+					 "--statistics");
+		pg_log_error_hint("Try \"%s --help\" for more information.", progname);
+		exit_nicely(1);
+	}
+
+	if (schema_only && (with_statistics || statistics_only))
+	{
+		pg_log_error("options %s and %s cannot be used together",
+					 "-s/--schema-only",
+					 statistics_only ? "--statistics-only" :
+					 "--statistics");
+
 		pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 		exit_nicely(1);
 	}
@@ -449,14 +543,6 @@ main(int argc, char *argv[])
 		pg_fatal("option %s requires option %s",
 				 "--if-exists", "-c/--clean");
 
-	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);
-	}
-
 	/* Get format for dump. */
 	archDumpFormat = parseDumpFormat(format_name);
 
@@ -766,6 +852,10 @@ main(int argc, char *argv[])
 		fprintf(OPF, "\n");
 	}
 
+	dump_DBs = !globals_only && !roles_only && !tablespaces_only;
+	dump_tablespaces = !roles_only && !no_tablespaces;
+	dump_roles = !tablespaces_only;
+
 	if (!data_only && !statistics_only && !no_schema)
 	{
 		/*
@@ -782,13 +872,13 @@ main(int argc, char *argv[])
 		 */
 		if (output_clean || archDumpFormat != archNull)
 		{
-			if (!globals_only && !roles_only && !tablespaces_only)
+			if (dump_DBs)
 				dropDBs(conn);
 
-			if (!roles_only && !no_tablespaces)
+			if (dump_tablespaces)
 				dropTablespaces(conn);
 
-			if (!tablespaces_only)
+			if (dump_roles)
 				dropRoles(conn);
 		}
 
@@ -796,7 +886,7 @@ main(int argc, char *argv[])
 		 * Now create objects as requested.  Be careful that option logic here
 		 * is the same as for drops above.
 		 */
-		if (!tablespaces_only)
+		if (dump_roles)
 		{
 			/* Dump roles (users) */
 			dumpRoles(conn);
@@ -810,7 +900,7 @@ main(int argc, char *argv[])
 		}
 
 		/* Dump tablespaces */
-		if (!roles_only && !no_tablespaces)
+		if (dump_tablespaces)
 			dumpTablespaces(conn);
 	}
 
@@ -823,7 +913,7 @@ main(int argc, char *argv[])
 		fprintf(OPF, "\\unrestrict %s\n\n", restrict_key);
 	}
 
-	if (!globals_only && !roles_only && !tablespaces_only)
+	if (dump_DBs)
 		dumpDatabases(conn);
 
 	if (archDumpFormat == archNull)
diff --git a/src/bin/pg_dump/t/001_basic.pl b/src/bin/pg_dump/t/001_basic.pl
index a895bc314b0..e1aad448905 100644
--- a/src/bin/pg_dump/t/001_basic.pl
+++ b/src/bin/pg_dump/t/001_basic.pl
@@ -220,12 +220,138 @@ command_fails_like(
 	'pg_dumpall: options -g/--globals-only and -t/--tablespaces-only cannot be used together'
 );
 
+command_fails_like(
+	[ 'pg_dumpall', '--data-only', '--no-data'],
+	qr/\Qpg_dumpall: error: options -a\/--data-only and --no-data cannot be used together\E/,
+	'pg_dumpall: error: options --no-data and -a/--data-only cannot be used together'
+);
+
+command_fails_like(
+	[ 'pg_dumpall', '--schema-only', '--no-schema'],
+	qr/\Qpg_dumpall: error: options -s\/--schema-only and --no-schema cannot be used together\E/,
+	'pg_dumpall: error: 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: error: 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: error: options --statistics and --no-statistics cannot be used together'
+);
+
+command_fails_like(
+	[ 'pg_dumpall', '-g', '--statistics' ],
+	qr/\Qpg_dumpall: error: options -g\/--globals-only and --statistics cannot be used together\E/,
+	'pg_dumpall: error: options -g/--globals-only and --statistics cannot be used together'
+);
+
+command_fails_like(
+	[ 'pg_dumpall', '-g', '--statistics-only' ],
+	qr/\Qpg_dumpall: error: options -g\/--globals-only and --statistics-only cannot be used together\E/,
+	'pg_dumpall: error: options -g/--globals-only and --statistics-only cannot be used together'
+);
+
+command_fails_like(
+	[ 'pg_dumpall', '-g', '-s' ],
+	qr/\Qpg_dumpall: error: options -g\/--globals-only and -s\/--schema-only cannot be used together\E/,
+	'pg_dumpall: error: options -g/--globals-only and -s/--schema-only cannot be used together'
+);
+
+command_fails_like(
+	[ 'pg_dumpall', '-g', '-a' ],
+	qr/\Qpg_dumpall: error: options -g\/--globals-only and -a\/--data-only cannot be used together\E/,
+	'pg_dumpall: error: options -g/--globals-only and -a/--data-only cannot be used together'
+);
+
 command_fails_like(
 	[ 'pg_dumpall', '-r', '-t' ],
 	qr/\Qpg_dumpall: error: options -r\/--roles-only and -t\/--tablespaces-only cannot be used together\E/,
 	'pg_dumpall: options -r/--roles-only and -t/--tablespaces-only cannot be used together'
 );
 
+command_fails_like(
+	[ 'pg_dumpall', '-r', '-s' ],
+	qr/\Qpg_dumpall: error: options -r\/--roles-only and -s\/--schema-only cannot be used together\E/,
+	'pg_dumpall: error: options -r/--roles-only and -s/--schema-only cannot be used together'
+);
+
+command_fails_like(
+	[ 'pg_dumpall', '-r', '--statistics' ],
+	qr/\Qpg_dumpall: error: options -r\/--roles-only and --statistics cannot be used together\E/,
+	'pg_dumpall: error: options -r/--roles-only and --statistics cannot be used together'
+);
+
+command_fails_like(
+	[ 'pg_dumpall', '-r', '--statistics-only' ],
+	qr/\Qpg_dumpall: error: options -r\/--roles-only and --statistics-only cannot be used together\E/,
+	'pg_dumpall: error: options -r/--roles-only and --statistics-only cannot be used together'
+);
+
+command_fails_like(
+	[ 'pg_dumpall', '-r', '-a' ],
+	qr/\Qpg_dumpall: error: options -r\/--roles-only and -a\/--data-only cannot be used together\E/,
+	'pg_dumpall: error: options -r/--roles-only and -a/--data-only cannot be used together'
+);
+
+command_fails_like(
+	[ 'pg_dumpall', '-t', '-s' ],
+	qr/\Qpg_dumpall: error: options -t\/--tablespaces-only and -s\/--schema-only cannot be used together\E/,
+	'pg_dumpall: error: options -t/--tablespaces-only and -s/--schema-only cannot be used together'
+);
+
+command_fails_like(
+	[ 'pg_dumpall', '-t', '--statistics' ],
+	qr/\Qpg_dumpall: error: options -t\/--tablespaces-only and --statistics cannot be used together\E/,
+	'pg_dumpall: error: options -t/--tablespaces-only and --statistics cannot be used together'
+);
+
+command_fails_like(
+	[ 'pg_dumpall', '-t', '--statistics-only'],
+	qr/\Qpg_dumpall: error: options -t\/--tablespaces-only and --statistics-only cannot be used together\E/,
+	'pg_dumpall: error: options -t/--tablespaces-only and --statistics-only cannot be used together'
+);
+
+command_fails_like(
+	[ 'pg_dumpall', '-t', '-a'],
+	qr/\Qpg_dumpall: error: options -t\/--tablespaces-only and -a\/--data-only cannot be used together\E/,
+	'pg_dumpall: error: options -t/--tablespaces-only and -a/--data-only cannot be used together'
+);
+
+command_fails_like(
+	[ 'pg_dumpall', '-a', '-s' ],
+	qr/\Qpg_dumpall: error: options -a\/--data-only and -s\/--schema-only cannot be used together\E/,
+	'pg_dumpall: error: options -a/--data-only and -s/--schema-only cannot be used together'
+);
+
+command_fails_like(
+	[ 'pg_dumpall', '-a', '--statistics' ],
+	qr/\Qpg_dumpall: error: options -a\/--data-only and --statistics cannot be used together\E/,
+	'pg_dumpall: error: options -a/--data-only and --statistics cannot be used together'
+);
+
+command_fails_like(
+	[ 'pg_dumpall', '-a', '--statistics-only' ],
+	qr/\Qpg_dumpall: error: options -a\/--data-only and --statistics-only cannot be used together\E/,
+	'pg_dumpall: error: options -a/--data-only and --statistics-only cannot be used together'
+);
+
+command_fails_like(
+	[ 'pg_dumpall', '-s', '--statistics' ],
+	qr/\Qpg_dumpall: error: options -s\/--schema-only and --statistics cannot be used together\E/,
+	'pg_dumpall: error: options -s/--schema-only and --statistics cannot be used together'
+);
+
+command_fails_like(
+	[ 'pg_dumpall', '-s', '--statistics-only' ],
+	qr/\Qpg_dumpall: error: options -s\/--schema-only and --statistics-only cannot be used together\E/,
+	'pg_dumpall: error: options /--schema-only and --statistics-only cannot be used together'
+);
+
 command_fails_like(
 	[ 'pg_dumpall', '--if-exists' ],
 	qr/\Qpg_dumpall: error: option --if-exists requires option -c\/--clean\E/,
diff --git a/src/bin/pg_dump/t/002_pg_dump.pl b/src/bin/pg_dump/t/002_pg_dump.pl
index f15bd06adcc..b06941f7088 100644
--- a/src/bin/pg_dump/t/002_pg_dump.pl
+++ b/src/bin/pg_dump/t/002_pg_dump.pl
@@ -322,7 +322,6 @@ my %pgdump_runs = (
 			'--file' => "$tempdir/pg_dumpall_globals.sql",
 			'--globals-only',
 			'--no-sync',
-			'--statistics',
 		],
 	},
 	pg_dumpall_globals_clean => {
@@ -332,7 +331,6 @@ my %pgdump_runs = (
 			'--globals-only',
 			'--clean',
 			'--no-sync',
-			'--statistics',
 		],
 	},
 	pg_dumpall_dbprivs => {
-- 
2.34.1

Reply via email to