BitmapOr Combines Two Index Scans
An OR over two single-column indexes cannot share one composite btree, so the planner may merge both index bitmaps with a BitmapOr node instead.
Code
-- Each side of the OR hits a different single-column index: the planner can merge both bitmaps with BitmapOr
CREATE INDEX idx_messages_sender ON messages (sender);
CREATE INDEX idx_messages_priority ON messages (priority);
EXPLAIN (COSTS OFF)
SELECT message_id
FROM messages
WHERE sender = 'bob' OR priority = 'high';
-- Plan shape (large table): a top-level BitmapOr with two Bitmap Index Scans beneath it,
-- one per single-column index above; the bitmaps merge by row union before a single heap pass,
-- and a row matching both sides is fetched from the heap exactly once
-- Unlike AND, a composite index cannot rescue OR - a single btree cannot serve two columns'
-- independent equality conditions at once, so there is no "build a composite and skip the merge" equivalent;
-- on small tables the planner may still seq-scan; trust the actual EXPLAIN outputHow the snippet works
SELECT / CREATE / EXPLAIN statements share the script, touching messages.
What the statements actually assemble: a WHERE clause narrowing which rows one statement affects.
Applying it
The messages table is the working surface here.
Statements that matter
EXPLAIN (COSTS OFF)
SELECT message_id
FROM messages
WHERE sender = 'bob' OR priority = 'high';
CREATE INDEX idx_messages_priority ON messages (priority);
CREATE INDEX idx_messages_sender ON messages (sender);
Interactive Tools for This Topic
PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.Multi-Mode Escape & Unescape CodecEscape and unescape strings safely across JSON, JavaScript, SQL single-quoted literals, POSIX shell single quotes, and RFC 4180 CSV cells in your browser.SQL FormatterReformat SQL by uppercasing reserved words and breaking lines before SELECT, FROM, WHERE, and other clauses.
FAQ
- Name the tables involved in this file.
- `messages`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.