Sub topic 3
What an Index-Only Scan Is and What It Needs
This chapter assumes you know basic SQL, B-tree indexes, and simple EXPLAIN plans. You should also know that PostgreSQL uses multiversion concurrency control (MVCC), which lets transactions see appropriate versions of rows without blocking every reader.
The basic idea
A normal index scan uses an index to find matching rows, then visits the table heap to read the remaining column values. The heap is PostgreSQL's main table storage.
An index-only scan can return the requested values directly from the index. It avoids most heap visits, which can reduce random I/O for queries that read a small number of rows.
For example, this query needs only customer_id and total:
CREATE INDEX orders_customer_total_idx
ON orders (customer_id) INCLUDE (total);
VACUUM (ANALYZE) orders;
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, total
FROM orders
WHERE customer_id = 42;The index uses customer_id as its search key. It stores total as additional payload. If PostgreSQL can confirm that the matching heap pages are visible to the transaction, it can return both values from the index.
How it differs from other scans
PostgreSQL chooses a scan type based on estimated cost.
| Scan type | Uses the index to find rows | Reads the heap |
|---|---|---|
| Sequential Scan | No | Usually many pages |
| Index Scan | Yes | Yes, for matching rows |
| Bitmap Heap Scan | Yes, through a bitmap | Yes, for matching pages |
| Index Only Scan | Yes | Only when visibility must be checked |
An index-only scan still relates to the heap. The name means that the index contains the requested column values, not that PostgreSQL can always ignore the table.
The two requirements
An index-only scan needs both conditions below.
The index contains every requested value
Every column needed by the query must be available in the index. This includes columns in the SELECT list and sometimes columns needed for filtering or ordering.
A key column can provide both search and returned data. An included column provides returned data but is not part of the index's search ordering.
For example, an index on only customer_id can locate matching rows, but it cannot return total without reading the heap. Adding total with INCLUDE makes the query eligible for an index-only scan.
The index access method must also support index-only scans. PostgreSQL B-tree indexes support them. Other access methods have different capabilities.
PostgreSQL can verify row visibility cheaply
PostgreSQL's MVCC rules mean that an index entry alone does not always prove that its table row is visible to the current transaction. The index does not store all transaction visibility information needed for that decision.
PostgreSQL records visibility information by heap page in a separate structure called the visibility map. When a page is marked all-visible, every row version on that page is visible to all transactions that need to see it. PostgreSQL can then trust the index entry without visiting that page.
If the page is not marked all-visible, PostgreSQL must visit the heap to check visibility. The plan can still be called an index-only scan, but it loses much of its benefit.
Why VACUUM matters
VACUUM updates table and visibility information. After enough rows are inserted or changed, pages can lose their all-visible status. A later VACUUM may mark them all-visible again when their row versions are safe to read.
Updates commonly clear the bit for affected pages. Therefore, a covering index alone does not guarantee fast index-only scans. Table maintenance and the workload's update pattern matter too.
Check the result
In EXPLAIN (ANALYZE), inspect Heap Fetches under an Index Only Scan.
Heap Fetches: 0means the scan answered the query without heap visits.- A small number means some pages required visibility checks.
- A large number means the table's visibility map provides little benefit for this query.
Exercise
An index contains both columns selected by a query, but EXPLAIN (ANALYZE) reports many heap fetches. What is the likely reason?
Show answer
The matching heap pages are not marked all-visible, so PostgreSQL must visit them to check MVCC visibility. Running suitable VACUUM maintenance may reduce the heap fetches.