liveBook · Manning

Index-only scan

As explained in Just Use Postgres!

An index-only scan satisfies a query from index contents without fetching the corresponding table rows when the required data is available in the index.

An index-only scan answers a query entirely from index contents, avoiding table-row access when the index contains every required value. PostgreSQL reports this plan node with Heap Fetches: 0. section 4.5.1

Counting scores without table access

After creating idx_score on score, EXPLAIN reports Index Only Scan using idx_score for the score range. The query finds 970 rows in 0.350 ms instead of 1.753 ms with a sequential scan.

Covering a leaderboard filter

A covering index on region, score, and win_count with username included supplies every value for the filtered and ordered query, reducing execution time to 0.602 ms.

                               QUERY PLAN
------------------------------------------------------------------------
 Index Only Scan using idx_composite_covering_index on player_stats 
 ➥  (actual time=0.076..0.497 rows=1580 loops=1)
   Index Cond: ((region = 'NA'::text) AND (score > 5000) 
 ➥  AND (win_count > 10))
   Heap Fetches: 0
 Planning Time: 0.320 ms
 Execution Time: 0.602 ms

Inspect the complete covering-index plan, including its predicates, timing, and Heap Fetches value. section 4.7

How Just Use Postgres! covers it

How deep it goes
Explains execution-plan evidence and covering-index construction, but defers broader edge cases and administration.
Examples
Uses score-range counts and a region-filtered player leaderboard to compare sequential, bitmap, and index-only plans.
What it assumes
Assumes basic SQL queries and indexes while demonstrating index creation, EXPLAIN, and ANALYZE usage.
What you can do after
Diagnose/debug with EXPLAIN ANALYZE by recognizing index-only plans and adding INCLUDE columns for covered queries.
How it connects
Connects B-tree indexes, covering indexes, sequential scans, bitmap index scans, and EXPLAIN ANALYZE.

Also explained in

Compare how 2 books explain index-only scan →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Just Use Postgres!. The section links point to the parts of the book it draws on.