liveBook · Manning

Index scan

As explained in Just Use Postgres!

An index scan is a database access strategy that retrieves matching table rows through an index rather than examining the entire table.

An index scan retrieves matching rows through an index instead of examining every table row. PostgreSQL exposes the chosen access method through EXPLAIN, making it possible to verify whether a query uses an index or falls back to a sequential scan. section 4.4

Use execution plans to read an index scan in context:

  • An Index Cond identifies the indexed predicate used to locate rows, such as player_id = 250. section 4.4
  • A B-tree index ordered by score can scan the highest values first, producing a leaderboard efficiently with ORDER BY score DESC LIMIT 5. section 4.5.1
  • Composite indexes depend on column order: an index on (recorded_at, watch_id) supports recorded_at alone or with watch_id, but traditionally not watch_id alone. section 9.7.1
  • Partition-local indexes can combine with partition selection, reducing a time-series query to the relevant partition before scanning indexed rows. section 9.7.1

Verify a primary-key index scan

Run EXPLAIN on a dashboard lookup: SELECT username, level, score FROM game.player_stats WHERE player_id = 250; The plan reports “Index Scan using player_stats_pkey” with “Index Cond: (player_id = 250).” EXPLAIN ANALYZE adds observed timing, while EXPLAIN (analyze, buffers on) also reports buffer activity. section 4.4

                          QUERY PLAN
--------------------------------------------------------------
 Index Scan using player_stats_pkey on player_stats  
 ➥  (cost=0.29..8.30 rows=1 width=18)
   Index Cond: (player_id = 250)
(2 rows)

Inspect the complete execution plan for the primary-key lookup and compare its index condition with the query predicate. section 4.4

How Just Use Postgres! covers it

How deep it goes
Explains execution-plan evidence, B-tree ordering, composite-index constraints, and timing, but does not develop planner internals.
Examples
Uses player lookups, score ranges, leaderboards, and partitioned heart-rate aggregation queries.
What you can do after
Diagnose query plans with EXPLAIN and confirm whether predicates use primary-key, B-tree, composite, or partition-local indexes.
How it connects
Connects index scans with EXPLAIN, B-tree indexes, composite indexes, sequential scans, partitioning, and index-only scans.

Also explained in

Compare how 3 books explain index 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.