From e8ecbc65ef61acdc8d3184ec93ac4f4877358fc1 Mon Sep 17 00:00:00 2001 From: Daniil Davidov Date: Sun, 23 Nov 2025 01:07:47 +0700 Subject: [PATCH v22 2/5] Logging for parallel autovacuum --- src/backend/access/heap/vacuumlazy.c | 61 ++++++++++++++++++++++++++- src/backend/commands/vacuumparallel.c | 29 ++++++++++--- src/include/commands/vacuum.h | 28 +++++++++++- src/tools/pgindent/typedefs.list | 3 ++ 4 files changed, 111 insertions(+), 10 deletions(-) diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c index 4be267ff657..91be2502c09 100644 --- a/src/backend/access/heap/vacuumlazy.c +++ b/src/backend/access/heap/vacuumlazy.c @@ -340,6 +340,12 @@ typedef struct LVRelState int num_index_scans; int num_dead_items_resets; Size total_dead_items_bytes; + + /* + * Total number of planned and actually launched parallel workers for + * index scans. + */ + PVWorkersUsage workers_usage; /* Counters that follow are only for scanned_pages */ int64 tuples_deleted; /* # deleted from table */ int64 tuples_frozen; /* # newly frozen */ @@ -778,6 +784,11 @@ heap_vacuum_rel(Relation rel, const VacuumParams params, vacrel->vm_new_visible_frozen_pages = 0; vacrel->vm_new_frozen_pages = 0; + vacrel->workers_usage.vacuum.nlaunched = 0; + vacrel->workers_usage.vacuum.nplanned = 0; + vacrel->workers_usage.cleanup.nlaunched = 0; + vacrel->workers_usage.cleanup.nplanned = 0; + /* * Get cutoffs that determine which deleted tuples are considered DEAD, * not just RECENTLY_DEAD, and which XIDs/MXIDs to freeze. Then determine @@ -1120,6 +1131,50 @@ heap_vacuum_rel(Relation rel, const VacuumParams params, orig_rel_pages == 0 ? 100.0 : 100.0 * vacrel->lpdead_item_pages / orig_rel_pages, vacrel->lpdead_items); + if (vacrel->workers_usage.vacuum.nplanned > 0) + { + /* Stats for vacuum phase of index vacuuming. */ + + if (AmAutoVacuumWorkerProcess()) + { + /* Worker usage stats for parallel autovacuum. */ + appendStringInfo(&buf, + _("parallel workers: index vacuum: %d planned, %d reserved, %d launched in total\n"), + vacrel->workers_usage.vacuum.nplanned, + vacrel->workers_usage.vacuum.nreserved, + vacrel->workers_usage.vacuum.nlaunched); + } + else + { + /* Worker usage stats for manual VACUUM (PARALLEL). */ + appendStringInfo(&buf, + _("parallel workers: index vacuum: %d planned, %d launched in total\n"), + vacrel->workers_usage.vacuum.nplanned, + vacrel->workers_usage.vacuum.nlaunched); + } + } + if (vacrel->workers_usage.cleanup.nplanned > 0) + { + /* Stats for cleanup phase of index vacuuming. */ + + if (AmAutoVacuumWorkerProcess()) + { + /* Worker usage stats for parallel autovacuum. */ + appendStringInfo(&buf, + _("parallel workers: index cleanup: %d planned, %d reserved, %d launched\n"), + vacrel->workers_usage.cleanup.nplanned, + vacrel->workers_usage.cleanup.nreserved, + vacrel->workers_usage.cleanup.nlaunched); + } + else + { + /* Worker usage stats for manual VACUUM (PARALLEL). */ + appendStringInfo(&buf, + _("parallel workers: index cleanup: %d planned, %d launched\n"), + vacrel->workers_usage.cleanup.nplanned, + vacrel->workers_usage.cleanup.nlaunched); + } + } for (int i = 0; i < vacrel->nindexes; i++) { IndexBulkDeleteResult *istat = vacrel->indstats[i]; @@ -2664,7 +2719,8 @@ lazy_vacuum_all_indexes(LVRelState *vacrel) { /* Outsource everything to parallel variant */ parallel_vacuum_bulkdel_all_indexes(vacrel->pvs, old_live_tuples, - vacrel->num_index_scans); + vacrel->num_index_scans, + &vacrel->workers_usage); /* * Do a postcheck to consider applying wraparound failsafe now. Note @@ -3097,7 +3153,8 @@ lazy_cleanup_all_indexes(LVRelState *vacrel) /* Outsource everything to parallel variant */ parallel_vacuum_cleanup_all_indexes(vacrel->pvs, reltuples, vacrel->num_index_scans, - estimated_count); + estimated_count, + &vacrel->workers_usage); } /* Reset the progress counters */ diff --git a/src/backend/commands/vacuumparallel.c b/src/backend/commands/vacuumparallel.c index d3e0c32b7ee..86d9f2b74c9 100644 --- a/src/backend/commands/vacuumparallel.c +++ b/src/backend/commands/vacuumparallel.c @@ -227,7 +227,7 @@ struct ParallelVacuumState 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, - bool vacuum); + bool vacuum, PVWorkersStats *wstats); static void parallel_vacuum_process_safe_indexes(ParallelVacuumState *pvs); static void parallel_vacuum_process_unsafe_indexes(ParallelVacuumState *pvs); static void parallel_vacuum_process_one_index(ParallelVacuumState *pvs, Relation indrel, @@ -502,7 +502,7 @@ parallel_vacuum_reset_dead_items(ParallelVacuumState *pvs) */ void parallel_vacuum_bulkdel_all_indexes(ParallelVacuumState *pvs, long num_table_tuples, - int num_index_scans) + int num_index_scans, PVWorkersUsage *wusage) { Assert(!IsParallelWorker()); @@ -513,7 +513,8 @@ parallel_vacuum_bulkdel_all_indexes(ParallelVacuumState *pvs, long num_table_tup pvs->shared->reltuples = num_table_tuples; pvs->shared->estimated_count = true; - parallel_vacuum_process_all_indexes(pvs, num_index_scans, true); + parallel_vacuum_process_all_indexes(pvs, num_index_scans, true, + &wusage->vacuum); } /* @@ -521,7 +522,8 @@ parallel_vacuum_bulkdel_all_indexes(ParallelVacuumState *pvs, long num_table_tup */ void parallel_vacuum_cleanup_all_indexes(ParallelVacuumState *pvs, long num_table_tuples, - int num_index_scans, bool estimated_count) + int num_index_scans, bool estimated_count, + PVWorkersUsage *wusage) { Assert(!IsParallelWorker()); @@ -533,7 +535,8 @@ parallel_vacuum_cleanup_all_indexes(ParallelVacuumState *pvs, long num_table_tup pvs->shared->reltuples = num_table_tuples; pvs->shared->estimated_count = estimated_count; - parallel_vacuum_process_all_indexes(pvs, num_index_scans, false); + parallel_vacuum_process_all_indexes(pvs, num_index_scans, false, + &wusage->cleanup); } /* @@ -618,7 +621,7 @@ parallel_vacuum_compute_workers(Relation *indrels, int nindexes, int nrequested, */ static void parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scans, - bool vacuum) + bool vacuum, PVWorkersStats *wstats) { int nworkers; PVIndVacStatus new_status; @@ -655,13 +658,23 @@ parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scan */ nworkers = Min(nworkers, pvs->pcxt->nworkers); + /* Remember this value, if we asked to */ + if (wstats != NULL && nworkers > 0) + wstats->nplanned += nworkers; + /* * Reserve workers in autovacuum global state. Note that we may be given * fewer workers than we requested. */ if (AmAutoVacuumWorkerProcess() && nworkers > 0) + { AutoVacuumReserveParallelWorkers(&nworkers); + /* Remember this value, if we asked to */ + if (wstats != NULL) + wstats->nreserved += nworkers; + } + /* * Set index vacuum status and mark whether parallel vacuum worker can * process it. @@ -728,6 +741,10 @@ parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scan /* Enable shared cost balance for leader backend */ VacuumSharedCostBalance = &(pvs->shared->cost_balance); VacuumActiveNWorkers = &(pvs->shared->active_nworkers); + + /* Remember this value, if we asked to */ + if (wstats != NULL) + wstats->nlaunched += pvs->pcxt->nworkers_launched; } if (vacuum) diff --git a/src/include/commands/vacuum.h b/src/include/commands/vacuum.h index e885a4b9c77..d3dc4e8cc67 100644 --- a/src/include/commands/vacuum.h +++ b/src/include/commands/vacuum.h @@ -300,6 +300,28 @@ typedef struct VacDeadItemsInfo int64 num_items; /* current # of entries */ } VacDeadItemsInfo; +/* + * Helper for the PVWorkersUsage structure (see below), to avoid repetition. + */ +typedef struct PVWorkersStats +{ + int nplanned; /* # of parallel workers we are planned to + * launch */ + int nreserved; /* for autovacuum only - # of parallel workers + * we have managed to reserve */ + int nlaunched; /* # of launched parallel workers */ +} PVWorkersStats; + +/* + * PVWorkersUsage stores information about total number of launched, reserved + * and planned workers during parallel vacuum (both for vacuum and cleanup). + */ +typedef struct PVWorkersUsage +{ + PVWorkersStats vacuum; + PVWorkersStats cleanup; +} PVWorkersUsage; + /* GUC parameters */ extern PGDLLIMPORT int default_statistics_target; /* PGDLLIMPORT for PostGIS */ extern PGDLLIMPORT int vacuum_freeze_min_age; @@ -394,11 +416,13 @@ extern TidStore *parallel_vacuum_get_dead_items(ParallelVacuumState *pvs, extern void parallel_vacuum_reset_dead_items(ParallelVacuumState *pvs); extern void parallel_vacuum_bulkdel_all_indexes(ParallelVacuumState *pvs, long num_table_tuples, - int num_index_scans); + int num_index_scans, + PVWorkersUsage *wusage); extern void parallel_vacuum_cleanup_all_indexes(ParallelVacuumState *pvs, long num_table_tuples, int num_index_scans, - bool estimated_count); + bool estimated_count, + PVWorkersUsage *wusage); extern void parallel_vacuum_main(dsm_segment *seg, shm_toc *toc); /* in commands/analyze.c */ diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list index 241945734ec..ae1047ddf5d 100644 --- a/src/tools/pgindent/typedefs.list +++ b/src/tools/pgindent/typedefs.list @@ -2069,6 +2069,8 @@ PVIndStats PVIndVacStatus PVOID PVShared +PVWorkersUsage +PVWorkersStats PX_Alias PX_Cipher PX_Combo @@ -2407,6 +2409,7 @@ PullFilterOps PushFilter PushFilterOps PushFunction +PVWorkersUsage PyCFunction PyMethodDef PyModuleDef -- 2.43.0