From a8c8a2bbf943b157eb6f0e754cb9aaa432e5bce3 Mon Sep 17 00:00:00 2001 From: Masahiko Sawada Date: Mon, 1 Jul 2024 15:17:46 +0900 Subject: [PATCH v3 1/4] Support parallel heap scan during lazy vacuum. Commit 40d964ec99 allowed vacuum command to process indexes in parallel. This change extends the parallel vacuum to support parallel heap scan during lazy vacuum. --- src/backend/access/heap/heapam_handler.c | 6 + src/backend/access/heap/vacuumlazy.c | 1135 ++++++++++++++++++---- src/backend/commands/vacuumparallel.c | 311 +++++- src/backend/storage/ipc/procarray.c | 9 - src/include/access/heapam.h | 8 + src/include/access/tableam.h | 87 ++ src/include/commands/vacuum.h | 8 +- src/include/utils/snapmgr.h | 14 +- 8 files changed, 1313 insertions(+), 265 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 8c59b77b64..c8602f4d30 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -2625,6 +2625,12 @@ static const TableAmRoutine heapam_methods = { .relation_copy_data = heapam_relation_copy_data, .relation_copy_for_cluster = heapam_relation_copy_for_cluster, .relation_vacuum = heap_vacuum_rel, + + .parallel_vacuum_compute_workers = heap_parallel_vacuum_compute_workers, + .parallel_vacuum_estimate = heap_parallel_vacuum_estimate, + .parallel_vacuum_initialize = heap_parallel_vacuum_initialize, + .parallel_vacuum_scan_worker = heap_parallel_vacuum_scan_worker, + .scan_analyze_next_block = heapam_scan_analyze_next_block, .scan_analyze_next_tuple = heapam_scan_analyze_next_tuple, .index_build_range_scan = heapam_index_build_range_scan, diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c index d82aa3d489..fd6c054901 100644 --- a/src/backend/access/heap/vacuumlazy.c +++ b/src/backend/access/heap/vacuumlazy.c @@ -49,6 +49,7 @@ #include "common/int.h" #include "executor/instrument.h" #include "miscadmin.h" +#include "optimizer/paths.h" #include "pgstat.h" #include "portability/instr_time.h" #include "postmaster/autovacuum.h" @@ -117,10 +118,24 @@ #define PREFETCH_SIZE ((BlockNumber) 32) /* - * Macro to check if we are in a parallel vacuum. If true, we are in the - * parallel mode and the DSM segment is initialized. + * DSM keys for heap parallel vacuum scan. Unlike other parallel execution code, we + * we don't need to worry about DSM keys conflicting with plan_node_id, but need to + * avoid conflicting with DSM keys used in vacuumparallel.c. + */ +#define LV_PARALLEL_SCAN_SHARED 0xFFFF0001 +#define LV_PARALLEL_SCAN_DESC 0xFFFF0002 +#define LV_PARALLEL_SCAN_DESC_WORKER 0xFFFF0003 + +/* + * Macros to check if we are in parallel heap vacuuming, parallel index vacuuming, + * or both. If ParallelVacuumIsActive() is true, we are in the parallel mode, meaning + * that we have dead items TIDs on shared memory area. */ #define ParallelVacuumIsActive(vacrel) ((vacrel)->pvs != NULL) +#define ParallelIndexVacuumIsActive(vacrel) \ + (ParallelVacuumIsActive(vacrel) && parallel_vacuum_get_nworkers_index((vacrel)->pvs) > 0) +#define ParallelHeapVacuumIsActive(vacrel) \ + (ParallelVacuumIsActive(vacrel) && parallel_vacuum_get_nworkers_table((vacrel)->pvs) > 0) /* Phases of vacuum during which we report error context. */ typedef enum @@ -133,6 +148,108 @@ typedef enum VACUUM_ERRCB_PHASE_TRUNCATE, } VacErrPhase; +/* + * Relation statistics collected during heap scanning and need to be shared among + * parallel vacuum workers. + */ +typedef struct LVRelScanStats +{ + BlockNumber scanned_pages; /* # pages examined (not skipped via VM) */ + BlockNumber removed_pages; /* # pages removed by relation truncation */ + BlockNumber frozen_pages; /* # pages with newly frozen tuples */ + BlockNumber lpdead_item_pages; /* # pages with LP_DEAD items */ + BlockNumber missed_dead_pages; /* # pages with missed dead tuples */ + BlockNumber nonempty_pages; /* actually, last nonempty page + 1 */ + + /* Counters that follow are only for scanned_pages */ + int64 tuples_deleted; /* # deleted from table */ + int64 tuples_frozen; /* # newly frozen */ + int64 lpdead_items; /* # deleted from indexes */ + int64 live_tuples; /* # live tuples remaining */ + int64 recently_dead_tuples; /* # dead, but not yet removable */ + int64 missed_dead_tuples; /* # removable, but not removed */ + + /* Tracks oldest extant XID/MXID for setting relfrozenxid/relminmxid. */ + TransactionId NewRelfrozenXid; + MultiXactId NewRelminMxid; + bool skippedallvis; +} LVRelScanStats; + +/* + * Struct for information that need to be shared among parallel vacuum workers + */ +typedef struct PHVShared +{ + bool aggressive; + bool skipwithvm; + + /* The initial values shared by the leader process */ + TransactionId NewRelfrozenXid; + MultiXactId NewRelminMxid; + bool skippedallvis; + + /* VACUUM operation's cutoffs for freezing and pruning */ + struct VacuumCutoffs cutoffs; + GlobalVisState vistest; + + /* per-worker scan stats for parallel heap vacuum scan */ + LVRelScanStats worker_scan_stats[FLEXIBLE_ARRAY_MEMBER]; +} PHVShared; +#define SizeOfPHVShared (offsetof(PHVShared, worker_scan_stats)) + +/* Per-worker scan state for parallel heap vacuum scan */ +typedef struct PHVScanWorkerState +{ + bool initialized; + + /* per-worker parallel table scan state */ + ParallelBlockTableScanWorkerData state; + + /* + * True if a parallel vacuum scan worker allocated blocks in state but + * might have not scanned all of them. The leader process will take over + * for scanning these remaining blocks. + */ + bool maybe_have_blocks; + + /* current block number being processed */ + pg_atomic_uint32 cur_blkno; +} PHVScanWorkerState; + +/* Struct for parallel heap vacuum */ +typedef struct PHVState +{ + /* Parallel scan description shared among parallel workers */ + ParallelBlockTableScanDesc pscandesc; + + /* Shared information */ + PHVShared *shared; + + /* + * Points to all per-worker scan state array stored on DSM area. + * + * During parallel heap scan, each worker allocates some chunks of blocks + * to scan in its scan state, and could exit while leaving some chunks + * un-scanned if the size of dead_items TIDs is close to overrunning the + * the available space. We store scan states on shared memory area so that + * workers can resume heap scans from the previous point. + */ + PHVScanWorkerState *scanstates; + + /* Assigned per-worker scan state */ + PHVScanWorkerState *myscanstate; + + /* + * All blocks up to this value has been scanned, i.e. minimum of cur_blkno + * among all PHVScanWorkerState. It's updated by + * parallel_heap_vacuum_compute_min_blkno(). + */ + BlockNumber min_blkno; + + /* The number of workers launched for parallel heap vacuum */ + int nworkers_launched; +} PHVState; + typedef struct LVRelState { /* Target heap relation and its indexes */ @@ -144,6 +261,9 @@ typedef struct LVRelState BufferAccessStrategy bstrategy; ParallelVacuumState *pvs; + /* Parallel heap vacuum state and sizes for each struct */ + PHVState *phvstate; + /* Aggressive VACUUM? (must set relfrozenxid >= FreezeLimit) */ bool aggressive; /* Use visibility map to skip? (disabled by DISABLE_PAGE_SKIPPING) */ @@ -159,10 +279,6 @@ typedef struct LVRelState /* VACUUM operation's cutoffs for freezing and pruning */ struct VacuumCutoffs cutoffs; GlobalVisState *vistest; - /* Tracks oldest extant XID/MXID for setting relfrozenxid/relminmxid */ - TransactionId NewRelfrozenXid; - MultiXactId NewRelminMxid; - bool skippedallvis; /* Error reporting state */ char *dbname; @@ -188,12 +304,10 @@ typedef struct LVRelState VacDeadItemsInfo *dead_items_info; BlockNumber rel_pages; /* total number of pages */ - BlockNumber scanned_pages; /* # pages examined (not skipped via VM) */ - BlockNumber removed_pages; /* # pages removed by relation truncation */ - BlockNumber frozen_pages; /* # pages with newly frozen tuples */ - BlockNumber lpdead_item_pages; /* # pages with LP_DEAD items */ - BlockNumber missed_dead_pages; /* # pages with missed dead tuples */ - BlockNumber nonempty_pages; /* actually, last nonempty page + 1 */ + BlockNumber next_fsm_block_to_vacuum; + + /* Statistics collected during heap scan */ + LVRelScanStats *scan_stats; /* Statistics output by us, for table */ double new_rel_tuples; /* new estimated total # of tuples */ @@ -203,13 +317,6 @@ typedef struct LVRelState /* Instrumentation counters */ int num_index_scans; - /* Counters that follow are only for scanned_pages */ - int64 tuples_deleted; /* # deleted from table */ - int64 tuples_frozen; /* # newly frozen */ - int64 lpdead_items; /* # deleted from indexes */ - int64 live_tuples; /* # live tuples remaining */ - int64 recently_dead_tuples; /* # dead, but not yet removable */ - int64 missed_dead_tuples; /* # removable, but not removed */ /* State maintained by heap_vac_scan_next_block() */ BlockNumber current_block; /* last block returned */ @@ -229,6 +336,7 @@ typedef struct LVSavedErrInfo /* non-export function prototypes */ static void lazy_scan_heap(LVRelState *vacrel); +static bool do_lazy_scan_heap(LVRelState *vacrel); static bool heap_vac_scan_next_block(LVRelState *vacrel, BlockNumber *blkno, bool *all_visible_according_to_vm); static void find_next_unskippable_block(LVRelState *vacrel, bool *skipsallvis); @@ -271,6 +379,12 @@ static void dead_items_cleanup(LVRelState *vacrel); static bool heap_page_is_all_visible(LVRelState *vacrel, Buffer buf, TransactionId *visibility_cutoff_xid, bool *all_frozen); static void update_relstats_all_indexes(LVRelState *vacrel); + +static void do_parallel_lazy_scan_heap(LVRelState *vacrel); +static void parallel_heap_vacuum_compute_min_blkno(LVRelState *vacrel); +static void parallel_heap_vacuum_gather_scan_stats(LVRelState *vacrel); +static void parallel_heap_complete_unfinised_scan(LVRelState *vacrel); + static void vacuum_error_callback(void *arg); static void update_vacuum_error_info(LVRelState *vacrel, LVSavedErrInfo *saved_vacrel, @@ -296,6 +410,7 @@ heap_vacuum_rel(Relation rel, VacuumParams *params, BufferAccessStrategy bstrategy) { LVRelState *vacrel; + LVRelScanStats *scan_stats; bool verbose, instrument, skipwithvm, @@ -406,14 +521,28 @@ heap_vacuum_rel(Relation rel, VacuumParams *params, Assert(params->index_cleanup == VACOPTVALUE_AUTO); } + vacrel->next_fsm_block_to_vacuum = 0; + /* Initialize page counters explicitly (be tidy) */ - vacrel->scanned_pages = 0; - vacrel->removed_pages = 0; - vacrel->frozen_pages = 0; - vacrel->lpdead_item_pages = 0; - vacrel->missed_dead_pages = 0; - vacrel->nonempty_pages = 0; - /* dead_items_alloc allocates vacrel->dead_items later on */ + scan_stats = palloc(sizeof(LVRelScanStats)); + scan_stats->scanned_pages = 0; + scan_stats->removed_pages = 0; + scan_stats->frozen_pages = 0; + scan_stats->lpdead_item_pages = 0; + scan_stats->missed_dead_pages = 0; + scan_stats->nonempty_pages = 0; + + /* Initialize remaining counters (be tidy) */ + scan_stats->tuples_deleted = 0; + scan_stats->tuples_frozen = 0; + scan_stats->lpdead_items = 0; + scan_stats->live_tuples = 0; + scan_stats->recently_dead_tuples = 0; + scan_stats->missed_dead_tuples = 0; + + vacrel->scan_stats = scan_stats; + + vacrel->num_index_scans = 0; /* Allocate/initialize output statistics state */ vacrel->new_rel_tuples = 0; @@ -421,14 +550,7 @@ heap_vacuum_rel(Relation rel, VacuumParams *params, vacrel->indstats = (IndexBulkDeleteResult **) palloc0(vacrel->nindexes * sizeof(IndexBulkDeleteResult *)); - /* Initialize remaining counters (be tidy) */ - vacrel->num_index_scans = 0; - vacrel->tuples_deleted = 0; - vacrel->tuples_frozen = 0; - vacrel->lpdead_items = 0; - vacrel->live_tuples = 0; - vacrel->recently_dead_tuples = 0; - vacrel->missed_dead_tuples = 0; + /* dead_items_alloc allocates vacrel->dead_items later on */ /* * Get cutoffs that determine which deleted tuples are considered DEAD, @@ -450,9 +572,9 @@ heap_vacuum_rel(Relation rel, VacuumParams *params, vacrel->rel_pages = orig_rel_pages = RelationGetNumberOfBlocks(rel); vacrel->vistest = GlobalVisTestFor(rel); /* Initialize state used to track oldest extant XID/MXID */ - vacrel->NewRelfrozenXid = vacrel->cutoffs.OldestXmin; - vacrel->NewRelminMxid = vacrel->cutoffs.OldestMxact; - vacrel->skippedallvis = false; + vacrel->scan_stats->NewRelfrozenXid = vacrel->cutoffs.OldestXmin; + vacrel->scan_stats->NewRelminMxid = vacrel->cutoffs.OldestMxact; + vacrel->scan_stats->skippedallvis = false; skipwithvm = true; if (params->options & VACOPT_DISABLE_PAGE_SKIPPING) { @@ -533,15 +655,15 @@ heap_vacuum_rel(Relation rel, VacuumParams *params, * value >= FreezeLimit, and relminmxid to a value >= MultiXactCutoff. * Non-aggressive VACUUMs may advance them by any amount, or not at all. */ - Assert(vacrel->NewRelfrozenXid == vacrel->cutoffs.OldestXmin || + Assert(vacrel->scan_stats->NewRelfrozenXid == vacrel->cutoffs.OldestXmin || TransactionIdPrecedesOrEquals(vacrel->aggressive ? vacrel->cutoffs.FreezeLimit : vacrel->cutoffs.relfrozenxid, - vacrel->NewRelfrozenXid)); - Assert(vacrel->NewRelminMxid == vacrel->cutoffs.OldestMxact || + vacrel->scan_stats->NewRelfrozenXid)); + Assert(vacrel->scan_stats->NewRelminMxid == vacrel->cutoffs.OldestMxact || MultiXactIdPrecedesOrEquals(vacrel->aggressive ? vacrel->cutoffs.MultiXactCutoff : vacrel->cutoffs.relminmxid, - vacrel->NewRelminMxid)); - if (vacrel->skippedallvis) + vacrel->scan_stats->NewRelminMxid)); + if (vacrel->scan_stats->skippedallvis) { /* * Must keep original relfrozenxid in a non-aggressive VACUUM that @@ -549,8 +671,8 @@ heap_vacuum_rel(Relation rel, VacuumParams *params, * values will have missed unfrozen XIDs from the pages we skipped. */ Assert(!vacrel->aggressive); - vacrel->NewRelfrozenXid = InvalidTransactionId; - vacrel->NewRelminMxid = InvalidMultiXactId; + vacrel->scan_stats->NewRelfrozenXid = InvalidTransactionId; + vacrel->scan_stats->NewRelminMxid = InvalidMultiXactId; } /* @@ -571,7 +693,7 @@ heap_vacuum_rel(Relation rel, VacuumParams *params, */ vac_update_relstats(rel, new_rel_pages, vacrel->new_live_tuples, new_rel_allvisible, vacrel->nindexes > 0, - vacrel->NewRelfrozenXid, vacrel->NewRelminMxid, + vacrel->scan_stats->NewRelfrozenXid, vacrel->scan_stats->NewRelminMxid, &frozenxid_updated, &minmulti_updated, false); /* @@ -587,8 +709,8 @@ heap_vacuum_rel(Relation rel, VacuumParams *params, pgstat_report_vacuum(RelationGetRelid(rel), rel->rd_rel->relisshared, Max(vacrel->new_live_tuples, 0), - vacrel->recently_dead_tuples + - vacrel->missed_dead_tuples); + vacrel->scan_stats->recently_dead_tuples + + vacrel->scan_stats->missed_dead_tuples); pgstat_progress_end_command(); if (instrument) @@ -661,21 +783,21 @@ heap_vacuum_rel(Relation rel, VacuumParams *params, vacrel->relname, vacrel->num_index_scans); appendStringInfo(&buf, _("pages: %u removed, %u remain, %u scanned (%.2f%% of total)\n"), - vacrel->removed_pages, + vacrel->scan_stats->removed_pages, new_rel_pages, - vacrel->scanned_pages, + vacrel->scan_stats->scanned_pages, orig_rel_pages == 0 ? 100.0 : - 100.0 * vacrel->scanned_pages / orig_rel_pages); + 100.0 * vacrel->scan_stats->scanned_pages / orig_rel_pages); appendStringInfo(&buf, _("tuples: %lld removed, %lld remain, %lld are dead but not yet removable\n"), - (long long) vacrel->tuples_deleted, + (long long) vacrel->scan_stats->tuples_deleted, (long long) vacrel->new_rel_tuples, - (long long) vacrel->recently_dead_tuples); - if (vacrel->missed_dead_tuples > 0) + (long long) vacrel->scan_stats->recently_dead_tuples); + if (vacrel->scan_stats->missed_dead_tuples > 0) appendStringInfo(&buf, _("tuples missed: %lld dead from %u pages not removed due to cleanup lock contention\n"), - (long long) vacrel->missed_dead_tuples, - vacrel->missed_dead_pages); + (long long) vacrel->scan_stats->missed_dead_tuples, + vacrel->scan_stats->missed_dead_pages); diff = (int32) (ReadNextTransactionId() - vacrel->cutoffs.OldestXmin); appendStringInfo(&buf, @@ -683,25 +805,25 @@ heap_vacuum_rel(Relation rel, VacuumParams *params, vacrel->cutoffs.OldestXmin, diff); if (frozenxid_updated) { - diff = (int32) (vacrel->NewRelfrozenXid - + diff = (int32) (vacrel->scan_stats->NewRelfrozenXid - vacrel->cutoffs.relfrozenxid); appendStringInfo(&buf, _("new relfrozenxid: %u, which is %d XIDs ahead of previous value\n"), - vacrel->NewRelfrozenXid, diff); + vacrel->scan_stats->NewRelfrozenXid, diff); } if (minmulti_updated) { - diff = (int32) (vacrel->NewRelminMxid - + diff = (int32) (vacrel->scan_stats->NewRelminMxid - vacrel->cutoffs.relminmxid); appendStringInfo(&buf, _("new relminmxid: %u, which is %d MXIDs ahead of previous value\n"), - vacrel->NewRelminMxid, diff); + vacrel->scan_stats->NewRelminMxid, diff); } appendStringInfo(&buf, _("frozen: %u pages from table (%.2f%% of total) had %lld tuples frozen\n"), - vacrel->frozen_pages, + vacrel->scan_stats->frozen_pages, orig_rel_pages == 0 ? 100.0 : - 100.0 * vacrel->frozen_pages / orig_rel_pages, - (long long) vacrel->tuples_frozen); + 100.0 * vacrel->scan_stats->frozen_pages / orig_rel_pages, + (long long) vacrel->scan_stats->tuples_frozen); if (vacrel->do_index_vacuuming) { if (vacrel->nindexes == 0 || vacrel->num_index_scans == 0) @@ -721,10 +843,10 @@ heap_vacuum_rel(Relation rel, VacuumParams *params, msgfmt = _("%u pages from table (%.2f%% of total) have %lld dead item identifiers\n"); } appendStringInfo(&buf, msgfmt, - vacrel->lpdead_item_pages, + vacrel->scan_stats->lpdead_item_pages, orig_rel_pages == 0 ? 100.0 : - 100.0 * vacrel->lpdead_item_pages / orig_rel_pages, - (long long) vacrel->lpdead_items); + 100.0 * vacrel->scan_stats->lpdead_item_pages / orig_rel_pages, + (long long) vacrel->scan_stats->lpdead_items); for (int i = 0; i < vacrel->nindexes; i++) { IndexBulkDeleteResult *istat = vacrel->indstats[i]; @@ -825,14 +947,8 @@ heap_vacuum_rel(Relation rel, VacuumParams *params, static void lazy_scan_heap(LVRelState *vacrel) { - BlockNumber rel_pages = vacrel->rel_pages, - blkno, - next_fsm_block_to_vacuum = 0; - bool all_visible_according_to_vm; - - TidStore *dead_items = vacrel->dead_items; + BlockNumber rel_pages = vacrel->rel_pages; VacDeadItemsInfo *dead_items_info = vacrel->dead_items_info; - Buffer vmbuffer = InvalidBuffer; const int initprog_index[] = { PROGRESS_VACUUM_PHASE, PROGRESS_VACUUM_TOTAL_HEAP_BLKS, @@ -852,6 +968,72 @@ lazy_scan_heap(LVRelState *vacrel) vacrel->next_unskippable_allvis = false; vacrel->next_unskippable_vmbuffer = InvalidBuffer; + /* + * Do the actual work. If parallel heap vacuum is active, we scan and + * vacuum heap with parallel workers. + */ + if (ParallelHeapVacuumIsActive(vacrel)) + do_parallel_lazy_scan_heap(vacrel); + else + do_lazy_scan_heap(vacrel); + + /* report that everything is now scanned */ + pgstat_progress_update_param(PROGRESS_VACUUM_HEAP_BLKS_SCANNED, rel_pages); + + /* now we can compute the new value for pg_class.reltuples */ + vacrel->new_live_tuples = vac_estimate_reltuples(vacrel->rel, rel_pages, + vacrel->scan_stats->scanned_pages, + vacrel->scan_stats->live_tuples); + + /* + * Also compute the total number of surviving heap entries. In the + * (unlikely) scenario that new_live_tuples is -1, take it as zero. + */ + vacrel->new_rel_tuples = + Max(vacrel->new_live_tuples, 0) + vacrel->scan_stats->recently_dead_tuples + + vacrel->scan_stats->missed_dead_tuples; + + /* + * Do index vacuuming (call each index's ambulkdelete routine), then do + * related heap vacuuming + */ + if (dead_items_info->num_items > 0) + lazy_vacuum(vacrel); + + /* + * Vacuum the remainder of the Free Space Map. We must do this whether or + * not there were indexes, and whether or not we bypassed index vacuuming. + */ + if (rel_pages > vacrel->next_fsm_block_to_vacuum) + FreeSpaceMapVacuumRange(vacrel->rel, vacrel->next_fsm_block_to_vacuum, + rel_pages); + + /* report all blocks vacuumed */ + pgstat_progress_update_param(PROGRESS_VACUUM_HEAP_BLKS_VACUUMED, rel_pages); + + /* Do final index cleanup (call each index's amvacuumcleanup routine) */ + if (vacrel->nindexes > 0 && vacrel->do_index_cleanup) + lazy_cleanup_all_indexes(vacrel); +} + +/* + * Workhorse for lazy_scan_heap(). + * + * Return true if we processed all blocks, otherwise false if we exit from this function + * while not completing the heap scan due to full of dead item TIDs. In serial heap scan + * case, this function always returns true. In parallel heap vacuum scan, this function + * is called by both worker processes and the leader process, and could return false. + */ +static bool +do_lazy_scan_heap(LVRelState *vacrel) +{ + bool all_visible_according_to_vm; + TidStore *dead_items = vacrel->dead_items; + VacDeadItemsInfo *dead_items_info = vacrel->dead_items_info; + BlockNumber blkno; + Buffer vmbuffer = InvalidBuffer; + bool scan_done = true; + while (heap_vac_scan_next_block(vacrel, &blkno, &all_visible_according_to_vm)) { Buffer buf; @@ -859,13 +1041,20 @@ lazy_scan_heap(LVRelState *vacrel) bool has_lpdead_items; bool got_cleanup_lock = false; - vacrel->scanned_pages++; + vacrel->scan_stats->scanned_pages++; /* Report as block scanned, update error traceback information */ pgstat_progress_update_param(PROGRESS_VACUUM_HEAP_BLKS_SCANNED, blkno); update_vacuum_error_info(vacrel, NULL, VACUUM_ERRCB_PHASE_SCAN_HEAP, blkno, InvalidOffsetNumber); + /* + * If parallel vacuum scan is enabled, advertise the current block + * number + */ + if (ParallelHeapVacuumIsActive(vacrel)) + pg_atomic_write_u32(&(vacrel->phvstate->myscanstate->cur_blkno), (uint32) blkno); + vacuum_delay_point(); /* @@ -877,46 +1066,10 @@ lazy_scan_heap(LVRelState *vacrel) * one-pass strategy, and the two-pass strategy with the index_cleanup * param set to 'off'. */ - if (vacrel->scanned_pages % FAILSAFE_EVERY_PAGES == 0) + if (!IsParallelWorker() && + vacrel->scan_stats->scanned_pages % FAILSAFE_EVERY_PAGES == 0) lazy_check_wraparound_failsafe(vacrel); - /* - * Consider if we definitely have enough space to process TIDs on page - * already. If we are close to overrunning the available space for - * dead_items TIDs, pause and do a cycle of vacuuming before we tackle - * this page. - */ - if (TidStoreMemoryUsage(dead_items) > dead_items_info->max_bytes) - { - /* - * Before beginning index vacuuming, we release any pin we may - * hold on the visibility map page. This isn't necessary for - * correctness, but we do it anyway to avoid holding the pin - * across a lengthy, unrelated operation. - */ - if (BufferIsValid(vmbuffer)) - { - ReleaseBuffer(vmbuffer); - vmbuffer = InvalidBuffer; - } - - /* Perform a round of index and heap vacuuming */ - vacrel->consider_bypass_optimization = false; - lazy_vacuum(vacrel); - - /* - * Vacuum the Free Space Map to make newly-freed space visible on - * upper-level FSM pages. Note we have not yet processed blkno. - */ - FreeSpaceMapVacuumRange(vacrel->rel, next_fsm_block_to_vacuum, - blkno); - next_fsm_block_to_vacuum = blkno; - - /* Report that we are once again scanning the heap */ - pgstat_progress_update_param(PROGRESS_VACUUM_PHASE, - PROGRESS_VACUUM_PHASE_SCAN_HEAP); - } - /* * Pin the visibility map page in case we need to mark the page * all-visible. In most cases this will be very cheap, because we'll @@ -1005,9 +1158,10 @@ lazy_scan_heap(LVRelState *vacrel) * revisit this page. Since updating the FSM is desirable but not * absolutely required, that's OK. */ - if (vacrel->nindexes == 0 - || !vacrel->do_index_vacuuming - || !has_lpdead_items) + if (!IsParallelWorker() && + (vacrel->nindexes == 0 + || !vacrel->do_index_vacuuming + || !has_lpdead_items)) { Size freespace = PageGetHeapFreeSpace(page); @@ -1021,57 +1175,172 @@ lazy_scan_heap(LVRelState *vacrel) * held the cleanup lock and lazy_scan_prune() was called. */ if (got_cleanup_lock && vacrel->nindexes == 0 && has_lpdead_items && - blkno - next_fsm_block_to_vacuum >= VACUUM_FSM_EVERY_PAGES) + blkno - vacrel->next_fsm_block_to_vacuum >= VACUUM_FSM_EVERY_PAGES) { - FreeSpaceMapVacuumRange(vacrel->rel, next_fsm_block_to_vacuum, - blkno); - next_fsm_block_to_vacuum = blkno; + BlockNumber fsm_vac_up_to; + + /* + * If parallel heap vacuum scan is active, compute the minimum + * block number we scanned so far. + */ + if (ParallelHeapVacuumIsActive(vacrel)) + { + parallel_heap_vacuum_compute_min_blkno(vacrel); + fsm_vac_up_to = vacrel->phvstate->min_blkno; + } + else + { + /* blkno is already processed */ + fsm_vac_up_to = blkno + 1; + } + + FreeSpaceMapVacuumRange(vacrel->rel, vacrel->next_fsm_block_to_vacuum, + fsm_vac_up_to); + vacrel->next_fsm_block_to_vacuum = fsm_vac_up_to; } } else UnlockReleaseBuffer(buf); + + /* + * Consider if we definitely have enough space to process TIDs on page + * already. If we are close to overrunning the available space for + * dead_items TIDs, pause and do a cycle of vacuuming before we tackle + * this page. + */ + if (TidStoreMemoryUsage(dead_items) > dead_items_info->max_bytes) + { + /* + * Before beginning index vacuuming, we release any pin we may + * hold on the visibility map page. This isn't necessary for + * correctness, but we do it anyway to avoid holding the pin + * across a lengthy, unrelated operation. + */ + if (BufferIsValid(vmbuffer)) + { + ReleaseBuffer(vmbuffer); + vmbuffer = InvalidBuffer; + } + + if (ParallelHeapVacuumIsActive(vacrel)) + { + /* Remember we might have some unprocessed blocks */ + scan_done = false; + + /* + * Pause the heap scan without invoking index and heap + * vacuuming. The leader process also skips FSM vacuum since + * some blocks before blkno might have not processed yet. The + * leader will wait for all workers to finish and perform + * index and heap vacuuming, and then perform FSM vacuum. + */ + break; + } + + /* Perform a round of index and heap vacuuming */ + vacrel->consider_bypass_optimization = false; + lazy_vacuum(vacrel); + + /* + * Vacuum the Free Space Map to make newly-freed space visible on + * upper-level FSM pages. + */ + FreeSpaceMapVacuumRange(vacrel->rel, vacrel->next_fsm_block_to_vacuum, + blkno + 1); + vacrel->next_fsm_block_to_vacuum = blkno; + + /* Report that we are once again scanning the heap */ + pgstat_progress_update_param(PROGRESS_VACUUM_PHASE, + PROGRESS_VACUUM_PHASE_SCAN_HEAP); + + continue; + } } vacrel->blkno = InvalidBlockNumber; if (BufferIsValid(vmbuffer)) ReleaseBuffer(vmbuffer); - /* report that everything is now scanned */ - pgstat_progress_update_param(PROGRESS_VACUUM_HEAP_BLKS_SCANNED, blkno); + return scan_done; +} - /* now we can compute the new value for pg_class.reltuples */ - vacrel->new_live_tuples = vac_estimate_reltuples(vacrel->rel, rel_pages, - vacrel->scanned_pages, - vacrel->live_tuples); +/* + * A parallel scan variant of heap_vac_scan_next_block. + * + * In parallel vacuum scan, we don't use the SKIP_PAGES_THRESHOLD optimization. + */ +static bool +heap_vac_scan_next_block_parallel(LVRelState *vacrel, BlockNumber *blkno, + bool *all_visible_according_to_vm) +{ + PHVState *phvstate = vacrel->phvstate; + BlockNumber next_block; + Buffer vmbuffer = InvalidBuffer; + uint8 mapbits = 0; - /* - * Also compute the total number of surviving heap entries. In the - * (unlikely) scenario that new_live_tuples is -1, take it as zero. - */ - vacrel->new_rel_tuples = - Max(vacrel->new_live_tuples, 0) + vacrel->recently_dead_tuples + - vacrel->missed_dead_tuples; + Assert(ParallelHeapVacuumIsActive(vacrel)); - /* - * Do index vacuuming (call each index's ambulkdelete routine), then do - * related heap vacuuming - */ - if (dead_items_info->num_items > 0) - lazy_vacuum(vacrel); + for (;;) + { + next_block = table_block_parallelscan_nextpage(vacrel->rel, + &(phvstate->myscanstate->state), + phvstate->pscandesc); - /* - * Vacuum the remainder of the Free Space Map. We must do this whether or - * not there were indexes, and whether or not we bypassed index vacuuming. - */ - if (blkno > next_fsm_block_to_vacuum) - FreeSpaceMapVacuumRange(vacrel->rel, next_fsm_block_to_vacuum, blkno); + /* Have we reached the end of the table? */ + if (!BlockNumberIsValid(next_block) || next_block >= vacrel->rel_pages) + { + if (BufferIsValid(vmbuffer)) + ReleaseBuffer(vmbuffer); - /* report all blocks vacuumed */ - pgstat_progress_update_param(PROGRESS_VACUUM_HEAP_BLKS_VACUUMED, blkno); + *blkno = vacrel->rel_pages; + return false; + } - /* Do final index cleanup (call each index's amvacuumcleanup routine) */ - if (vacrel->nindexes > 0 && vacrel->do_index_cleanup) - lazy_cleanup_all_indexes(vacrel); + /* We always treat the last block as unsafe to skip */ + if (next_block == vacrel->rel_pages - 1) + break; + + mapbits = visibilitymap_get_status(vacrel->rel, next_block, &vmbuffer); + + /* + * A block is unskippable if it is not all visible according to the + * visibility map. + */ + if ((mapbits & VISIBILITYMAP_ALL_VISIBLE) == 0) + { + Assert((mapbits & VISIBILITYMAP_ALL_FROZEN) == 0); + break; + } + + /* DISABLE_PAGE_SKIPPING makes all skipping unsafe */ + if (!vacrel->skipwithvm) + break; + + /* + * Aggressive VACUUM caller can't skip pages just because they are + * all-visible. + */ + if ((mapbits & VISIBILITYMAP_ALL_FROZEN) == 0) + { + + if (vacrel->aggressive) + break; + + /* + * All-visible block is safe to skip in non-aggressive case. But + * remember that the final range contains such a block for later. + */ + vacrel->scan_stats->skippedallvis = true; + } + } + + if (BufferIsValid(vmbuffer)) + ReleaseBuffer(vmbuffer); + + *blkno = next_block; + *all_visible_according_to_vm = (mapbits & VISIBILITYMAP_ALL_VISIBLE) != 0; + + return true; } /* @@ -1098,6 +1367,9 @@ heap_vac_scan_next_block(LVRelState *vacrel, BlockNumber *blkno, { BlockNumber next_block; + if (ParallelHeapVacuumIsActive(vacrel)) + return heap_vac_scan_next_block_parallel(vacrel, blkno, all_visible_according_to_vm); + /* relies on InvalidBlockNumber + 1 overflowing to 0 on first call */ next_block = vacrel->current_block + 1; @@ -1147,7 +1419,7 @@ heap_vac_scan_next_block(LVRelState *vacrel, BlockNumber *blkno, { next_block = vacrel->next_unskippable_block; if (skipsallvis) - vacrel->skippedallvis = true; + vacrel->scan_stats->skippedallvis = true; } } @@ -1220,11 +1492,12 @@ find_next_unskippable_block(LVRelState *vacrel, bool *skipsallvis) /* * Caller must scan the last page to determine whether it has tuples - * (caller must have the opportunity to set vacrel->nonempty_pages). - * This rule avoids having lazy_truncate_heap() take access-exclusive - * lock on rel to attempt a truncation that fails anyway, just because - * there are tuples on the last page (it is likely that there will be - * tuples on other nearby pages as well, but those can be skipped). + * (caller must have the opportunity to set + * vacrel->scan_stats->nonempty_pages). This rule avoids having + * lazy_truncate_heap() take access-exclusive lock on rel to attempt a + * truncation that fails anyway, just because there are tuples on the + * last page (it is likely that there will be tuples on other nearby + * pages as well, but those can be skipped). * * Implement this by always treating the last block as unsafe to skip. */ @@ -1449,10 +1722,10 @@ lazy_scan_prune(LVRelState *vacrel, heap_page_prune_and_freeze(rel, buf, vacrel->vistest, prune_options, &vacrel->cutoffs, &presult, PRUNE_VACUUM_SCAN, &vacrel->offnum, - &vacrel->NewRelfrozenXid, &vacrel->NewRelminMxid); + &vacrel->scan_stats->NewRelfrozenXid, &vacrel->scan_stats->NewRelminMxid); - Assert(MultiXactIdIsValid(vacrel->NewRelminMxid)); - Assert(TransactionIdIsValid(vacrel->NewRelfrozenXid)); + Assert(MultiXactIdIsValid(vacrel->scan_stats->NewRelminMxid)); + Assert(TransactionIdIsValid(vacrel->scan_stats->NewRelfrozenXid)); if (presult.nfrozen > 0) { @@ -1461,7 +1734,7 @@ lazy_scan_prune(LVRelState *vacrel, * nfrozen == 0, since it only counts pages with newly frozen tuples * (don't confuse that with pages newly set all-frozen in VM). */ - vacrel->frozen_pages++; + vacrel->scan_stats->frozen_pages++; } /* @@ -1496,7 +1769,7 @@ lazy_scan_prune(LVRelState *vacrel, */ if (presult.lpdead_items > 0) { - vacrel->lpdead_item_pages++; + vacrel->scan_stats->lpdead_item_pages++; /* * deadoffsets are collected incrementally in @@ -1511,15 +1784,15 @@ lazy_scan_prune(LVRelState *vacrel, } /* Finally, add page-local counts to whole-VACUUM counts */ - vacrel->tuples_deleted += presult.ndeleted; - vacrel->tuples_frozen += presult.nfrozen; - vacrel->lpdead_items += presult.lpdead_items; - vacrel->live_tuples += presult.live_tuples; - vacrel->recently_dead_tuples += presult.recently_dead_tuples; + vacrel->scan_stats->tuples_deleted += presult.ndeleted; + vacrel->scan_stats->tuples_frozen += presult.nfrozen; + vacrel->scan_stats->lpdead_items += presult.lpdead_items; + vacrel->scan_stats->live_tuples += presult.live_tuples; + vacrel->scan_stats->recently_dead_tuples += presult.recently_dead_tuples; /* Can't truncate this page */ if (presult.hastup) - vacrel->nonempty_pages = blkno + 1; + vacrel->scan_stats->nonempty_pages = blkno + 1; /* Did we find LP_DEAD items? */ *has_lpdead_items = (presult.lpdead_items > 0); @@ -1669,8 +1942,8 @@ lazy_scan_noprune(LVRelState *vacrel, missed_dead_tuples; bool hastup; HeapTupleHeader tupleheader; - TransactionId NoFreezePageRelfrozenXid = vacrel->NewRelfrozenXid; - MultiXactId NoFreezePageRelminMxid = vacrel->NewRelminMxid; + TransactionId NoFreezePageRelfrozenXid = vacrel->scan_stats->NewRelfrozenXid; + MultiXactId NoFreezePageRelminMxid = vacrel->scan_stats->NewRelminMxid; OffsetNumber deadoffsets[MaxHeapTuplesPerPage]; Assert(BufferGetBlockNumber(buf) == blkno); @@ -1797,8 +2070,8 @@ lazy_scan_noprune(LVRelState *vacrel, * this particular page until the next VACUUM. Remember its details now. * (lazy_scan_prune expects a clean slate, so we have to do this last.) */ - vacrel->NewRelfrozenXid = NoFreezePageRelfrozenXid; - vacrel->NewRelminMxid = NoFreezePageRelminMxid; + vacrel->scan_stats->NewRelfrozenXid = NoFreezePageRelfrozenXid; + vacrel->scan_stats->NewRelminMxid = NoFreezePageRelminMxid; /* Save any LP_DEAD items found on the page in dead_items */ if (vacrel->nindexes == 0) @@ -1825,25 +2098,25 @@ lazy_scan_noprune(LVRelState *vacrel, * indexes will be deleted during index vacuuming (and then marked * LP_UNUSED in the heap) */ - vacrel->lpdead_item_pages++; + vacrel->scan_stats->lpdead_item_pages++; dead_items_add(vacrel, blkno, deadoffsets, lpdead_items); - vacrel->lpdead_items += lpdead_items; + vacrel->scan_stats->lpdead_items += lpdead_items; } /* * Finally, add relevant page-local counts to whole-VACUUM counts */ - vacrel->live_tuples += live_tuples; - vacrel->recently_dead_tuples += recently_dead_tuples; - vacrel->missed_dead_tuples += missed_dead_tuples; + vacrel->scan_stats->live_tuples += live_tuples; + vacrel->scan_stats->recently_dead_tuples += recently_dead_tuples; + vacrel->scan_stats->missed_dead_tuples += missed_dead_tuples; if (missed_dead_tuples > 0) - vacrel->missed_dead_pages++; + vacrel->scan_stats->missed_dead_pages++; /* Can't truncate this page */ if (hastup) - vacrel->nonempty_pages = blkno + 1; + vacrel->scan_stats->nonempty_pages = blkno + 1; /* Did we find LP_DEAD items? */ *has_lpdead_items = (lpdead_items > 0); @@ -1872,7 +2145,7 @@ lazy_vacuum(LVRelState *vacrel) /* Should not end up here with no indexes */ Assert(vacrel->nindexes > 0); - Assert(vacrel->lpdead_item_pages > 0); + Assert(vacrel->scan_stats->lpdead_item_pages > 0); if (!vacrel->do_index_vacuuming) { @@ -1906,7 +2179,7 @@ lazy_vacuum(LVRelState *vacrel) BlockNumber threshold; Assert(vacrel->num_index_scans == 0); - Assert(vacrel->lpdead_items == vacrel->dead_items_info->num_items); + Assert(vacrel->scan_stats->lpdead_items == vacrel->dead_items_info->num_items); Assert(vacrel->do_index_vacuuming); Assert(vacrel->do_index_cleanup); @@ -1933,7 +2206,7 @@ lazy_vacuum(LVRelState *vacrel) * cases then this may need to be reconsidered. */ threshold = (double) vacrel->rel_pages * BYPASS_THRESHOLD_PAGES; - bypass = (vacrel->lpdead_item_pages < threshold && + bypass = (vacrel->scan_stats->lpdead_item_pages < threshold && (TidStoreMemoryUsage(vacrel->dead_items) < (32L * 1024L * 1024L))); } @@ -2026,7 +2299,7 @@ lazy_vacuum_all_indexes(LVRelState *vacrel) progress_start_val[1] = vacrel->nindexes; pgstat_progress_update_multi_param(2, progress_start_index, progress_start_val); - if (!ParallelVacuumIsActive(vacrel)) + if (!ParallelIndexVacuumIsActive(vacrel)) { for (int idx = 0; idx < vacrel->nindexes; idx++) { @@ -2071,7 +2344,7 @@ lazy_vacuum_all_indexes(LVRelState *vacrel) * place). */ Assert(vacrel->num_index_scans > 0 || - vacrel->dead_items_info->num_items == vacrel->lpdead_items); + vacrel->dead_items_info->num_items == vacrel->scan_stats->lpdead_items); Assert(allindexes || VacuumFailsafeActive); /* @@ -2180,8 +2453,8 @@ lazy_vacuum_heap_rel(LVRelState *vacrel) * the second heap pass. No more, no less. */ Assert(vacrel->num_index_scans > 1 || - (vacrel->dead_items_info->num_items == vacrel->lpdead_items && - vacuumed_pages == vacrel->lpdead_item_pages)); + (vacrel->dead_items_info->num_items == vacrel->scan_stats->lpdead_items && + vacuumed_pages == vacrel->scan_stats->lpdead_item_pages)); ereport(DEBUG2, (errmsg("table \"%s\": removed %lld dead item identifiers in %u pages", @@ -2334,7 +2607,7 @@ lazy_check_wraparound_failsafe(LVRelState *vacrel) vacrel->do_index_cleanup = false; vacrel->do_rel_truncate = false; - /* Reset the progress counters */ + /* Reset the progress scan_stats */ pgstat_progress_update_multi_param(2, progress_index, progress_val); ereport(WARNING, @@ -2362,7 +2635,7 @@ static void lazy_cleanup_all_indexes(LVRelState *vacrel) { double reltuples = vacrel->new_rel_tuples; - bool estimated_count = vacrel->scanned_pages < vacrel->rel_pages; + bool estimated_count = vacrel->scan_stats->scanned_pages < vacrel->rel_pages; const int progress_start_index[] = { PROGRESS_VACUUM_PHASE, PROGRESS_VACUUM_INDEXES_TOTAL @@ -2385,7 +2658,7 @@ lazy_cleanup_all_indexes(LVRelState *vacrel) progress_start_val[1] = vacrel->nindexes; pgstat_progress_update_multi_param(2, progress_start_index, progress_start_val); - if (!ParallelVacuumIsActive(vacrel)) + if (!ParallelIndexVacuumIsActive(vacrel)) { for (int idx = 0; idx < vacrel->nindexes; idx++) { @@ -2409,7 +2682,7 @@ lazy_cleanup_all_indexes(LVRelState *vacrel) estimated_count); } - /* Reset the progress counters */ + /* Reset the progress scan_stats */ pgstat_progress_update_multi_param(2, progress_end_index, progress_end_val); } @@ -2543,7 +2816,7 @@ should_attempt_truncation(LVRelState *vacrel) if (!vacrel->do_rel_truncate || VacuumFailsafeActive) return false; - possibly_freeable = vacrel->rel_pages - vacrel->nonempty_pages; + possibly_freeable = vacrel->rel_pages - vacrel->scan_stats->nonempty_pages; if (possibly_freeable > 0 && (possibly_freeable >= REL_TRUNCATE_MINIMUM || possibly_freeable >= vacrel->rel_pages / REL_TRUNCATE_FRACTION)) @@ -2569,7 +2842,7 @@ lazy_truncate_heap(LVRelState *vacrel) /* Update error traceback information one last time */ update_vacuum_error_info(vacrel, NULL, VACUUM_ERRCB_PHASE_TRUNCATE, - vacrel->nonempty_pages, InvalidOffsetNumber); + vacrel->scan_stats->nonempty_pages, InvalidOffsetNumber); /* * Loop until no more truncating can be done. @@ -2670,7 +2943,7 @@ lazy_truncate_heap(LVRelState *vacrel) * without also touching reltuples, since the tuple count wasn't * changed by the truncation. */ - vacrel->removed_pages += orig_rel_pages - new_rel_pages; + vacrel->scan_stats->removed_pages += orig_rel_pages - new_rel_pages; vacrel->rel_pages = new_rel_pages; ereport(vacrel->verbose ? INFO : DEBUG2, @@ -2678,7 +2951,7 @@ lazy_truncate_heap(LVRelState *vacrel) vacrel->relname, orig_rel_pages, new_rel_pages))); orig_rel_pages = new_rel_pages; - } while (new_rel_pages > vacrel->nonempty_pages && lock_waiter_detected); + } while (new_rel_pages > vacrel->scan_stats->nonempty_pages && lock_waiter_detected); } /* @@ -2706,7 +2979,7 @@ count_nondeletable_pages(LVRelState *vacrel, bool *lock_waiter_detected) StaticAssertStmt((PREFETCH_SIZE & (PREFETCH_SIZE - 1)) == 0, "prefetch size must be power of 2"); prefetchedUntil = InvalidBlockNumber; - while (blkno > vacrel->nonempty_pages) + while (blkno > vacrel->scan_stats->nonempty_pages) { Buffer buf; Page page; @@ -2818,7 +3091,7 @@ count_nondeletable_pages(LVRelState *vacrel, bool *lock_waiter_detected) * pages still are; we need not bother to look at the last known-nonempty * page. */ - return vacrel->nonempty_pages; + return vacrel->scan_stats->nonempty_pages; } /* @@ -2836,12 +3109,8 @@ dead_items_alloc(LVRelState *vacrel, int nworkers) autovacuum_work_mem != -1 ? autovacuum_work_mem : maintenance_work_mem; - /* - * Initialize state for a parallel vacuum. As of now, only one worker can - * be used for an index, so we invoke parallelism only if there are at - * least two indexes on a table. - */ - if (nworkers >= 0 && vacrel->nindexes > 1 && vacrel->do_index_vacuuming) + /* Initialize state for a parallel vacuum */ + if (nworkers >= 0) { /* * Since parallel workers cannot access data in temporary tables, we @@ -2859,11 +3128,20 @@ dead_items_alloc(LVRelState *vacrel, int nworkers) vacrel->relname))); } else + { + /* + * We initialize parallel heap scan/vacuuming or index vacuuming + * or both based on the table size and the number of indexes. Note + * that only one worker can be used for an index, we invoke + * parallelism for index vacuuming only if there are at least two + * indexes on a table. + */ vacrel->pvs = parallel_vacuum_init(vacrel->rel, vacrel->indrels, vacrel->nindexes, nworkers, vac_work_mem, vacrel->verbose ? INFO : DEBUG2, - vacrel->bstrategy); + vacrel->bstrategy, (void *) vacrel); + } /* * If parallel mode started, dead_items and dead_items_info spaces are @@ -2904,9 +3182,19 @@ dead_items_add(LVRelState *vacrel, BlockNumber blkno, OffsetNumber *offsets, }; int64 prog_val[2]; + /* + * Protect both dead_items and dead_items_info from concurrent updates in + * parallel heap scan cases. + */ + if (ParallelHeapVacuumIsActive(vacrel)) + TidStoreLockExclusive(dead_items); + TidStoreSetBlockOffsets(dead_items, blkno, offsets, num_offsets); vacrel->dead_items_info->num_items += num_offsets; + if (ParallelHeapVacuumIsActive(vacrel)) + TidStoreUnlock(dead_items); + /* update the progress information */ prog_val[0] = vacrel->dead_items_info->num_items; prog_val[1] = TidStoreMemoryUsage(dead_items); @@ -3108,6 +3396,453 @@ update_relstats_all_indexes(LVRelState *vacrel) } } +/* + * Compute the number of parallel workers for parallel vacuum heap scan. + * + * The calculation logic is borrowed from compute_parallel_worker(). + */ +int +heap_parallel_vacuum_compute_workers(Relation rel, int nrequested) +{ + int parallel_workers = 0; + int heap_parallel_threshold; + int heap_pages; + + if (nrequested == 0) + { + /* + * Select the number of workers based on the log of the size of the + * relation. This probably needs to be a good deal more + * sophisticated, but we need something here for now. Note that the + * upper limit of the min_parallel_table_scan_size GUC is chosen to + * prevent overflow here. + */ + heap_parallel_threshold = Max(min_parallel_table_scan_size, 1); + heap_pages = RelationGetNumberOfBlocks(rel); + while (heap_pages >= (BlockNumber) (heap_parallel_threshold * 3)) + { + parallel_workers++; + heap_parallel_threshold *= 3; + if (heap_parallel_threshold > INT_MAX / 3) + break; + } + } + else + parallel_workers = nrequested; + + return parallel_workers; +} + +/* Estimate shared memory sizes required for parallel heap vacuum */ +static inline void +heap_parallel_estimate_shared_memory_size(Relation rel, int nworkers, Size *pscan_len, + Size *shared_len, Size *pscanwork_len) +{ + Size size = 0; + + size = add_size(size, SizeOfPHVShared); + size = add_size(size, mul_size(sizeof(LVRelScanStats), nworkers)); + *shared_len = size; + + *pscan_len = table_block_parallelscan_estimate(rel); + + *pscanwork_len = mul_size(sizeof(PHVScanWorkerState), nworkers); +} + +/* + * Compute the amount of space we'll need in the parallel heap vacuum + * DSM, and inform pcxt->estimator about our needs. + * + * nworkers is the number of workers for the table vacuum. Note that it could + * be different than pcxt->nworkers since it is the maximum of number of + * workers for table vacuum and index vacuum. + */ +void +heap_parallel_vacuum_estimate(Relation rel, ParallelContext *pcxt, + int nworkers, void *state) +{ + Size pscan_len; + Size shared_len; + Size pscanwork_len; + + heap_parallel_estimate_shared_memory_size(rel, nworkers, &pscan_len, + &shared_len, &pscanwork_len); + + /* space for PHVShared */ + shm_toc_estimate_chunk(&pcxt->estimator, shared_len); + shm_toc_estimate_keys(&pcxt->estimator, 1); + + /* space for ParallelBlockTableScanDesc */ + pscan_len = table_block_parallelscan_estimate(rel); + shm_toc_estimate_chunk(&pcxt->estimator, pscan_len); + shm_toc_estimate_keys(&pcxt->estimator, 1); + + /* space for per-worker scan state, PHVScanWorkerState */ + pscanwork_len = mul_size(sizeof(PHVScanWorkerState), nworkers); + shm_toc_estimate_chunk(&pcxt->estimator, pscanwork_len); + shm_toc_estimate_keys(&pcxt->estimator, 1); +} + +/* + * Set up shared memory for parallel heap vacuum. + */ +void +heap_parallel_vacuum_initialize(Relation rel, ParallelContext *pcxt, + int nworkers, void *state) +{ + LVRelState *vacrel = (LVRelState *) state; + PHVState *phvstate = vacrel->phvstate; + ParallelBlockTableScanDesc pscan; + PHVScanWorkerState *pscanwork; + PHVShared *shared; + Size pscan_len; + Size shared_len; + Size pscanwork_len; + + phvstate = (PHVState *) palloc(sizeof(PHVState)); + + heap_parallel_estimate_shared_memory_size(rel, nworkers, &pscan_len, + &shared_len, &pscanwork_len); + + shared = shm_toc_allocate(pcxt->toc, shared_len); + + /* Prepare the shared information */ + + MemSet(shared, 0, shared_len); + shared->aggressive = vacrel->aggressive; + shared->skipwithvm = vacrel->skipwithvm; + shared->cutoffs = vacrel->cutoffs; + shared->NewRelfrozenXid = vacrel->scan_stats->NewRelfrozenXid; + shared->NewRelminMxid = vacrel->scan_stats->NewRelminMxid; + shared->skippedallvis = vacrel->scan_stats->skippedallvis; + + /* + * XXX: we copy the contents of vistest to the shared area, but in order + * to do that, we need to either expose GlobalVisTest or to provide + * functions to copy contents of GlobalVisTest to somewhere. Currently we + * do the former but not sure it's the best choice. + * + * Alternative idea is to have each worker determine cutoff and have their + * own vistest. But we need to carefully consider it since parallel + * workers end up having different cutoff and horizon. + */ + shared->vistest = *vacrel->vistest; + + shm_toc_insert(pcxt->toc, LV_PARALLEL_SCAN_SHARED, shared); + + phvstate->shared = shared; + + /* prepare the parallel block table scan description */ + pscan = shm_toc_allocate(pcxt->toc, pscan_len); + shm_toc_insert(pcxt->toc, LV_PARALLEL_SCAN_DESC, pscan); + + /* initialize parallel scan description */ + table_block_parallelscan_initialize(rel, (ParallelTableScanDesc) pscan); + phvstate->pscandesc = pscan; + + /* prepare the workers' parallel block table scan state */ + pscanwork = shm_toc_allocate(pcxt->toc, pscanwork_len); + MemSet(pscanwork, 0, pscanwork_len); + shm_toc_insert(pcxt->toc, LV_PARALLEL_SCAN_DESC_WORKER, pscanwork); + phvstate->scanstates = pscanwork; + + vacrel->phvstate = phvstate; +} + +/* + * Main function for parallel heap vacuum workers. + */ +void +heap_parallel_vacuum_scan_worker(Relation rel, ParallelVacuumState *pvs, + ParallelWorkerContext *pwcxt) +{ + LVRelState vacrel = {0}; + PHVState *phvstate; + PHVShared *shared; + ParallelBlockTableScanDesc pscandesc; + PHVScanWorkerState *scanstate; + LVRelScanStats *scan_stats; + ErrorContextCallback errcallback; + bool scan_done; + + phvstate = palloc(sizeof(PHVState)); + + pscandesc = (ParallelBlockTableScanDesc) shm_toc_lookup(pwcxt->toc, + LV_PARALLEL_SCAN_DESC, + false); + phvstate->pscandesc = pscandesc; + + shared = (PHVShared *) shm_toc_lookup(pwcxt->toc, LV_PARALLEL_SCAN_SHARED, + false); + phvstate->shared = shared; + + scanstate = (PHVScanWorkerState *) shm_toc_lookup(pwcxt->toc, + LV_PARALLEL_SCAN_DESC_WORKER, + false); + + phvstate->myscanstate = &(scanstate[ParallelWorkerNumber]); + scan_stats = &(shared->worker_scan_stats[ParallelWorkerNumber]); + + /* Prepare LVRelState */ + vacrel.rel = rel; + vacrel.indrels = parallel_vacuum_get_table_indexes(pvs, &vacrel.nindexes); + vacrel.pvs = pvs; + vacrel.phvstate = phvstate; + vacrel.aggressive = shared->aggressive; + vacrel.skipwithvm = shared->skipwithvm; + vacrel.cutoffs = shared->cutoffs; + vacrel.vistest = &(shared->vistest); + vacrel.dead_items = parallel_vacuum_get_dead_items(pvs, + &vacrel.dead_items_info); + vacrel.rel_pages = RelationGetNumberOfBlocks(rel); + vacrel.scan_stats = scan_stats; + + /* initialize per-worker relation statistics */ + MemSet(scan_stats, 0, sizeof(LVRelScanStats)); + + /* Set fields necessary for heap scan */ + vacrel.scan_stats->NewRelfrozenXid = shared->NewRelfrozenXid; + vacrel.scan_stats->NewRelminMxid = shared->NewRelminMxid; + vacrel.scan_stats->skippedallvis = shared->skippedallvis; + + /* Initialize the per-worker scan state if not yet */ + if (!phvstate->myscanstate->initialized) + { + table_block_parallelscan_startblock_init(rel, + &(phvstate->myscanstate->state), + phvstate->pscandesc); + + pg_atomic_init_u32(&(phvstate->myscanstate->cur_blkno), 0); + phvstate->myscanstate->maybe_have_blocks = false; + phvstate->myscanstate->initialized = true; + } + + /* + * Setup error traceback support for ereport() for parallel table vacuum + * workers + */ + vacrel.dbname = get_database_name(MyDatabaseId); + vacrel.relnamespace = get_database_name(RelationGetNamespace(rel)); + vacrel.relname = pstrdup(RelationGetRelationName(rel)); + vacrel.indname = NULL; + vacrel.phase = VACUUM_ERRCB_PHASE_SCAN_HEAP; + errcallback.callback = vacuum_error_callback; + errcallback.arg = &vacrel; + errcallback.previous = error_context_stack; + error_context_stack = &errcallback; + + scan_done = do_lazy_scan_heap(&vacrel); + + /* Pop the error context stack */ + error_context_stack = errcallback.previous; + + /* + * If the leader or a worker finishes the heap scan because dead_items + * TIDs is close to the limit, it might have some allocated blocks in its + * scan state. Since this scan state might not be used in the next heap + * scan, we remember that it might have some unconsumed blocks so that the + * leader complete the scans after the heap scan phase finishes. + */ + phvstate->myscanstate->maybe_have_blocks = !scan_done; +} + +/* + * Complete parallel heaps scans that have remaining blocks in their + * chunks. + */ +static void +parallel_heap_complete_unfinised_scan(LVRelState *vacrel) +{ + int nworkers; + + Assert(!IsParallelWorker()); + + nworkers = parallel_vacuum_get_nworkers_table(vacrel->pvs); + + for (int i = 0; i < nworkers; i++) + { + PHVScanWorkerState *wstate = &(vacrel->phvstate->scanstates[i]); + bool scan_done PG_USED_FOR_ASSERTS_ONLY; + + if (!wstate->maybe_have_blocks) + + continue; + + /* Attache the worker's scan state and do heap scan */ + vacrel->phvstate->myscanstate = wstate; + scan_done = do_lazy_scan_heap(vacrel); + + Assert(scan_done); + } + + /* + * We don't need to gather the scan statistics here because statistics + * have already been accumulated the leaders statistics directly. + */ +} + +/* + * Compute the minimum block number we have scanned so far and update + * vacrel->min_blkno. + */ +static void +parallel_heap_vacuum_compute_min_blkno(LVRelState *vacrel) +{ + PHVState *phvstate = vacrel->phvstate; + + Assert(ParallelHeapVacuumIsActive(vacrel)); + + /* + * We check all worker scan states here to compute the minimum block + * number among all scan states. + */ + for (int i = 0; i < phvstate->nworkers_launched; i++) + { + PHVScanWorkerState *wstate = &(phvstate->scanstates[i]); + BlockNumber blkno; + + /* Skip if no worker has been initialized the scan state */ + if (!wstate->initialized) + continue; + + blkno = pg_atomic_read_u32(&(wstate->cur_blkno)); + if (blkno < phvstate->min_blkno) + phvstate->min_blkno = blkno; + } +} + +/* + * Accumulate relation scan_stats that parallel workers collected into the + * leader's counters. + */ +static void +parallel_heap_vacuum_gather_scan_stats(LVRelState *vacrel) +{ + PHVState *phvstate = vacrel->phvstate; + + Assert(ParallelHeapVacuumIsActive(vacrel)); + Assert(!IsParallelWorker()); + + /* Gather the scan statistics that workers collected */ + for (int i = 0; i < phvstate->nworkers_launched; i++) + { + LVRelScanStats *ss = &(phvstate->shared->worker_scan_stats[i]); + + vacrel->scan_stats->scanned_pages += ss->scanned_pages; + vacrel->scan_stats->removed_pages += ss->removed_pages; + vacrel->scan_stats->frozen_pages += ss->frozen_pages; + vacrel->scan_stats->lpdead_item_pages += ss->lpdead_item_pages; + vacrel->scan_stats->missed_dead_pages += ss->missed_dead_pages; + vacrel->scan_stats->vacuumed_pages += ss->vacuumed_pages; + vacrel->scan_stats->tuples_deleted += ss->tuples_deleted; + vacrel->scan_stats->tuples_frozen += ss->tuples_frozen; + vacrel->scan_stats->lpdead_items += ss->lpdead_items; + vacrel->scan_stats->live_tuples += ss->live_tuples; + vacrel->scan_stats->recently_dead_tuples += ss->recently_dead_tuples; + vacrel->scan_stats->missed_dead_tuples += ss->missed_dead_tuples; + + if (ss->nonempty_pages < vacrel->scan_stats->nonempty_pages) + vacrel->scan_stats->nonempty_pages = ss->nonempty_pages; + + if (TransactionIdPrecedes(ss->NewRelfrozenXid, vacrel->scan_stats->NewRelfrozenXid)) + vacrel->scan_stats->NewRelfrozenXid = ss->NewRelfrozenXid; + + if (MultiXactIdPrecedesOrEquals(ss->NewRelminMxid, vacrel->scan_stats->NewRelminMxid)) + vacrel->scan_stats->NewRelminMxid = ss->NewRelminMxid; + + if (!vacrel->scan_stats->skippedallvis && ss->skippedallvis) + vacrel->scan_stats->skippedallvis = true; + } + + /* Also, compute the minimum block number we scanned so far */ + parallel_heap_vacuum_compute_min_blkno(vacrel); +} + +/* + * A parallel variant of do_lazy_scan_heap(). The leader process launches parallel + * workers to scan the heap in parallel. + */ +static void +do_parallel_lazy_scan_heap(LVRelState *vacrel) +{ + PHVScanWorkerState *scanstate; + + Assert(ParallelHeapVacuumIsActive(vacrel)); + Assert(!IsParallelWorker()); + + /* launcher workers */ + vacrel->phvstate->nworkers_launched = parallel_vacuum_table_scan_begin(vacrel->pvs); + + /* initialize parallel scan description to join as a worker */ + scanstate = palloc(sizeof(PHVScanWorkerState)); + table_block_parallelscan_startblock_init(vacrel->rel, &(scanstate->state), + vacrel->phvstate->pscandesc); + vacrel->phvstate->myscanstate = scanstate; + + for (;;) + { + bool scan_done; + + /* + * Scan the table until either we are close to overrunning the + * available space for dead_items TIDs or we reach the end of the + * table. + */ + scan_done = do_lazy_scan_heap(vacrel); + + /* stop parallel workers and gather the collected stats */ + parallel_vacuum_table_scan_end(vacrel->pvs); + parallel_heap_vacuum_gather_scan_stats(vacrel); + + /* + * If the heap scan paused in the middle of the table due to full of + * dead_items TIDs, perform a round of index and heap vacuuming. + */ + if (!scan_done) + { + /* Perform a round of index and heap vacuuming */ + vacrel->consider_bypass_optimization = false; + lazy_vacuum(vacrel); + + /* + * Vacuum the Free Space Map to make newly-freed space visible on + * upper-level FSM pages. + */ + if (vacrel->phvstate->min_blkno > vacrel->next_fsm_block_to_vacuum) + { + /* + * min_blkno should have already been updated when gathering + * statistics + */ + FreeSpaceMapVacuumRange(vacrel->rel, vacrel->next_fsm_block_to_vacuum, + vacrel->phvstate->min_blkno + 1); + vacrel->next_fsm_block_to_vacuum = vacrel->phvstate->min_blkno; + } + + /* Report that we are once again scanning the heap */ + pgstat_progress_update_param(PROGRESS_VACUUM_PHASE, + PROGRESS_VACUUM_PHASE_SCAN_HEAP); + + /* re-launcher workers */ + vacrel->phvstate->nworkers_launched = + parallel_vacuum_table_scan_begin(vacrel->pvs); + + continue; + } + + /* We reach the end of the table */ + break; + } + + /* + * The parallel heap vacuum finished, but it's possible that some workers + * have allocated blocks but not processed yet. This can happen for + * example when workers exit because of full of dead_items TIDs and the + * leader process could launch fewer workers in the next cycle. + */ + parallel_heap_complete_unfinised_scan(vacrel); +} + /* * Error context callback for errors occurring during vacuum. The error * context messages for index phases should match the messages set in parallel diff --git a/src/backend/commands/vacuumparallel.c b/src/backend/commands/vacuumparallel.c index 4fd6574e12..1101e799f9 100644 --- a/src/backend/commands/vacuumparallel.c +++ b/src/backend/commands/vacuumparallel.c @@ -6,15 +6,24 @@ * This file contains routines that are intended to support setting up, using, * and tearing down a ParallelVacuumState. * - * In a parallel vacuum, we perform both index bulk deletion and index cleanup - * with parallel worker processes. Individual indexes are processed by one - * vacuum process. ParallelVacuumState contains shared information as well as - * the memory space for storing dead items allocated in the DSA area. We - * launch parallel worker processes at the start of parallel index - * bulk-deletion and index cleanup and once all indexes are processed, the - * parallel worker processes exit. Each time we process indexes in parallel, - * the parallel context is re-initialized so that the same DSM can be used for - * multiple passes of index bulk-deletion and index cleanup. + * In a parallel vacuum, we perform table scan or both index bulk deletion and + * index cleanup or all of them with parallel worker processes. Different + * numbers of workers are launched for the table vacuuming and index processing. + * ParallelVacuumState contains shared information as well as the memory space + * for storing dead items allocated in the DSA area. + * + * When initializing parallel table vacuum scan, we invoke table AM routines for + * estimating DSM sizes and initializing DSM memory. Parallel table vacuum + * workers invoke the table AM routine for vacuuming the table. + * + * For processing indexes in parallel, individual indexes are processed by one + * vacuum process. We launch parallel worker processes at the start of parallel index + * bulk-deletion and index cleanup and once all indexes are processed, the parallel + * worker processes exit. + * + * Each time we process table or indexes in parallel, the parallel context is + * re-initialized so that the same DSM can be used for multiple passes of table vacuum + * or index bulk-deletion and index cleanup. * * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California @@ -28,6 +37,7 @@ #include "access/amapi.h" #include "access/table.h" +#include "access/tableam.h" #include "access/xact.h" #include "commands/progress.h" #include "commands/vacuum.h" @@ -65,6 +75,12 @@ typedef struct PVShared int elevel; uint64 queryid; + /* + * True if the caller wants parallel workers to invoke vacuum table scan + * callback. + */ + bool do_vacuum_table_scan; + /* * Fields for both index vacuum and cleanup. * @@ -164,6 +180,9 @@ struct ParallelVacuumState /* NULL for worker processes */ ParallelContext *pcxt; + /* Passed to parallel table scan workers. NULL for leader process */ + ParallelWorkerContext *pwcxt; + /* Parent Heap Relation */ Relation heaprel; @@ -193,6 +212,16 @@ struct ParallelVacuumState /* Points to WAL usage area in DSM */ WalUsage *wal_usage; + /* + * The number of workers for parallel table scan/vacuuming and index + * vacuuming, respectively. + */ + int nworkers_for_table; + int nworkers_for_index; + + /* How many times parallel table vacuum scan is called? */ + int num_table_scans; + /* * False if the index is totally unsuitable target for all parallel * processing. For example, the index could be < @@ -221,8 +250,9 @@ struct ParallelVacuumState PVIndVacStatus status; }; -static int parallel_vacuum_compute_workers(Relation *indrels, int nindexes, int nrequested, - bool *will_parallel_vacuum); +static void parallel_vacuum_compute_workers(Relation rel, Relation *indrels, int nindexes, + int nrequested, int *nworkers_table, + int *nworkers_index, bool *will_parallel_vacuum); static void parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scans, bool vacuum); static void parallel_vacuum_process_safe_indexes(ParallelVacuumState *pvs); @@ -242,7 +272,7 @@ static void parallel_vacuum_error_callback(void *arg); ParallelVacuumState * parallel_vacuum_init(Relation rel, Relation *indrels, int nindexes, int nrequested_workers, int vac_work_mem, - int elevel, BufferAccessStrategy bstrategy) + int elevel, BufferAccessStrategy bstrategy, void *state) { ParallelVacuumState *pvs; ParallelContext *pcxt; @@ -256,6 +286,8 @@ parallel_vacuum_init(Relation rel, Relation *indrels, int nindexes, Size est_shared_len; int nindexes_mwm = 0; int parallel_workers = 0; + int nworkers_table; + int nworkers_index; int querylen; /* @@ -263,15 +295,17 @@ parallel_vacuum_init(Relation rel, Relation *indrels, int nindexes, * relation */ Assert(nrequested_workers >= 0); - Assert(nindexes > 0); /* * Compute the number of parallel vacuum workers to launch */ will_parallel_vacuum = (bool *) palloc0(sizeof(bool) * nindexes); - parallel_workers = parallel_vacuum_compute_workers(indrels, nindexes, - nrequested_workers, - will_parallel_vacuum); + parallel_vacuum_compute_workers(rel, indrels, nindexes, nrequested_workers, + &nworkers_table, &nworkers_index, + will_parallel_vacuum); + + parallel_workers = Max(nworkers_table, nworkers_index); + if (parallel_workers <= 0) { /* Can't perform vacuum in parallel -- return NULL */ @@ -285,6 +319,8 @@ parallel_vacuum_init(Relation rel, Relation *indrels, int nindexes, pvs->will_parallel_vacuum = will_parallel_vacuum; pvs->bstrategy = bstrategy; pvs->heaprel = rel; + pvs->nworkers_for_table = nworkers_table; + pvs->nworkers_for_index = nworkers_index; EnterParallelMode(); pcxt = CreateParallelContext("postgres", "parallel_vacuum_main", @@ -327,6 +363,10 @@ parallel_vacuum_init(Relation rel, Relation *indrels, int nindexes, else querylen = 0; /* keep compiler quiet */ + /* Estimate AM-specific space for parallel table vacuum */ + if (nworkers_table > 0) + table_parallel_vacuum_estimate(rel, pcxt, nworkers_table, state); + InitializeParallelDSM(pcxt); /* Prepare index vacuum stats */ @@ -419,6 +459,10 @@ parallel_vacuum_init(Relation rel, Relation *indrels, int nindexes, PARALLEL_VACUUM_KEY_QUERY_TEXT, sharedquery); } + /* Prepare AM-specific DSM for parallel table vacuum */ + if (nworkers_table > 0) + table_parallel_vacuum_initialize(rel, pcxt, nworkers_table, state); + /* Success -- return parallel vacuum state */ return pvs; } @@ -534,33 +578,47 @@ parallel_vacuum_cleanup_all_indexes(ParallelVacuumState *pvs, long num_table_tup } /* - * Compute the number of parallel worker processes to request. Both index - * vacuum and index cleanup can be executed with parallel workers. - * The index is eligible for parallel vacuum iff its size is greater than - * min_parallel_index_scan_size as invoking workers for very small indexes - * can hurt performance. + * Compute the number of parallel worker processes to request for table + * vacuum and index vacuum/cleanup. + * + * For parallel table vacuum, it asks AM-specific routine to compute the + * number of parallel worker processes. The result is set to *nworkers_table. * - * nrequested is the number of parallel workers that user requested. If - * nrequested is 0, we compute the parallel degree based on nindexes, that is - * the number of indexes that support parallel vacuum. This function also - * sets will_parallel_vacuum to remember indexes that participate in parallel - * vacuum. + * For parallel index vacuum, The index is eligible for parallel vacuum iff + * its size is greater than min_parallel_index_scan_size as invoking workers + * for very small indexes can hurt performance. nrequested is the number of + * parallel workers that user requested. If nrequested is 0, we compute the + * parallel degree based on nindexes, that is the number of indexes that + * support parallel vacuum. This function also sets will_parallel_vacuum to + * remember indexes that participate in parallel vacuum. */ -static int -parallel_vacuum_compute_workers(Relation *indrels, int nindexes, int nrequested, - bool *will_parallel_vacuum) +static void +parallel_vacuum_compute_workers(Relation rel, Relation *indrels, int nindexes, + int nrequested, int *nworkers_table, + int *nworkers_index, bool *will_parallel_vacuum) { int nindexes_parallel = 0; int nindexes_parallel_bulkdel = 0; int nindexes_parallel_cleanup = 0; - int parallel_workers; + int parallel_workers_table = 0; + int parallel_workers_index = 0; + + *nworkers_table = 0; + *nworkers_index = 0; /* * We don't allow performing parallel operation in standalone backend or * when parallelism is disabled. */ if (!IsUnderPostmaster || max_parallel_maintenance_workers == 0) - return 0; + return; + + /* + * Compute the number of workers for parallel table scan. Cap by + * max_parallel_maintenance_workers. + */ + parallel_workers_table = Min(table_paralle_vacuum_compute_workers(rel, nrequested), + max_parallel_maintenance_workers); /* * Compute the number of indexes that can participate in parallel vacuum. @@ -591,17 +649,18 @@ parallel_vacuum_compute_workers(Relation *indrels, int nindexes, int nrequested, nindexes_parallel--; /* No index supports parallel vacuum */ - if (nindexes_parallel <= 0) - return 0; - - /* Compute the parallel degree */ - parallel_workers = (nrequested > 0) ? - Min(nrequested, nindexes_parallel) : nindexes_parallel; + if (nindexes_parallel > 0) + { + /* Compute the parallel degree for parallel index vacuum */ + parallel_workers_index = (nrequested > 0) ? + Min(nrequested, nindexes_parallel) : nindexes_parallel; - /* Cap by max_parallel_maintenance_workers */ - parallel_workers = Min(parallel_workers, max_parallel_maintenance_workers); + /* Cap by max_parallel_maintenance_workers */ + parallel_workers_index = Min(parallel_workers_index, max_parallel_maintenance_workers); + } - return parallel_workers; + *nworkers_table = parallel_workers_table; + *nworkers_index = parallel_workers_index; } /* @@ -671,7 +730,7 @@ parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scan if (nworkers > 0) { /* Reinitialize parallel context to relaunch parallel workers */ - if (num_index_scans > 0) + if (num_index_scans > 0 || pvs->num_table_scans > 0) ReinitializeParallelDSM(pvs->pcxt); /* @@ -980,6 +1039,139 @@ parallel_vacuum_index_is_parallel_safe(Relation indrel, int num_index_scans, return true; } +/* + * Prepare DSM and shared vacuum delays, and launch parallel workers for parallel + * table vacuum. Return the number of parallel workers launched. + * + * The caller must call parallel_vacuum_table_scan_end() to finish the parallel + * table vacuum. + */ +int +parallel_vacuum_table_scan_begin(ParallelVacuumState *pvs) +{ + Assert(!IsParallelWorker()); + + if (pvs->nworkers_for_table == 0) + return 0; + + pg_atomic_write_u32(&(pvs->shared->cost_balance), VacuumCostBalance); + pg_atomic_write_u32(&(pvs->shared->active_nworkers), 0); + + pvs->shared->do_vacuum_table_scan = true; + + if (pvs->num_table_scans > 0) + ReinitializeParallelDSM(pvs->pcxt); + + /* + * The number of workers might vary between table vacuum and index + * processing + */ + ReinitializeParallelWorkers(pvs->pcxt, pvs->nworkers_for_table); + LaunchParallelWorkers(pvs->pcxt); + + if (pvs->pcxt->nworkers_launched > 0) + { + /* + * Reset the local cost values for leader backend as we have already + * accumulated the remaining balance of heap. + */ + VacuumCostBalance = 0; + VacuumCostBalanceLocal = 0; + + /* Enable shared cost balance for leader backend */ + VacuumSharedCostBalance = &(pvs->shared->cost_balance); + VacuumActiveNWorkers = &(pvs->shared->active_nworkers); + + /* Include the worker count for the leader itself */ + pg_atomic_add_fetch_u32(VacuumActiveNWorkers, 1); + } + + ereport(pvs->shared->elevel, + (errmsg(ngettext("launched %d parallel vacuum worker for table processing (planned: %d)", + "launched %d parallel vacuum workers for table processing (planned: %d)", + pvs->pcxt->nworkers_launched), + pvs->pcxt->nworkers_launched, pvs->nworkers_for_table))); + + return pvs->pcxt->nworkers_launched; +} + +/* + * Wait for all workers for parallel table vacuum scan, and gather statistics. + */ +void +parallel_vacuum_table_scan_end(ParallelVacuumState *pvs) +{ + Assert(!IsParallelWorker()); + + if (pvs->nworkers_for_table == 0) + return; + + WaitForParallelWorkersToFinish(pvs->pcxt); + + /* Decrement the worker count for the leader itself */ + pg_atomic_sub_fetch_u32(VacuumActiveNWorkers, 1); + + for (int i = 0; i < pvs->pcxt->nworkers_launched; i++) + InstrAccumParallelQuery(&pvs->buffer_usage[i], &pvs->wal_usage[i]); + + /* + * Carry the shared balance value to heap scan and disable shared costing + */ + if (VacuumSharedCostBalance) + { + VacuumCostBalance = pg_atomic_read_u32(VacuumSharedCostBalance); + VacuumSharedCostBalance = NULL; + VacuumActiveNWorkers = NULL; + } + + pvs->shared->do_vacuum_table_scan = false; + pvs->num_table_scans++; +} + +/* Return the array of indexes associated to the given table to be vacuumed */ +Relation * +parallel_vacuum_get_table_indexes(ParallelVacuumState *pvs, int *nindexes) +{ + *nindexes = pvs->nindexes; + + return pvs->indrels; +} + +/* Return the number of workers for parallel table vacuum */ +int +parallel_vacuum_get_nworkers_table(ParallelVacuumState *pvs) +{ + return pvs->nworkers_for_table; +} + +/* Return the number of workers for parallel index processing */ +int +parallel_vacuum_get_nworkers_index(ParallelVacuumState *pvs) +{ + return pvs->nworkers_for_index; +} + +/* + * A parallel worker invokes table-AM specified vacuum scan callback. + */ +static void +parallel_vacuum_process_table(ParallelVacuumState *pvs) +{ + Assert(VacuumActiveNWorkers); + + /* Increment the active worker before starting the table vacuum */ + pg_atomic_add_fetch_u32(VacuumActiveNWorkers, 1); + + /* Do table vacuum scan */ + table_parallel_vacuum_scan(pvs->heaprel, pvs, pvs->pwcxt); + + /* + * We have completed the table vacuum so decrement the active worker + * count. + */ + pg_atomic_sub_fetch_u32(VacuumActiveNWorkers, 1); +} + /* * Perform work within a launched parallel process. * @@ -999,7 +1191,6 @@ parallel_vacuum_main(dsm_segment *seg, shm_toc *toc) WalUsage *wal_usage; int nindexes; char *sharedquery; - ErrorContextCallback errcallback; /* * A parallel vacuum worker must have only PROC_IN_VACUUM flag since we @@ -1031,7 +1222,6 @@ parallel_vacuum_main(dsm_segment *seg, shm_toc *toc) * matched to the leader's one. */ vac_open_indexes(rel, RowExclusiveLock, &nindexes, &indrels); - Assert(nindexes > 0); if (shared->maintenance_work_mem_worker > 0) maintenance_work_mem = shared->maintenance_work_mem_worker; @@ -1062,6 +1252,10 @@ parallel_vacuum_main(dsm_segment *seg, shm_toc *toc) pvs.relname = pstrdup(RelationGetRelationName(rel)); pvs.heaprel = rel; + pvs.pwcxt = palloc(sizeof(ParallelWorkerContext)); + pvs.pwcxt->toc = toc; + pvs.pwcxt->seg = seg; + /* These fields will be filled during index vacuum or cleanup */ pvs.indname = NULL; pvs.status = PARALLEL_INDVAC_STATUS_INITIAL; @@ -1070,17 +1264,29 @@ parallel_vacuum_main(dsm_segment *seg, shm_toc *toc) pvs.bstrategy = GetAccessStrategyWithSize(BAS_VACUUM, shared->ring_nbuffers * (BLCKSZ / 1024)); - /* Setup error traceback support for ereport() */ - errcallback.callback = parallel_vacuum_error_callback; - errcallback.arg = &pvs; - errcallback.previous = error_context_stack; - error_context_stack = &errcallback; - /* Prepare to track buffer usage during parallel execution */ InstrStartParallelQuery(); - /* Process indexes to perform vacuum/cleanup */ - parallel_vacuum_process_safe_indexes(&pvs); + if (pvs.shared->do_vacuum_table_scan) + { + parallel_vacuum_process_table(&pvs); + } + else + { + ErrorContextCallback errcallback; + + /* Setup error traceback support for ereport() */ + errcallback.callback = parallel_vacuum_error_callback; + errcallback.arg = &pvs; + errcallback.previous = error_context_stack; + error_context_stack = &errcallback; + + /* Process indexes to perform vacuum/cleanup */ + parallel_vacuum_process_safe_indexes(&pvs); + + /* Pop the error context stack */ + error_context_stack = errcallback.previous; + } /* Report buffer/WAL usage during parallel execution */ buffer_usage = shm_toc_lookup(toc, PARALLEL_VACUUM_KEY_BUFFER_USAGE, false); @@ -1090,9 +1296,6 @@ parallel_vacuum_main(dsm_segment *seg, shm_toc *toc) TidStoreDetach(dead_items); - /* Pop the error context stack */ - error_context_stack = errcallback.previous; - vac_close_indexes(nindexes, indrels, RowExclusiveLock); table_close(rel, ShareUpdateExclusiveLock); FreeAccessStrategy(pvs.bstrategy); diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c index 36610a1c7e..5b2b08a844 100644 --- a/src/backend/storage/ipc/procarray.c +++ b/src/backend/storage/ipc/procarray.c @@ -164,15 +164,6 @@ typedef struct ProcArrayStruct * * The typedef is in the header. */ -struct GlobalVisState -{ - /* XIDs >= are considered running by some backend */ - FullTransactionId definitely_needed; - - /* XIDs < are not considered to be running by any backend */ - FullTransactionId maybe_needed; -}; - /* * Result of ComputeXidHorizons(). */ diff --git a/src/include/access/heapam.h b/src/include/access/heapam.h index b951466ced..e81513c2db 100644 --- a/src/include/access/heapam.h +++ b/src/include/access/heapam.h @@ -21,6 +21,7 @@ #include "access/skey.h" #include "access/table.h" /* for backward compatibility */ #include "access/tableam.h" +#include "commands/vacuum.h" #include "nodes/lockoptions.h" #include "nodes/primnodes.h" #include "storage/bufpage.h" @@ -400,6 +401,13 @@ extern void log_heap_prune_and_freeze(Relation relation, Buffer buffer, struct VacuumParams; extern void heap_vacuum_rel(Relation rel, struct VacuumParams *params, BufferAccessStrategy bstrategy); +extern int heap_parallel_vacuum_compute_workers(Relation rel, int requested); +extern void heap_parallel_vacuum_estimate(Relation rel, ParallelContext *pcxt, + int nworkers, void *state); +extern void heap_parallel_vacuum_initialize(Relation rel, ParallelContext *pcxt, + int nworkers, void *state); +extern void heap_parallel_vacuum_scan_worker(Relation rel, ParallelVacuumState *pvs, + ParallelWorkerContext *pwcxt); /* in heap/heapam_visibility.c */ extern bool HeapTupleSatisfiesVisibility(HeapTuple htup, Snapshot snapshot, diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h index da661289c1..fc48f74828 100644 --- a/src/include/access/tableam.h +++ b/src/include/access/tableam.h @@ -20,6 +20,7 @@ #include "access/relscan.h" #include "access/sdir.h" #include "access/xact.h" +#include "commands/vacuum.h" #include "executor/tuptable.h" #include "storage/read_stream.h" #include "utils/rel.h" @@ -655,6 +656,46 @@ typedef struct TableAmRoutine struct VacuumParams *params, BufferAccessStrategy bstrategy); + /* ------------------------------------------------------------------------ + * Callbacks for parallel table vacuum. + * ------------------------------------------------------------------------ + */ + + /* + * Compute the number of parallel workers for parallel table vacuum. The + * function must return 0 to disable parallel table vacuum. + */ + int (*parallel_vacuum_compute_workers) (Relation rel, int requested); + + /* + * Compute the amount of DSM space AM need in the parallel table vacuum. + * + * Not called if parallel table vacuum is disabled. + */ + void (*parallel_vacuum_estimate) (Relation rel, + ParallelContext *pcxt, + int nworkers, + void *state); + + /* + * Initialize DSM space for parallel table vacuum. + * + * Not called if parallel table vacuum is disabled. + */ + void (*parallel_vacuum_initialize) (Relation rel, + ParallelContext *pctx, + int nworkers, + void *state); + + /* + * This callback is called for parallel table vacuum workers. + * + * Not called if parallel table vacuum is disabled. + */ + void (*parallel_vacuum_scan_worker) (Relation rel, + ParallelVacuumState *pvs, + ParallelWorkerContext *pwcxt); + /* * Prepare to analyze block `blockno` of `scan`. The scan has been started * with table_beginscan_analyze(). See also @@ -1710,6 +1751,52 @@ table_relation_vacuum(Relation rel, struct VacuumParams *params, rel->rd_tableam->relation_vacuum(rel, params, bstrategy); } +/* ---------------------------------------------------------------------------- + * Parallel vacuum related functions. + * ---------------------------------------------------------------------------- + */ + +/* + * Return the number of parallel workers for a parallel vacuum scan of this + * relation. + */ +static inline int +table_paralle_vacuum_compute_workers(Relation rel, int requested) +{ + return rel->rd_tableam->parallel_vacuum_compute_workers(rel, requested); +} + +/* + * Estimate the size of shared memory needed for a parallel vacuum scan of this + * of this relation. + */ +static inline void +table_parallel_vacuum_estimate(Relation rel, ParallelContext *pcxt, int nworkers, + void *state) +{ + rel->rd_tableam->parallel_vacuum_estimate(rel, pcxt, nworkers, state); +} + +/* + * Initialize shared memory area for a parallel vacuum scan of this relation. + */ +static inline void +table_parallel_vacuum_initialize(Relation rel, ParallelContext *pcxt, int nworkers, + void *state) +{ + rel->rd_tableam->parallel_vacuum_initialize(rel, pcxt, nworkers, state); +} + +/* + * Start a parallel vacuum scan of this relation. + */ +static inline void +table_parallel_vacuum_scan(Relation rel, ParallelVacuumState *pvs, + ParallelWorkerContext *pwcxt) +{ + rel->rd_tableam->parallel_vacuum_scan_worker(rel, pvs, pwcxt); +} + /* * Prepare to analyze the next block in the read stream. The scan needs to * have been started with table_beginscan_analyze(). Note that this routine diff --git a/src/include/commands/vacuum.h b/src/include/commands/vacuum.h index 759f9a87d3..a225f31429 100644 --- a/src/include/commands/vacuum.h +++ b/src/include/commands/vacuum.h @@ -360,7 +360,8 @@ extern void VacuumUpdateCosts(void); extern ParallelVacuumState *parallel_vacuum_init(Relation rel, Relation *indrels, int nindexes, int nrequested_workers, int vac_work_mem, int elevel, - BufferAccessStrategy bstrategy); + BufferAccessStrategy bstrategy, + void *state); extern void parallel_vacuum_end(ParallelVacuumState *pvs, IndexBulkDeleteResult **istats); extern TidStore *parallel_vacuum_get_dead_items(ParallelVacuumState *pvs, VacDeadItemsInfo **dead_items_info_p); @@ -372,6 +373,11 @@ extern void parallel_vacuum_cleanup_all_indexes(ParallelVacuumState *pvs, long num_table_tuples, int num_index_scans, bool estimated_count); +extern int parallel_vacuum_table_scan_begin(ParallelVacuumState *pvs); +extern void parallel_vacuum_table_scan_end(ParallelVacuumState *pvs); +extern int parallel_vacuum_get_nworkers_table(ParallelVacuumState *pvs); +extern int parallel_vacuum_get_nworkers_index(ParallelVacuumState *pvs); +extern Relation *parallel_vacuum_get_table_indexes(ParallelVacuumState *pvs, int *nindexes); extern void parallel_vacuum_main(dsm_segment *seg, shm_toc *toc); /* in commands/analyze.c */ diff --git a/src/include/utils/snapmgr.h b/src/include/utils/snapmgr.h index 9398a84051..6ccb19a29f 100644 --- a/src/include/utils/snapmgr.h +++ b/src/include/utils/snapmgr.h @@ -102,8 +102,20 @@ extern char *ExportSnapshot(Snapshot snapshot); /* * These live in procarray.c because they're intimately linked to the * procarray contents, but thematically they better fit into snapmgr.h. + * + * XXX the struct definition is temporarily moved from procarray.c for + * parallel table vacuum development. We need to find a suitable way for + * parallel table vacuum workers to share the GlobalVisState. */ -typedef struct GlobalVisState GlobalVisState; +typedef struct GlobalVisState +{ + /* XIDs >= are considered running by some backend */ + FullTransactionId definitely_needed; + + /* XIDs < are not considered to be running by any backend */ + FullTransactionId maybe_needed; +} GlobalVisState; + extern GlobalVisState *GlobalVisTestFor(Relation rel); extern bool GlobalVisTestIsRemovableXid(GlobalVisState *state, TransactionId xid); extern bool GlobalVisTestIsRemovableFullXid(GlobalVisState *state, FullTransactionId fxid); -- 2.43.5