From aa86e425e5f7a4c8247b8f6d39d852de36e37456 Mon Sep 17 00:00:00 2001 From: Daniil Davidov Date: Thu, 15 Jan 2026 23:15:48 +0700 Subject: [PATCH v20 3/5] Cost based parameters propagation for parallel autovacuum --- src/backend/commands/vacuum.c | 29 +++++- src/backend/commands/vacuumparallel.c | 134 ++++++++++++++++++++++++++ src/backend/postmaster/autovacuum.c | 2 +- src/include/commands/vacuum.h | 2 + 4 files changed, 164 insertions(+), 3 deletions(-) diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index aa4fbec143f..3cc84bedfb7 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -2430,8 +2430,27 @@ vacuum_delay_point(bool is_analyze) /* Always check for interrupts */ CHECK_FOR_INTERRUPTS(); - if (InterruptPending || - (!VacuumCostActive && !ConfigReloadPending)) + if (InterruptPending) + return; + + if (!AmAutoVacuumWorkerProcess() && IsParallelWorker()) + { + /* + * If we are parallel *autovacuum* worker, check whether related to + * cost-based delay parameters had changed in the leader worker. If + * so, corresponding parameters will be updated to the values which + * leader worker is operating on. + * + * Do it before checking VacuumCostActive, because its value might be + * changed after leader's parameters consumption. + * + * Note, that this function has no effect if we are non-autovacuum + * parallel worker. + */ + parallel_vacuum_update_shared_delay_params(); + } + + if (!VacuumCostActive && !ConfigReloadPending) return; /* @@ -2445,6 +2464,12 @@ vacuum_delay_point(bool is_analyze) ConfigReloadPending = false; ProcessConfigFile(PGC_SIGHUP); VacuumUpdateCosts(); + + /* + * If we are parallel autovacuum leader and some of cost-based + * parameters had changed, let other parallel workers know. + */ + parallel_vacuum_propagate_cost_based_params(); } /* diff --git a/src/backend/commands/vacuumparallel.c b/src/backend/commands/vacuumparallel.c index 44c1258b69e..e3561057334 100644 --- a/src/backend/commands/vacuumparallel.c +++ b/src/backend/commands/vacuumparallel.c @@ -53,6 +53,39 @@ #define PARALLEL_VACUUM_KEY_WAL_USAGE 4 #define PARALLEL_VACUUM_KEY_INDEX_STATS 5 +/* + * Only autovacuum leader can reload config file. We use this structure in + * parallel autovacuum for keeping worker's parameters in sync with leader's + * parameters. + */ +typedef struct PVSharedCostParams +{ + /* + * Each time leader worker updates its parameters, it must increase + * generation. Every parallel worker keeps the generation + * (shared_params_local_generation) at which it had last time received + * parameters from the leader. + * + * It is enough for worker to compare it's local_generation with the field + * below to determine whether it needs to receive new parameters' values. + */ + pg_atomic_uint32 generation; + + slock_t spinlock; /* protects all fields below */ + + /* Copies of corresponding parameters from autovacuum leader process */ + double cost_delay; + int cost_limit; + int cost_page_dirty; + int cost_page_hit; + int cost_page_miss; +} PVSharedCostParams; + +static PVSharedCostParams * pv_shared_cost_params = NULL; + +/* See comments for structure above for the explanation. */ +static uint32 shared_params_generation_local = 0; + /* * Shared information among parallel workers. So this is allocated in the DSM * segment. @@ -122,6 +155,18 @@ typedef struct PVShared /* Statistics of shared dead items */ VacDeadItemsInfo dead_items_info; + + /* + * If 'true' then we are running parallel autovacuum. Otherwise, we are + * running parallel maintenence VACUUM. + */ + bool am_parallel_autovacuum; + + /* + * Struct for syncing parameters between supportive parallel autovacuum + * workers with leader worker. + */ + PVSharedCostParams cost_params; } PVShared; /* Status used during parallel index vacuum or cleanup */ @@ -395,6 +440,20 @@ parallel_vacuum_init(Relation rel, Relation *indrels, int nindexes, pg_atomic_init_u32(&(shared->active_nworkers), 0); pg_atomic_init_u32(&(shared->idx), 0); + shared->am_parallel_autovacuum = AmAutoVacuumWorkerProcess(); + + if (shared->am_parallel_autovacuum) + { + shared->cost_params.cost_delay = vacuum_cost_delay; + shared->cost_params.cost_limit = vacuum_cost_limit; + shared->cost_params.cost_page_dirty = VacuumCostPageDirty; + shared->cost_params.cost_page_hit = VacuumCostPageHit; + shared->cost_params.cost_page_miss = VacuumCostPageMiss; + pg_atomic_init_u32(&shared->cost_params.generation, 0); + SpinLockInit(&shared->cost_params.spinlock); + pv_shared_cost_params = &(shared->cost_params); + } + shm_toc_insert(pcxt->toc, PARALLEL_VACUUM_KEY_SHARED, shared); pvs->shared = shared; @@ -537,6 +596,78 @@ parallel_vacuum_cleanup_all_indexes(ParallelVacuumState *pvs, long num_table_tup parallel_vacuum_process_all_indexes(pvs, num_index_scans, false, wusage); } +/* + * Function to be called from parallel autovacuum worker in order to sync + * some cost-based delay parameter with the leader worker. + */ +void +parallel_vacuum_update_shared_delay_params(void) +{ + uint32 params_generation; + + Assert(IsParallelWorker()); + + /* Check whether we are running parallel autovacuum */ + if (pv_shared_cost_params == NULL) + return; + + params_generation = pg_atomic_read_u32(&pv_shared_cost_params->generation); + Assert(shared_params_generation_local <= params_generation); + + /* Return if parameters had not changed in the leader */ + if (params_generation == shared_params_generation_local) + return; + + SpinLockAcquire(&pv_shared_cost_params->spinlock); + + VacuumCostDelay = pv_shared_cost_params->cost_delay; + VacuumCostLimit = pv_shared_cost_params->cost_limit; + VacuumCostPageDirty = pv_shared_cost_params->cost_page_dirty; + VacuumCostPageHit = pv_shared_cost_params->cost_page_hit; + VacuumCostPageMiss = pv_shared_cost_params->cost_page_miss; + + SpinLockRelease(&pv_shared_cost_params->spinlock); + + VacuumUpdateCosts(); + + shared_params_generation_local = params_generation; +} + +/* + * Function to be called from parallel autovacuum leader in order to propagate + * some cost-based parameters to the supportive workers. + */ +void +parallel_vacuum_propagate_cost_based_params(void) +{ + uint32 params_generation; + + Assert(AmAutoVacuumWorkerProcess()); + + /* Check whether we are running parallel autovacuum */ + if (pv_shared_cost_params == NULL) + return; + + params_generation = pg_atomic_read_u32(&pv_shared_cost_params->generation); + + SpinLockAcquire(&pv_shared_cost_params->spinlock); + + pv_shared_cost_params->cost_delay = vacuum_cost_delay; + pv_shared_cost_params->cost_limit = vacuum_cost_limit; + pv_shared_cost_params->cost_page_dirty = VacuumCostPageDirty; + pv_shared_cost_params->cost_page_hit = VacuumCostPageHit; + pv_shared_cost_params->cost_page_miss = VacuumCostPageMiss; + + /* + * Increase generation of the parameters, i.e. let parallel workers know + * that they should re-read shared cost params. + */ + pg_atomic_write_u32(&pv_shared_cost_params->generation, + params_generation + 1); + + SpinLockRelease(&pv_shared_cost_params->spinlock); +} + /* * Compute the number of parallel worker processes to request. Both index * vacuum and index cleanup can be executed with parallel workers. @@ -1100,6 +1231,9 @@ parallel_vacuum_main(dsm_segment *seg, shm_toc *toc) VacuumSharedCostBalance = &(shared->cost_balance); VacuumActiveNWorkers = &(shared->active_nworkers); + if (shared->am_parallel_autovacuum) + pv_shared_cost_params = &(shared->cost_params); + /* Set parallel vacuum state */ pvs.indrels = indrels; pvs.nindexes = nindexes; diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c index ddd7bbdf520..3ee858c5fbd 100644 --- a/src/backend/postmaster/autovacuum.c +++ b/src/backend/postmaster/autovacuum.c @@ -1692,7 +1692,7 @@ VacuumUpdateCosts(void) } else { - /* Must be explicit VACUUM or ANALYZE */ + /* Must be explicit VACUUM or ANALYZE or parallel autovacuum worker */ vacuum_cost_delay = VacuumCostDelay; vacuum_cost_limit = VacuumCostLimit; } diff --git a/src/include/commands/vacuum.h b/src/include/commands/vacuum.h index 7cbb59d124f..712e1254613 100644 --- a/src/include/commands/vacuum.h +++ b/src/include/commands/vacuum.h @@ -414,6 +414,8 @@ extern void parallel_vacuum_cleanup_all_indexes(ParallelVacuumState *pvs, int num_index_scans, bool estimated_count, PVWorkersUsage *wusage); +extern void parallel_vacuum_update_shared_delay_params(void); +extern void parallel_vacuum_propagate_cost_based_params(void); extern void parallel_vacuum_main(dsm_segment *seg, shm_toc *toc); /* in commands/analyze.c */ -- 2.43.0