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

Check for invalid down link while prefetching B-Tree leave pages for index-only scan #535

Merged
merged 6 commits into from
Dec 13, 2024
Merged
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
40 changes: 27 additions & 13 deletions src/backend/access/nbtree/nbtsearch.c
Original file line number Diff line number Diff line change
Expand Up @@ -854,9 +854,8 @@ _bt_compare(Relation rel,

/*
* _bt_read_parent_for_prefetch - read parent page and extract references to children for prefetch.
* This functions returns offset of first item.
*/
static int
static void
_bt_read_parent_for_prefetch(IndexScanDesc scan, BlockNumber parent, ScanDirection dir)
{
Relation rel = scan->indexRelation;
Expand Down Expand Up @@ -886,6 +885,7 @@ _bt_read_parent_for_prefetch(IndexScanDesc scan, BlockNumber parent, ScanDirecti
so->next_parent = opaque->btpo_next;
if (so->next_parent == P_NONE)
next_parent_prefetch_index = -1;

for (i = 0, j = 0; i < n_child; i++)
{
ItemId itemid = PageGetItemId(page, offnum + i);
Expand All @@ -900,6 +900,7 @@ _bt_read_parent_for_prefetch(IndexScanDesc scan, BlockNumber parent, ScanDirecti
so->next_parent = opaque->btpo_prev;
if (so->next_parent == P_NONE)
next_parent_prefetch_index = -1;

for (i = 0, j = 0; i < n_child; i++)
{
ItemId itemid = PageGetItemId(page, offnum + n_child - i - 1);
Expand All @@ -912,7 +913,6 @@ _bt_read_parent_for_prefetch(IndexScanDesc scan, BlockNumber parent, ScanDirecti
so->n_prefetch_blocks = j;
so->last_prefetch_index = 0;
_bt_relbuf(rel, buf);
return offnum;
}

/*
Expand Down Expand Up @@ -1478,16 +1478,30 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
/* Start prefetching for index only scan */
if (so->prefetch_maximum > 0 && stack != NULL && scan->xs_want_itup) /* index only scan */
{
int first_offset = _bt_read_parent_for_prefetch(scan, stack->bts_blkno, dir);
int skip = ScanDirectionIsForward(dir)
? stack->bts_offset - first_offset
: first_offset + so->n_prefetch_blocks - 1 - stack->bts_offset;
Assert(so->n_prefetch_blocks >= skip);
so->current_prefetch_distance = INCREASE_PREFETCH_DISTANCE_STEP;
so->n_prefetch_requests = Min(so->current_prefetch_distance, so->n_prefetch_blocks - skip);
so->last_prefetch_index = skip + so->n_prefetch_requests;
for (int j = skip; j < so->last_prefetch_index; j++)
PrefetchBuffer(rel, MAIN_FORKNUM, so->prefetch_blocks[j]);
BlockNumber leaf = BufferGetBlockNumber(buf);

_bt_read_parent_for_prefetch(scan, stack->bts_blkno, dir);

/*
* We can not use stack->bts_offset because once parent page is unlocked, it can be updated and state captured by
* by _bt_read_parent_for_prefetch may not match with _bt_search
*/

for (int j = 0; j < so->n_prefetch_blocks; j++)
{
if (so->prefetch_blocks[j] == leaf)
{
so->current_prefetch_distance = INCREASE_PREFETCH_DISTANCE_STEP;
so->n_prefetch_requests = Min(so->current_prefetch_distance, so->n_prefetch_blocks - j);
so->last_prefetch_index = j + so->n_prefetch_requests;

do {
PrefetchBuffer(rel, MAIN_FORKNUM, so->prefetch_blocks[j]);
} while (++j < so->last_prefetch_index);

break;
}
}
}

/* don't need to keep the stack around... */
Expand Down