On Wed, Feb 4, 2026 at 9:56 AM wangpeng <[email protected]> wrote: > > Hi, > I reviewed and tested this patch. I noticed that: > pg_dumpall --globals-only --statistics ----> error > pg_dumpall --globals-only --statistics-only ----> pass > maybe there is inconsistent for *-only options > is that intentional? >
Thanks for pointing this out. It should fail too. I missed this combination. The attached v2 should be bullet-proof. On Wed, Feb 4, 2026 at 5:25 AM Nathan Bossart <[email protected]> wrote: > > I wonder if we ought to create "derivative flags" like we did for pg_dump > in commit 96a81c1be9. That could make some of this stuff easier to > maintain and to follow. https://git.postgresql.org/cgit/postgresql.git/commit/?id=96a81c1be929d122719bd289f6e24824f37e1ff6 added new fields to RestoreOptions and DumpOptions. These global objects dump(roles, tablespaces) are not directly related to pg_restore for now, pg_restore does not support options like --roles-only or --tablespaces-only. Creating "derivative flags" requires careful consideration of their default values, which adds complexity for relatively little benefit. Overall we don't need to implement similar logic now, i think. commitgest entry: https://commitfest.postgresql.org/patch/6459 -- jian https://www.enterprisedb.com/
From cc69f0ddb9c1dd03d76337306fee8e26ff82dbd4 Mon Sep 17 00:00:00 2001 From: jian he <[email protected]> Date: Wed, 4 Feb 2026 16:10:00 +0800 Subject: [PATCH v2 1/1] pg_dumpall error out conflict option make the below command error out: pg_dumpall --globals-only --statistics-only pg_dumpall --globals-only --statistics 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 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 | 107 ++++++++++++++++++++----------- src/bin/pg_dump/t/001_basic.pl | 60 +++++++++++++++++ src/bin/pg_dump/t/002_pg_dump.pl | 2 - 3 files changed, 130 insertions(+), 39 deletions(-) diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c index 30fecd0c252..cfbd31f3bd4 100644 --- a/src/bin/pg_dump/pg_dumpall.c +++ b/src/bin/pg_dump/pg_dumpall.c @@ -198,6 +198,7 @@ main(int argc, char *argv[]) char *use_role = NULL; const char *dumpencoding = NULL; trivalue prompt_password = TRI_DEFAULT; + 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; @@ -434,6 +436,40 @@ 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 (globals_only && statistics_only) + pg_fatal("options %s and %s cannot be used together", + "-g/--globals-only", "--statistics-only"); + + 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 (with_statistics && roles_only) + pg_fatal("options %s and %s cannot be used together", + "--statistics", "-r/--roles-only"); + if (statistics_only && roles_only) + pg_fatal("options %s and %s cannot be used together", + "--statistics-only", "-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 (with_statistics && tablespaces_only) + pg_fatal("options %s and %s cannot be used together", + "--statistics", "-t/--tablespaces-only"); + if (statistics_only && tablespaces_only) + pg_fatal("options %s and %s cannot be used together", + "--statistics-only", "-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 +659,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/001_basic.pl b/src/bin/pg_dump/t/001_basic.pl index ab9310eb42b..99db465c9ed 100644 --- a/src/bin/pg_dump/t/001_basic.pl +++ b/src/bin/pg_dump/t/001_basic.pl @@ -226,6 +226,66 @@ command_fails_like( 'pg_dumpall: options -r/--roles-only and -t/--tablespaces-only 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', '--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', '-a', '-r' ], + qr/\Qpg_dumpall: error: options -a\/--data-only and -r\/--roles-only cannot be used together\E/, + 'pg_dumpall: error: options -a/--data-only and -r/--roles-only cannot be used together' +); + +command_fails_like( + [ 'pg_dumpall', '-s', '-r' ], + qr/\Qpg_dumpall: error: options -s\/--schema-only and -r\/--roles-only cannot be used together\E/, + 'pg_dumpall: error: options -s/--schema-only and -r/--roles-only cannot be used together' +); + +command_fails_like( + [ 'pg_dumpall', '--statistics', '-r' ], + qr/\Qpg_dumpall: error: options --statistics and -r\/--roles-only cannot be used together\E/, + 'pg_dumpall: error: options --statistics and -r/--roles-only cannot be used together' +); + +command_fails_like( + [ 'pg_dumpall', '--statistics-only', '-r' ], + qr/\Qpg_dumpall: error: options --statistics-only and -r\/--roles-only cannot be used together\E/, + 'pg_dumpall: error: options --statistics-only and -r/--roles-only cannot be used together' +); + +command_fails_like( + [ 'pg_dumpall', '-a', '-t' ], + qr/\Qpg_dumpall: error: options -a\/--data-only and -t\/--tablespaces-only cannot be used together\E/, + 'pg_dumpall: error: options -a/--data-only and -t/--tablespaces-only cannot be used together' +); + +command_fails_like( + [ 'pg_dumpall', '-s', '-t' ], + qr/\Qpg_dumpall: error: options -s\/--schema-only and -t\/--tablespaces-only cannot be used together\E/, + 'pg_dumpall: error: options -s/--schema-only and -t/--tablespaces-only cannot be used together' +); + +command_fails_like( + [ 'pg_dumpall', '--statistics', '-t' ], + qr/\Qpg_dumpall: error: options --statistics and -t\/--tablespaces-only cannot be used together\E/, + 'pg_dumpall: error: options --statistics and -t/--tablespaces-only cannot be used together' +); + +command_fails_like( + [ 'pg_dumpall', '--statistics-only', '-t'], + qr/\Qpg_dumpall: error: options --statistics-only and -t\/--tablespaces-only cannot be used together\E/, + 'pg_dumpall: error: options --statistics-only and -t/--tablespaces-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 28812d28aa9..2c8c2b63268 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
