All glossary terms

Index scan

What is an index scan in PostgreSQL?

An index scan uses an index to find the matching rows and then fetches them from the table. EXPLAIN shows it as Index Scan; an Index Only Scan can skip the table.

In an index scan, PostgreSQL walks an index to find the entries that match the query's condition and then reads the corresponding rows from the table. It is efficient when a query selects a small fraction of a table's rows, such as a lookup by primary key.

Related access methods also appear in EXPLAIN. A Bitmap Index Scan first collects the locations of all matching rows and then reads the table pages in physical order, which suits medium-sized results. An Index Only Scan answers the query from the index alone when every column it needs is in the index, and it skips the table for pages that the visibility map marks as all-visible.

Example

The example creates a table with a primary key, which PostgreSQL backs with a B-tree index, and asks EXPLAIN about a lookup by id. The output shows Index Scan using orders_pkey.

CREATE TABLE orders (id int PRIMARY KEY, customer_id int, total numeric);
INSERT INTO orders
SELECT g, g % 1000, g * 1.5 FROM generate_series(1, 100000) AS g;
ANALYZE orders;

EXPLAIN SELECT * FROM orders WHERE id = 42;

Plain SQL for stock PostgreSQL 16 or 17, no extensions needed. Run it in an empty database you can throw away.

Sources

Official PostgreSQL documentation (in English).

Related terms