PostgreSQL Source Code git master
Loading...
Searching...
No Matches
vacuumparallel.c File Reference
#include "postgres.h"
#include "access/amapi.h"
#include "access/table.h"
#include "access/xact.h"
#include "commands/progress.h"
#include "commands/vacuum.h"
#include "executor/instrument.h"
#include "optimizer/paths.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
#include "storage/proc.h"
#include "tcop/tcopprot.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
Include dependency graph for vacuumparallel.c:

Go to the source code of this file.

Data Structures

struct  PVSharedCostParams
 
struct  PVShared
 
struct  PVIndStats
 
struct  ParallelVacuumState
 

Macros

#define PARALLEL_VACUUM_KEY_SHARED   1
 
#define PARALLEL_VACUUM_KEY_QUERY_TEXT   2
 
#define PARALLEL_VACUUM_KEY_BUFFER_USAGE   3
 
#define PARALLEL_VACUUM_KEY_WAL_USAGE   4
 
#define PARALLEL_VACUUM_KEY_INDEX_STATS   5
 

Typedefs

typedef struct PVSharedCostParams PVSharedCostParams
 
typedef struct PVShared PVShared
 
typedef enum PVIndVacStatus PVIndVacStatus
 
typedef struct PVIndStats PVIndStats
 

Enumerations

enum  PVIndVacStatus { PARALLEL_INDVAC_STATUS_INITIAL = 0 , PARALLEL_INDVAC_STATUS_NEED_BULKDELETE , PARALLEL_INDVAC_STATUS_NEED_CLEANUP , PARALLEL_INDVAC_STATUS_COMPLETED }
 

Functions

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, PVWorkerStats *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, PVIndStats *indstats)
 
static bool parallel_vacuum_index_is_parallel_safe (Relation indrel, int num_index_scans, bool vacuum)
 
static void parallel_vacuum_error_callback (void *arg)
 
static void parallel_vacuum_set_cost_parameters (PVSharedCostParams *params)
 
static void parallel_vacuum_dsm_detach (dsm_segment *seg, Datum arg)
 
ParallelVacuumStateparallel_vacuum_init (Relation rel, Relation *indrels, int nindexes, int nrequested_workers, int vac_work_mem, int elevel, BufferAccessStrategy bstrategy)
 
void parallel_vacuum_end (ParallelVacuumState *pvs, IndexBulkDeleteResult **istats)
 
TidStoreparallel_vacuum_get_dead_items (ParallelVacuumState *pvs, VacDeadItemsInfo **dead_items_info_p)
 
void parallel_vacuum_reset_dead_items (ParallelVacuumState *pvs)
 
void parallel_vacuum_bulkdel_all_indexes (ParallelVacuumState *pvs, long num_table_tuples, int num_index_scans, PVWorkerStats *wstats)
 
void parallel_vacuum_cleanup_all_indexes (ParallelVacuumState *pvs, long num_table_tuples, int num_index_scans, bool estimated_count, PVWorkerStats *wstats)
 
void parallel_vacuum_update_shared_delay_params (void)
 
void parallel_vacuum_propagate_shared_delay_params (void)
 
void parallel_vacuum_main (dsm_segment *seg, shm_toc *toc)
 

Variables

static PVSharedCostParamspv_shared_cost_params = NULL
 
static uint32 shared_params_generation_local = 0
 

Macro Definition Documentation

◆ PARALLEL_VACUUM_KEY_BUFFER_USAGE

#define PARALLEL_VACUUM_KEY_BUFFER_USAGE   3

Definition at line 59 of file vacuumparallel.c.

◆ PARALLEL_VACUUM_KEY_INDEX_STATS

#define PARALLEL_VACUUM_KEY_INDEX_STATS   5

Definition at line 61 of file vacuumparallel.c.

◆ PARALLEL_VACUUM_KEY_QUERY_TEXT

#define PARALLEL_VACUUM_KEY_QUERY_TEXT   2

Definition at line 58 of file vacuumparallel.c.

◆ PARALLEL_VACUUM_KEY_SHARED

#define PARALLEL_VACUUM_KEY_SHARED   1

Definition at line 57 of file vacuumparallel.c.

◆ PARALLEL_VACUUM_KEY_WAL_USAGE

#define PARALLEL_VACUUM_KEY_WAL_USAGE   4

Definition at line 60 of file vacuumparallel.c.

Typedef Documentation

◆ PVIndStats

◆ PVIndVacStatus

◆ PVShared

◆ PVSharedCostParams

Enumeration Type Documentation

◆ PVIndVacStatus

Enumerator
PARALLEL_INDVAC_STATUS_INITIAL 
PARALLEL_INDVAC_STATUS_NEED_BULKDELETE 
PARALLEL_INDVAC_STATUS_NEED_CLEANUP 
PARALLEL_INDVAC_STATUS_COMPLETED 

Definition at line 174 of file vacuumparallel.c.

Function Documentation

◆ parallel_vacuum_bulkdel_all_indexes()

void parallel_vacuum_bulkdel_all_indexes ( ParallelVacuumState pvs,
long  num_table_tuples,
int  num_index_scans,
PVWorkerStats wstats 
)

Definition at line 595 of file vacuumparallel.c.

597{
599
600 /*
601 * We can only provide an approximate value of num_heap_tuples, at least
602 * for now.
603 */
605 pvs->shared->estimated_count = true;
606
607 parallel_vacuum_process_all_indexes(pvs, num_index_scans, true, wstats);
608}
#define Assert(condition)
Definition c.h:943
#define IsParallelWorker()
Definition parallel.h:62
static int fb(int x)
double reltuples
bool estimated_count
static void parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scans, bool vacuum, PVWorkerStats *wstats)

References Assert, PVShared::estimated_count, fb(), IsParallelWorker, parallel_vacuum_process_all_indexes(), PVShared::reltuples, and ParallelVacuumState::shared.

Referenced by lazy_vacuum_all_indexes().

◆ parallel_vacuum_cleanup_all_indexes()

void parallel_vacuum_cleanup_all_indexes ( ParallelVacuumState pvs,
long  num_table_tuples,
int  num_index_scans,
bool  estimated_count,
PVWorkerStats wstats 
)

Definition at line 614 of file vacuumparallel.c.

617{
619
620 /*
621 * We can provide a better estimate of total number of surviving tuples
622 * (we assume indexes are more interested in that than in the number of
623 * nominally live tuples).
624 */
626 pvs->shared->estimated_count = estimated_count;
627
628 parallel_vacuum_process_all_indexes(pvs, num_index_scans, false, wstats);
629}

References Assert, PVShared::estimated_count, fb(), IsParallelWorker, parallel_vacuum_process_all_indexes(), PVShared::reltuples, and ParallelVacuumState::shared.

Referenced by lazy_cleanup_all_indexes().

◆ parallel_vacuum_compute_workers()

static int parallel_vacuum_compute_workers ( Relation indrels,
int  nindexes,
int  nrequested,
bool will_parallel_vacuum 
)
static

Definition at line 742 of file vacuumparallel.c.

744{
745 int nindexes_parallel = 0;
746 int nindexes_parallel_bulkdel = 0;
747 int nindexes_parallel_cleanup = 0;
748 int parallel_workers;
749 int max_workers;
750
754
755 /*
756 * We don't allow performing parallel operation in standalone backend or
757 * when parallelism is disabled.
758 */
759 if (!IsUnderPostmaster || max_workers == 0)
760 return 0;
761
762 /*
763 * Compute the number of indexes that can participate in parallel vacuum.
764 */
765 for (int i = 0; i < nindexes; i++)
766 {
767 Relation indrel = indrels[i];
769
770 /* Skip index that is not a suitable target for parallel index vacuum */
773 continue;
774
775 will_parallel_vacuum[i] = true;
776
778 nindexes_parallel_bulkdel++;
781 nindexes_parallel_cleanup++;
782 }
783
784 nindexes_parallel = Max(nindexes_parallel_bulkdel,
785 nindexes_parallel_cleanup);
786
787 /* The leader process takes one index */
789
790 /* No index supports parallel vacuum */
791 if (nindexes_parallel <= 0)
792 return 0;
793
794 /* Compute the parallel degree */
795 parallel_workers = (nrequested > 0) ?
797
798 /* Cap by GUC variable */
799 parallel_workers = Min(parallel_workers, max_workers);
800
801 return parallel_workers;
802}
int min_parallel_index_scan_size
Definition allpaths.c:86
#define RelationGetNumberOfBlocks(reln)
Definition bufmgr.h:309
#define Min(x, y)
Definition c.h:1091
uint8_t uint8
Definition c.h:622
#define Max(x, y)
Definition c.h:1085
int max_parallel_maintenance_workers
Definition globals.c:136
bool IsUnderPostmaster
Definition globals.c:122
int autovacuum_max_parallel_workers
Definition globals.c:148
int i
Definition isn.c:77
#define AmAutoVacuumWorkerProcess()
Definition miscadmin.h:398
uint8 amparallelvacuumoptions
Definition amapi.h:284
const struct IndexAmRoutine * rd_indam
Definition rel.h:206
#define VACUUM_OPTION_PARALLEL_CLEANUP
Definition vacuum.h:62
#define VACUUM_OPTION_NO_PARALLEL
Definition vacuum.h:41
#define VACUUM_OPTION_PARALLEL_BULKDEL
Definition vacuum.h:47
#define VACUUM_OPTION_PARALLEL_COND_CLEANUP
Definition vacuum.h:54

References AmAutoVacuumWorkerProcess, IndexAmRoutine::amparallelvacuumoptions, autovacuum_max_parallel_workers, fb(), i, IsUnderPostmaster, Max, max_parallel_maintenance_workers, Min, min_parallel_index_scan_size, RelationData::rd_indam, RelationGetNumberOfBlocks, VACUUM_OPTION_NO_PARALLEL, VACUUM_OPTION_PARALLEL_BULKDEL, VACUUM_OPTION_PARALLEL_CLEANUP, and VACUUM_OPTION_PARALLEL_COND_CLEANUP.

Referenced by parallel_vacuum_init().

◆ parallel_vacuum_dsm_detach()

static void parallel_vacuum_dsm_detach ( dsm_segment seg,
Datum  arg 
)
static

Definition at line 552 of file vacuumparallel.c.

553{
556}
static PVSharedCostParams * pv_shared_cost_params

References AmAutoVacuumWorkerProcess, Assert, fb(), and pv_shared_cost_params.

Referenced by parallel_vacuum_init().

◆ parallel_vacuum_end()

void parallel_vacuum_end ( ParallelVacuumState pvs,
IndexBulkDeleteResult **  istats 
)

Definition at line 515 of file vacuumparallel.c.

516{
518
519 /* Copy the updated statistics */
520 for (int i = 0; i < pvs->nindexes; i++)
521 {
522 PVIndStats *indstats = &(pvs->indstats[i]);
523
524 if (indstats->istat_updated)
525 {
527 memcpy(istats[i], &indstats->istat, sizeof(IndexBulkDeleteResult));
528 }
529 else
530 istats[i] = NULL;
531 }
532
534
537
540
542 pfree(pvs);
543}
void DestroyParallelContext(ParallelContext *pcxt)
Definition parallel.c:959
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
#define palloc0_object(type)
Definition fe_memutils.h:75
void pfree(void *pointer)
Definition mcxt.c:1616
IndexBulkDeleteResult istat
ParallelContext * pcxt
void TidStoreDestroy(TidStore *ts)
Definition tidstore.c:317
void ExitParallelMode(void)
Definition xact.c:1094

References AmAutoVacuumWorkerProcess, Assert, ParallelVacuumState::dead_items, DestroyParallelContext(), ExitParallelMode(), fb(), i, ParallelVacuumState::indstats, IsParallelWorker, PVIndStats::istat, PVIndStats::istat_updated, memcpy(), ParallelVacuumState::nindexes, palloc0_object, ParallelVacuumState::pcxt, pfree(), pv_shared_cost_params, TidStoreDestroy(), and ParallelVacuumState::will_parallel_vacuum.

Referenced by dead_items_cleanup().

◆ parallel_vacuum_error_callback()

static void parallel_vacuum_error_callback ( void arg)
static

Definition at line 1344 of file vacuumparallel.c.

1345{
1347
1348 switch (errinfo->status)
1349 {
1351 errcontext("while vacuuming index \"%s\" of relation \"%s.%s\"",
1352 errinfo->indname,
1353 errinfo->relnamespace,
1354 errinfo->relname);
1355 break;
1357 errcontext("while cleaning up index \"%s\" of relation \"%s.%s\"",
1358 errinfo->indname,
1359 errinfo->relnamespace,
1360 errinfo->relname);
1361 break;
1364 default:
1365 return;
1366 }
1367}
Datum arg
Definition elog.c:1323
#define errcontext
Definition elog.h:200

References arg, errcontext, fb(), PARALLEL_INDVAC_STATUS_COMPLETED, PARALLEL_INDVAC_STATUS_INITIAL, PARALLEL_INDVAC_STATUS_NEED_BULKDELETE, and PARALLEL_INDVAC_STATUS_NEED_CLEANUP.

Referenced by parallel_vacuum_main().

◆ parallel_vacuum_get_dead_items()

TidStore * parallel_vacuum_get_dead_items ( ParallelVacuumState pvs,
VacDeadItemsInfo **  dead_items_info_p 
)

Definition at line 562 of file vacuumparallel.c.

563{
565 return pvs->dead_items;
566}
VacDeadItemsInfo dead_items_info

References ParallelVacuumState::dead_items, PVShared::dead_items_info, fb(), and ParallelVacuumState::shared.

Referenced by dead_items_alloc(), and dead_items_reset().

◆ parallel_vacuum_index_is_parallel_safe()

static bool parallel_vacuum_index_is_parallel_safe ( Relation  indrel,
int  num_index_scans,
bool  vacuum 
)
static

Definition at line 1159 of file vacuumparallel.c.

1161{
1163
1164 vacoptions = indrel->rd_indam->amparallelvacuumoptions;
1165
1166 /* In parallel vacuum case, check if it supports parallel bulk-deletion */
1167 if (vacuum)
1169
1170 /* Not safe, if the index does not support parallel cleanup */
1173 return false;
1174
1175 /*
1176 * Not safe, if the index supports parallel cleanup conditionally, but we
1177 * have already processed the index (for bulkdelete). We do this to avoid
1178 * the need to invoke workers when parallel index cleanup doesn't need to
1179 * scan the index. See the comments for option
1180 * VACUUM_OPTION_PARALLEL_COND_CLEANUP to know when indexes support
1181 * parallel cleanup conditionally.
1182 */
1183 if (num_index_scans > 0 &&
1185 return false;
1186
1187 return true;
1188}
void vacuum(List *relations, const VacuumParams *params, BufferAccessStrategy bstrategy, MemoryContext vac_context, bool isTopLevel)
Definition vacuum.c:494

References fb(), vacuum(), VACUUM_OPTION_PARALLEL_BULKDEL, VACUUM_OPTION_PARALLEL_CLEANUP, and VACUUM_OPTION_PARALLEL_COND_CLEANUP.

Referenced by parallel_vacuum_process_all_indexes().

◆ parallel_vacuum_init()

ParallelVacuumState * parallel_vacuum_init ( Relation  rel,
Relation indrels,
int  nindexes,
int  nrequested_workers,
int  vac_work_mem,
int  elevel,
BufferAccessStrategy  bstrategy 
)

Definition at line 305 of file vacuumparallel.c.

308{
310 ParallelContext *pcxt;
311 PVShared *shared;
312 TidStore *dead_items;
313 PVIndStats *indstats;
314 BufferUsage *buffer_usage;
315 WalUsage *wal_usage;
316 bool *will_parallel_vacuum;
319 int nindexes_mwm = 0;
320 int parallel_workers = 0;
321 int querylen;
322
323 /*
324 * A parallel vacuum must be requested and there must be indexes on the
325 * relation
326 */
328 Assert(nindexes > 0);
329
330 /*
331 * Compute the number of parallel vacuum workers to launch
332 */
333 will_parallel_vacuum = palloc0_array(bool, nindexes);
334 parallel_workers = parallel_vacuum_compute_workers(indrels, nindexes,
336 will_parallel_vacuum);
337 if (parallel_workers <= 0)
338 {
339 /* Can't perform vacuum in parallel -- return NULL */
340 pfree(will_parallel_vacuum);
341 return NULL;
342 }
343
345 pvs->indrels = indrels;
346 pvs->nindexes = nindexes;
347 pvs->will_parallel_vacuum = will_parallel_vacuum;
348 pvs->bstrategy = bstrategy;
349 pvs->heaprel = rel;
350
352 pcxt = CreateParallelContext("postgres", "parallel_vacuum_main",
353 parallel_workers);
354 Assert(pcxt->nworkers > 0);
355 pvs->pcxt = pcxt;
356
357 /* Estimate size for index vacuum stats -- PARALLEL_VACUUM_KEY_INDEX_STATS */
358 est_indstats_len = mul_size(sizeof(PVIndStats), nindexes);
361
362 /* Estimate size for shared information -- PARALLEL_VACUUM_KEY_SHARED */
363 est_shared_len = sizeof(PVShared);
366
367 /*
368 * Estimate space for BufferUsage and WalUsage --
369 * PARALLEL_VACUUM_KEY_BUFFER_USAGE and PARALLEL_VACUUM_KEY_WAL_USAGE.
370 *
371 * If there are no extensions loaded that care, we could skip this. We
372 * have no way of knowing whether anyone's looking at pgBufferUsage or
373 * pgWalUsage, so do it unconditionally.
374 */
376 mul_size(sizeof(BufferUsage), pcxt->nworkers));
379 mul_size(sizeof(WalUsage), pcxt->nworkers));
381
382 /* Finally, estimate PARALLEL_VACUUM_KEY_QUERY_TEXT space */
384 {
388 }
389 else
390 querylen = 0; /* keep compiler quiet */
391
393
394 /* Prepare index vacuum stats */
395 indstats = (PVIndStats *) shm_toc_allocate(pcxt->toc, est_indstats_len);
396 MemSet(indstats, 0, est_indstats_len);
397 for (int i = 0; i < nindexes; i++)
398 {
399 Relation indrel = indrels[i];
401
402 /*
403 * Cleanup option should be either disabled, always performing in
404 * parallel or conditionally performing in parallel.
405 */
409
410 if (!will_parallel_vacuum[i])
411 continue;
412
413 if (indrel->rd_indam->amusemaintenanceworkmem)
414 nindexes_mwm++;
415
416 /*
417 * Remember the number of indexes that support parallel operation for
418 * each phase.
419 */
426 }
428 pvs->indstats = indstats;
429
430 /* Prepare shared information */
431 shared = (PVShared *) shm_toc_allocate(pcxt->toc, est_shared_len);
432 MemSet(shared, 0, est_shared_len);
433 shared->relid = RelationGetRelid(rel);
434 shared->elevel = elevel;
437 (nindexes_mwm > 0) ?
438 vac_work_mem / Min(parallel_workers, nindexes_mwm) :
440
441 shared->dead_items_info.max_bytes = vac_work_mem * (size_t) 1024;
442
443 /* Prepare DSA space for dead items */
446 pvs->dead_items = dead_items;
447 shared->dead_items_handle = TidStoreGetHandle(dead_items);
449
450 /* Use the same buffer size for all workers */
451 shared->ring_nbuffers = GetAccessStrategyBufferCount(bstrategy);
452
453 pg_atomic_init_u32(&(shared->cost_balance), 0);
454 pg_atomic_init_u32(&(shared->active_nworkers), 0);
455 pg_atomic_init_u32(&(shared->idx), 0);
456
458
459 /*
460 * Initialize shared cost-based vacuum delay parameters if it's for
461 * autovacuum.
462 */
463 if (shared->is_autovacuum)
464 {
468
471 }
472
474 pvs->shared = shared;
475
476 /*
477 * Allocate space for each worker's BufferUsage and WalUsage; no need to
478 * initialize
479 */
480 buffer_usage = shm_toc_allocate(pcxt->toc,
481 mul_size(sizeof(BufferUsage), pcxt->nworkers));
483 pvs->buffer_usage = buffer_usage;
484 wal_usage = shm_toc_allocate(pcxt->toc,
485 mul_size(sizeof(WalUsage), pcxt->nworkers));
487 pvs->wal_usage = wal_usage;
488
489 /* Store query string for workers */
491 {
492 char *sharedquery;
493
494 sharedquery = (char *) shm_toc_allocate(pcxt->toc, querylen + 1);
496 sharedquery[querylen] = '\0';
497 shm_toc_insert(pcxt->toc,
499 }
500
501 /* Success -- return parallel vacuum state */
502 return pvs;
503}
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition atomics.h:219
void InitializeParallelDSM(ParallelContext *pcxt)
Definition parallel.c:213
ParallelContext * CreateParallelContext(const char *library_name, const char *function_name, int nworkers)
Definition parallel.c:175
int64 pgstat_get_my_query_id(void)
#define MemSet(start, val, len)
Definition c.h:1107
size_t Size
Definition c.h:689
dsa_handle dsa_get_handle(dsa_area *area)
Definition dsa.c:498
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition dsm.c:1140
#define palloc0_array(type, count)
Definition fe_memutils.h:77
int GetAccessStrategyBufferCount(BufferAccessStrategy strategy)
Definition freelist.c:551
const char * debug_query_string
Definition postgres.c:94
uint64_t Datum
Definition postgres.h:70
#define RelationGetRelid(relation)
Definition rel.h:516
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition shm_toc.c:88
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition shm_toc.c:171
#define shm_toc_estimate_chunk(e, sz)
Definition shm_toc.h:51
#define shm_toc_estimate_keys(e, cnt)
Definition shm_toc.h:53
Size mul_size(Size s1, Size s2)
Definition shmem.c:1063
static void SpinLockInit(volatile slock_t *lock)
Definition spin.h:50
pg_atomic_uint32 generation
bool is_autovacuum
pg_atomic_uint32 cost_balance
int maintenance_work_mem_worker
pg_atomic_uint32 active_nworkers
PVSharedCostParams cost_params
dsa_pointer dead_items_handle
dsa_handle dead_items_dsa_handle
pg_atomic_uint32 idx
dsm_segment * seg
Definition parallel.h:44
shm_toc_estimator estimator
Definition parallel.h:43
shm_toc * toc
Definition parallel.h:46
BufferAccessStrategy bstrategy
BufferUsage * buffer_usage
size_t max_bytes
Definition vacuum.h:298
dsa_area * TidStoreGetDSA(TidStore *ts)
Definition tidstore.c:544
dsa_pointer TidStoreGetHandle(TidStore *ts)
Definition tidstore.c:552
TidStore * TidStoreCreateShared(size_t max_bytes, int tranche_id)
Definition tidstore.c:208
#define VACUUM_OPTION_MAX_VALID_VALUE
Definition vacuum.h:65
static int parallel_vacuum_compute_workers(Relation *indrels, int nindexes, int nrequested, bool *will_parallel_vacuum)
#define PARALLEL_VACUUM_KEY_INDEX_STATS
#define PARALLEL_VACUUM_KEY_QUERY_TEXT
#define PARALLEL_VACUUM_KEY_BUFFER_USAGE
#define PARALLEL_VACUUM_KEY_SHARED
#define PARALLEL_VACUUM_KEY_WAL_USAGE
static void parallel_vacuum_dsm_detach(dsm_segment *seg, Datum arg)
static void parallel_vacuum_set_cost_parameters(PVSharedCostParams *params)
void EnterParallelMode(void)
Definition xact.c:1081

References PVShared::active_nworkers, AmAutoVacuumWorkerProcess, IndexAmRoutine::amparallelvacuumoptions, Assert, ParallelVacuumState::bstrategy, ParallelVacuumState::buffer_usage, PVShared::cost_balance, PVShared::cost_params, CreateParallelContext(), ParallelVacuumState::dead_items, PVShared::dead_items_dsa_handle, PVShared::dead_items_handle, PVShared::dead_items_info, debug_query_string, dsa_get_handle(), PVShared::elevel, EnterParallelMode(), ParallelContext::estimator, fb(), PVSharedCostParams::generation, GetAccessStrategyBufferCount(), ParallelVacuumState::heaprel, i, PVShared::idx, ParallelVacuumState::indrels, ParallelVacuumState::indstats, InitializeParallelDSM(), PVShared::is_autovacuum, PVShared::maintenance_work_mem_worker, VacDeadItemsInfo::max_bytes, memcpy(), MemSet, Min, mul_size(), PVSharedCostParams::mutex, ParallelVacuumState::nindexes, ParallelVacuumState::nindexes_parallel_bulkdel, ParallelVacuumState::nindexes_parallel_cleanup, ParallelVacuumState::nindexes_parallel_condcleanup, ParallelContext::nworkers, on_dsm_detach(), palloc0_array, palloc0_object, parallel_vacuum_compute_workers(), parallel_vacuum_dsm_detach(), PARALLEL_VACUUM_KEY_BUFFER_USAGE, PARALLEL_VACUUM_KEY_INDEX_STATS, PARALLEL_VACUUM_KEY_QUERY_TEXT, PARALLEL_VACUUM_KEY_SHARED, PARALLEL_VACUUM_KEY_WAL_USAGE, parallel_vacuum_set_cost_parameters(), ParallelVacuumState::pcxt, pfree(), pg_atomic_init_u32(), pgstat_get_my_query_id(), pv_shared_cost_params, PVShared::queryid, RelationData::rd_indam, RelationGetRelid, PVShared::relid, PVShared::ring_nbuffers, ParallelContext::seg, ParallelVacuumState::shared, shm_toc_allocate(), shm_toc_estimate_chunk, shm_toc_estimate_keys, shm_toc_insert(), SpinLockInit(), TidStoreCreateShared(), TidStoreGetDSA(), TidStoreGetHandle(), ParallelContext::toc, VACUUM_OPTION_MAX_VALID_VALUE, VACUUM_OPTION_PARALLEL_BULKDEL, VACUUM_OPTION_PARALLEL_CLEANUP, VACUUM_OPTION_PARALLEL_COND_CLEANUP, ParallelVacuumState::wal_usage, and ParallelVacuumState::will_parallel_vacuum.

Referenced by dead_items_alloc().

◆ parallel_vacuum_main()

void parallel_vacuum_main ( dsm_segment seg,
shm_toc toc 
)

Definition at line 1197 of file vacuumparallel.c.

1198{
1200 Relation rel;
1201 Relation *indrels;
1202 PVIndStats *indstats;
1203 PVShared *shared;
1204 TidStore *dead_items;
1205 BufferUsage *buffer_usage;
1206 WalUsage *wal_usage;
1207 int nindexes;
1208 char *sharedquery;
1209 ErrorContextCallback errcallback;
1210
1211 /*
1212 * A parallel vacuum worker must have only PROC_IN_VACUUM flag since we
1213 * don't support parallel vacuum for autovacuum as of now.
1214 */
1216
1217 elog(DEBUG1, "starting parallel vacuum worker");
1218
1219 shared = (PVShared *) shm_toc_lookup(toc, PARALLEL_VACUUM_KEY_SHARED, false);
1220
1221 /* Set debug_query_string for individual workers */
1225
1226 /* Track query ID */
1227 pgstat_report_query_id(shared->queryid, false);
1228
1229 /*
1230 * Open table. The lock mode is the same as the leader process. It's
1231 * okay because the lock mode does not conflict among the parallel
1232 * workers.
1233 */
1235
1236 /*
1237 * Open all indexes. indrels are sorted in order by OID, which should be
1238 * matched to the leader's one.
1239 */
1240 vac_open_indexes(rel, RowExclusiveLock, &nindexes, &indrels);
1241 Assert(nindexes > 0);
1242
1243 /*
1244 * Apply the desired value of maintenance_work_mem within this process.
1245 * Really we should use SetConfigOption() to change a GUC, but since we're
1246 * already in parallel mode guc.c would complain about that. Fortunately,
1247 * by the same token guc.c will not let any user-defined code change it.
1248 * So just avert your eyes while we do this:
1249 */
1250 if (shared->maintenance_work_mem_worker > 0)
1252
1253 /* Set index statistics */
1254 indstats = (PVIndStats *) shm_toc_lookup(toc,
1256 false);
1257
1258 /* Find dead_items in shared memory */
1259 dead_items = TidStoreAttach(shared->dead_items_dsa_handle,
1260 shared->dead_items_handle);
1261
1262 /* Set cost-based vacuum delay */
1263 if (shared->is_autovacuum)
1264 {
1265 /*
1266 * Parallel autovacuum workers initialize cost-based delay parameters
1267 * from the leader's shared state rather than GUC defaults, because
1268 * the leader may have applied per-table or autovacuum-specific
1269 * overrides. pv_shared_cost_params must be set before calling
1270 * parallel_vacuum_update_shared_delay_params().
1271 */
1274 }
1275 else
1277
1282
1283 /* Set parallel vacuum state */
1284 pvs.indrels = indrels;
1285 pvs.nindexes = nindexes;
1286 pvs.indstats = indstats;
1287 pvs.shared = shared;
1288 pvs.dead_items = dead_items;
1291 pvs.heaprel = rel;
1292
1293 /* These fields will be filled during index vacuum or cleanup */
1294 pvs.indname = NULL;
1296
1297 /* Each parallel VACUUM worker gets its own access strategy. */
1299 shared->ring_nbuffers * (BLCKSZ / 1024));
1300
1301 /* Setup error traceback support for ereport() */
1303 errcallback.arg = &pvs;
1304 errcallback.previous = error_context_stack;
1305 error_context_stack = &errcallback;
1306
1307 /* Prepare to track buffer usage during parallel execution */
1309
1310 /* Process indexes to perform vacuum/cleanup */
1312
1313 /* Report buffer/WAL usage during parallel execution */
1314 buffer_usage = shm_toc_lookup(toc, PARALLEL_VACUUM_KEY_BUFFER_USAGE, false);
1315 wal_usage = shm_toc_lookup(toc, PARALLEL_VACUUM_KEY_WAL_USAGE, false);
1317 &wal_usage[ParallelWorkerNumber]);
1318
1319 /* Report any remaining cost-based vacuum delay time */
1323
1324 TidStoreDetach(dead_items);
1325
1326 /* Pop the error context stack */
1327 error_context_stack = errcallback.previous;
1328
1329 vac_close_indexes(nindexes, indrels, RowExclusiveLock);
1332
1333 if (shared->is_autovacuum)
1335}
void VacuumUpdateCosts(void)
int ParallelWorkerNumber
Definition parallel.c:117
void pgstat_progress_parallel_incr_param(int index, int64 incr)
void pgstat_report_query_id(int64 query_id, bool force)
void pgstat_report_activity(BackendState state, const char *cmd_str)
@ STATE_RUNNING
@ BAS_VACUUM
Definition bufmgr.h:40
ErrorContextCallback * error_context_stack
Definition elog.c:100
#define DEBUG1
Definition elog.h:31
#define elog(elevel,...)
Definition elog.h:228
BufferAccessStrategy GetAccessStrategyWithSize(BufferAccessStrategyType btype, int ring_size_kb)
Definition freelist.c:511
void FreeAccessStrategy(BufferAccessStrategy strategy)
Definition freelist.c:608
int VacuumCostBalance
Definition globals.c:160
int maintenance_work_mem
Definition globals.c:135
void InstrEndParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition instrument.c:287
void InstrStartParallelQuery(void)
Definition instrument.c:279
#define ShareUpdateExclusiveLock
Definition lockdefs.h:39
#define RowExclusiveLock
Definition lockdefs.h:38
char * get_namespace_name(Oid nspid)
Definition lsyscache.c:3561
char * pstrdup(const char *in)
Definition mcxt.c:1781
#define PROC_IN_VACUUM
Definition proc.h:62
#define PROGRESS_VACUUM_DELAY_TIME
Definition progress.h:31
#define RelationGetRelationName(relation)
Definition rel.h:550
#define RelationGetNamespace(relation)
Definition rel.h:557
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition shm_toc.c:239
PGPROC * MyProc
Definition proc.c:71
struct ErrorContextCallback * previous
Definition elog.h:299
void(* callback)(void *arg)
Definition elog.h:300
uint8 statusFlags
Definition proc.h:210
PVIndVacStatus status
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40
void TidStoreDetach(TidStore *ts)
Definition tidstore.c:269
TidStore * TidStoreAttach(dsa_handle area_handle, dsa_pointer handle)
Definition tidstore.c:244
pg_atomic_uint32 * VacuumActiveNWorkers
Definition vacuum.c:118
bool track_cost_delay_timing
Definition vacuum.c:83
void vac_open_indexes(Relation relation, LOCKMODE lockmode, int *nindexes, Relation **Irel)
Definition vacuum.c:2369
int VacuumCostBalanceLocal
Definition vacuum.c:119
int64 parallel_vacuum_worker_delay_ns
Definition vacuum.c:96
void vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode)
Definition vacuum.c:2412
pg_atomic_uint32 * VacuumSharedCostBalance
Definition vacuum.c:117
static void parallel_vacuum_error_callback(void *arg)
void parallel_vacuum_update_shared_delay_params(void)
static void parallel_vacuum_process_safe_indexes(ParallelVacuumState *pvs)

References PVShared::active_nworkers, ErrorContextCallback::arg, Assert, BAS_VACUUM, ParallelVacuumState::bstrategy, ErrorContextCallback::callback, PVShared::cost_balance, PVShared::cost_params, ParallelVacuumState::dead_items, PVShared::dead_items_dsa_handle, PVShared::dead_items_handle, DEBUG1, debug_query_string, elog, error_context_stack, fb(), FreeAccessStrategy(), get_namespace_name(), GetAccessStrategyWithSize(), ParallelVacuumState::heaprel, ParallelVacuumState::indname, ParallelVacuumState::indrels, ParallelVacuumState::indstats, InstrEndParallelQuery(), InstrStartParallelQuery(), PVShared::is_autovacuum, maintenance_work_mem, PVShared::maintenance_work_mem_worker, MyProc, ParallelVacuumState::nindexes, PARALLEL_INDVAC_STATUS_INITIAL, parallel_vacuum_error_callback(), PARALLEL_VACUUM_KEY_BUFFER_USAGE, PARALLEL_VACUUM_KEY_INDEX_STATS, PARALLEL_VACUUM_KEY_QUERY_TEXT, PARALLEL_VACUUM_KEY_SHARED, PARALLEL_VACUUM_KEY_WAL_USAGE, parallel_vacuum_process_safe_indexes(), parallel_vacuum_update_shared_delay_params(), parallel_vacuum_worker_delay_ns, ParallelWorkerNumber, pgstat_progress_parallel_incr_param(), pgstat_report_activity(), pgstat_report_query_id(), ErrorContextCallback::previous, PROC_IN_VACUUM, PROGRESS_VACUUM_DELAY_TIME, pstrdup(), pv_shared_cost_params, PVShared::queryid, RelationGetNamespace, RelationGetRelationName, PVShared::relid, ParallelVacuumState::relname, ParallelVacuumState::relnamespace, PVShared::ring_nbuffers, RowExclusiveLock, ParallelVacuumState::shared, ShareUpdateExclusiveLock, shm_toc_lookup(), STATE_RUNNING, ParallelVacuumState::status, PGPROC::statusFlags, table_close(), table_open(), TidStoreAttach(), TidStoreDetach(), track_cost_delay_timing, vac_close_indexes(), vac_open_indexes(), VacuumActiveNWorkers, VacuumCostBalance, VacuumCostBalanceLocal, VacuumSharedCostBalance, and VacuumUpdateCosts().

◆ parallel_vacuum_process_all_indexes()

static void parallel_vacuum_process_all_indexes ( ParallelVacuumState pvs,
int  num_index_scans,
bool  vacuum,
PVWorkerStats wstats 
)
static

Definition at line 811 of file vacuumparallel.c.

813{
814 int nworkers;
816
818
819 if (vacuum)
820 {
822
823 /* Determine the number of parallel workers to launch */
824 nworkers = pvs->nindexes_parallel_bulkdel;
825 }
826 else
827 {
829
830 /* Determine the number of parallel workers to launch */
831 nworkers = pvs->nindexes_parallel_cleanup;
832
833 /* Add conditionally parallel-aware indexes if in the first time call */
834 if (num_index_scans == 0)
835 nworkers += pvs->nindexes_parallel_condcleanup;
836 }
837
838 /* The leader process will participate */
839 nworkers--;
840
841 /*
842 * It is possible that parallel context is initialized with fewer workers
843 * than the number of indexes that need a separate worker in the current
844 * phase, so we need to consider it. See
845 * parallel_vacuum_compute_workers().
846 */
847 nworkers = Min(nworkers, pvs->pcxt->nworkers);
848
849 /* Update the statistics, if we asked to */
850 if (wstats != NULL && nworkers > 0)
851 wstats->nplanned += nworkers;
852
853 /*
854 * Set index vacuum status and mark whether parallel vacuum worker can
855 * process it.
856 */
857 for (int i = 0; i < pvs->nindexes; i++)
858 {
859 PVIndStats *indstats = &(pvs->indstats[i]);
860
862 indstats->status = new_status;
864 (pvs->will_parallel_vacuum[i] &&
866 num_index_scans,
867 vacuum));
868 }
869
870 /* Reset the parallel index processing and progress counters */
871 pg_atomic_write_u32(&(pvs->shared->idx), 0);
872
873 /* Setup the shared cost-based vacuum delay and launch workers */
874 if (nworkers > 0)
875 {
876 /* Reinitialize parallel context to relaunch parallel workers */
877 if (num_index_scans > 0)
879
880 /*
881 * Set up shared cost balance and the number of active workers for
882 * vacuum delay. We need to do this before launching workers as
883 * otherwise, they might not see the updated values for these
884 * parameters.
885 */
888
889 /*
890 * The number of workers can vary between bulkdelete and cleanup
891 * phase.
892 */
893 ReinitializeParallelWorkers(pvs->pcxt, nworkers);
894
896
897 if (pvs->pcxt->nworkers_launched > 0)
898 {
899 /*
900 * Reset the local cost values for leader backend as we have
901 * already accumulated the remaining balance of heap.
902 */
905
906 /* Enable shared cost balance for leader backend */
909
910 /* Update the statistics, if we asked to */
911 if (wstats != NULL)
912 wstats->nlaunched += pvs->pcxt->nworkers_launched;
913 }
914
915 if (vacuum)
916 ereport(pvs->shared->elevel,
917 (errmsg(ngettext("launched %d parallel vacuum worker for index vacuuming (planned: %d)",
918 "launched %d parallel vacuum workers for index vacuuming (planned: %d)",
919 pvs->pcxt->nworkers_launched),
920 pvs->pcxt->nworkers_launched, nworkers)));
921 else
922 ereport(pvs->shared->elevel,
923 (errmsg(ngettext("launched %d parallel vacuum worker for index cleanup (planned: %d)",
924 "launched %d parallel vacuum workers for index cleanup (planned: %d)",
925 pvs->pcxt->nworkers_launched),
926 pvs->pcxt->nworkers_launched, nworkers)));
927 }
928
929 /* Vacuum the indexes that can be processed by only leader process */
931
932 /*
933 * Join as a parallel worker. The leader vacuums alone processes all
934 * parallel-safe indexes in the case where no workers are launched.
935 */
937
938 /*
939 * Next, accumulate buffer and WAL usage. (This must wait for the workers
940 * to finish, or we might get incomplete data.)
941 */
942 if (nworkers > 0)
943 {
944 /* Wait for all vacuum workers to finish */
946
947 for (int i = 0; i < pvs->pcxt->nworkers_launched; i++)
949 }
950
951 /*
952 * Reset all index status back to initial (while checking that we have
953 * vacuumed all indexes).
954 */
955 for (int i = 0; i < pvs->nindexes; i++)
956 {
957 PVIndStats *indstats = &(pvs->indstats[i]);
958
960 elog(ERROR, "parallel index vacuum on index \"%s\" is not completed",
962
964 }
965
966 /*
967 * Carry the shared balance value to heap scan and disable shared costing
968 */
970 {
974 }
975}
static void pg_atomic_write_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition atomics.h:274
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition atomics.h:237
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition parallel.c:805
void LaunchParallelWorkers(ParallelContext *pcxt)
Definition parallel.c:583
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition parallel.c:511
void ReinitializeParallelWorkers(ParallelContext *pcxt, int nworkers_to_launch)
Definition parallel.c:568
#define ngettext(s, p, n)
Definition c.h:1270
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
void InstrAccumParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition instrument.c:297
static char * errmsg
bool parallel_workers_can_process
PVIndVacStatus status
int nworkers_launched
Definition parallel.h:39
static bool parallel_vacuum_index_is_parallel_safe(Relation indrel, int num_index_scans, bool vacuum)
static void parallel_vacuum_process_unsafe_indexes(ParallelVacuumState *pvs)

References PVShared::active_nworkers, Assert, ParallelVacuumState::buffer_usage, PVShared::cost_balance, PVShared::elevel, elog, ereport, errmsg, ERROR, fb(), i, PVShared::idx, ParallelVacuumState::indrels, ParallelVacuumState::indstats, InstrAccumParallelQuery(), IsParallelWorker, LaunchParallelWorkers(), Min, ngettext, ParallelVacuumState::nindexes, ParallelVacuumState::nindexes_parallel_bulkdel, ParallelVacuumState::nindexes_parallel_cleanup, ParallelVacuumState::nindexes_parallel_condcleanup, ParallelContext::nworkers, ParallelContext::nworkers_launched, PARALLEL_INDVAC_STATUS_COMPLETED, PARALLEL_INDVAC_STATUS_INITIAL, PARALLEL_INDVAC_STATUS_NEED_BULKDELETE, PARALLEL_INDVAC_STATUS_NEED_CLEANUP, parallel_vacuum_index_is_parallel_safe(), parallel_vacuum_process_safe_indexes(), parallel_vacuum_process_unsafe_indexes(), PVIndStats::parallel_workers_can_process, ParallelVacuumState::pcxt, pg_atomic_read_u32(), pg_atomic_write_u32(), ReinitializeParallelDSM(), ReinitializeParallelWorkers(), RelationGetRelationName, ParallelVacuumState::shared, PVIndStats::status, vacuum(), VacuumActiveNWorkers, VacuumCostBalance, VacuumCostBalanceLocal, VacuumSharedCostBalance, WaitForParallelWorkersToFinish(), ParallelVacuumState::wal_usage, and ParallelVacuumState::will_parallel_vacuum.

Referenced by parallel_vacuum_bulkdel_all_indexes(), and parallel_vacuum_cleanup_all_indexes().

◆ parallel_vacuum_process_one_index()

static void parallel_vacuum_process_one_index ( ParallelVacuumState pvs,
Relation  indrel,
PVIndStats indstats 
)
static

Definition at line 1073 of file vacuumparallel.c.

1075{
1076 IndexBulkDeleteResult *istat = NULL;
1079
1080 /*
1081 * Update the pointer to the corresponding bulk-deletion result if someone
1082 * has already updated it
1083 */
1084 if (indstats->istat_updated)
1085 istat = &(indstats->istat);
1086
1087 ivinfo.index = indrel;
1088 ivinfo.heaprel = pvs->heaprel;
1089 ivinfo.analyze_only = false;
1090 ivinfo.report_progress = false;
1091 ivinfo.message_level = DEBUG2;
1092 ivinfo.estimated_count = pvs->shared->estimated_count;
1093 ivinfo.num_heap_tuples = pvs->shared->reltuples;
1094 ivinfo.strategy = pvs->bstrategy;
1095
1096 /* Update error traceback information */
1098 pvs->status = indstats->status;
1099
1100 switch (indstats->status)
1101 {
1104 &pvs->shared->dead_items_info);
1105 break;
1108 break;
1109 default:
1110 elog(ERROR, "unexpected parallel vacuum index status %d for index \"%s\"",
1111 indstats->status,
1113 }
1114
1115 /*
1116 * Copy the index bulk-deletion result returned from ambulkdelete and
1117 * amvacuumcleanup to the DSM segment if it's the first cycle because they
1118 * allocate locally and it's possible that an index will be vacuumed by a
1119 * different vacuum process the next cycle. Copying the result normally
1120 * happens only the first time an index is vacuumed. For any additional
1121 * vacuum pass, we directly point to the result on the DSM segment and
1122 * pass it to vacuum index APIs so that workers can update it directly.
1123 *
1124 * Since all vacuum workers write the bulk-deletion result at different
1125 * slots we can write them without locking.
1126 */
1127 if (!indstats->istat_updated && istat_res != NULL)
1128 {
1129 memcpy(&(indstats->istat), istat_res, sizeof(IndexBulkDeleteResult));
1130 indstats->istat_updated = true;
1131
1132 /* Free the locally-allocated bulk-deletion result */
1134 }
1135
1136 /*
1137 * Update the status to completed. No need to lock here since each worker
1138 * touches different indexes.
1139 */
1141
1142 /* Reset error traceback information */
1144 pfree(pvs->indname);
1145 pvs->indname = NULL;
1146
1147 /*
1148 * Call the parallel variant of pgstat_progress_incr_param so workers can
1149 * report progress of index vacuum to the leader.
1150 */
1152}
#define DEBUG2
Definition elog.h:30
#define PROGRESS_VACUUM_INDEXES_PROCESSED
Definition progress.h:30
IndexBulkDeleteResult * vac_cleanup_one_index(IndexVacuumInfo *ivinfo, IndexBulkDeleteResult *istat)
Definition vacuum.c:2679
IndexBulkDeleteResult * vac_bulkdel_one_index(IndexVacuumInfo *ivinfo, IndexBulkDeleteResult *istat, TidStore *dead_items, VacDeadItemsInfo *dead_items_info)
Definition vacuum.c:2658

References ParallelVacuumState::bstrategy, ParallelVacuumState::dead_items, PVShared::dead_items_info, DEBUG2, elog, ERROR, PVShared::estimated_count, fb(), ParallelVacuumState::heaprel, ParallelVacuumState::indname, PVIndStats::istat, PVIndStats::istat_updated, memcpy(), PARALLEL_INDVAC_STATUS_COMPLETED, PARALLEL_INDVAC_STATUS_NEED_BULKDELETE, PARALLEL_INDVAC_STATUS_NEED_CLEANUP, pfree(), pgstat_progress_parallel_incr_param(), PROGRESS_VACUUM_INDEXES_PROCESSED, pstrdup(), RelationGetRelationName, PVShared::reltuples, ParallelVacuumState::shared, PVIndStats::status, ParallelVacuumState::status, vac_bulkdel_one_index(), and vac_cleanup_one_index().

Referenced by parallel_vacuum_process_safe_indexes(), and parallel_vacuum_process_unsafe_indexes().

◆ parallel_vacuum_process_safe_indexes()

static void parallel_vacuum_process_safe_indexes ( ParallelVacuumState pvs)
static

Definition at line 982 of file vacuumparallel.c.

983{
984 /*
985 * Increment the active worker count if we are able to launch any worker.
986 */
989
990 /* Loop until all indexes are vacuumed */
991 for (;;)
992 {
993 int idx;
994 PVIndStats *indstats;
995
996 /* Get an index number to process */
997 idx = pg_atomic_fetch_add_u32(&(pvs->shared->idx), 1);
998
999 /* Done for all indexes? */
1000 if (idx >= pvs->nindexes)
1001 break;
1002
1003 indstats = &(pvs->indstats[idx]);
1004
1005 /*
1006 * Skip vacuuming index that is unsafe for workers or has an
1007 * unsuitable target for parallel index vacuum (this is vacuumed in
1008 * parallel_vacuum_process_unsafe_indexes() by the leader).
1009 */
1010 if (!indstats->parallel_workers_can_process)
1011 continue;
1012
1013 /* Do vacuum or cleanup of the index */
1014 parallel_vacuum_process_one_index(pvs, pvs->indrels[idx], indstats);
1015 }
1016
1017 /*
1018 * We have completed the index vacuum so decrement the active worker
1019 * count.
1020 */
1023}
Datum idx(PG_FUNCTION_ARGS)
Definition _int_op.c:262
static uint32 pg_atomic_sub_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition atomics.h:439
static uint32 pg_atomic_fetch_add_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition atomics.h:366
static uint32 pg_atomic_add_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition atomics.h:424
static void parallel_vacuum_process_one_index(ParallelVacuumState *pvs, Relation indrel, PVIndStats *indstats)

References idx(), PVShared::idx, ParallelVacuumState::indrels, ParallelVacuumState::indstats, ParallelVacuumState::nindexes, parallel_vacuum_process_one_index(), PVIndStats::parallel_workers_can_process, pg_atomic_add_fetch_u32(), pg_atomic_fetch_add_u32(), pg_atomic_sub_fetch_u32(), ParallelVacuumState::shared, and VacuumActiveNWorkers.

Referenced by parallel_vacuum_main(), and parallel_vacuum_process_all_indexes().

◆ parallel_vacuum_process_unsafe_indexes()

static void parallel_vacuum_process_unsafe_indexes ( ParallelVacuumState pvs)
static

Definition at line 1036 of file vacuumparallel.c.

1037{
1039
1040 /*
1041 * Increment the active worker count if we are able to launch any worker.
1042 */
1045
1046 for (int i = 0; i < pvs->nindexes; i++)
1047 {
1048 PVIndStats *indstats = &(pvs->indstats[i]);
1049
1050 /* Skip, indexes that are safe for workers */
1051 if (indstats->parallel_workers_can_process)
1052 continue;
1053
1054 /* Do vacuum or cleanup of the index */
1055 parallel_vacuum_process_one_index(pvs, pvs->indrels[i], indstats);
1056 }
1057
1058 /*
1059 * We have completed the index vacuum so decrement the active worker
1060 * count.
1061 */
1064}

References Assert, i, ParallelVacuumState::indrels, ParallelVacuumState::indstats, IsParallelWorker, ParallelVacuumState::nindexes, parallel_vacuum_process_one_index(), PVIndStats::parallel_workers_can_process, pg_atomic_add_fetch_u32(), pg_atomic_sub_fetch_u32(), and VacuumActiveNWorkers.

Referenced by parallel_vacuum_process_all_indexes().

◆ parallel_vacuum_propagate_shared_delay_params()

void parallel_vacuum_propagate_shared_delay_params ( void  )

Definition at line 695 of file vacuumparallel.c.

696{
698
699 /*
700 * Quick return if the leader process is not sharing the delay parameters.
701 */
703 return;
704
705 /*
706 * Check if any delay parameters have changed. We can read them without
707 * locks as only the leader can modify them.
708 */
714 return;
715
716 /* Update the shared delay parameters */
720
721 /*
722 * Increment the generation of the parameters, i.e. let parallel workers
723 * know that they should re-read shared cost params.
724 */
726}
int VacuumCostPageMiss
Definition globals.c:155
int VacuumCostPageDirty
Definition globals.c:156
int VacuumCostPageHit
Definition globals.c:154
static void SpinLockRelease(volatile slock_t *lock)
Definition spin.h:62
static void SpinLockAcquire(volatile slock_t *lock)
Definition spin.h:56
double vacuum_cost_delay
Definition vacuum.c:92
int vacuum_cost_limit
Definition vacuum.c:93

References AmAutoVacuumWorkerProcess, Assert, PVSharedCostParams::cost_delay, PVSharedCostParams::cost_limit, PVSharedCostParams::cost_page_dirty, PVSharedCostParams::cost_page_hit, PVSharedCostParams::cost_page_miss, fb(), PVSharedCostParams::generation, PVSharedCostParams::mutex, parallel_vacuum_set_cost_parameters(), pg_atomic_fetch_add_u32(), pv_shared_cost_params, SpinLockAcquire(), SpinLockRelease(), vacuum_cost_delay, vacuum_cost_limit, VacuumCostPageDirty, VacuumCostPageHit, and VacuumCostPageMiss.

Referenced by vacuum_delay_point().

◆ parallel_vacuum_reset_dead_items()

void parallel_vacuum_reset_dead_items ( ParallelVacuumState pvs)

Definition at line 570 of file vacuumparallel.c.

571{
572 VacDeadItemsInfo *dead_items_info = &(pvs->shared->dead_items_info);
573
574 /*
575 * Free the current tidstore and return allocated DSA segments to the
576 * operating system. Then we recreate the tidstore with the same max_bytes
577 * limitation we just used.
578 */
580 pvs->dead_items = TidStoreCreateShared(dead_items_info->max_bytes,
582
583 /* Update the DSA pointer for dead_items to the new one */
586
587 /* Reset the counter */
588 dead_items_info->num_items = 0;
589}
int64 num_items
Definition vacuum.h:299

References ParallelVacuumState::dead_items, PVShared::dead_items_dsa_handle, PVShared::dead_items_handle, PVShared::dead_items_info, dsa_get_handle(), fb(), VacDeadItemsInfo::max_bytes, VacDeadItemsInfo::num_items, ParallelVacuumState::shared, TidStoreCreateShared(), TidStoreDestroy(), TidStoreGetDSA(), and TidStoreGetHandle().

Referenced by dead_items_reset().

◆ parallel_vacuum_set_cost_parameters()

◆ parallel_vacuum_update_shared_delay_params()

void parallel_vacuum_update_shared_delay_params ( void  )

Definition at line 651 of file vacuumparallel.c.

652{
654
656
657 /* Quick return if the worker is not running for the autovacuum */
659 return;
660
663
664 /* Return if parameters had not changed in the leader */
666 return;
667
675
677
679
680 elog(DEBUG2,
681 "parallel autovacuum worker updated cost params: cost_limit=%d, cost_delay=%g, cost_page_miss=%d, cost_page_dirty=%d, cost_page_hit=%d",
687}
uint32_t uint32
Definition c.h:624
int VacuumCostLimit
Definition globals.c:157
double VacuumCostDelay
Definition globals.c:158
static uint32 shared_params_generation_local

References Assert, PVSharedCostParams::cost_delay, PVSharedCostParams::cost_limit, PVSharedCostParams::cost_page_dirty, PVSharedCostParams::cost_page_hit, PVSharedCostParams::cost_page_miss, DEBUG2, elog, fb(), PVSharedCostParams::generation, IsParallelWorker, PVSharedCostParams::mutex, pg_atomic_read_u32(), pv_shared_cost_params, shared_params_generation_local, SpinLockAcquire(), SpinLockRelease(), vacuum_cost_delay, vacuum_cost_limit, VacuumCostDelay, VacuumCostLimit, VacuumCostPageDirty, VacuumCostPageHit, VacuumCostPageMiss, and VacuumUpdateCosts().

Referenced by parallel_vacuum_main(), and vacuum_delay_point().

Variable Documentation

◆ pv_shared_cost_params

◆ shared_params_generation_local

uint32 shared_params_generation_local = 0
static

Definition at line 282 of file vacuumparallel.c.

Referenced by parallel_vacuum_update_shared_delay_params().