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

Quan Zongliang <quanzongliang@yeah.net>

From: Quan Zongliang <quanzongliang@yeah.net>
To: Mohamed ALi <moali.pg@gmail.com>, pgsql-hackers@lists.postgresql.org
Date: 2026-06-05T09:25:52Z
Lists: pgsql-hackers

On 5/16/26 12:47 AM, Mohamed ALi wrote:
> 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.
> 
The parameter "D and d(dname)" conforms to the usage of "N and n." 
Adhering to the original definition, this is fine.

In the vacuum_all_databases function. If the condition 'if 
(dbsToExclude)' is used. All 'exclude-database' processing (including 
variable definitions) can be placed in a separate code block. How about 
this? Irrelevant personal preferences.

--
Quan Zongliang

> 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).
>