From 72c89129e058a8fbb97b7a8bf807de73070c6b9a Mon Sep 17 00:00:00 2001 From: Konstantin Knizhnik Date: Mon, 25 Nov 2024 08:56:19 +0200 Subject: [PATCH] Replace assert which doesn't take in account presence of invalid downlinks --- src/backend/access/nbtree/nbtsearch.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/backend/access/nbtree/nbtsearch.c b/src/backend/access/nbtree/nbtsearch.c index ba747147945..735e61ab7d7 100644 --- a/src/backend/access/nbtree/nbtsearch.c +++ b/src/backend/access/nbtree/nbtsearch.c @@ -1478,12 +1478,15 @@ _bt_first(IndexScanDesc scan, ScanDirection 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 i = skip; i < so->last_prefetch_index; i++) - PrefetchBuffer(rel, MAIN_FORKNUM, so->prefetch_blocks[i]); + /* n_prefetch_blocks can be smaller than skip because of skipped invalid downlinks */ + if (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 i = skip; i < so->last_prefetch_index; i++) + PrefetchBuffer(rel, MAIN_FORKNUM, so->prefetch_blocks[i]); + } } /* don't need to keep the stack around... */