Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PG15: Prefetch cleanup: index bulkdelete prefetching #246

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion contrib/pg_prewarm/pg_prewarm.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ pg_prewarm(PG_FUNCTION_ARGS)
for (block = first_block; block <= last_block; ++block)
{
Buffer buf;
int prefetch_stop = block + Min(last_block - block + 1, io_concurrency);
BlockNumber prefetch_stop = block + Min(last_block - block + 1,
io_concurrency);
CHECK_FOR_INTERRUPTS();
while (prefetch_block < prefetch_stop)
{
Expand Down
20 changes: 20 additions & 0 deletions src/backend/access/gist/gistvacuum.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
#include "utils/memutils.h"
#include "utils/spccache.h"

/* Working state needed by gistbulkdelete */
typedef struct
Expand Down Expand Up @@ -130,8 +131,14 @@ gistvacuumscan(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
BlockNumber num_pages;
bool needLock;
BlockNumber blkno;
BlockNumber prefetch_blkno;
int io_concurrency;
MemoryContext oldctx;

io_concurrency = get_tablespace_maintenance_io_concurrency(
rel->rd_rel->reltablespace
);

/*
* Reset fields that track information about the entire index now. This
* avoids double-counting in the case where a single VACUUM command
Expand Down Expand Up @@ -209,6 +216,7 @@ gistvacuumscan(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
needLock = !RELATION_IS_LOCAL(rel);

blkno = GIST_ROOT_BLKNO;
prefetch_blkno = blkno;
for (;;)
{
/* Get the current relation length */
Expand All @@ -221,9 +229,21 @@ gistvacuumscan(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
/* Quit if we've scanned the whole relation */
if (blkno >= num_pages)
break;

if (prefetch_blkno < blkno)
prefetch_blkno = blkno;
for (; prefetch_blkno < num_pages &&
prefetch_blkno < blkno + io_concurrency; prefetch_blkno++)
PrefetchBuffer(rel, MAIN_FORKNUM, prefetch_blkno);

/* Iterate over pages, then loop back to recheck length */
for (; blkno < num_pages; blkno++)
{
if (io_concurrency > 0 && prefetch_blkno < num_pages)
PrefetchBuffer(rel, MAIN_FORKNUM, prefetch_blkno++);

gistvacuumpage(&vstate, blkno, blkno);
}
}

/*
Expand Down
16 changes: 16 additions & 0 deletions src/backend/access/hash/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "utils/builtins.h"
#include "utils/index_selfuncs.h"
#include "utils/rel.h"
#include "utils/spccache.h"

/* Working state for hashbuild and its callback */
typedef struct
Expand Down Expand Up @@ -466,13 +467,17 @@ hashbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
Bucket orig_maxbucket;
Bucket cur_maxbucket;
Bucket cur_bucket;
Bucket prf_bucket;
Buffer metabuf = InvalidBuffer;
HashMetaPage metap;
HashMetaPage cachedmetap;
int io_concurrency;

tuples_removed = 0;
num_index_tuples = 0;

io_concurrency = get_tablespace_maintenance_io_concurrency(rel->rd_rel->reltablespace);

/*
* We need a copy of the metapage so that we can use its hashm_spares[]
* values to compute bucket page addresses, but a cached copy should be
Expand All @@ -487,9 +492,14 @@ hashbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,

/* Scan the buckets that we know exist */
cur_bucket = 0;
prf_bucket = cur_bucket;
cur_maxbucket = orig_maxbucket;

loop_top:
for (; prf_bucket <= cur_maxbucket &&
prf_bucket < cur_bucket + io_concurrency; prf_bucket++)
PrefetchBuffer(rel, MAIN_FORKNUM, BUCKET_TO_BLKNO(cachedmetap, prf_bucket));

while (cur_bucket <= cur_maxbucket)
{
BlockNumber bucket_blkno;
Expand All @@ -500,6 +510,12 @@ hashbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
Page page;
bool split_cleanup = false;

if (io_concurrency > 0 && prf_bucket <= cur_maxbucket)
{
PrefetchBuffer(rel, MAIN_FORKNUM, BUCKET_TO_BLKNO(cachedmetap, prf_bucket));
prf_bucket++;
}

/* Get address of bucket's start page */
bucket_blkno = BUCKET_TO_BLKNO(cachedmetap, cur_bucket);

Expand Down
25 changes: 17 additions & 8 deletions src/backend/access/heap/vacuumlazy.c
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,8 @@ lazy_scan_heap(LVRelState *vacrel)
*/
visibilitymap_pin(vacrel->rel, blkno, &vmbuffer);

if (vacrel->io_concurrency > 0) {
if (vacrel->io_concurrency > 0)
{
/*
* Prefetch io_concurrency blocks ahead
*/
Expand Down Expand Up @@ -2464,32 +2465,40 @@ lazy_vacuum_heap_rel(LVRelState *vacrel)

tblk = ItemPointerGetBlockNumber(&vacrel->dead_items->items[index]);

if (vacrel->io_concurrency > 0) {
if (vacrel->io_concurrency > 0)
{
/*
* If we're just starting out, prefetch N consecutive blocks.
* If not, only the next 1 block
*/
if (pindex == 0) {
if (pindex == 0)
{
int prefetch_budget = Min(vacrel->dead_items->num_items,
Min(vacrel->rel_pages,
vacrel->io_concurrency));
BlockNumber prev_prefetch = ItemPointerGetBlockNumber(&vacrel->dead_items->items[pindex]);
PrefetchBuffer(vacrel->rel, MAIN_FORKNUM, prev_prefetch);

while (++pindex < vacrel->dead_items->num_items &&
prefetch_budget > 0) {
prefetch_budget > 0)
{
ItemPointer ptr = &vacrel->dead_items->items[pindex];
if (ItemPointerGetBlockNumber(ptr) != prev_prefetch) {
if (ItemPointerGetBlockNumber(ptr) != prev_prefetch)
{
prev_prefetch = ItemPointerGetBlockNumber(ptr);
prefetch_budget -= 1;
PrefetchBuffer(vacrel->rel, MAIN_FORKNUM, prev_prefetch);
}
}
} else if (pindex < vacrel->dead_items->num_items) {
}
else if (pindex < vacrel->dead_items->num_items)
{
BlockNumber previous = ItemPointerGetBlockNumber(&vacrel->dead_items->items[pindex]);
while (++pindex < vacrel->dead_items->num_items) {
while (++pindex < vacrel->dead_items->num_items)
{
BlockNumber toPrefetch = ItemPointerGetBlockNumber(&vacrel->dead_items->items[pindex]);
if (previous != toPrefetch) {
if (previous != toPrefetch)
{
PrefetchBuffer(vacrel->rel, MAIN_FORKNUM, toPrefetch);
break;
}
Expand Down
18 changes: 18 additions & 0 deletions src/backend/access/nbtree/nbtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "utils/builtins.h"
#include "utils/index_selfuncs.h"
#include "utils/memutils.h"
#include "utils/spccache.h"


/*
Expand Down Expand Up @@ -908,6 +909,8 @@ btvacuumscan(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
BTVacState vstate;
BlockNumber num_pages;
BlockNumber scanblkno;
BlockNumber prefetch_blkno;
int io_concurrency;
bool needLock;

/*
Expand Down Expand Up @@ -947,6 +950,9 @@ btvacuumscan(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
vstate.maxbufsize = 0;
vstate.pendingpages = NULL;
vstate.npendingpages = 0;

io_concurrency = get_tablespace_maintenance_io_concurrency(rel->rd_rel->reltablespace);

/* Consider applying _bt_pendingfsm_finalize optimization */
_bt_pendingfsm_init(rel, &vstate, (callback == NULL));

Expand Down Expand Up @@ -975,6 +981,8 @@ btvacuumscan(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
needLock = !RELATION_IS_LOCAL(rel);

scanblkno = BTREE_METAPAGE + 1;
prefetch_blkno = scanblkno;

for (;;)
{
/* Get the current relation length */
Expand All @@ -991,9 +999,19 @@ btvacuumscan(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
/* Quit if we've scanned the whole relation */
if (scanblkno >= num_pages)
break;

if (prefetch_blkno < scanblkno)
prefetch_blkno = scanblkno;
for (; prefetch_blkno < num_pages &&
prefetch_blkno < scanblkno + io_concurrency; prefetch_blkno++)
PrefetchBuffer(rel, MAIN_FORKNUM, prefetch_blkno);

/* Iterate over pages, then loop back to recheck length */
for (; scanblkno < num_pages; scanblkno++)
{
if (io_concurrency > 0 && prefetch_blkno < num_pages)
PrefetchBuffer(rel, MAIN_FORKNUM, prefetch_blkno++);

btvacuumpage(&vstate, scanblkno);
if (info->report_progress)
pgstat_progress_update_param(PROGRESS_SCAN_BLOCKS_DONE,
Expand Down
22 changes: 21 additions & 1 deletion src/backend/access/spgist/spgvacuum.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
#include "utils/snapmgr.h"
#include "utils/spccache.h"


/* Entry in pending-list of TIDs we need to revisit */
Expand Down Expand Up @@ -796,7 +797,14 @@ spgvacuumscan(spgBulkDeleteState *bds)
Relation index = bds->info->index;
bool needLock;
BlockNumber num_pages,
blkno;
blkno,
prefetch_blkno;
int io_concurrency;

/* initiate concurrency */
io_concurrency = get_tablespace_maintenance_io_concurrency(
index->rd_rel->reltablespace
);

/* Finish setting up spgBulkDeleteState */
initSpGistState(&bds->spgstate, index);
Expand Down Expand Up @@ -824,6 +832,8 @@ spgvacuumscan(spgBulkDeleteState *bds)
* in btvacuumscan().
*/
blkno = SPGIST_METAPAGE_BLKNO + 1;
prefetch_blkno = blkno;

for (;;)
{
/* Get the current relation length */
Expand All @@ -836,9 +846,19 @@ spgvacuumscan(spgBulkDeleteState *bds)
/* Quit if we've scanned the whole relation */
if (blkno >= num_pages)
break;

if (prefetch_blkno < blkno)
prefetch_blkno = blkno;
for (; prefetch_blkno < num_pages &&
prefetch_blkno < blkno + io_concurrency; prefetch_blkno++)
PrefetchBuffer(index, MAIN_FORKNUM, prefetch_blkno);

/* Iterate over pages, then loop back to recheck length */
for (; blkno < num_pages; blkno++)
{
if (io_concurrency > 0 && prefetch_blkno < num_pages)
PrefetchBuffer(index, MAIN_FORKNUM, prefetch_blkno++);

spgvacuumpage(bds, blkno);
/* empty the pending-list after each page */
if (bds->pendingList != NULL)
Expand Down