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.
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
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;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:
the oldest open orders: the query's WHERE matches the index's, so the index is a list of exactly these rows, in order.
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.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:
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;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:
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;| idint4 | nametext | emailtext |
|---|---|---|
| 4 | Zoë Martin | Zoe.Martin@Example.com |
| 5 | Zoe Martin | zoe.martin@example.com |
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:
the same expression in the query: an index scan. it finds both Zoes, whose emails differ only in case.
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:
CREATE UNIQUE INDEX customers_email_ci ON customers (lower(email));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".