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

From: Mohamed ALi <moali(dot)pg(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: [PATCH] vacuumdb: Add --exclude-database option
Date: 2026-05-15 16:47:30
Message-ID: CAGnOmWpn6ncyW662=vBRGqK5_8ZXSRcjXA=g=kyvfvr36767bg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

Attachment Content-Type Size
test_exclude_database_results.txt text/plain 10.8 KB
test_exclude_database.sh text/x-sh 19.0 KB
v1-0001-vacuumdb-Add-exclude-database-option-to-skip-data.patch application/octet-stream 10.1 KB

Browse pgsql-hackers by date

  From Date Subject
Previous Message Peter Geoghegan 2026-05-15 16:31:59 Re: First draft of PG 19 release notes