Sub topic 2
Checking Whether the Planner Uses an Index
Assume you know basic SELECT, WHERE, ORDER BY, and CREATE INDEX. You should also know that PostgreSQL chooses a query plan before it runs a query. The planner compares possible plans and estimates their cost.
Read a plan with EXPLAIN
EXPLAIN shows the plan PostgreSQL would use without running the query. For example:
EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC
LIMIT 20;A useful plan might contain:
Limit
-> Index Scan using orders_customer_created_idx on orders
Index Cond: (customer_id = 42)The Index Scan node means PostgreSQL is walking the index and fetching matching table rows. The Index Cond shows the condition used to search the index. In this example, the index can find customer 42 and return rows in created_at DESC order.
Common scan nodes have different meanings:
| Plan node | Meaning |
|---|---|
| Seq Scan | Reads the table, usually because many rows match or the table is small |
| Index Scan | Reads index entries, then fetches matching table rows |
| Index Only Scan | Answers the query from the index when the needed table data is available |
| Bitmap Index Scan | Finds many index matches before fetching table pages in batches |
PostgreSQL can use an index even when the plan also contains a Bitmap Heap Scan. Look for a child Bitmap Index Scan node to see which index found the rows.
Compare estimates with actual execution
Add ANALYZE to run the query and report what happened:
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, created_at
FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC
LIMIT 20;ANALYZE reports actual row counts and execution times. BUFFERS reports pages read from memory or storage. Compare the estimated rows with the actual rows:
Index Scan ... (cost=0.42..18.50 rows=20 width=16)
(actual time=0.04..0.12 rows=20 loops=1)A large difference between estimated and actual rows can indicate stale statistics, an unusual data distribution, or a condition the planner cannot estimate well. Run ANALYZE orders; after substantial data changes so the planner can refresh its statistics.
EXPLAIN ANALYZE executes the statement. Use it carefully with INSERT, UPDATE, or DELETE. For a write statement, use a transaction and roll it back when appropriate.
BEGIN;
EXPLAIN (ANALYZE, BUFFERS)
DELETE FROM orders
WHERE id = 1001;
ROLLBACK;Why PostgreSQL might ignore an index
Index use is a cost decision, not a promise. A sequential scan can be faster when a query returns most rows, when the table is small, or when fetching many table rows would cause random page reads.
An index might also be unsuitable when:
- The query does not constrain the leading column of a multicolumn index.
- The query expression does not match an expression index.
- The index predicate cannot be proven from the query conditions.
- Statistics are stale.
- A function or type conversion prevents an index condition.
You can temporarily test whether a sequential scan affects the decision:
SET enable_seqscan = off;
EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 42;
RESET enable_seqscan;This is a diagnostic experiment, not a production setting. It does not make an unsuitable index useful.
> callout note > Do not judge an index only by its name in the plan. Check the scan type, the Index Cond, estimated and actual rows, buffers, and total execution time.
Exercise
Exercise
A query has an index on (customerid, createdat), but EXPLAIN shows a sequential scan for WHERE created_at > now() - interval '1 day'. What is the most likely reason?
Show answer
The query does not constrain the index's leading column, customerid, so the multicolumn index cannot efficiently narrow the search by createdat.