Sub topic 1
What an Index Does and Why the Planner May Ignore It
Prerequisites
This chapter assumes you already know:
- Basic SQL:
SELECT,WHERE,JOIN, andORDER BY - Creating tables and inserting rows in PostgreSQL
- Common column types such as
integer,text,timestamptz, andjsonb
What an index does
An index is a separate, sorted structure that maps column values to the rows holding them. Without an index, PostgreSQL performs a sequential scan, reading every row in the table to find matches. With a suitable index, it can jump straight to the matching rows.
The planner chooses the cheapest plan based on statistics. It may ignore an index when:
- The query matches a large share of the table, so reading the table sequentially is cheaper.
- The table is small enough that the index overhead is not worth it.
- The condition wraps the column in a function the index does not store, such as
WHERE lower(email) = ...against a plain index onemail. - Statistics are stale. Running
ANALYZErefreshes them.
Reading query plans with EXPLAIN (ANALYZE, BUFFERS)
EXPLAIN shows the plan the planner chose. Adding ANALYZE runs the query and reports actual times and row counts. Adding BUFFERS reports how many pages were read from memory or disk.
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, total
FROM orders
WHERE customer_id = 42;Look for Index Scan or Bitmap Index Scan nodes, which show an index in use. A Seq Scan node means the table was read in full. Compare actual rows with rows in the estimate. A large gap suggests stale statistics.
B-tree: the default index and what it supports
A B-tree is a balanced, sorted tree. It is the default index type for CREATE INDEX. It supports the operators <, <=, =, >=, and >, along with BETWEEN, IS NULL, and ORDER BY on the indexed column.
CREATE TABLE orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer_id bigint NOT NULL,
status text NOT NULL,
total numeric(10,2),
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX orders_customer_idx ON orders (customer_id);Multicolumn, partial, expression, and covering B-tree indexes
A multicolumn index serves filters on its leading column. A query that filters on customer_id and sorts by created_at can use one index on both columns, in that order.
CREATE INDEX orders_customer_created_idx
ON orders (customer_id, created_at DESC);A partial index covers only rows that match a WHERE clause. An expression index stores a computed value. A covering index uses INCLUDE to store extra columns, which can allow index-only scans.
CREATE INDEX orders_open_idx ON orders (created_at) WHERE status = 'open';
CREATE INDEX users_lower_email_idx ON users (lower(email));
CREATE INDEX orders_customer_incl_idx ON orders (customer_id) INCLUDE (total);Exercise
A query filters on customerid and sorts by createdat. Which index lets the planner satisfy both with one scan, and why would a separate index on created_at alone be less effective?
Show answer
A multicolumn index on (customerid, createdat). The leading column matches the equality filter, and the second column is already sorted within each customer, so no separate sort is needed. An index on createdat alone must read rows across all customers in time order and then filter by customerid, which often reads far more rows.