Hi.
-------------------------------<<<<<<<
/* 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" 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");
-------------------------------<<<<<<<
The above is from src/bin/pg_dump/pg_dump.c, this is too much.
We can just use two IF statements:
if (data_only && (schema_only || with_statistics || statistics_only))
pg_fatal("options %s and %s cannot be used together",
"-a/--data-only",
schema_only ? "-s/--schema-only" :
with_statistics ? "--statistics" :
"--statistics-only");
if (schema_only && (with_statistics || statistics_only))
pg_fatal("options %s and %s cannot be used together",
"-s/--schema-only",
with_statistics ? "--statistics" :
"--statistics-only");
First "if (data_only && (schema_only" implies that the second IF check
won't have a combination
of `` if (schema_only && (data_only``.
Maybe we can use ELSE IF here.
We can do the same thing for pg_restore.c
--
jian
https://www.enterprisedb.com/
From 69a1041a070bb4e733308a6398c28f5da7907d08 Mon Sep 17 00:00:00 2001
From: jian he <[email protected]>
Date: Mon, 2 Mar 2026 12:54:18 +0800
Subject: [PATCH v1 1/1] pg_dump pg_restore refactor --only option error check
discussion: https://postgr.es/m/
commitfest entry: https://commitfest.postgresql.org/patch/
---
src/bin/pg_dump/pg_dump.c | 25 ++++++++++---------------
src/bin/pg_dump/pg_restore.c | 25 ++++++++++---------------
src/bin/pg_dump/t/001_basic.pl | 12 ++++++------
3 files changed, 26 insertions(+), 36 deletions(-)
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index dd8adef0a3e..bb0f155adac 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -827,15 +827,18 @@ main(int argc, char **argv)
dopt.dump_inserts = DUMP_DEFAULT_ROWS_PER_INSERT;
/* reject conflicting "-only" options */
- if (data_only && schema_only)
+ if (data_only && (schema_only || with_statistics || statistics_only))
pg_fatal("options %s and %s cannot be used together",
- "-s/--schema-only", "-a/--data-only");
- if (schema_only && statistics_only)
+ "-a/--data-only",
+ schema_only ? "-s/--schema-only" :
+ with_statistics ? "--statistics" :
+ "--statistics-only");
+
+ if (schema_only && (with_statistics || 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");
+ "-s/--schema-only",
+ with_statistics ? "--statistics" :
+ "--statistics-only");
/* reject conflicting "-only" and "no-" options */
if (data_only && no_data)
@@ -853,14 +856,6 @@ main(int argc, char **argv)
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");
diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c
index 14d886fc86e..0e7acdd720c 100644
--- a/src/bin/pg_dump/pg_restore.c
+++ b/src/bin/pg_dump/pg_restore.c
@@ -422,15 +422,18 @@ main(int argc, char **argv)
}
/* reject conflicting "-only" options */
- if (data_only && schema_only)
+ if (data_only && (schema_only || with_statistics || statistics_only))
pg_fatal("options %s and %s cannot be used together",
- "-s/--schema-only", "-a/--data-only");
- if (schema_only && statistics_only)
+ "-a/--data-only",
+ schema_only ? "-s/--schema-only" :
+ with_statistics ? "--statistics" :
+ "--statistics-only");
+
+ if (schema_only && (with_statistics || 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");
+ "-s/--schema-only",
+ with_statistics ? "--statistics" :
+ "--statistics-only");
/* reject conflicting "-only" and "no-" options */
if (data_only && no_data)
@@ -448,14 +451,6 @@ main(int argc, char **argv)
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");
diff --git a/src/bin/pg_dump/t/001_basic.pl b/src/bin/pg_dump/t/001_basic.pl
index a895bc314b0..c6dafadf35b 100644
--- a/src/bin/pg_dump/t/001_basic.pl
+++ b/src/bin/pg_dump/t/001_basic.pl
@@ -45,9 +45,9 @@ command_fails_like(
'pg_dumpall: too many command-line arguments');
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'
+ [ 'pg_dump', '-a', '-s'],
+ 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(
@@ -86,9 +86,9 @@ command_fails_like(
'pg_restore: error: one of -d/--dbname and -f/--file must be specified');
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'
+ [ 'pg_restore', '-a', '-s', '-f -' ],
+ 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.34.1