Sub topic 11
Troubleshooting: When the Planner Does Not Pick an Index-Only Scan
Assume you know basic SELECT, WHERE, CREATE INDEX, B-tree indexes, and simple EXPLAIN plans. This chapter uses the orders table from the earlier examples.
An index-only scan can answer a query from an index without reading the table heap. PostgreSQL chooses this plan only when it expects the index to be cheaper than other choices and when the index contains every value the query needs.
Check the query and index first
A covering index contains the columns needed by a query. Search columns normally belong in the index key. Columns returned by the query but not used for searching can be payload columns listed with INCLUDE.
CREATE INDEX orders_customer_total_idx
ON orders (customer_id) INCLUDE (total);
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, total
FROM orders
WHERE customer_id = 42;This query can use an index-only scan because both selected columns are available in the index. A query that selects note cannot use this index alone:
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, total, note
FROM orders
WHERE customer_id = 42;PostgreSQL must visit the heap for note, so it may choose an ordinary Index Scan or a Bitmap Heap Scan.
The query must also match a partial index predicate. For example, this index only covers new orders:
CREATE INDEX orders_new_customer_idx
ON orders (customer_id) INCLUDE (total)
WHERE status = 'new';
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, total
FROM orders
WHERE status = 'new' AND customer_id = 42;Without status = 'new' in the query, PostgreSQL cannot assume that the partial index contains all matching rows.
The visibility map can force heap visits
PostgreSQL uses multiversion concurrency control, or MVCC, to keep multiple row versions while transactions use the table. An index entry does not contain enough information to prove that its row version is visible to the current transaction.
The visibility map records whether every row on a table page is known to be visible. If a page is marked all-visible, an index-only scan can trust the index entry and skip that heap page. If it is not marked all-visible, PostgreSQL checks the heap.
flowchart TD
A[Query uses only indexed columns] --> B{Page is all-visible?}
B -->|Yes| C[Return values from index]
B -->|No| D[Visit heap to check visibility]
D --> E[Return values after visibility check]Read `EXPLAIN` carefully
Use ANALYZE to execute the query and report actual behavior. Use BUFFERS to show buffer activity.
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, total
FROM orders
WHERE customer_id = 42;Look for these signs:
Index Only Scanmeans PostgreSQL selected the scan type.Heap Fetches: 0means no heap visits were needed.- A large
Heap Fetchesvalue means the scan often needed visibility checks. Seq Scanmeans PostgreSQL estimated that reading the table was cheaper.Bitmap Heap Scanmeans PostgreSQL gathered matching index entries, then read heap pages in batches.
Run VACUUM (ANALYZE) after a test load or after substantial changes:
VACUUM (ANALYZE) orders;
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, total
FROM orders
WHERE customer_id = 42;VACUUM can mark eligible pages all-visible. ANALYZE refreshes the statistics used by the planner. Neither command guarantees an index-only scan. The planner still compares estimated costs.
Why the planner prefers another plan
The planner may reject an index-only scan for several valid reasons:
- The condition matches a large percentage of the table, making a sequential scan cheaper.
- The table is small enough that reading it directly costs little.
- Statistics are stale or inaccurate.
- The query needs a column absent from the index.
- Many pages are not all-visible, so heap checks reduce the benefit.
- The index access method does not support index-only scans for the operation.
- The estimated random I/O cost is high compared with a sequential read.
You can inspect the estimate without running the query:
EXPLAIN
SELECT customer_id, total
FROM orders
WHERE customer_id = 42;For diagnosis only, temporarily disabling sequential scans can reveal whether an index plan is possible:
SET LOCAL enable_seqscan = off;
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, total
FROM orders
WHERE customer_id = 42;Do not leave this setting enabled as a permanent fix. It changes planner behavior globally for the session and can make other queries slower.
A practical troubleshooting order
- Confirm that the index contains every selected and filtered column.
- Check whether a partial-index predicate appears in the query.
- Run
EXPLAIN (ANALYZE, BUFFERS). - Check the scan type and
Heap Fetches. - Run
VACUUM (ANALYZE)when appropriate and test again. - Compare estimated row counts with actual row counts.
- Decide whether the read benefit justifies the index's storage and write cost.
Exercise
An Index Only Scan reports Heap Fetches: 18,000. Does that mean the index was not used, and what should you try first?
Show answer
The index was used, but PostgreSQL had to visit the heap for many visibility checks. Check whether recent writes cleared all-visible bits, then run VACUUM (ANALYZE) and measure the plan again.