From a9fed86155bb2de79aa73f5163a9a5bd4bb5a82f Mon Sep 17 00:00:00 2001 From: Bharath Rupireddy Date: Tue, 13 Oct 2026 22:21:30 +0000 Subject: [PATCH v1] Honor reduced workers after heap scan phase for parallel autovacuum This commit re-checks the number of parallel workers when they are launched, instead of only when the parallel context is set up before the heap scan phase. Only a reduction can take effect, as the DSM is sized in parallel_vacuum_init(). Author: Bharath Rupireddy Discussion: https://postgr.es/m/ Backpatch-through: 19 --- src/backend/commands/vacuumparallel.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/backend/commands/vacuumparallel.c b/src/backend/commands/vacuumparallel.c index 4e432c50d37..eb69c267411 100644 --- a/src/backend/commands/vacuumparallel.c +++ b/src/backend/commands/vacuumparallel.c @@ -283,6 +283,7 @@ static PVSharedCostParams *pv_shared_cost_params = NULL; */ static uint32 shared_params_generation_local = 0; +static inline int parallel_vacuum_max_workers(void); static int parallel_vacuum_compute_workers(Relation *indrels, int nindexes, int nrequested, bool *will_parallel_vacuum); static void parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scans, @@ -769,6 +770,17 @@ parallel_vacuum_refresh_cost_params(void) parallel_vacuum_propagate_shared_delay_params(); } +/* + * Upper limit on the number of parallel workers a vacuum may use. + */ +static inline int +parallel_vacuum_max_workers(void) +{ + return AmAutoVacuumWorkerProcess() ? + autovacuum_max_parallel_workers : + max_parallel_maintenance_workers; +} + /* * Compute the number of parallel worker processes to request. Both index * vacuum and index cleanup can be executed with parallel workers. @@ -792,9 +804,7 @@ parallel_vacuum_compute_workers(Relation *indrels, int nindexes, int nrequested, int parallel_workers; int max_workers; - max_workers = AmAutoVacuumWorkerProcess() ? - autovacuum_max_parallel_workers : - max_parallel_maintenance_workers; + max_workers = parallel_vacuum_max_workers(); /* * We don't allow performing parallel operation in standalone backend or @@ -890,6 +900,14 @@ parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scan */ nworkers = Min(nworkers, pvs->pcxt->nworkers); + /* + * Re-apply the limit used in parallel_vacuum_compute_workers(), as it may + * have been set to a lower value while we were scanning the heap. Only a + * reduction can take effect here: the DSM was sized for pcxt->nworkers in + * parallel_vacuum_init(). + */ + nworkers = Min(nworkers, parallel_vacuum_max_workers()); + /* Update the statistics, if we asked to */ if (wstats != NULL && nworkers > 0) wstats->nplanned += nworkers; -- 2.47.3