skip to content

partial and expression indexes

index only the rows you query, or lower(email) instead of email.

address
everything.indexes.partial_and_expression
part
part 8, indexes, from the outside
chapter
chapter 6 of 12
reading time
7 min

two ways to make an index fit the query instead of the table. a partial index has a WHERE: it only holds the rows you actually search, so it's smaller and cheaper to keep up. an expression index indexes a computed value like lower(email), so a query that searches on that expression can use it.

both only work when the query says the same thing the index says.

the picture

you know the "open orders" screen every shop has? 113 of 10,000 orders are open (placed or paid), and that screen asks for them all day. an index on ordered_at for the whole table is a phone book of the whole city, to find the 113 people on your street. a partial index is the list of your street.

orders (ordered_at): 10,000 entries240 KB
... WHERE status IN ('placed', 'paid'): 113 entries16 KB
real output, postgres 18.6 (PGlite 0.5.12) · partial-expression
the same column indexed twice: every order, and only the open ones. sizes in KB, from the capture.

240 KB against 16 KB: the smallest an index can be, a metapage and one leaf. and the saving isn't only space. an order that's delivered never touches the partial index again: no new entries when it's updated, nothing for VACUUM to clean.

partial indexes

sqldb=shop · session pe
CREATE INDEX orders_open ON orders (ordered_at)
WHERE status IN ('placed', 'paid');
 
EXPLAIN (COSTS OFF)
SELECT id, customer_id, ordered_at FROM orders
WHERE status IN ('placed', 'paid')
ORDER BY ordered_at
LIMIT 10;
output, from postgres 18.6
#1 · CREATE INDEX
#2 · EXPLAIN
Limit -> Index Scan using orders_open on orders

no Filter line, no Index Cond on status: every entry in the index is an open order, so there's nothing to check. the planner can use a partial index when it can prove from the query's WHERE that every row the query wants is in the index. that's a logical test, done when the query is planned:

WHERE status =
SELECT id, customer_id, ordered_at FROM orders WHERE status IN ('placed', 'paid') ORDER BY ordered_at LIMIT 10
pages read by the whole query (Buffers: shared hit + read)
2 pages
2 pages
83 pages

the oldest open orders: the query's WHERE matches the index's, so the index is a list of exactly these rows, in order.

real output, postgres 18.6 (PGlite 0.5.12) · partial-expression
three queries against the partial index on open orders. IN ('placed', 'paid') matches its WHERE exactly. status = 'placed' implies it, so the index still works, with status checked per row. 'shipped' rows aren't in it at all, so it's useless there.
predict the output
the partial index has WHERE status IN ('placed', 'paid'). which query can use it?
sql
SELECT id FROM orders WHERE status = 'paid' ORDER BY ordered_at LIMIT 10;

a partial unique index

UNIQUE on a partial index means unique among the rows it covers. "one open cart per customer" is one line:

sqldb=shop · session pe
continues session pe (1 earlier block run first)
CREATE UNIQUE INDEX one_placed_order ON orders (customer_id) WHERE status = 'placed';
INSERT INTO orders (customer_id, status, ordered_at)
SELECT customer_id, 'placed', '2026-07-01' FROM orders WHERE status = 'placed' ORDER BY id LIMIT 1;
output, from postgres 18.6
#1 · CREATE INDEX
#2 · ERROR 23505
ERROR 23505: duplicate key value violates unique constraint "one_placed_order"
DETAIL: Key (customer_id)=(266) already exists.

the existing 51 placed orders all belong to different customers, so the index builds, and a second placed order for the same customer is refused (23505). delivered orders, any number of them, are none of its business. the same trick makes soft deletes work with uniqueness: UNIQUE (email) WHERE deleted_at IS NULL (soft delete).

expression indexes

an index on email is sorted by email. a query on lower(email) asks about a different value, which the index doesn't have (why isn't my index used?). so index the expression:

sqldb=shop · session ex
CREATE INDEX customers_lower_email ON customers (lower(email));
ANALYZE customers;
SELECT id, name, email FROM customers
WHERE lower(email) = 'zoe.martin@example.com'
ORDER BY id;
output, from postgres 18.6
#1 · CREATE INDEX
#2 · ANALYZE
#3 · SELECT 2
idint4nametextemailtext
4Zoë MartinZoe.Martin@Example.com
5Zoe Martinzoe.martin@example.com
(2 rows)

two customers: Zoë at Zoe.Martin@Example.com and Zoe at zoe.martin@example.com, emails that differ only in case. the index stores lower(email) for every row, computed once when the row is written, and the query has to use the same expression to find it:

WHERE =
SELECT id, name, email FROM customers WHERE lower(email) = 'zoe.martin@example.com'

the same expression in the query: an index scan. it finds both Zoes, whose emails differ only in case.

real output, postgres 18.6 (PGlite 0.5.12) · partial-expression
an expression index on lower(email) next to the table's unique index on email. the expression finds both Zoes. the bare column uses the unique index and finds one. ILIKE asks something neither index can answer.

the ANALYZE above isn't decoration. postgres keeps statistics for each indexed expression (they show up in pg_stats under the index's name), so after an ANALYZE the planner knows how selective lower(email) = ... is instead of guessing.

and a unique expression index is how you'd make emails unique ignoring case. on this data it can't be built, because of the two Zoes:

sqldb=shop
CREATE UNIQUE INDEX customers_email_ci ON customers (lower(email));
output, from postgres 18.6
ERROR 23505: could not create unique index "customers_email_ci"
DETAIL: Key (lower(email))=(zoe.martin@example.com) is duplicated.

the error names the duplicate. that's the index doing its job before it exists.

both at once

an index can be partial and on an expression. "active customers by lowercase email" is CREATE INDEX ON customers (lower(email)) WHERE email IS NOT NULL, which also skips the NULL emails nobody can search for. the same rules hold: the query must repeat the expression, and its WHERE must imply the index's.

keylines

  • a partial index holds only the rows matching its WHERE. smaller, and rows outside it cost nothing.
  • the query's WHERE must imply the index's, at plan time. parameters can break that.
  • an expression index stores a computed value. the query must use the same expression.
  • index expressions must be IMMUTABLE: no time zones, no now().
  • partial unique indexes enforce rules like "one open order per customer".