Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Performance & internals
Stage 5: Specialization

Part 18 / Topic 2 of 7

Index-friendly predicates

Part 17's expression-index lesson generalises into the tuner's first reflex: keep the column bare; let the constant side do the work. A predicate an index can serve is called sargable, and un-sargable ones all share a shape — the column got transformed:

Index-blind Index-friendly
WHERE lower(email) = $1 expression index — or store normalised
WHERE order_date + 7 < now() WHERE order_date < now() - interval '7 days'
WHERE amount::text LIKE '4%' WHERE amount >= 400 AND amount < 500 (say what you mean)
WHERE name LIKE '%son' trigram GIN (Part 14), or rethink

Watch one rewrite flip a plan — same question, the arithmetic moved off the column:

EXPLAIN (COSTS OFF) SELECT * FROM big_orders WHERE id + 0 = 150000;
EXPLAIN (COSTS OFF) SELECT * FROM big_orders WHERE id = 150000 - 0;
              QUERY PLAN               
---------------------------------------
 Gather
   Workers Planned: 1
   ->  Parallel Seq Scan on big_orders
         Filter: ((id + 0) = 150000)
(4 rows)

                   QUERY PLAN                   
------------------------------------------------
 Index Scan using big_orders_pkey on big_orders
   Index Cond: (id = 150000)
(2 rows)

The first computes id + 0 for all 200,000 rows; the second descends the primary key. (The + 0 is a stand-in for every innocent-looking date_trunc(col), col * 1.18, coalesce(col, 0) that production code wraps around indexed columns.) The reflex, every time a plan ignores an index you know exists: look at the column's side of the operator. Is anything touching it?

🧪 Quick quiz — answer from memory, then check.

  1. Why does WHERE created_at + interval '1 day' > now() skip the index?
  2. The sargable rewrite?
Show the answers
  1. The index stores created_at, not created_at-plus-a-day — every row must be computed before comparing.
  2. WHERE created_at > now() - interval '1 day' — bare column, constant math.

Done here? The practice set for Part 18 — 5 exercises with solutions — is on the part page.

Search the guide