Showing "Table Block Fetches" in EXPLAIN ANALYZE for index-only scans

Started by Peter Geogheganabout 1 month ago5 messageshackers
Jump to latest

Attached patch teaches EXPLAIN ANALYZE to show "Table Block Fetches"
instead of "Heap Fetches" when reporting on an index-only scan node. I
believe that users will find this information more useful and
relevant: it describes the exact physical cost of *not* having the
relevant heap pages marked as all-visible in the visibility map,
without bringing tuples into it.

"Table Block Fetches" is exactly the number of times an index-only
scan had to switch to a new heap block. I'm not fussed about the name
we use for this, though I think that avoiding references to heap is
desirable. This information is table AM agnostic, at least in theory.

I lean towards not showing anything about IoS heap tuple fetches in
EXPLAIN ANALYZE, which is how it's done in this V1. But if there's a
consensus in favor of keeping it, I can easily produce a revision that
counts and reports on both things.

This approach became much easier following my recent commit ddce1da5,
which added a new slot-based table AM index scan interface. That
commit put the responsibility for maintaining the scan's "Heap
Fetches" counter into heapam. I suspect that the current approach
might be as much a consequence of how awkward it would have been to
teach nodeIndexonlyscan.c to do things this way as anything else.

--
Peter Geoghegan

Attachments:

t254220_1
v1-0001-Show-block-not-tuple-fetches-in-EXPLAIN-ANALYZE.patchapplication/octet-stream; name=v1-0001-Show-block-not-tuple-fetches-in-EXPLAIN-ANALYZE.patchDownload+76-73
#2Sami Imseih
samimseih.pg@gmail.com
In reply to: Peter Geoghegan (#1)
Re: Showing "Table Block Fetches" in EXPLAIN ANALYZE for index-only scans

Hi,

"Table Block Fetches" is exactly the number of times an index-only
scan had to switch to a new heap block. I'm not fussed about the name
we use for this, though I think that avoiding references to heap is
desirable. This information is table AM agnostic, at least in theory.

I did not look too much into the patch details, but I do think this is a
good idea. For example, there can be a real cost difference between checking
visibility for 20 table tuples spread across 20 table blocks and checking
visibility for 20 table tuples packed onto 1 table block. So having such
additional information seems valuable. Exposing this in pg_stat_*_indexes
would also make sense.

I lean towards not showing anything about IoS heap tuple fetches in
EXPLAIN ANALYZE, which is how it's done in this V1. But if there's a
consensus in favor of keeping it, I can easily produce a revision that
counts and reports on both things.

I think we should keep the equivalent of "Heap Fetches", although I do
think the name itself has always seemed too vague. It is easy to read it
as blocks, or as rows returned, when it is really counting table tuple
visibility checks. "Table Tuple Fetches" would be clear along with the one
you're proposing.

--
Sami Imseih
Amazon Web Services (AWS)

#3Greg Burd
greg@burd.me
In reply to: Sami Imseih (#2)
Re: Showing "Table Block Fetches" in EXPLAIN ANALYZE for index-only scans

On Thu, Oct 15, 2026 at 6:04 PM Sami Imseih <samimseih.pg@gmail.com> wrote:

I think we should keep the equivalent of "Heap Fetches", although I do
think the name itself has always seemed too vague. It is easy to read it
as blocks, or as rows returned, when it is really counting table tuple
visibility checks. "Table Tuple Fetches" would be clear along with the one
you're proposing.

+1 to both: count blocks as Peter's v1 does, and keep a tuple count
beside it.

One case for keeping the tuple count that doesn't depend on heap. In
heap with selective index updates (CF 6968), an index-only scan whose
page isn't all-visible fetches the row and may then drop the entry,
because the HOT chain it walked changed a column that index covers. The
two counters together show that. Many tuple fetches on few blocks, with
fewer rows returned than entries fetched, means stale entries that
VACUUM hasn't reached. Neither number alone says that.

A question about where this goes next, since ddce1da5 put these counters
in the table AM. The in-place AM I'm working on (FLUX, CF 7389)
disables index-only scans because delete-marked entries can carry stale
keys. Its costs are on plain Index Scans: rows rebuilt from UNDO, and
entries rejected after the fetch. Would you be against the same
IndexScanInstrumentation fields being shown on plain Index Scan nodes
too, filled by the table AM, if a later patch wanted that? I'm not
asking for it in this one.

best.

-greg

#4Rui Zhao
zhaorui126@gmail.com
In reply to: Peter Geoghegan (#1)
Re: Showing "Table Block Fetches" in EXPLAIN ANALYZE for index-only scans

Hi Peter,

Thanks for working on this. I agree with Sami and Greg that it would
be useful to show both counts. "Heap Fetches" tells me how often the
scan had to visit the heap to check visibility. Even if every check is
on the same page, I still want to know whether it happened once or a
hundred times. "Table Block Fetches" tells me how many times the scan
had to acquire a heap page for those checks.

I tested a parallel index-only scan with two workers. All 140 rows
were on one heap page. With leader participation off, each worker
fetched that page once, so EXPLAIN reported "Table Block Fetches: 2".
When the leader also scanned rows, the count rose to 3. In both runs
I could see the total, but not how many fetches came from each worker.

EXPLAIN (ANALYZE, VERBOSE) already shows each worker's row count and
buffer usage. Since v1 records block fetches per worker too, could we
show them there, alongside those figures? Without VERBOSE, the node
total would be enough.

For the output code, show_tidbitmap_info() in explain.c prints the node
statistics first and the worker details in a separate loop. We could
follow that layout at the end of show_indexscan_info(), after:

ExplainPropertyUInteger("Index Searches", NULL, nsearches, es);

if (nodeTag(plan) == T_IndexOnlyScan && SharedInfo &&
es->verbose && es->workers_state)
{
for (int i = 0; i < SharedInfo->num_workers; ++i)
{
IndexScanInstrumentation *wi = &SharedInfo->winstrument[i];

if (planstate->worker_instrument->instrument[i].nloops <= 0)
continue;

ExplainOpenWorker(i, es);
ExplainPropertyUInteger("Table Block Fetches", NULL,
wi->ntableblockfetches, es);
ExplainCloseWorker(i, es);
}
}

If we keep just the total, perhaps perform.sgml could mention that
fetching the same heap page in two workers counts as two fetches.

Keeping the tuple count would also preserve an existing check in
horizons.spec. The test inserts two rows and expects "Heap Fetches: 2"
on the first scan. Both rows are on the same heap page, so v1 reports
"Table Block Fetches: 1". I got the same block count when I inserted
just one row. The new check can therefore pass even if the scan visits
only one of the two index entries.

When rebasing v1 onto master, I hit a conflict in show_indexscan_info().
I kept the ioss_Instrument NULL check from a20d50a00e and changed the
field read inside it from ntabletuplefetches to ntableblockfetches.

Regards,
Rui

In reply to: Sami Imseih (#2)
Re: Showing "Table Block Fetches" in EXPLAIN ANALYZE for index-only scans

On Thu, Oct 15, 2026 at 6:04 PM Sami Imseih <samimseih.pg@gmail.com> wrote:

I did not look too much into the patch details, but I do think this is a
good idea. For example, there can be a real cost difference between checking
visibility for 20 table tuples spread across 20 table blocks and checking
visibility for 20 table tuples packed onto 1 table block. So having such
additional information seems valuable.

Right.

Exposing this in pg_stat_*_indexes would also make sense.

Let's leave that for another patch.

I think we should keep the equivalent of "Heap Fetches", although I do
think the name itself has always seemed too vague. It is easy to read it
as blocks, or as rows returned, when it is really counting table tuple
visibility checks. "Table Tuple Fetches" would be clear along with the one
you're proposing.

Attached v2 shows both rows/tuples and blocks on a single line, as
"Table Fetches: rows=N blocks=N" for text format EXPLAIN. For the
non-text formats, we show separate "Table Row Fetches" and "Table
Block Fetches" fields.

--
Peter Geoghegan

Attachments:

t254220_5
v2-0001-EXPLAIN-ANALYZE-Show-index-only-scan-block-fetche.patchapplication/octet-stream; name=v2-0001-EXPLAIN-ANALYZE-Show-index-only-scan-block-fetche.patchDownload+70-45