Sub topic 15
Indexing for `lower()`, `LIKE` and Trigram Searches
Assume you know SELECT, WHERE, and basic index concepts. This chapter uses PostgreSQL expression indexes, B-tree indexes, and the pg_trgm extension.
Text filters can become slow when PostgreSQL must apply a function or inspect many rows before it finds matches. The right index lets PostgreSQL narrow the search first.
Indexing case-insensitive searches with `lower()`
A normal index on email supports comparisons against the stored value. It does not automatically support a query that transforms the column:
CREATE INDEX users_email_idx ON users (email);
SELECT id
FROM users
WHERE lower(email) = lower('[email protected]');The query applies lower() to every candidate value. Create an expression index instead:
CREATE INDEX users_email_lower_idx
ON users (lower(email));
SELECT id
FROM users
WHERE lower(email) = lower('[email protected]');An expression index stores the result of the expression for each row. The expression in the query should match the indexed expression. For example, upper(email) = upper(...) does not directly match an index on lower(email).
If email addresses must be unique regardless of case, use a unique expression index:
CREATE UNIQUE INDEX users_email_lower_unique
ON users (lower(email));This prevents both [email protected] and [email protected] from being stored.
Choosing an index for `LIKE`
A B-tree index can usually help with a fixed prefix:
CREATE INDEX products_name_idx ON products (name);
SELECT id, name
FROM products
WHERE name LIKE 'phone%';The database can search for values beginning with phone. A leading wildcard removes that fixed starting point:
SELECT id, name
FROM products
WHERE name LIKE '%phone%';This query may require checking many rows. ILIKE performs case-insensitive matching, but it has the same prefix limitation.
| Search condition | Typical useful index |
|---|---|
| `name = 'Phone'` | B-tree on `name` |
| `name LIKE 'Phone%'` | B-tree on `name` |
| `lower(name) LIKE 'phone%'` | Expression B-tree on `lower(name)` |
| `name ILIKE '%phone%'` | Trigram GIN or GiST index |
For predictable case-insensitive prefix searches, create the matching expression index:
CREATE INDEX products_name_lower_idx
ON products (lower(name));
SELECT id, name
FROM products
WHERE lower(name) LIKE 'phone%';Using trigram indexes for arbitrary fragments
The pg_trgm extension breaks text into overlapping groups of three characters called trigrams. A trigram index can speed up LIKE, ILIKE, regular-expression searches, and similarity searches, including patterns with a leading wildcard.
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE INDEX products_name_trgm_idx
ON products USING gin (name gin_trgm_ops);
SELECT id, name
FROM products
WHERE name ILIKE '%phone%';GIN indexes are often a good choice for read-heavy containment searches. They can use more storage and make writes more expensive because the index must be updated. GiST trigram indexes support additional distance and similarity operations and can be preferable when those operators matter.
Common errors
- Creating an index on
emailbut queryinglower(email). - Expecting a normal B-tree index to accelerate
LIKE '%term%'. - Forgetting that an index improves reads but adds storage and write work.
- Ignoring collation and case rules when deciding what “case-insensitive” means.
Exercise
Which index best supports WHERE lower(name) LIKE 'desk%', and which index supports WHERE name ILIKE '%desk%'?
Show answer
Use a B-tree expression index on lower(name) for the fixed-prefix query. Use a pg_trgm GIN or GiST index on name for the fragment search.