Review
vacuumdb, clusterdb, reindexdb: Add --continue (v3)
- Proposal version
- 3
- CommitFest entry
- 7072
- Base commit
- 412848dac37f2e4455e1505f83f0af0f3d28a67d
Summary
The patch adds a --continue switch to vacuumdb, reindexdb and
clusterdb. With --all, a failure to connect to one database becomes a
warning and the tool moves on to the next database instead of aborting;
the motivating case is managed services that keep a database the
customer's role cannot connect to. The series applies cleanly to
master, builds without new warnings, its TAP additions pass and
demonstrably fail without the C changes, and hands-on runs behave as
the documentation says. I found no correctness bug. The gaps are of
scope and documentation: the option covers only the first connection to
each database (extra --jobs connections and every post-connection
error still abort the run), exit status is 0 no matter how many
databases were skipped and the docs do not say so, reindexdb/clusterdb
print a progress line for a database they then skip, and
--analyze-in-stages repeats each warning per stage. The name
--continue promises more than the option does. Ready for Committer is
defensible provided the committer tightens the documentation; nothing
here needs a redesign.
What was reviewed
- Series: 1 patch, "vacuumdb, clusterdb, reindexdb: Add --continue" (no version in the file name; the thread subject calls it v3), from message EAFC6FD9-01D5-426B-BAAF-D933C4C31356@thebuild.com (CommitFest entry 7072), author Christophe Pettus.
- Applied on: 412848dac37f2e4455e1505f83f0af0f3d28a67d ("Treat any
negative ws_file as closed in xlogreader.c", master), cleanly, by
cherry-picking the cfbot commit c2427826f00. The posted patch file
itself was generated against 6e89028ec48 and no longer applies with
git amto the bot's base 1b5dd3a24a4, because e9c03f0f1bf ("reindexdb: Forbid index-level REINDEX with CONCURRENTLY and multiple jobs") rewrote two context lines in reindexdb.c main(). The only textual difference between the posted patch and the bot's commit is that context, so the bot applied what was posted. v2 and v3 have identical content; v2 had the leading space stripped from blank context lines, which is why cfbot could not apply it. - Built: meson, debug, cassert, injection points, TAP tests, lz4, zstd,
liburing, libnuma, icu,
-Wtype-limits -Werror=missing-braces. New warnings: none.pgindent --diffon the six touched C/H files: clean. - Ran: setup +
scriptssuite: 16/16 OK (010_clusterdb 23 subtests, 090_reindexdb 72, 100_vacuumdb 126, all pass);regress: OK;ninja docs: OK, the three ref pages render. Reverted the six C/H files to master while keeping the test and doc changes: the three TAP files then fail with exactly the new subtests (clusterdb: 4 of 23; the--continue requires --allregex, and the exit status, stdout and stderr checks of the--all --continuerun), so the tests do exercise the feature. Hands-on runs against a throwaway cluster with databases refusing connections through REVOKE CONNECT, CONNECTION LIMIT 0 and a pg_hbarejectline, plus CONNECTION LIMIT 1 and a schema-permission failure, as superuser and as a non- superuser member of pg_maintain, with and without--continue, with--jobs 2,--analyze-in-stages,--quiet,--echo,--dry-run,--maintenance-db, repeated-t,-s/-S/-tcombinations, and a pg_hba configuration rejecting every database but one.
Motivation and scope
The motivation is real and clearly stated. Hosted services keep a
database (e.g. cloudsqladmin, rdsadmin) that the customer's role is
not allowed to CONNECT to. It has datallowconn = true, so the
--all query in all three tools
SELECT datname FROM pg_database WHERE datallowconn AND datconnlimit <> -2 ORDER BY 1;
lists it, the connection attempt is fatal, and every database sorting after it is never processed. Databases with datallowconn = false or datconnlimit = -2 are already excluded, so the patch is about server-side refusals of the connection: CONNECT privilege, CONNECTION LIMIT, pg_hba rejection, and the like. I confirmed each of these is turned into a warning and skipped.
Scope: one new long option in each of the three tools, a check that it
is accompanied by --all, a small change to connectDatabase() in
src/fe_utils/connect_utils.c so the failed PGconn (and hence the error
text) reaches the caller, a bool return from cluster_one_database()
and reindex_one_database() so a database is reported once even when
the per-database function is called several times for it, docs and
TAP tests. The size is proportionate.
What the option does not cover, and what the documentation should say
(details under Correctness): additional connections opened for
--jobs, and any error after the first connection succeeds.
Design and alternatives
-
Opt-in switch versus changed default. v1 changed the default. Daniel Gustafsson argued for a switch so that
--allkeeps its exit-code semantics, and the author agreed. I agree: silently returning 0 while skipping databases cannot be the default. The switch is correctly rejected without--all, so a database named on the command line can never be skipped (verified for-d, positional, and-a -din all three tools). -
Returning the failed PGconn from connectDatabase(fail_ok = true) instead of NULL. This is the simplest way to get PQerrorMessage() into the warning. It changes an exported contract (see Compatibility); the only in-tree fail_ok caller, connectMaintenanceDatabase(), is adjusted. Fine.
-
An alternative the thread did not consider:
--exclude-database= PATTERN, which pg_dumpall and pg_amcheck (-D) already offer. It lets the user name the provider's database explicitly, keeps every unexpected connection failure fatal, and is consistent with existing tools; its cost is that the user must know the name. The two are complementary rather than exclusive. A committer may want to decide whether that is the better fit for the stated use case, or whether both are wanted. Note thatpg_amcheck -aaborts on an unconnectable database in exactly the same way (src/bin/pg_amcheck/pg_amcheck.c, connectDatabase(..., false, true) in the per-database loop) and is not touched here; it has-Das the workaround. -
Naming.
--continueonly governs the initial connection to each database. Related options elsewhere are more specific: pg_restore--exit-on-error, pg_amcheck--on-error-stop, pgbench--continue-on-error, psql ON_ERROR_STOP. A reader of--continuewill assume "continue past errors", which it does not do (examples below). Something like--skip-unconnectablewould describe the behaviour; the thread settled on--continueat Daniel's suggestion with no objection, so I raise this as a question for the committer, not a blocker. In reindexdb--conis now ambiguous with--concurrently; getopt reports that properly. -
Exit status. With
--continue, the exit status is 0 even if every database except the maintenance one was skipped (reproduced below). Daniel noted this is unavoidable once skipping is allowed. It should nevertheless be stated in the docs. A committer could consider a distinct non-zero status when at least one database was skipped (the tools currently only use 0 and 1), but that would defeat the stated purpose for cron jobs, so documenting is the pragmatic answer.
Correctness
No correctness problem found. What follows are scope and behaviour findings, most important first.
-
--jobsconnections are not covered. In vacuumdb and reindexdb the first connection is made in vacuum_one_database() / reindex_one_database() with the new flag and adopted into the slot array via ParallelSlotsAdoptConn(); every further connection is opened lazily by connect_slot() in src/fe_utils/parallel_slot.c:slot->connection = connectDatabase(sa->cparams, sa->progname, sa->echo, false, true);with fail_ok = false, so it still calls pg_fatal(). Reproducer: a database with
CONNECTION LIMIT 1and three tables, run as a non-superuser:$ vacuumdb -a --continue -j 2 -U u vacuumdb: vacuuming database "a_ok" vacuumdb: warning: skipping database "b_noconnect": ... permission denied for database "b_noconnect" vacuumdb: warning: skipping database "c_limit0": ... too many connections for database "c_limit0" vacuumdb: vacuuming database "d_limit1" vacuumdb: error: connection to server ... failed: FATAL: too many connections for database "d_limit1" exit=1reindexdb
-j 2behaves the same. This is pre-existing behaviour and arguably outside "a database that cannot be connected to", but the doc text does not make that distinction. Making connect_slot() honour the flag is not obviously right either (a half-processed database would then be reported as "skipped"), so I suggest documenting: the option affects only the initial connection to each database. -
Post-connection failures still abort the run, by design. Two examples from the same cluster:
$ vacuumdb -a --continue -U u # u lacks USAGE on schema priv in one db ... vacuumdb: vacuuming database "h_schemaperm" vacuumdb: error: processing of database "h_schemaperm" failed: ERROR: permission denied for schema priv exit=1 # postgres, template1 not processed $ clusterdb -a --continue -t t1 -U u # one database has no t1 ... clusterdb: error: query failed: ERROR: relation "t1" does not exist exit=1Correct per the patch's intent, but given the option's name the docs should say explicitly that errors from the maintenance commands themselves are unaffected.
-
Inconsistent progress output between the tools. reindex_all_ databases() and cluster_all_databases() print the progress line before calling the per-database function, vacuum_one_database() prints it after connecting. So for a skipped database:
clusterdb: clustering database "b_noconnect" clusterdb: warning: skipping database "b_noconnect": ...while vacuumdb prints only the warning. Cosmetic, but the three tools are not quite identical: they agree in exit status and in what is skipped, not in stdout. Moving the printf in the two
*_all_databases()loops after a successful connection would need the progress message to move into the*_one_database()functions; alternatively accept the difference. -
--analyze-in-stageswarns three times per skipped database. vacuum_all_databases() loops stages outermost and databases inside, and each stage reconnects, so each unconnectable database produces one warning per stage (verified: three identical warnings for each of three databases). Harmless; a per-database "skipped" flag kept across stages, next to the existing found_objs array, would silence the repeats if a committer cares. -
Exit status 0 with nearly everything skipped. With pg_hba allowing role u into
postgresonly:$ vacuumdb -a --continue -U u vacuumdb: warning: skipping database "a_ok": ... pg_hba.conf rejects connection ... (7 more warnings) vacuumdb: vacuuming database "postgres" vacuumdb: warning: skipping database "template1": ... exit=0reindexdb and clusterdb identical. This is what the thread agreed to, and it follows that a server restart or network loss after the maintenance connection would also end with status 0 and a string of "Connection refused" warnings (not reproduced; follows from the code, since nothing distinguishes a server-side refusal from a transport failure). The doc must state the exit-status semantics.
Verified as correct: the maintenance-database connection remains fatal
with --continue (--maintenance-db b_noconnect exits 1); --quiet
suppresses the progress lines but not the warnings, which is right;
--echo and --dry-run are unaffected; exactly one warning per
skipped database with repeated -t in clusterdb (the break in the
per-table loop) and with -s/-S/-t/-i combinations in reindexdb
(the continue chain in reindex_all_databases()); a superuser bypasses
CONNECT privilege and CONNECTION LIMIT, so only the pg_hba reject is
skipped for it, as expected; PQdb() is valid on a failed PGconn because
dbName is set while parsing options; every path PQfinish()es the
connection; the trailing newline of PQerrorMessage() is stripped by
pg_log_generic_v() ("strip one newline, for PQerrorMessage()" in
src/common/logging.c), so the warning renders on one line plus libpq's
DETAIL line; connectMaintenanceDatabase()'s postgres-then-template1
fallback behaves as before.
Performance
n/a: one PQfinish() on an already-failed connection; successful runs are unchanged.
Compatibility
- Default behaviour is unchanged; without
--continuethe three tools fail at the first unconnectable database with the same message and exit status 1 as before (verified). - connectDatabase() contract: with fail_ok = true it now returns a PGconn in CONNECTION_BAD state instead of NULL. libpgfeutils.a and fe_utils/connect_utils.h are installed ("libpgfeutils could be useful to contrib", src/fe_utils/Makefile), so an out-of-tree program that passes fail_ok = true and tests for NULL would now carry on with a dead connection; its next executeQuery() would fail loudly rather than silently. No in-tree caller besides connectMaintenanceDatabase() uses fail_ok. Static library, so no ABI concern. The commit message mentions the change; good.
- New translatable strings: the help lines, "skipping database "%s": %s", and a reuse of the existing msgid "cannot use the "%s" option without "%s"" with "continue"/"all" as arguments, consistent with neighbouring messages.
- No catalog, protocol, WAL, pg_dump or pg_upgrade impact. Nothing
platform-specific; the TAP tests use
--create-role, which works with SSPI on Windows as in other tests.
Code, tests, docs, style
Code:
- src/bin/scripts/vacuuming.c, vacuum_one_database(): the nine-line comment above the connectDatabase() call restates the docs and the commit message; the mechanics ("With --continue, a failed connection is reported as a warning and the database skipped; see vacuum_all_databases()") would suffice.
- src/bin/scripts/reindexdb.c, reindex_all_databases(): the
&& !reindex_one_database(...)/continuechain and its comment are correct; one warning per database, verified. - src/bin/scripts/clusterdb.c, cluster_all_databases():
breakafter a failed connection in the per-table loop; correct, verified. - src/fe_utils/connect_utils.c: the new header comment describes the contract accurately.
- pgindent clean, no new warnings,
git show --checkclean.
Tests:
- Each of 010_clusterdb.pl, 090_reindexdb.pl and 100_vacuumdb.pl now
creates and starts a second cluster named "continue". The suite
already has one fresh-node file per tool dedicated to
--all(011_clusterdb_all.pl, 091_reindexdb_all.pl, 101_vacuumdb_all.pl). The new tests belong there: initialise that node withauth_extra => ['--create-role' => 'regress_continue']and add the three checks, saving three initdb/start cycles and keeping the--allcoverage in one place. The comment in 100_vacuumdb.pl correctly explains why the main node of that file cannot be reused (superuser-owned schemas, as Neil Chen found); the_allfiles do not have that problem. - The comment "connections can only be refused to a role that is not a
superuser" is not quite accurate (a pg_hba
rejectline applies to superusers too), but the chosen approach is simpler and portable. - The "--all fails on a database that cannot be connected to" subtest passes with and without the patch; good as a guard for the default.
- Not covered:
--jobs(would document finding 1) and--analyze-in-stages(finding 4). Optional. - Perl additions are within 79 columns at tab width 4; I could not run pgperltidy here, the author should.
Docs (doc/src/sgml/ref/{vacuumdb,reindexdb,clusterdb}.sgml):
- The three identical entries are placed in the right alphabetical position and render correctly.
- Add: (a) the exit status is 0 regardless of how many databases were
skipped; (b) only the initial connection to each database is
affected, not additional connections opened for
--jobsnor errors raised by the commands themselves; (c) consider "refuses the connection" rather than "cannot be connected to", which better matches what is actually skipped. - The help text ("with --all, skip databases that cannot be connected to instead of exiting") is fine and aligned in all three tools.
Commit message: accurate, explains the API change and the bool return; subject in the usual style.
Discussion and consensus
- Christophe Pettus, 2026-07-27, v1: vacuumdb only, unconditional skip, exit 0.
- Neil Chen, 2026-07-29: (1) reindexdb and clusterdb have the same problem; (2) exit 0 hides skipped databases from scripts; (3) the test fails on master because earlier tests leave superuser-owned schemas that the unprivileged role cannot ANALYZE.
- Daniel Gustafsson (committer), 2026-07-29: make the three tools
alike; add a
--continueswitch instead of changing the default, because of the exit code; later "only skimmed ... seems like a good idea". - v2, 2026-07-29: all three points addressed; Neil Chen: +1.
- v3, 2026-08-04: re-post so cfbot can apply it (whitespace only).
- Pierre Forstmann, 2026-08-31, CF-app review: installcheck-world and feature tested, docs read, "looks good to me", status set to Ready for Committer.
No objection is outstanding. Not discussed: the option name, the
--exclude-database alternative, the --jobs scope, documenting the
exit status, and where the tests should live. For a patch of this size
the discussion is adequate but thin; the Ready for Committer status is
defensible.
Verdict
Ready for committer, with changes a committer can reasonably fold in:
- Document the exit-status semantics and the limit of the option to the initial connection per database (findings 1, 2, 5).
- Decide on the name (
--continueversus something like--skip-unconnectable), and whether--exclude-databaseis wanted instead of or in addition to it. - Optionally: move the new TAP checks into the existing
*_all.plfiles; align the progress-line behaviour of reindexdb/clusterdb with vacuumdb (finding 3); dedupe the per-stage warnings (finding 4).
No correctness bug was found; the assessment is held back by the scope/documentation gaps of findings 1 and 5, not by a defect. A committer should double-check the installed-API change to connectDatabase() is acceptable (it is in-tree clean) and run pgperltidy on the three test files.