liveBook · Manning

Bitmap heap scan

As explained in Just Use Postgres!

A bitmap heap scan reads table rows identified by a bitmap produced by an index scan and may recheck conditions for lossy pages.

A bitmap heap scan is the second phase of bitmap index access. A preceding Bitmap Index Scan collects matching row or page locations into a bitmap; the heap scan then visits those locations and retrieves the requested table data in bulk. section 4.5.2

Read a bitmap heap scan in an execution plan by checking:

  • The Bitmap Index Scan, which applies the index condition and builds the bitmap instead of fetching table rows immediately. section 4.5.2
  • The Bitmap Heap Scan, which visits the table locations recorded in the bitmap and returns the requested columns. section 4.5.2
  • Recheck Cond, which shows that PostgreSQL evaluates the condition again while examining heap rows, especially when the bitmap contains approximate page locations. section 5.6.2 section 9.7.2
  • Heap Blocks and Rows Removed by Index Recheck, which indicate exact or lossy bitmap behavior and the amount of extra filtering performed. section 5.6.2 section 9.7.2

Finding several champions

For a query using IN, PostgreSQL can build one bitmap from the hash index and then read the matching player rows together:\n\nSELECT username, champion_title, score, level\nFROM game.player_stats\nWHERE champion_title IN ('Airforce Warlord', 'Naval Warlord', 'Land Warlord', 'Cyber Warlord', 'Space Warlord');\n\nThe plan reports Bitmap Heap Scan on player_stats with rows=5 and Heap Blocks: exact=4, followed by Bitmap Index Scan on idx_champion_title. The heap phase also reports Recheck Cond, making the two-stage execution visible.

How Just Use Postgres! covers it

How deep it goes
Explains two-phase execution, exact and lossy bitmaps, condition rechecks, and plan indicators without covering implementation internals.
Examples
Demonstrates champion lookups, JSON containment queries, full-text search, and time-series filtering through execution plans.
What it assumes
Assumes familiarity with SQL queries and EXPLAIN plans while introducing bitmap index and heap phases through concrete queries.
What you can do after
Diagnose execution plans by identifying bitmap phases, candidate-row rechecks, exact heap blocks, and rows discarded during recheck.
How it connects
Connects bitmap heap scans to bitmap index scans, GIN indexes, hash indexes, BRIN indexes, and lossy bitmaps.

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.