PostgreSQL Source Code  git master
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 "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  PVShared
 
struct  PVIndStats
 
struct  ParallelVacuumState
 

Macros

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

Typedefs

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)
 
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)
 
ParallelVacuumStateparallel_vacuum_init (Relation rel, Relation *indrels, int nindexes, int nrequested_workers, int max_items, int elevel, BufferAccessStrategy bstrategy)
 
void parallel_vacuum_end (ParallelVacuumState *pvs, IndexBulkDeleteResult **istats)
 
VacDeadItemsparallel_vacuum_get_dead_items (ParallelVacuumState *pvs)
 
void parallel_vacuum_bulkdel_all_indexes (ParallelVacuumState *pvs, long num_table_tuples, int num_index_scans)
 
void parallel_vacuum_cleanup_all_indexes (ParallelVacuumState *pvs, long num_table_tuples, int num_index_scans, bool estimated_count)
 
void parallel_vacuum_main (dsm_segment *seg, shm_toc *toc)
 

Macro Definition Documentation

◆ PARALLEL_VACUUM_KEY_BUFFER_USAGE

#define PARALLEL_VACUUM_KEY_BUFFER_USAGE   4

Definition at line 50 of file vacuumparallel.c.

◆ PARALLEL_VACUUM_KEY_DEAD_ITEMS

#define PARALLEL_VACUUM_KEY_DEAD_ITEMS   2

Definition at line 48 of file vacuumparallel.c.

◆ PARALLEL_VACUUM_KEY_INDEX_STATS

#define PARALLEL_VACUUM_KEY_INDEX_STATS   6

Definition at line 52 of file vacuumparallel.c.

◆ PARALLEL_VACUUM_KEY_QUERY_TEXT

#define PARALLEL_VACUUM_KEY_QUERY_TEXT   3

Definition at line 49 of file vacuumparallel.c.

◆ PARALLEL_VACUUM_KEY_SHARED

#define PARALLEL_VACUUM_KEY_SHARED   1

Definition at line 47 of file vacuumparallel.c.

◆ PARALLEL_VACUUM_KEY_WAL_USAGE

#define PARALLEL_VACUUM_KEY_WAL_USAGE   5

Definition at line 51 of file vacuumparallel.c.

Typedef Documentation

◆ PVIndStats

typedef struct PVIndStats PVIndStats

◆ PVIndVacStatus

◆ PVShared

typedef struct PVShared PVShared

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 116 of file vacuumparallel.c.

117 {
PVIndVacStatus
@ PARALLEL_INDVAC_STATUS_NEED_CLEANUP
@ PARALLEL_INDVAC_STATUS_INITIAL
@ PARALLEL_INDVAC_STATUS_NEED_BULKDELETE
@ PARALLEL_INDVAC_STATUS_COMPLETED

Function Documentation

◆ parallel_vacuum_bulkdel_all_indexes()

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

Definition at line 469 of file vacuumparallel.c.

471 {
473 
474  /*
475  * We can only provide an approximate value of num_heap_tuples, at least
476  * for now.
477  */
478  pvs->shared->reltuples = num_table_tuples;
479  pvs->shared->estimated_count = true;
480 
481  parallel_vacuum_process_all_indexes(pvs, num_index_scans, true);
482 }
#define IsParallelWorker()
Definition: parallel.h:60
Assert(fmt[strlen(fmt) - 1] !='\n')
double reltuples
bool estimated_count
static void parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scans, bool vacuum)

References Assert(), PVShared::estimated_count, 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 
)

Definition at line 488 of file vacuumparallel.c.

490 {
492 
493  /*
494  * We can provide a better estimate of total number of surviving tuples
495  * (we assume indexes are more interested in that than in the number of
496  * nominally live tuples).
497  */
498  pvs->shared->reltuples = num_table_tuples;
499  pvs->shared->estimated_count = estimated_count;
500 
501  parallel_vacuum_process_all_indexes(pvs, num_index_scans, false);
502 }

References Assert(), PVShared::estimated_count, 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 518 of file vacuumparallel.c.

520 {
521  int nindexes_parallel = 0;
522  int nindexes_parallel_bulkdel = 0;
523  int nindexes_parallel_cleanup = 0;
524  int parallel_workers;
525 
526  /*
527  * We don't allow performing parallel operation in standalone backend or
528  * when parallelism is disabled.
529  */
531  return 0;
532 
533  /*
534  * Compute the number of indexes that can participate in parallel vacuum.
535  */
536  for (int i = 0; i < nindexes; i++)
537  {
538  Relation indrel = indrels[i];
539  uint8 vacoptions = indrel->rd_indam->amparallelvacuumoptions;
540 
541  /* Skip index that is not a suitable target for parallel index vacuum */
542  if (vacoptions == VACUUM_OPTION_NO_PARALLEL ||
544  continue;
545 
546  will_parallel_vacuum[i] = true;
547 
548  if ((vacoptions & VACUUM_OPTION_PARALLEL_BULKDEL) != 0)
549  nindexes_parallel_bulkdel++;
550  if (((vacoptions & VACUUM_OPTION_PARALLEL_CLEANUP) != 0) ||
551  ((vacoptions & VACUUM_OPTION_PARALLEL_COND_CLEANUP) != 0))
552  nindexes_parallel_cleanup++;
553  }
554 
555  nindexes_parallel = Max(nindexes_parallel_bulkdel,
556  nindexes_parallel_cleanup);
557 
558  /* The leader process takes one index */
559  nindexes_parallel--;
560 
561  /* No index supports parallel vacuum */
562  if (nindexes_parallel <= 0)
563  return 0;
564 
565  /* Compute the parallel degree */
566  parallel_workers = (nrequested > 0) ?
567  Min(nrequested, nindexes_parallel) : nindexes_parallel;
568 
569  /* Cap by max_parallel_maintenance_workers */
570  parallel_workers = Min(parallel_workers, max_parallel_maintenance_workers);
571 
572  return parallel_workers;
573 }
int min_parallel_index_scan_size
Definition: allpaths.c:82
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:229
#define Min(x, y)
Definition: c.h:991
#define Max(x, y)
Definition: c.h:985
unsigned char uint8
Definition: c.h:491
int max_parallel_maintenance_workers
Definition: globals.c:131
bool IsUnderPostmaster
Definition: globals.c:117
int i
Definition: isn.c:73
uint8 amparallelvacuumoptions
Definition: amapi.h:255
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 IndexAmRoutine::amparallelvacuumoptions, 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_end()

void parallel_vacuum_end ( ParallelVacuumState pvs,
IndexBulkDeleteResult **  istats 
)

Definition at line 433 of file vacuumparallel.c.

434 {
436 
437  /* Copy the updated statistics */
438  for (int i = 0; i < pvs->nindexes; i++)
439  {
440  PVIndStats *indstats = &(pvs->indstats[i]);
441 
442  if (indstats->istat_updated)
443  {
444  istats[i] = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult));
445  memcpy(istats[i], &indstats->istat, sizeof(IndexBulkDeleteResult));
446  }
447  else
448  istats[i] = NULL;
449  }
450 
453 
455  pfree(pvs);
456 }
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:929
void pfree(void *pointer)
Definition: mcxt.c:1508
void * palloc0(Size size)
Definition: mcxt.c:1334
bool istat_updated
IndexBulkDeleteResult istat
ParallelContext * pcxt
PVIndStats * indstats
void ExitParallelMode(void)
Definition: xact.c:1061

References Assert(), DestroyParallelContext(), ExitParallelMode(), i, ParallelVacuumState::indstats, IsParallelWorker, PVIndStats::istat, PVIndStats::istat_updated, ParallelVacuumState::nindexes, palloc0(), ParallelVacuumState::pcxt, pfree(), 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 1074 of file vacuumparallel.c.

1075 {
1076  ParallelVacuumState *errinfo = arg;
1077 
1078  switch (errinfo->status)
1079  {
1081  errcontext("while vacuuming index \"%s\" of relation \"%s.%s\"",
1082  errinfo->indname,
1083  errinfo->relnamespace,
1084  errinfo->relname);
1085  break;
1087  errcontext("while cleaning up index \"%s\" of relation \"%s.%s\"",
1088  errinfo->indname,
1089  errinfo->relnamespace,
1090  errinfo->relname);
1091  break;
1094  default:
1095  return;
1096  }
1097 }
#define errcontext
Definition: elog.h:196
void * arg
PVIndVacStatus status

References arg, errcontext, ParallelVacuumState::indname, PARALLEL_INDVAC_STATUS_COMPLETED, PARALLEL_INDVAC_STATUS_INITIAL, PARALLEL_INDVAC_STATUS_NEED_BULKDELETE, PARALLEL_INDVAC_STATUS_NEED_CLEANUP, ParallelVacuumState::relname, ParallelVacuumState::relnamespace, and ParallelVacuumState::status.

Referenced by parallel_vacuum_main().

◆ parallel_vacuum_get_dead_items()

VacDeadItems* parallel_vacuum_get_dead_items ( ParallelVacuumState pvs)

Definition at line 460 of file vacuumparallel.c.

461 {
462  return pvs->dead_items;
463 }
VacDeadItems * dead_items

References ParallelVacuumState::dead_items.

Referenced by dead_items_alloc().

◆ 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 919 of file vacuumparallel.c.

921 {
922  uint8 vacoptions;
923 
924  vacoptions = indrel->rd_indam->amparallelvacuumoptions;
925 
926  /* In parallel vacuum case, check if it supports parallel bulk-deletion */
927  if (vacuum)
928  return ((vacoptions & VACUUM_OPTION_PARALLEL_BULKDEL) != 0);
929 
930  /* Not safe, if the index does not support parallel cleanup */
931  if (((vacoptions & VACUUM_OPTION_PARALLEL_CLEANUP) == 0) &&
932  ((vacoptions & VACUUM_OPTION_PARALLEL_COND_CLEANUP) == 0))
933  return false;
934 
935  /*
936  * Not safe, if the index supports parallel cleanup conditionally, but we
937  * have already processed the index (for bulkdelete). We do this to avoid
938  * the need to invoke workers when parallel index cleanup doesn't need to
939  * scan the index. See the comments for option
940  * VACUUM_OPTION_PARALLEL_COND_CLEANUP to know when indexes support
941  * parallel cleanup conditionally.
942  */
943  if (num_index_scans > 0 &&
944  ((vacoptions & VACUUM_OPTION_PARALLEL_COND_CLEANUP) != 0))
945  return false;
946 
947  return true;
948 }
void vacuum(List *relations, VacuumParams *params, BufferAccessStrategy bstrategy, MemoryContext vac_context, bool isTopLevel)
Definition: vacuum.c:479

References IndexAmRoutine::amparallelvacuumoptions, RelationData::rd_indam, 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  max_items,
int  elevel,
BufferAccessStrategy  bstrategy 
)

Definition at line 234 of file vacuumparallel.c.

237 {
238  ParallelVacuumState *pvs;
239  ParallelContext *pcxt;
240  PVShared *shared;
241  VacDeadItems *dead_items;
242  PVIndStats *indstats;
243  BufferUsage *buffer_usage;
244  WalUsage *wal_usage;
245  bool *will_parallel_vacuum;
246  Size est_indstats_len;
247  Size est_shared_len;
248  Size est_dead_items_len;
249  int nindexes_mwm = 0;
250  int parallel_workers = 0;
251  int querylen;
252 
253  /*
254  * A parallel vacuum must be requested and there must be indexes on the
255  * relation
256  */
257  Assert(nrequested_workers >= 0);
258  Assert(nindexes > 0);
259 
260  /*
261  * Compute the number of parallel vacuum workers to launch
262  */
263  will_parallel_vacuum = (bool *) palloc0(sizeof(bool) * nindexes);
264  parallel_workers = parallel_vacuum_compute_workers(indrels, nindexes,
265  nrequested_workers,
266  will_parallel_vacuum);
267  if (parallel_workers <= 0)
268  {
269  /* Can't perform vacuum in parallel -- return NULL */
270  pfree(will_parallel_vacuum);
271  return NULL;
272  }
273 
275  pvs->indrels = indrels;
276  pvs->nindexes = nindexes;
277  pvs->will_parallel_vacuum = will_parallel_vacuum;
278  pvs->bstrategy = bstrategy;
279  pvs->heaprel = rel;
280 
282  pcxt = CreateParallelContext("postgres", "parallel_vacuum_main",
283  parallel_workers);
284  Assert(pcxt->nworkers > 0);
285  pvs->pcxt = pcxt;
286 
287  /* Estimate size for index vacuum stats -- PARALLEL_VACUUM_KEY_INDEX_STATS */
288  est_indstats_len = mul_size(sizeof(PVIndStats), nindexes);
289  shm_toc_estimate_chunk(&pcxt->estimator, est_indstats_len);
290  shm_toc_estimate_keys(&pcxt->estimator, 1);
291 
292  /* Estimate size for shared information -- PARALLEL_VACUUM_KEY_SHARED */
293  est_shared_len = sizeof(PVShared);
294  shm_toc_estimate_chunk(&pcxt->estimator, est_shared_len);
295  shm_toc_estimate_keys(&pcxt->estimator, 1);
296 
297  /* Estimate size for dead_items -- PARALLEL_VACUUM_KEY_DEAD_ITEMS */
298  est_dead_items_len = vac_max_items_to_alloc_size(max_items);
299  shm_toc_estimate_chunk(&pcxt->estimator, est_dead_items_len);
300  shm_toc_estimate_keys(&pcxt->estimator, 1);
301 
302  /*
303  * Estimate space for BufferUsage and WalUsage --
304  * PARALLEL_VACUUM_KEY_BUFFER_USAGE and PARALLEL_VACUUM_KEY_WAL_USAGE.
305  *
306  * If there are no extensions loaded that care, we could skip this. We
307  * have no way of knowing whether anyone's looking at pgBufferUsage or
308  * pgWalUsage, so do it unconditionally.
309  */
311  mul_size(sizeof(BufferUsage), pcxt->nworkers));
312  shm_toc_estimate_keys(&pcxt->estimator, 1);
314  mul_size(sizeof(WalUsage), pcxt->nworkers));
315  shm_toc_estimate_keys(&pcxt->estimator, 1);
316 
317  /* Finally, estimate PARALLEL_VACUUM_KEY_QUERY_TEXT space */
318  if (debug_query_string)
319  {
320  querylen = strlen(debug_query_string);
321  shm_toc_estimate_chunk(&pcxt->estimator, querylen + 1);
322  shm_toc_estimate_keys(&pcxt->estimator, 1);
323  }
324  else
325  querylen = 0; /* keep compiler quiet */
326 
327  InitializeParallelDSM(pcxt);
328 
329  /* Prepare index vacuum stats */
330  indstats = (PVIndStats *) shm_toc_allocate(pcxt->toc, est_indstats_len);
331  MemSet(indstats, 0, est_indstats_len);
332  for (int i = 0; i < nindexes; i++)
333  {
334  Relation indrel = indrels[i];
335  uint8 vacoptions = indrel->rd_indam->amparallelvacuumoptions;
336 
337  /*
338  * Cleanup option should be either disabled, always performing in
339  * parallel or conditionally performing in parallel.
340  */
341  Assert(((vacoptions & VACUUM_OPTION_PARALLEL_CLEANUP) == 0) ||
342  ((vacoptions & VACUUM_OPTION_PARALLEL_COND_CLEANUP) == 0));
343  Assert(vacoptions <= VACUUM_OPTION_MAX_VALID_VALUE);
344 
345  if (!will_parallel_vacuum[i])
346  continue;
347 
348  if (indrel->rd_indam->amusemaintenanceworkmem)
349  nindexes_mwm++;
350 
351  /*
352  * Remember the number of indexes that support parallel operation for
353  * each phase.
354  */
355  if ((vacoptions & VACUUM_OPTION_PARALLEL_BULKDEL) != 0)
357  if ((vacoptions & VACUUM_OPTION_PARALLEL_CLEANUP) != 0)
359  if ((vacoptions & VACUUM_OPTION_PARALLEL_COND_CLEANUP) != 0)
361  }
363  pvs->indstats = indstats;
364 
365  /* Prepare shared information */
366  shared = (PVShared *) shm_toc_allocate(pcxt->toc, est_shared_len);
367  MemSet(shared, 0, est_shared_len);
368  shared->relid = RelationGetRelid(rel);
369  shared->elevel = elevel;
371  (nindexes_mwm > 0) ?
372  maintenance_work_mem / Min(parallel_workers, nindexes_mwm) :
374 
375  /* Use the same buffer size for all workers */
376  shared->ring_nbuffers = GetAccessStrategyBufferCount(bstrategy);
377 
378  pg_atomic_init_u32(&(shared->cost_balance), 0);
379  pg_atomic_init_u32(&(shared->active_nworkers), 0);
380  pg_atomic_init_u32(&(shared->idx), 0);
381 
383  pvs->shared = shared;
384 
385  /* Prepare the dead_items space */
386  dead_items = (VacDeadItems *) shm_toc_allocate(pcxt->toc,
387  est_dead_items_len);
388  dead_items->max_items = max_items;
389  dead_items->num_items = 0;
390  MemSet(dead_items->items, 0, sizeof(ItemPointerData) * max_items);
392  pvs->dead_items = dead_items;
393 
394  /*
395  * Allocate space for each worker's BufferUsage and WalUsage; no need to
396  * initialize
397  */
398  buffer_usage = shm_toc_allocate(pcxt->toc,
399  mul_size(sizeof(BufferUsage), pcxt->nworkers));
401  pvs->buffer_usage = buffer_usage;
402  wal_usage = shm_toc_allocate(pcxt->toc,
403  mul_size(sizeof(WalUsage), pcxt->nworkers));
405  pvs->wal_usage = wal_usage;
406 
407  /* Store query string for workers */
408  if (debug_query_string)
409  {
410  char *sharedquery;
411 
412  sharedquery = (char *) shm_toc_allocate(pcxt->toc, querylen + 1);
413  memcpy(sharedquery, debug_query_string, querylen + 1);
414  sharedquery[querylen] = '\0';
415  shm_toc_insert(pcxt->toc,
416  PARALLEL_VACUUM_KEY_QUERY_TEXT, sharedquery);
417  }
418 
419  /* Success -- return parallel vacuum state */
420  return pvs;
421 }
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:216
void InitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:205
ParallelContext * CreateParallelContext(const char *library_name, const char *function_name, int nworkers)
Definition: parallel.c:167
#define MemSet(start, val, len)
Definition: c.h:1007
size_t Size
Definition: c.h:592
int GetAccessStrategyBufferCount(BufferAccessStrategy strategy)
Definition: freelist.c:624
int maintenance_work_mem
Definition: globals.c:130
const char * debug_query_string
Definition: postgres.c:87
#define RelationGetRelid(relation)
Definition: rel.h:507
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
#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:510
bool amusemaintenanceworkmem
Definition: amapi.h:251
pg_atomic_uint32 cost_balance
int maintenance_work_mem_worker
pg_atomic_uint32 active_nworkers
int ring_nbuffers
pg_atomic_uint32 idx
shm_toc_estimator estimator
Definition: parallel.h:41
shm_toc * toc
Definition: parallel.h:44
BufferAccessStrategy bstrategy
BufferUsage * buffer_usage
ItemPointerData items[FLEXIBLE_ARRAY_MEMBER]
Definition: vacuum.h:289
int max_items
Definition: vacuum.h:285
int num_items
Definition: vacuum.h:286
Size vac_max_items_to_alloc_size(int max_items)
Definition: vacuum.c:2537
#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
struct PVShared PVShared
#define PARALLEL_VACUUM_KEY_DEAD_ITEMS
void EnterParallelMode(void)
Definition: xact.c:1048

References PVShared::active_nworkers, IndexAmRoutine::amparallelvacuumoptions, IndexAmRoutine::amusemaintenanceworkmem, Assert(), ParallelVacuumState::bstrategy, ParallelVacuumState::buffer_usage, PVShared::cost_balance, CreateParallelContext(), ParallelVacuumState::dead_items, debug_query_string, PVShared::elevel, EnterParallelMode(), ParallelContext::estimator, GetAccessStrategyBufferCount(), ParallelVacuumState::heaprel, i, PVShared::idx, ParallelVacuumState::indrels, ParallelVacuumState::indstats, InitializeParallelDSM(), VacDeadItems::items, maintenance_work_mem, PVShared::maintenance_work_mem_worker, VacDeadItems::max_items, MemSet, Min, mul_size(), ParallelVacuumState::nindexes, ParallelVacuumState::nindexes_parallel_bulkdel, ParallelVacuumState::nindexes_parallel_cleanup, ParallelVacuumState::nindexes_parallel_condcleanup, VacDeadItems::num_items, ParallelContext::nworkers, palloc0(), parallel_vacuum_compute_workers(), PARALLEL_VACUUM_KEY_BUFFER_USAGE, PARALLEL_VACUUM_KEY_DEAD_ITEMS, PARALLEL_VACUUM_KEY_INDEX_STATS, PARALLEL_VACUUM_KEY_QUERY_TEXT, PARALLEL_VACUUM_KEY_SHARED, PARALLEL_VACUUM_KEY_WAL_USAGE, ParallelVacuumState::pcxt, pfree(), pg_atomic_init_u32(), RelationData::rd_indam, RelationGetRelid, PVShared::relid, PVShared::ring_nbuffers, ParallelVacuumState::shared, shm_toc_allocate(), shm_toc_estimate_chunk, shm_toc_estimate_keys, shm_toc_insert(), ParallelContext::toc, vac_max_items_to_alloc_size(), 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 957 of file vacuumparallel.c.

958 {
960  Relation rel;
961  Relation *indrels;
962  PVIndStats *indstats;
963  PVShared *shared;
964  VacDeadItems *dead_items;
965  BufferUsage *buffer_usage;
966  WalUsage *wal_usage;
967  int nindexes;
968  char *sharedquery;
969  ErrorContextCallback errcallback;
970 
971  /*
972  * A parallel vacuum worker must have only PROC_IN_VACUUM flag since we
973  * don't support parallel vacuum for autovacuum as of now.
974  */
976 
977  elog(DEBUG1, "starting parallel vacuum worker");
978 
979  shared = (PVShared *) shm_toc_lookup(toc, PARALLEL_VACUUM_KEY_SHARED, false);
980 
981  /* Set debug_query_string for individual workers */
982  sharedquery = shm_toc_lookup(toc, PARALLEL_VACUUM_KEY_QUERY_TEXT, true);
983  debug_query_string = sharedquery;
985 
986  /*
987  * Open table. The lock mode is the same as the leader process. It's
988  * okay because the lock mode does not conflict among the parallel
989  * workers.
990  */
991  rel = table_open(shared->relid, ShareUpdateExclusiveLock);
992 
993  /*
994  * Open all indexes. indrels are sorted in order by OID, which should be
995  * matched to the leader's one.
996  */
997  vac_open_indexes(rel, RowExclusiveLock, &nindexes, &indrels);
998  Assert(nindexes > 0);
999 
1000  if (shared->maintenance_work_mem_worker > 0)
1002 
1003  /* Set index statistics */
1004  indstats = (PVIndStats *) shm_toc_lookup(toc,
1006  false);
1007 
1008  /* Set dead_items space */
1009  dead_items = (VacDeadItems *) shm_toc_lookup(toc,
1011  false);
1012 
1013  /* Set cost-based vacuum delay */
1015  VacuumCostBalance = 0;
1016  VacuumPageHit = 0;
1017  VacuumPageMiss = 0;
1018  VacuumPageDirty = 0;
1022 
1023  /* Set parallel vacuum state */
1024  pvs.indrels = indrels;
1025  pvs.nindexes = nindexes;
1026  pvs.indstats = indstats;
1027  pvs.shared = shared;
1028  pvs.dead_items = dead_items;
1031  pvs.heaprel = rel;
1032 
1033  /* These fields will be filled during index vacuum or cleanup */
1034  pvs.indname = NULL;
1036 
1037  /* Each parallel VACUUM worker gets its own access strategy. */
1039  shared->ring_nbuffers * (BLCKSZ / 1024));
1040 
1041  /* Setup error traceback support for ereport() */
1043  errcallback.arg = &pvs;
1044  errcallback.previous = error_context_stack;
1045  error_context_stack = &errcallback;
1046 
1047  /* Prepare to track buffer usage during parallel execution */
1049 
1050  /* Process indexes to perform vacuum/cleanup */
1052 
1053  /* Report buffer/WAL usage during parallel execution */
1054  buffer_usage = shm_toc_lookup(toc, PARALLEL_VACUUM_KEY_BUFFER_USAGE, false);
1055  wal_usage = shm_toc_lookup(toc, PARALLEL_VACUUM_KEY_WAL_USAGE, false);
1057  &wal_usage[ParallelWorkerNumber]);
1058 
1059  /* Pop the error context stack */
1060  error_context_stack = errcallback.previous;
1061 
1062  vac_close_indexes(nindexes, indrels, RowExclusiveLock);
1065 }
void VacuumUpdateCosts(void)
Definition: autovacuum.c:1632
int ParallelWorkerNumber
Definition: parallel.c:112
void pgstat_report_activity(BackendState state, const char *cmd_str)
@ STATE_RUNNING
@ BAS_VACUUM
Definition: bufmgr.h:38
ErrorContextCallback * error_context_stack
Definition: elog.c:94
#define DEBUG1
Definition: elog.h:30
#define elog(elevel,...)
Definition: elog.h:224
BufferAccessStrategy GetAccessStrategyWithSize(BufferAccessStrategyType btype, int ring_size_kb)
Definition: freelist.c:584
void FreeAccessStrategy(BufferAccessStrategy strategy)
Definition: freelist.c:639
int64 VacuumPageHit
Definition: globals.c:154
int64 VacuumPageMiss
Definition: globals.c:155
int64 VacuumPageDirty
Definition: globals.c:156
int VacuumCostBalance
Definition: globals.c:158
void InstrEndParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition: instrument.c:208
void InstrStartParallelQuery(void)
Definition: instrument.c:200
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
#define RowExclusiveLock
Definition: lockdefs.h:38
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3344
char * pstrdup(const char *in)
Definition: mcxt.c:1683
#define PROC_IN_VACUUM
Definition: proc.h:58
#define RelationGetRelationName(relation)
Definition: rel.h:541
#define RelationGetNamespace(relation)
Definition: rel.h:548
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
PGPROC * MyProc
Definition: proc.c:66
struct ErrorContextCallback * previous
Definition: elog.h:295
void(* callback)(void *arg)
Definition: elog.h:296
uint8 statusFlags
Definition: proc.h:238
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
pg_atomic_uint32 * VacuumActiveNWorkers
Definition: vacuum.c:103
void vac_open_indexes(Relation relation, LOCKMODE lockmode, int *nindexes, Relation **Irel)
Definition: vacuum.c:2273
int VacuumCostBalanceLocal
Definition: vacuum.c:104
void vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode)
Definition: vacuum.c:2316
pg_atomic_uint32 * VacuumSharedCostBalance
Definition: vacuum.c:102
static void parallel_vacuum_error_callback(void *arg)
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, ParallelVacuumState::dead_items, DEBUG1, debug_query_string, elog, error_context_stack, FreeAccessStrategy(), get_namespace_name(), GetAccessStrategyWithSize(), ParallelVacuumState::heaprel, ParallelVacuumState::indname, ParallelVacuumState::indrels, ParallelVacuumState::indstats, InstrEndParallelQuery(), InstrStartParallelQuery(), 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_DEAD_ITEMS, PARALLEL_VACUUM_KEY_INDEX_STATS, PARALLEL_VACUUM_KEY_QUERY_TEXT, PARALLEL_VACUUM_KEY_SHARED, PARALLEL_VACUUM_KEY_WAL_USAGE, parallel_vacuum_process_safe_indexes(), ParallelWorkerNumber, pgstat_report_activity(), ErrorContextCallback::previous, PROC_IN_VACUUM, pstrdup(), 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(), vac_close_indexes(), vac_open_indexes(), VacuumActiveNWorkers, VacuumCostBalance, VacuumCostBalanceLocal, VacuumPageDirty, VacuumPageHit, VacuumPageMiss, VacuumSharedCostBalance, and VacuumUpdateCosts().

◆ parallel_vacuum_process_all_indexes()

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

Definition at line 580 of file vacuumparallel.c.

582 {
583  int nworkers;
584  PVIndVacStatus new_status;
585 
587 
588  if (vacuum)
589  {
591 
592  /* Determine the number of parallel workers to launch */
593  nworkers = pvs->nindexes_parallel_bulkdel;
594  }
595  else
596  {
598 
599  /* Determine the number of parallel workers to launch */
600  nworkers = pvs->nindexes_parallel_cleanup;
601 
602  /* Add conditionally parallel-aware indexes if in the first time call */
603  if (num_index_scans == 0)
604  nworkers += pvs->nindexes_parallel_condcleanup;
605  }
606 
607  /* The leader process will participate */
608  nworkers--;
609 
610  /*
611  * It is possible that parallel context is initialized with fewer workers
612  * than the number of indexes that need a separate worker in the current
613  * phase, so we need to consider it. See
614  * parallel_vacuum_compute_workers().
615  */
616  nworkers = Min(nworkers, pvs->pcxt->nworkers);
617 
618  /*
619  * Set index vacuum status and mark whether parallel vacuum worker can
620  * process it.
621  */
622  for (int i = 0; i < pvs->nindexes; i++)
623  {
624  PVIndStats *indstats = &(pvs->indstats[i]);
625 
627  indstats->status = new_status;
628  indstats->parallel_workers_can_process =
629  (pvs->will_parallel_vacuum[i] &&
631  num_index_scans,
632  vacuum));
633  }
634 
635  /* Reset the parallel index processing and progress counters */
636  pg_atomic_write_u32(&(pvs->shared->idx), 0);
637 
638  /* Setup the shared cost-based vacuum delay and launch workers */
639  if (nworkers > 0)
640  {
641  /* Reinitialize parallel context to relaunch parallel workers */
642  if (num_index_scans > 0)
644 
645  /*
646  * Set up shared cost balance and the number of active workers for
647  * vacuum delay. We need to do this before launching workers as
648  * otherwise, they might not see the updated values for these
649  * parameters.
650  */
653 
654  /*
655  * The number of workers can vary between bulkdelete and cleanup
656  * phase.
657  */
658  ReinitializeParallelWorkers(pvs->pcxt, nworkers);
659 
661 
662  if (pvs->pcxt->nworkers_launched > 0)
663  {
664  /*
665  * Reset the local cost values for leader backend as we have
666  * already accumulated the remaining balance of heap.
667  */
668  VacuumCostBalance = 0;
670 
671  /* Enable shared cost balance for leader backend */
674  }
675 
676  if (vacuum)
677  ereport(pvs->shared->elevel,
678  (errmsg(ngettext("launched %d parallel vacuum worker for index vacuuming (planned: %d)",
679  "launched %d parallel vacuum workers for index vacuuming (planned: %d)",
680  pvs->pcxt->nworkers_launched),
681  pvs->pcxt->nworkers_launched, nworkers)));
682  else
683  ereport(pvs->shared->elevel,
684  (errmsg(ngettext("launched %d parallel vacuum worker for index cleanup (planned: %d)",
685  "launched %d parallel vacuum workers for index cleanup (planned: %d)",
686  pvs->pcxt->nworkers_launched),
687  pvs->pcxt->nworkers_launched, nworkers)));
688  }
689 
690  /* Vacuum the indexes that can be processed by only leader process */
692 
693  /*
694  * Join as a parallel worker. The leader vacuums alone processes all
695  * parallel-safe indexes in the case where no workers are launched.
696  */
698 
699  /*
700  * Next, accumulate buffer and WAL usage. (This must wait for the workers
701  * to finish, or we might get incomplete data.)
702  */
703  if (nworkers > 0)
704  {
705  /* Wait for all vacuum workers to finish */
707 
708  for (int i = 0; i < pvs->pcxt->nworkers_launched; i++)
710  }
711 
712  /*
713  * Reset all index status back to initial (while checking that we have
714  * vacuumed all indexes).
715  */
716  for (int i = 0; i < pvs->nindexes; i++)
717  {
718  PVIndStats *indstats = &(pvs->indstats[i]);
719 
720  if (indstats->status != PARALLEL_INDVAC_STATUS_COMPLETED)
721  elog(ERROR, "parallel index vacuum on index \"%s\" is not completed",
723 
725  }
726 
727  /*
728  * Carry the shared balance value to heap scan and disable shared costing
729  */
731  {
734  VacuumActiveNWorkers = NULL;
735  }
736 }
static void pg_atomic_write_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:271
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:234
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition: parallel.c:775
void LaunchParallelWorkers(ParallelContext *pcxt)
Definition: parallel.c:552
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:488
void ReinitializeParallelWorkers(ParallelContext *pcxt, int nworkers_to_launch)
Definition: parallel.c:538
#define ngettext(s, p, n)
Definition: c.h:1168
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
void InstrAccumParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition: instrument.c:218
bool parallel_workers_can_process
PVIndVacStatus status
int nworkers_launched
Definition: parallel.h:37
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, 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 834 of file vacuumparallel.c.

836 {
837  IndexBulkDeleteResult *istat = NULL;
838  IndexBulkDeleteResult *istat_res;
839  IndexVacuumInfo ivinfo;
840 
841  /*
842  * Update the pointer to the corresponding bulk-deletion result if someone
843  * has already updated it
844  */
845  if (indstats->istat_updated)
846  istat = &(indstats->istat);
847 
848  ivinfo.index = indrel;
849  ivinfo.heaprel = pvs->heaprel;
850  ivinfo.analyze_only = false;
851  ivinfo.report_progress = false;
852  ivinfo.message_level = DEBUG2;
853  ivinfo.estimated_count = pvs->shared->estimated_count;
854  ivinfo.num_heap_tuples = pvs->shared->reltuples;
855  ivinfo.strategy = pvs->bstrategy;
856 
857  /* Update error traceback information */
858  pvs->indname = pstrdup(RelationGetRelationName(indrel));
859  pvs->status = indstats->status;
860 
861  switch (indstats->status)
862  {
864  istat_res = vac_bulkdel_one_index(&ivinfo, istat, pvs->dead_items);
865  break;
867  istat_res = vac_cleanup_one_index(&ivinfo, istat);
868  break;
869  default:
870  elog(ERROR, "unexpected parallel vacuum index status %d for index \"%s\"",
871  indstats->status,
872  RelationGetRelationName(indrel));
873  }
874 
875  /*
876  * Copy the index bulk-deletion result returned from ambulkdelete and
877  * amvacuumcleanup to the DSM segment if it's the first cycle because they
878  * allocate locally and it's possible that an index will be vacuumed by a
879  * different vacuum process the next cycle. Copying the result normally
880  * happens only the first time an index is vacuumed. For any additional
881  * vacuum pass, we directly point to the result on the DSM segment and
882  * pass it to vacuum index APIs so that workers can update it directly.
883  *
884  * Since all vacuum workers write the bulk-deletion result at different
885  * slots we can write them without locking.
886  */
887  if (!indstats->istat_updated && istat_res != NULL)
888  {
889  memcpy(&(indstats->istat), istat_res, sizeof(IndexBulkDeleteResult));
890  indstats->istat_updated = true;
891 
892  /* Free the locally-allocated bulk-deletion result */
893  pfree(istat_res);
894  }
895 
896  /*
897  * Update the status to completed. No need to lock here since each worker
898  * touches different indexes.
899  */
901 
902  /* Reset error traceback information */
904  pfree(pvs->indname);
905  pvs->indname = NULL;
906 
907  /*
908  * Call the parallel variant of pgstat_progress_incr_param so workers can
909  * report progress of index vacuum to the leader.
910  */
912 }
void pgstat_progress_parallel_incr_param(int index, int64 incr)
#define DEBUG2
Definition: elog.h:29
#define PROGRESS_VACUUM_INDEXES_PROCESSED
Definition: progress.h:29
Relation index
Definition: genam.h:46
double num_heap_tuples
Definition: genam.h:52
bool analyze_only
Definition: genam.h:48
BufferAccessStrategy strategy
Definition: genam.h:53
Relation heaprel
Definition: genam.h:47
bool report_progress
Definition: genam.h:49
int message_level
Definition: genam.h:51
bool estimated_count
Definition: genam.h:50
IndexBulkDeleteResult * vac_bulkdel_one_index(IndexVacuumInfo *ivinfo, IndexBulkDeleteResult *istat, VacDeadItems *dead_items)
Definition: vacuum.c:2491
IndexBulkDeleteResult * vac_cleanup_one_index(IndexVacuumInfo *ivinfo, IndexBulkDeleteResult *istat)
Definition: vacuum.c:2512

References IndexVacuumInfo::analyze_only, ParallelVacuumState::bstrategy, ParallelVacuumState::dead_items, DEBUG2, elog, ERROR, PVShared::estimated_count, IndexVacuumInfo::estimated_count, ParallelVacuumState::heaprel, IndexVacuumInfo::heaprel, IndexVacuumInfo::index, ParallelVacuumState::indname, PVIndStats::istat, PVIndStats::istat_updated, IndexVacuumInfo::message_level, IndexVacuumInfo::num_heap_tuples, 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, IndexVacuumInfo::report_progress, ParallelVacuumState::shared, PVIndStats::status, ParallelVacuumState::status, IndexVacuumInfo::strategy, 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 743 of file vacuumparallel.c.

744 {
745  /*
746  * Increment the active worker count if we are able to launch any worker.
747  */
750 
751  /* Loop until all indexes are vacuumed */
752  for (;;)
753  {
754  int idx;
755  PVIndStats *indstats;
756 
757  /* Get an index number to process */
758  idx = pg_atomic_fetch_add_u32(&(pvs->shared->idx), 1);
759 
760  /* Done for all indexes? */
761  if (idx >= pvs->nindexes)
762  break;
763 
764  indstats = &(pvs->indstats[idx]);
765 
766  /*
767  * Skip vacuuming index that is unsafe for workers or has an
768  * unsuitable target for parallel index vacuum (this is vacuumed in
769  * parallel_vacuum_process_unsafe_indexes() by the leader).
770  */
771  if (!indstats->parallel_workers_can_process)
772  continue;
773 
774  /* Do vacuum or cleanup of the index */
775  parallel_vacuum_process_one_index(pvs, pvs->indrels[idx], indstats);
776  }
777 
778  /*
779  * We have completed the index vacuum so decrement the active worker
780  * count.
781  */
784 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
static uint32 pg_atomic_sub_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:434
static uint32 pg_atomic_fetch_add_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.h:361
static uint32 pg_atomic_add_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.h:419
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 797 of file vacuumparallel.c.

798 {
800 
801  /*
802  * Increment the active worker count if we are able to launch any worker.
803  */
806 
807  for (int i = 0; i < pvs->nindexes; i++)
808  {
809  PVIndStats *indstats = &(pvs->indstats[i]);
810 
811  /* Skip, indexes that are safe for workers */
812  if (indstats->parallel_workers_can_process)
813  continue;
814 
815  /* Do vacuum or cleanup of the index */
816  parallel_vacuum_process_one_index(pvs, pvs->indrels[i], indstats);
817  }
818 
819  /*
820  * We have completed the index vacuum so decrement the active worker
821  * count.
822  */
825 }

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().