From 3fc7f8a1d82864c0d23d0d5c50d81a8cf85c5678 Mon Sep 17 00:00:00 2001 From: Satya Narlapuram Date: Wed, 14 Oct 2026 10:36:26 +0000 Subject: [PATCH] xlogprefetcher: record FPW/WILL_INIT blocks in recent-blocks window Extract the recent-block bookkeeping into a small inline helper XLogPrefetcherAddRecent() and call it for the FPW and WILL_INIT short-circuit paths in XLogPrefetcherNextBlock(). Without it, a later reference to the same block was not recognized as recently seen and went through the normal prefetch path. Author: Satya Narlapuram Reviewed-by: Valeriy Degtyarev Reviewed-by: Ethan Mertz --- src/backend/access/transam/xlogprefetcher.c | 33 +++++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/backend/access/transam/xlogprefetcher.c b/src/backend/access/transam/xlogprefetcher.c index dff57642ab4..cf92f821a1b 100644 --- a/src/backend/access/transam/xlogprefetcher.c +++ b/src/backend/access/transam/xlogprefetcher.c @@ -187,6 +187,9 @@ typedef struct XLogPrefetchStats int io_depth; /* Number of I/Os in progress. */ } XLogPrefetchStats; +static inline void XLogPrefetcherAddRecent(XLogPrefetcher *prefetcher, + RelFileLocator rlocator, + BlockNumber blockno); static inline void XLogPrefetcherAddFilter(XLogPrefetcher *prefetcher, RelFileLocator rlocator, BlockNumber blockno, @@ -674,17 +677,24 @@ XLogPrefetcherNextBlock(uintptr_t pgsr_private, XLogRecPtr *lsn) /* * If there is a full page image attached, we won't be reading the - * page, so don't bother trying to prefetch. + * page, so don't bother trying to prefetch. Record this block + * in the recent window so that later references to the same + * block are also skipped. */ if (block->has_image) { + XLogPrefetcherAddRecent(prefetcher, block->rlocator, block->blkno); XLogPrefetchIncrement(&SharedStats->skip_fpw); return LRQ_NEXT_NO_IO; } - /* There is no point in reading a page that will be zeroed. */ + /* + * There is no point in reading a page that will be zeroed. + * Record in the recent window like FPW above. + */ if (block->flags & BKPBLOCK_WILL_INIT) { + XLogPrefetcherAddRecent(prefetcher, block->rlocator, block->blkno); XLogPrefetchIncrement(&SharedStats->skip_init); return LRQ_NEXT_NO_IO; } @@ -711,10 +721,7 @@ XLogPrefetcherNextBlock(uintptr_t pgsr_private, XLogRecPtr *lsn) return LRQ_NEXT_NO_IO; } } - prefetcher->recent_rlocator[prefetcher->recent_idx] = block->rlocator; - prefetcher->recent_block[prefetcher->recent_idx] = block->blkno; - prefetcher->recent_idx = - (prefetcher->recent_idx + 1) % XLOGPREFETCHER_SEQ_WINDOW_SIZE; + XLogPrefetcherAddRecent(prefetcher, block->rlocator, block->blkno); /* * We could try to have a fast path for repeated references to the @@ -854,6 +861,20 @@ pg_stat_get_recovery_prefetch(PG_FUNCTION_ARGS) return (Datum) 0; } +/* + * Record a block in the recently seen blocks, so that the duplicate-check loop + * can suppress a later prefetch of the same block. + */ +static inline void +XLogPrefetcherAddRecent(XLogPrefetcher *prefetcher, RelFileLocator rlocator, + BlockNumber blockno) +{ + prefetcher->recent_rlocator[prefetcher->recent_idx] = rlocator; + prefetcher->recent_block[prefetcher->recent_idx] = blockno; + prefetcher->recent_idx = + (prefetcher->recent_idx + 1) % XLOGPREFETCHER_SEQ_WINDOW_SIZE; +} + /* * Don't prefetch any blocks >= 'blockno' from a given 'rlocator', until 'lsn' * has been replayed. -- 2.43.0