Skip to content

Commit fd2b00b

Browse files
MasahikoSawadaCommitfest Bot
authored and
Commitfest Bot
committed
Introduces table AM APIs for parallel table vacuuming.
This commit introduces the following new table AM APIs for parallel table vacuuming: - parallel_vacuum_compute_workers - parallel_vacuum_estimate - parallel_vacuum_initialize - parallel_vacuum_initialize_worker - parallel_vacuum_collect_dead_items While parallel_vacuum_compute_workers is required, other new callbacks are optional. There is no code using these new APIs for now. Upcoming parallel vacuum patches utilize these APIs. Reviewed-by: Amit Kapila <[email protected]> Reviewed-by: Hayato Kuroda <[email protected]> Reviewed-by: Peter Smith <[email protected]> Reviewed-by: Tomas Vondra <[email protected]> Reviewed-by: Dilip Kumar <[email protected]> Reviewed-by: Melanie Plageman <[email protected]> Discussion: https://postgr.es/m/CAD21AoAEfCNv-GgaDheDJ+s-p_Lv1H24AiJeNoPGCmZNSwL1YA@mail.gmail.com
1 parent d8555e5 commit fd2b00b

File tree

5 files changed

+168
-1
lines changed

5 files changed

+168
-1
lines changed

src/backend/access/heap/heapam_handler.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -2668,7 +2668,9 @@ static const TableAmRoutine heapam_methods = {
26682668

26692669
.scan_bitmap_next_tuple = heapam_scan_bitmap_next_tuple,
26702670
.scan_sample_next_block = heapam_scan_sample_next_block,
2671-
.scan_sample_next_tuple = heapam_scan_sample_next_tuple
2671+
.scan_sample_next_tuple = heapam_scan_sample_next_tuple,
2672+
2673+
.parallel_vacuum_compute_workers = heap_parallel_vacuum_compute_workers,
26722674
};
26732675

26742676

src/backend/access/heap/vacuumlazy.c

+12
Original file line numberDiff line numberDiff line change
@@ -3756,6 +3756,18 @@ update_relstats_all_indexes(LVRelState *vacrel)
37563756
}
37573757
}
37583758

3759+
/*
3760+
* Compute the number of workers for parallel heap vacuum.
3761+
*
3762+
* Return 0 to disable parallel vacuum.
3763+
*/
3764+
int
3765+
heap_parallel_vacuum_compute_workers(Relation rel, int nworkers_requested,
3766+
void *state)
3767+
{
3768+
return 0;
3769+
}
3770+
37593771
/*
37603772
* Error context callback for errors occurring during vacuum. The error
37613773
* context messages for index phases should match the messages set in parallel

src/backend/access/table/tableamapi.c

+11
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ GetTableAmRoutine(Oid amhandler)
8181
Assert(routine->relation_copy_data != NULL);
8282
Assert(routine->relation_copy_for_cluster != NULL);
8383
Assert(routine->relation_vacuum != NULL);
84+
Assert(routine->parallel_vacuum_compute_workers != NULL);
8485
Assert(routine->scan_analyze_next_block != NULL);
8586
Assert(routine->scan_analyze_next_tuple != NULL);
8687
Assert(routine->index_build_range_scan != NULL);
@@ -94,6 +95,16 @@ GetTableAmRoutine(Oid amhandler)
9495
Assert(routine->scan_sample_next_block != NULL);
9596
Assert(routine->scan_sample_next_tuple != NULL);
9697

98+
/*
99+
* Callbacks for parallel vacuum are also optional (except for
100+
* parallel_vacuum_compute_workers). But one callback implies presence of
101+
* the others.
102+
*/
103+
Assert(((((routine->parallel_vacuum_estimate == NULL) ==
104+
(routine->parallel_vacuum_initialize == NULL)) ==
105+
(routine->parallel_vacuum_initialize_worker == NULL)) ==
106+
(routine->parallel_vacuum_collect_dead_items == NULL)));
107+
97108
return routine;
98109
}
99110

src/include/access/heapam.h

+2
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ extern void log_heap_prune_and_freeze(Relation relation, Buffer buffer,
399399
struct VacuumParams;
400400
extern void heap_vacuum_rel(Relation rel,
401401
struct VacuumParams *params, BufferAccessStrategy bstrategy);
402+
extern int heap_parallel_vacuum_compute_workers(Relation rel, int nworkers_requested,
403+
void *state);
402404

403405
/* in heap/heapam_visibility.c */
404406
extern bool HeapTupleSatisfiesVisibility(HeapTuple htup, Snapshot snapshot,

src/include/access/tableam.h

+140
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ extern PGDLLIMPORT bool synchronize_seqscans;
3535

3636
struct BulkInsertStateData;
3737
struct IndexInfo;
38+
struct ParallelContext;
39+
struct ParallelVacuumState;
40+
struct ParallelWorkerContext;
3841
struct SampleScanState;
3942
struct VacuumParams;
4043
struct ValidateIndexState;
@@ -648,6 +651,81 @@ typedef struct TableAmRoutine
648651
struct VacuumParams *params,
649652
BufferAccessStrategy bstrategy);
650653

654+
/* ------------------------------------------------------------------------
655+
* Callbacks for parallel table vacuum.
656+
* ------------------------------------------------------------------------
657+
*/
658+
659+
/*
660+
* Compute the number of parallel workers for parallel table vacuum. The
661+
* parallel degree for parallel vacuum is further limited by
662+
* max_parallel_maintenance_workers. The function must return 0 to disable
663+
* parallel table vacuum.
664+
*
665+
* 'nworkers_requested' is a >=0 number and the requested number of
666+
* workers. This comes from the PARALLEL option. 0 means to choose the
667+
* parallel degree based on the table AM specific factors such as table
668+
* size.
669+
*/
670+
int (*parallel_vacuum_compute_workers) (Relation rel,
671+
int nworkers_requested,
672+
void *state);
673+
674+
/*
675+
* Estimate the size of shared memory needed for a parallel table vacuum
676+
* of this relation.
677+
*
678+
* Not called if parallel table vacuum is disabled.
679+
*
680+
* Optional callback, but either all other parallel vacuum callbacks need
681+
* to exist, or neither.
682+
*/
683+
void (*parallel_vacuum_estimate) (Relation rel,
684+
struct ParallelContext *pcxt,
685+
int nworkers,
686+
void *state);
687+
688+
/*
689+
* Initialize DSM space for parallel table vacuum.
690+
*
691+
* Not called if parallel table vacuum is disabled.
692+
*
693+
* Optional callback, but either all other parallel vacuum callbacks need
694+
* to exist, or neither.
695+
*/
696+
void (*parallel_vacuum_initialize) (Relation rel,
697+
struct ParallelContext *pctx,
698+
int nworkers,
699+
void *state);
700+
701+
/*
702+
* Initialize AM-specific vacuum state for worker processes.
703+
*
704+
* The state_out is the output parameter so that arbitrary data can be
705+
* passed to the subsequent callback, parallel_vacuum_remove_dead_items.
706+
*
707+
* Not called if parallel table vacuum is disabled.
708+
*
709+
* Optional callback, but either all other parallel vacuum callbacks need
710+
* to exist, or neither.
711+
*/
712+
void (*parallel_vacuum_initialize_worker) (Relation rel,
713+
struct ParallelVacuumState *pvs,
714+
struct ParallelWorkerContext *pwcxt,
715+
void **state_out);
716+
717+
/*
718+
* Execute a parallel scan to collect dead items.
719+
*
720+
* Not called if parallel table vacuum is disabled.
721+
*
722+
* Optional callback, but either all other parallel vacuum callbacks need
723+
* to exist, or neither.
724+
*/
725+
void (*parallel_vacuum_collect_dead_items) (Relation rel,
726+
struct ParallelVacuumState *pvs,
727+
void *state);
728+
651729
/*
652730
* Prepare to analyze block `blockno` of `scan`. The scan has been started
653731
* with table_beginscan_analyze(). See also
@@ -1670,6 +1748,68 @@ table_relation_vacuum(Relation rel, struct VacuumParams *params,
16701748
rel->rd_tableam->relation_vacuum(rel, params, bstrategy);
16711749
}
16721750

1751+
/* ----------------------------------------------------------------------------
1752+
* Parallel vacuum related functions.
1753+
* ----------------------------------------------------------------------------
1754+
*/
1755+
1756+
/*
1757+
* Compute the number of parallel workers for a parallel vacuum scan of this
1758+
* relation.
1759+
*/
1760+
static inline int
1761+
table_parallel_vacuum_compute_workers(Relation rel, int nworkers_requested,
1762+
void *state)
1763+
{
1764+
return rel->rd_tableam->parallel_vacuum_compute_workers(rel,
1765+
nworkers_requested,
1766+
state);
1767+
}
1768+
1769+
/*
1770+
* Estimate the size of shared memory needed for a parallel vacuum scan of this
1771+
* of this relation.
1772+
*/
1773+
static inline void
1774+
table_parallel_vacuum_estimate(Relation rel, struct ParallelContext *pcxt,
1775+
int nworkers, void *state)
1776+
{
1777+
Assert(nworkers > 0);
1778+
rel->rd_tableam->parallel_vacuum_estimate(rel, pcxt, nworkers, state);
1779+
}
1780+
1781+
/*
1782+
* Initialize shared memory area for a parallel vacuum scan of this relation.
1783+
*/
1784+
static inline void
1785+
table_parallel_vacuum_initialize(Relation rel, struct ParallelContext *pcxt,
1786+
int nworkers, void *state)
1787+
{
1788+
Assert(nworkers > 0);
1789+
rel->rd_tableam->parallel_vacuum_initialize(rel, pcxt, nworkers, state);
1790+
}
1791+
1792+
/*
1793+
* Initialize AM-specific vacuum state for worker processes.
1794+
*/
1795+
static inline void
1796+
table_parallel_vacuum_initialize_worker(Relation rel, struct ParallelVacuumState *pvs,
1797+
struct ParallelWorkerContext *pwcxt,
1798+
void **state_out)
1799+
{
1800+
rel->rd_tableam->parallel_vacuum_initialize_worker(rel, pvs, pwcxt, state_out);
1801+
}
1802+
1803+
/*
1804+
* Execute a parallel vacuum scan to collect dead items.
1805+
*/
1806+
static inline void
1807+
table_parallel_vacuum_collect_dead_items(Relation rel, struct ParallelVacuumState *pvs,
1808+
void *state)
1809+
{
1810+
rel->rd_tableam->parallel_vacuum_collect_dead_items(rel, pvs, state);
1811+
}
1812+
16731813
/*
16741814
* Prepare to analyze the next block in the read stream. The scan needs to
16751815
* have been started with table_beginscan_analyze(). Note that this routine

0 commit comments

Comments
 (0)