Re: index prefetching

From: Peter Geoghegan <pg(at)bowt(dot)ie>
To: Andres Freund <andres(at)anarazel(dot)de>
Cc: Tomas Vondra <tomas(at)vondra(dot)me>, Alexandre Felipe <o(dot)alexandre(dot)felipe(at)gmail(dot)com>, Thomas Munro <thomas(dot)munro(at)gmail(dot)com>, Nazir Bilal Yavuz <byavuz81(at)gmail(dot)com>, Robert Haas <robertmhaas(at)gmail(dot)com>, Melanie Plageman <melanieplageman(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Georgios <gkokolatos(at)protonmail(dot)com>, Konstantin Knizhnik <knizhnik(at)garret(dot)ru>, Dilip Kumar <dilipbalaut(at)gmail(dot)com>
Subject: Re: index prefetching
Date: 2026-03-31 17:39:30
Message-ID: CAH2-WzkFRoTjD9T8ykYDzOMxzGiWFqcAkbK8B=HjfpoMdM4E8A@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, Mar 27, 2026 at 8:35 PM Peter Geoghegan <pg(at)bowt(dot)ie> wrote:
> Attached is v18, which addresses the tricky issues I skipped in v17.

v19 is attached. It contains a few notable improvements over v18:

* The first commit (the one that simply moves code into the new
heapam_indexscan.c file) now moves heap_hot_search_buffer over there
as well.

Andres suggested this at one point, and I believe it wins us a small
but significant performance benefit.

> * The "Add UnlockBufferGetLSN utility function" commit now uses an
> atomic operation (in the common case where it actually drops the index
> page's pin), just like UnlockReleaseBuffer itself following Andres'
> commit f39cb8c011062d65e146c1e9d1aae221e96d8320 from today.

* v19 removes the "Add UnlockBufferGetLSN utility function" patch entirely.

Following Andres' commit f39cb8c0, this no longer seemed to help
performance (compared to just calling BufferGetLSNAtomic and
UnlockReleaseBuffer directly), so I removed it.

> I believe I have now acted on all of Andres' feedback, with one minor
> exception: Andres disliked how we use 'if (scan->xs_heapfetch)' to
> determine whether to use the scan's batch cache (we don't want to use
> it at all if the scan is ending). I just ran out of steam, so this
> version doesn't address the problem.

* v19 fixes this by adding a new helper function. This allows us to
pass down the required context (whether the scan is ending) to inform
the relevant batch recycling code whether it really should place its
caller's batch in the scan's cache (or whether it should just pfree it
right away).

As in previous versions, I'm including several extra read stream
patches to address various problems in that area. These problems, tied
to how index prefetching stresses the read stream that are new and not
encountered by existing read stream users. Andres wrote all of these
patches. Those aspects of the patch should be discussed on a new
thread dedicated to index prefetching-related read stream
enhancements: https://postgr.es/m/f3xxfrkafjxpyqxywcxricxgyizjirfceychyxsgn7bwjp5eda@kwbduhy7tfmu

(Though I'm also carrying some extra bug fix patches here, not
included on that other thread, to address preexisting read stream
bugs, albeit bugs highly unlikely to affect current read stream
users.)

--
Peter Geoghegan

Attachment Content-Type Size
v19-0014-read_stream-Only-increase-distance-when-waiting-.patch application/octet-stream 4.2 KB
v19-0017-aio-Fix-pgaio_io_wait-for-staged-IOs-B.patch application/octet-stream 6.3 KB
v19-0015-Hacky-implementation-of-making-read_stream_reset.patch application/octet-stream 5.1 KB
v19-0016-WIP-read-stream-Split-decision-about-look-ahead-.patch application/octet-stream 14.8 KB
v19-0018-WIP-aio-bufmgr-Fix-race-condition-leading-to-dea.patch application/octet-stream 3.1 KB
v19-0009-aio-io_uring-Allow-IO-methods-to-check-if-IO-com.patch application/octet-stream 5.1 KB
v19-0013-aio-io_uring-Trigger-async-processing-for-large-.patch application/octet-stream 7.2 KB
v19-0010-bufmgr-Return-whether-WaitReadBuffers-needed-to-.patch application/octet-stream 3.0 KB
v19-0012-read_stream-Prevent-distance-from-decaying-too-q.patch application/octet-stream 5.0 KB
v19-0011-read_stream-Issue-IO-synchronously-while-in-fast.patch application/octet-stream 2.7 KB
v19-0008-Make-hash-index-AM-use-amgetbatch-interface.patch application/octet-stream 47.3 KB
v19-0006-heapam-Optimize-pin-transfers-during-index-scans.patch application/octet-stream 6.6 KB
v19-0007-heapam-Add-index-scan-I-O-prefetching.patch application/octet-stream 46.8 KB
v19-0004-heapam-Keep-buffer-pins-across-index-rescans.patch application/octet-stream 3.7 KB
v19-0005-Add-interfaces-that-enable-index-prefetching.patch application/octet-stream 236.6 KB
v19-0001-Move-heapam_handler.c-index-scan-code-to-new-fil.patch application/octet-stream 21.2 KB
v19-0002-Add-slot-based-table-AM-index-scan-interface.patch application/octet-stream 59.6 KB
v19-0003-heapam-Track-heap-block-in-IndexFetchHeapData.patch application/octet-stream 4.8 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2026-03-31 17:41:18 Re: EXPLAIN: showing ReadStream / prefetch stats
Previous Message Srinath Reddy Sadipiralla 2026-03-31 17:37:17 Re: Adding REPACK [concurrently]