Sub topic 2
How PostgreSQL scans: sequential, index, and bitmap
Assume you know basic SELECT, WHERE, CREATE INDEX, and simple EXPLAIN output. This chapter uses PostgreSQL and an orders table with one million rows.
PostgreSQL can find matching rows in several ways. The planner compares their estimated costs and chooses the cheapest plan. The choice depends on table size, matching-row count, available indexes, and how the data is physically stored.
A small test table
Run this setup if you do not already have an orders table:
CREATE TABLE orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer_id integer NOT NULL,
status text NOT NULL,
total numeric(10,2) NOT NULL
);
INSERT INTO orders (customer_id, status, total)
SELECT (random() * 10000)::int,
(ARRAY['new', 'paid', 'shipped', 'cancelled'])[1 + (random() * 3)::int],
(random() * 500)::numeric(10,2)
FROM generate_series(1, 1000000);
CREATE INDEX orders_customer_idx ON orders (customer_id);
ANALYZE orders;ANALYZE collects statistics about the table. The planner uses those statistics to estimate how many rows a query will return.
Sequential scans
A sequential scan reads the table from beginning to end. PostgreSQL checks each row against the WHERE condition.
EXPLAIN
SELECT *
FROM orders
WHERE status = 'paid';A sequential scan is often the best choice when many rows match. Reading the table in physical order avoids many separate disk requests. It can also be efficient when the table is already cached in memory.
An index is not automatically faster. If a query needs most of the table, using an index would add work without avoiding much data.
Index scans
An index scan walks an index to find matching keys, then visits the table rows identified by those keys. A B-tree index stores keys in sorted order and points to table locations.
EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 42;For a selective condition, such as one customer among thousands, an index scan can avoid reading almost all rows. The plan commonly contains Index Scan and names orders_customer_idx.
The index finds the rows, but the table still contains the complete row data. PostgreSQL may need separate table reads for each match. These reads can become expensive when matches are scattered across many table pages.
Bitmap heap scans
A bitmap scan is a two-stage strategy:
- PostgreSQL scans the index and records the table pages containing matches.
- PostgreSQL visits those pages in a more organized order and checks the matching rows.
EXPLAIN
SELECT *
FROM orders
WHERE customer_id BETWEEN 1 AND 3000;The plan may contain both Bitmap Index Scan and Bitmap Heap Scan. This approach is useful when the condition matches more rows than a normal index scan should fetch one at a time, but not enough rows to justify reading the whole table.
A bitmap can become lossy when it represents a page rather than every matching row. PostgreSQL then rechecks the condition while reading that page. Rows Removed by Index Recheck in EXPLAIN (ANALYZE) shows this extra filtering.
Comparing the scan types
| Scan type | How it finds rows | Common use |
|---|---|---|
| Sequential Scan | Reads every table page | Many rows match, or no useful index exists |
| Index Scan | Finds keys, then visits table rows | A small number of rows match |
| Bitmap Heap Scan | Collects matching pages, then reads them | A moderate number of rows match |
Reading the planner's choice
Use EXPLAIN (ANALYZE, BUFFERS) to compare estimates with actual work:
EXPLAIN (ANALYZE, BUFFERS)
SELECT *
FROM orders
WHERE customer_id = 42;cost is the planner's estimate, not elapsed time. actual time and rows show what happened during execution. Buffers shows pages read from or found in shared memory.
If the estimate is far from the actual row count, run ANALYZE orders. Stale statistics can lead the planner to choose the wrong scan.
Exercise
Which scan is usually a good starting choice when a condition matches a small fraction of a large table?
Show answer
An Index Scan, because the index can locate the few matching rows without reading the entire table.