System design Level 2 This level · 23 of 48
Medium postgresql sql

Why does wrapping a column in a function or comparing it with a different type stop the index from being used?

The 30 second answer

A plain index stores the column's raw values, so it only helps when the query compares the raw column. lower of email, a date cast on a timestamp, or an integer column compared with a decimal parameter all compare a computed value instead. I rewrite the condition so the column stands alone, like a half-open date range, match the parameter type to the column, or create an expression index on exactly the expression the query uses.

Could you say this in an interview?

From the basics. An index on email is sorted by the stored email strings. A condition lower(email) = '[email protected]' asks about a different value, the lowercased one, which isn't sorted anywhere. PostgreSQL has to compute lower(email) for every row. The same happens whenever the column ends up inside an expression, including a type conversion PostgreSQL adds for you.

Three common forms.

-- 1. A function on the column
SELECT * FROM customers WHERE lower(email) = '[email protected]';

-- 2. A cast on the column to compare dates
SELECT * FROM orders WHERE created_at::date = DATE '2026-11-13';

-- 3. An implicit cast: integer column, numeric literal or parameter
SELECT * FROM products WHERE id = 42.0;

The third is sneaky. There's no integer = numeric operator, so PostgreSQL's operator resolution converts the integer side to numeric, and the plan shows the column inside a cast, as a filter like ((id)::numeric = 42.0), instead of an index condition. In Java this shows up when a BigDecimal is bound to a parameter compared with an integer column.

Fixes.

-- 1. Expression index that matches the query exactly
CREATE INDEX customers_lower_email ON customers (lower(email));

-- 2. Leave the column alone: a half-open range on the raw timestamp
SELECT * FROM orders
WHERE created_at >= TIMESTAMPTZ '2026-11-13' AND created_at < TIMESTAMPTZ '2026-11-14';

-- 3. Bind the right type
SELECT * FROM products WHERE id = 42;

The docs show that a query on lower(col1) can use an index defined on lower(col1), and warn that index expressions "are relatively expensive to maintain, because the derived expression(s) must be computed for each row insertion and non-HOT update".

A related trap: LIKE prefixes. name LIKE 'abc%' can use a B-tree, but the docs say that if the database doesn't use the C locale, the index needs a special operator class (text_pattern_ops). LIKE '%abc' can't use a B-tree at all; see the trigram question.

Trade-offs.

  • Expression indexes are narrow: an index on lower(email) doesn't help upper(email) or email ILIKE.
  • Rewriting the predicate is free and usually better than adding an index.
  • A case-insensitive collation or the citext type are alternatives for case-insensitive equality, each with its own semantics.

Alternate interview scenarios

"A query by product code is slow only from the Java service, not from psql."

Compare the bound parameter types. A parameter of a wider or different type can turn the index condition into a filter. Log the statement with its parameter types, or run EXPLAIN with the same types.

"Reports filter by month with date_trunc('month', created_at) = '2026-10-01'."

Rewrite to created_at >= '2026-10-01' AND created_at < '2026-11-01', which uses an index on created_at directly.

Sources (3)
  1. PostgreSQL docs: Indexes on expressionspostgresql.org
  2. PostgreSQL docs: Operator type conversionpostgresql.org
  3. PostgreSQL docs: Index types (B-tree and LIKE, operator classes)postgresql.org
Previous
Next