[PATCH] vacuumdb: Add --exclude-database option

Mohamed ALi <moali.pg@gmail.com>

From: Mohamed ALi <moali.pg@gmail.com>
To: pgsql-hackers@lists.postgresql.org
Date: 2026-05-15T16:47:30Z
Lists: pgsql-hackers

Attachments

Hi hackers,

Attached is a patch that adds --exclude-database (-D) to vacuumdb,
allowing users to skip specific databases when using --all.

Motivation
----------

"vacuumdb --all" vacuums every connectable database without exception.
In production environments, operators often need to skip certain databases
during maintenance — for example, test databases, large inactive databases
with historical data.
The current workaround is to manually vacuum each database
individually or write wrapper scripts.

Usage
-----

    vacuumdb --all --exclude-database=test_db
    vacuumdb --all -D db1 -D db2 -D db3

The option requires --all

Testing
-------

The patch passes all existing vacuumdb TAP tests (100_vacuumdb,
101_vacuumdb_all, 102_vacuumdb_stages) and includes 4 new TAP tests
in 101_vacuumdb_all.pl covering exclusion, multiple exclusions, and
error cases.

I've also attached a standalone test script (test_exclude_database.sh)
that exercises the feature with 11 test scenarios:

  1. Single database exclusion
  2. Multiple database exclusions (-D db1 -D db2)
  3. Exclude all databases
  4. Exclude non-existent database (silently ignored)
  5. Exclude maintenance database (postgres)
  6. Case sensitivity (exact case excludes, wrong case does not)
  7. --exclude-database without --all (error)
  8. --exclude-database with -d (error)
  9. SQL injection protection
 10. --help output
 11. -d with -D and --all (conflicting options error)

All tests pass. The test script is portable — it uses standard libpq
environment variables and auto-detects binaries. Test output is also
attached (test_exclude_database_results.txt).

-- 
Mohamed Ali
AWS RDS