hi.

please check the attached.

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

the above will all error out.
``pg_dumpall --globals-only --statistics`` should error,
the HEAD behavior does not respect "--statistics", maybe we can make
it not error out, but
that would contradict the meaning of "--globals-only", i think.

pg_dumpall --roles-only --no-schema --file=1.sql
pg_dumpall --roles-only --no-data --file=2.sql
pg_dumpall --roles-only --no-statistics --file=3.sql
pg_dumpall --tablespaces-only --no-schema --file=1.sql
pg_dumpall --tablespaces-only --no-data --file=2.sql
pg_dumpall --tablespaces-only --no-statistics --file=3.sql

The items listed above respect the 'only' option but ignore the 'no' option."


--
jian
https://www.enterprisedb.com/
From 9c2f7879094a94e001cb7019d285bba713a77a80 Mon Sep 17 00:00:00 2001
From: jian he <[email protected]>
Date: Tue, 3 Feb 2026 10:10:58 +0800
Subject: [PATCH v1 1/1] pg_dumpall only option fix

make the below option error out:

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

discussion: https://postgr.es/m/
---
 src/bin/pg_dump/pg_dumpall.c     | 118 ++++++++++++++++++++-----------
 src/bin/pg_dump/t/002_pg_dump.pl |   2 -
 2 files changed, 77 insertions(+), 43 deletions(-)

diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c
index 30fecd0c252..1c315b94d26 100644
--- a/src/bin/pg_dump/pg_dumpall.c
+++ b/src/bin/pg_dump/pg_dumpall.c
@@ -105,11 +105,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_statistics = 0;
 static int	server_version;
 static int	load_via_partition_root = 0;
 static int	on_conflict_do_nothing = 0;
-static int	statistics_only = 0;
 static int	sequence_data = 0;
 
 static char role_catalog[10];
@@ -182,8 +180,8 @@ main(int argc, char *argv[])
 		{"no-unlogged-table-data", no_argument, &no_unlogged_table_data, 1},
 		{"on-conflict-do-nothing", no_argument, &on_conflict_do_nothing, 1},
 		{"rows-per-insert", required_argument, NULL, 7},
-		{"statistics", no_argument, &with_statistics, 1},
-		{"statistics-only", no_argument, &statistics_only, 1},
+		{"statistics", no_argument, NULL, 10},
+		{"statistics-only", no_argument, NULL, 11},
 		{"filter", required_argument, NULL, 8},
 		{"sequence-data", no_argument, &sequence_data, 1},
 		{"restrict-key", required_argument, NULL, 9},
@@ -198,6 +196,9 @@ main(int argc, char *argv[])
 	char	   *use_role = NULL;
 	const char *dumpencoding = NULL;
 	trivalue	prompt_password = TRI_DEFAULT;
+	bool		with_statistics = false;
+	bool		statistics_only = false;
+	bool		schema_only = false;
 	bool		data_only = false;
 	bool		globals_only = false;
 	bool		roles_only = false;
@@ -299,6 +300,7 @@ main(int argc, char *argv[])
 				break;
 
 			case 's':
+				schema_only = true;
 				appendPQExpBufferStr(pgdumpopts, " -s");
 				break;
 
@@ -378,6 +380,12 @@ main(int argc, char *argv[])
 				appendPQExpBufferStr(pgdumpopts, " --restrict-key ");
 				appendShellString(pgdumpopts, optarg);
 				break;
+			case 10:
+				with_statistics = true;
+				break;
+			case 11:
+				statistics_only = true;
+				break;
 
 			default:
 				/* getopt_long already emitted a complaint */
@@ -434,6 +442,37 @@ main(int argc, char *argv[])
 		exit_nicely(1);
 	}
 
+	/* reject conflicting "-only" options */
+	if (globals_only && with_statistics)
+		pg_fatal("options %s and %s cannot be used together",
+				 "-g/--globals-only", "--statistics");
+
+	if (data_only && roles_only)
+		pg_fatal("options %s and %s cannot be used together",
+				 "-a/--data-only", "-r/--roles-only");
+	if (schema_only && roles_only)
+		pg_fatal("options %s and %s cannot be used together",
+				 "-s/--schema-only", "-r/--roles-only");
+	if (statistics_only && roles_only)
+		pg_fatal("options %s and %s cannot be used together",
+				 "--statistics-only", "-r/--roles-only");
+	if (with_statistics && roles_only)
+		pg_fatal("options %s and %s cannot be used together",
+				 "--statistics", "-r/--roles-only");
+
+	if (data_only && tablespaces_only)
+		pg_fatal("options %s and %s cannot be used together",
+				 "-a/--data-only", "-t/--tablespaces-only");
+	if (schema_only && tablespaces_only)
+		pg_fatal("options %s and %s cannot be used together",
+				 "-s/--schema-only", "-t/--tablespaces-only");
+	if (statistics_only && tablespaces_only)
+		pg_fatal("options %s and %s cannot be used together",
+				 "--statistics-only", "-t/--tablespaces-only");
+	if (with_statistics && tablespaces_only)
+		pg_fatal("options %s and %s cannot be used together",
+				 "--statistics", "-t/--tablespaces-only");
+
 	/*
 	 * If password values are not required in the dump, switch to using
 	 * pg_roles which is equally useful, just more likely to have unrestricted
@@ -623,48 +662,45 @@ main(int argc, char *argv[])
 	fprintf(OPF, "SET standard_conforming_strings = on;\n");
 	fprintf(OPF, "\n");
 
-	if (!data_only && !statistics_only && !no_schema)
+	/*
+	 * If asked to --clean, do that first.  We can avoid detailed dependency
+	 * analysis because databases never depend on each other, and tablespaces
+	 * never depend on each other.  Roles could have grants to each other, but
+	 * DROP ROLE will clean those up silently.
+	 */
+	if (output_clean)
 	{
-		/*
-		 * If asked to --clean, do that first.  We can avoid detailed
-		 * dependency analysis because databases never depend on each other,
-		 * and tablespaces never depend on each other.  Roles could have
-		 * grants to each other, but DROP ROLE will clean those up silently.
-		 */
-		if (output_clean)
-		{
-			if (!globals_only && !roles_only && !tablespaces_only)
-				dropDBs(conn);
+		if (!globals_only && !roles_only && !tablespaces_only)
+			dropDBs(conn);
 
-			if (!roles_only && !no_tablespaces)
-				dropTablespaces(conn);
-
-			if (!tablespaces_only)
-				dropRoles(conn);
-		}
-
-		/*
-		 * Now create objects as requested.  Be careful that option logic here
-		 * is the same as for drops above.
-		 */
-		if (!tablespaces_only)
-		{
-			/* Dump roles (users) */
-			dumpRoles(conn);
-
-			/* Dump role memberships */
-			dumpRoleMembership(conn);
-
-			/* Dump role GUC privileges */
-			if (server_version >= 150000 && !skip_acls)
-				dumpRoleGUCPrivs(conn);
-		}
-
-		/* Dump tablespaces */
 		if (!roles_only && !no_tablespaces)
-			dumpTablespaces(conn);
+			dropTablespaces(conn);
+
+		if (!tablespaces_only)
+			dropRoles(conn);
 	}
 
+	/*
+	 * Now create objects as requested.  Be careful that option logic here is
+	 * the same as for drops above.
+	 */
+	if (!tablespaces_only)
+	{
+		/* Dump roles (users) */
+		dumpRoles(conn);
+
+		/* Dump role memberships */
+		dumpRoleMembership(conn);
+
+		/* Dump role GUC privileges */
+		if (server_version >= 150000 && !skip_acls)
+			dumpRoleGUCPrivs(conn);
+	}
+
+	/* Dump tablespaces */
+	if (!roles_only && !no_tablespaces)
+		dumpTablespaces(conn);
+
 	/*
 	 * Exit restricted mode just before dumping the databases.  pg_dump will
 	 * handle entering restricted mode again as appropriate.
diff --git a/src/bin/pg_dump/t/002_pg_dump.pl b/src/bin/pg_dump/t/002_pg_dump.pl
index a8dcc2b5c75..340cf953a60 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