Re: index prefetching
Peter Geoghegan <pg@bowt.ie>
Commits
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
aio: io_uring: Trigger async processing for large IOs
- a9ee66881744 19 (unreleased) landed
-
read stream: Split decision about look ahead for AIO and combining
- 8ca147d582a5 19 (unreleased) landed
-
read_stream: Only increase read-ahead distance when waiting for IO
- f63ca3379025 19 (unreleased) landed
-
read_stream: Prevent distance from decaying too quickly
- 6e36930f9aaf 19 (unreleased) landed
-
Reduce ExecSeqScan* code size using pg_assume()
- b227b0bb4e03 19 (unreleased) cited
-
Fix rare bug in read_stream.c's split IO handling.
- b421223172a2 19 (unreleased) cited
-
Fix multiranges to behave more like dependent types.
- 3e8235ba4f9c 17.0 cited
-
Add EXPLAIN (MEMORY) to report planner memory consumption
- 5de890e3610d 17.0 cited
-
Optimize nbtree backward scan boundary cases.
- c9c0589fda0e 17.0 cited
-
Increment xactCompletionCount during subtransaction abort.
- 90c885cdab8b 14.0 cited
-
Add nbtree Valgrind buffer lock checks.
- 4a70f829d86c 14.0 cited
-
Add nbtree high key "continuescan" optimization.
- 29b64d1de7c7 12.0 cited
-
Reduce pinning and buffer content locking for btree scans.
- 2ed5b87f96d4 9.5.0 cited
-
Teach btree to handle ScalarArrayOpExpr quals natively.
- 9e8da0f75731 9.2.0 cited
On Tue, Aug 12, 2025 at 7:10 PM Tomas Vondra <tomas@vondra.me> wrote: > Actually, this might be a consequence of how backwards scans work (at > least in btree). I logged the block in index_scan_stream_read_next, and > this is what I see in the forward scan (at the beginning): Just to be clear: you did disable deduplication and then reindex, right? You're accounting for the known issue with posting list TIDs returning TIDs in the wrong order, relative to the scan direction (when the scan direction is backwards)? It won't be necessary to do this once I commit my patch that fixes the issue directly, on the nbtree side, but for now deduplication messes things up here. And so for now you have to work around it. > But with the backwards scan we apparently scan the values backwards, but > then the blocks for each value are accessed in forward direction. So we > do a couple blocks "forward" and then jump to the preceding value - but > that's a couple blocks *back*. And that breaks the lastBlock check. I don't think that this should be happening. The read stream ought to be seeing blocks in exactly the same order as everything else. > I believe this applies both to master and the prefetching, except that > master doesn't have read stream - so it only does sync I/O. In what sense is it an issue on master? On master, we simply access the TIDs in whatever order amgettuple returns TIDs in. That should always be scan order/index key space order, where heap TID counts as a tie-breaker/affects the key space in the presence of duplicates (at least once that issue with posting lists is fixed, or once deduplication has been disabled in a way that leaves no posting list TIDs around via a reindex). It is certainly not surprising that master does poorly on backwards scans. And it isn't all that surprising that master does worse on backwards scans when direct I/O is in use (per the explanation Andres offered just now). But master should nevertheless always read the TIDs in whatever order it gets them from amgettuple in. It sounds like amgetbatch doesn't really behave analogously to master here, at least with backwards scans. It sounds like you're saying that we *won't* feed TIDs heap block numbers to the read stream in exactly scan order (when we happen to be scanning backwards) -- which seems wrong to me. As you pointed out, a forwards scan of a DESC column index should feed heap blocks to the read stream in a way that is very similar to an equivalent backwards scan of a similar ASC column on the same table. There might be some very minor differences, due to differences in the precise leaf page boundaries among each of the indexes. But that should hardly be noticeable at all. > Could that hide the extra buffer accesses, somehow? I think that you meant to ask about *missing* buffer hits with the patch, for the forwards scan. That doesn't agree with the backwards scan with the patch, nor does it agree with master (with either the forwards or backwards scan). Note that the heap accesses themselves appear to have sane/consistent numbers, since we always see "read=49933" as expected for those, for all 4 query executions that I showed. The "missing buffer hits" issue seems like an issue with the instrumentation itself. Possibly one that is totally unrelated to everything else we're discussing. -- Peter Geoghegan