Sub topic 3
Why Index Expressions Must Be Immutable
This chapter assumes you know basic SQL, can create a table and an index, and can read simple EXPLAIN output. You should also know what an index is and how a B-tree stores sorted keys.
Why index expressions must be immutable
An expression index stores the result of a function, such as lower(email), for every row. PostgreSQL must be able to recompute that result later and get the same answer. A function whose result depends on session settings or on the current time cannot be stored safely. PostgreSQL rejects such functions in index definitions with the error "functions in index expression must be marked IMMUTABLE."
Casting a timestamptz to date is a common case. The result depends on the session time zone, so this statement fails:
-- Fails: the result depends on the session time zone.
CREATE INDEX orders_created_date_bad ON orders ((created_at::date));Fix the time zone inside the expression:
CREATE INDEX orders_created_date_utc_idx
ON orders (((created_at AT TIME ZONE 'UTC')::date));
EXPLAIN
SELECT count(*)
FROM orders
WHERE (created_at AT TIME ZONE 'UTC')::date = DATE '2026-01-15';The query must repeat the same expression. Otherwise the planner cannot match it to the index.
Covering queries with INCLUDE
An index can store extra columns with INCLUDE. These columns sit in the leaf entries but are not search keys. When a query asks only for indexed and included columns, PostgreSQL can answer it from the index alone, which is called an index-only scan.
CREATE INDEX orders_customer_cover_idx
ON orders (customer_id)
INCLUDE (total_cents);
EXPLAIN
SELECT customer_id, total_cents
FROM orders
WHERE customer_id = 42;Included columns do not help a WHERE clause on total_cents, because the index cannot search on them.
What indexes cost
Every index must change on each INSERT, and on each UPDATE that touches an indexed column. Each index also uses disk space and cache memory. An extra index speeds up some reads and slows down writes. Before you add one, check whether existing indexes are used with the pg_stat_user_indexes view.
Creating indexes on a live table
A plain CREATE INDEX blocks writes to the table until it finishes. CREATE INDEX CONCURRENTLY avoids that block, but it runs more slowly.
CREATE INDEX CONCURRENTLY orders_status_created_idx
ON orders (status, created_at);Choosing an index design for a workload
Use the table below to compare the designs.
| Design | Best when | Main cost |
|---|---|---|
| Multicolumn | Queries filter on a leading column and sort or filter on others | Larger than a single-column index |
| Partial | Queries always include a fixed condition and the subset is small | Only serves queries that imply the predicate |
| Expression | Queries filter on a function of a column | Expression must be immutable and repeated exactly |
| INCLUDE | You want index-only scans on a few extra columns | Extra storage in every leaf entry |
Exercise
Why does CREATE INDEX on (created_at::date) fail for a timestamptz column, and what is one fix?
Show answer
The cast depends on the session time zone, so the expression is not immutable. One fix is to fix the zone in the expression, such as ((created_at AT TIME ZONE 'UTC')::date), and use the same expression in the query.