Re: index prefetching
Tomas Vondra <tomas@vondra.me>
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
Attachments
- v2-0001-pgstattuple-analyze-TIDs-on-btree-leaf-pages.patch (text/x-patch) patch v2-0001
On 7/24/25 16:40, Peter Geoghegan wrote: > On Thu, Jul 24, 2025 at 7:19 AM Tomas Vondra <tomas@vondra.me> wrote: >> I got a bit bored yesterday, so I gave this a try and whipped up a patch >> that adds two pgstattuple functins that I think could be useful for >> analyzing index metrics that matter for prefetching. > > This seems quite useful. > > I notice that you're not accounting for posting lists. That'll lead to > miscounts of the number of heap blocks in many cases. I think that > that's worth fixing, even given that this patch is experimental. > Yeah, I forgot about that. Should be fixed in the v2. Admittedly I don't know that much about nbtree internals, so this is mostly copy pasting from verify_nbtree. >> It's trivial to summarize this into a per-index statistic (of course, >> there may be some inaccuracies when the run spans multiple ranges), but >> it also seems useful to be able to look at parts of the index. > > FWIW in my experience, the per-leaf-page "nhtids:nhblks" tends to be > fairly consistent across all leaf pages from a given index. There are > no doubt some exceptions, but they're probably pretty rare. > Yeah, probably. And we'll probably test on such uniform data sets, or at least we we'll start with those. But at some point I'd like to test with some of these "weird" indexes too, if only to test how well the prefetch heuristics adjusts the distance. >> Second, the index is walked sequentially in physical order, from block 0 >> to the last block. But that's not really what the index prefetch sees. >> To make it "more accurate" it'd be better to just scan the leaf pages as >> if during a "full index scan". > > Why not just do it that way to begin with? It wouldn't be complicated > to make the function follow a chain of right sibling links. > I have a very good reason why I didn't do it that way. I was lazy. But v2 should be doing that, I think. > I suggest an interface that takes a block number, and an nblocks int8 > argument that must be >= 1. The function would start from the block > number arg leaf page. If it's not a non-ignorable leaf page, throw an > error. Otherwise, count the number of distinct heap blocks on the leaf > page, and count the number of heap blocks on each additional leaf page > to the right -- until we've counted the heap blocks from nblocks-many > leaf pages (or until we reach the rightmost leaf page). > Yeah, this interface seems useful. I suppose it'll be handy when looking at an index scan, to get stats from the currently loaded batches. In principle you get that from v3 by filtering, but it might be slow on large indexes. I'll try doing that in v3. > I suggest that a P_IGNORE() page shouldn't have its heap blocks > counted, and shouldn't count towards our nblocks tally of leaf pages > whose heap blocks are to be counted. Upon encountering a P_IGNORE() > page, just move to the right without doing anything. Note that the > rightmost page cannot be P_IGNORE(). > I think v2 does all of this. > This scheme will always succeed, no matter the nblocks argument, > provided the initial leaf page is a valid leaf page (and provided the > nblocks arg is >= 1). > > I get that this is just a prototype that might not go anywhere, but > the scheme I've described requires few changes. > Yep, thanks. -- Tomas Vondra