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.

SqlData Integrity

Code

snippet
-- 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 output

How 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

FAQ

Name the tables involved in this file.
`messages`.