Postgres 16 has a B-tree index on (customer_id, created_at), with millions of distinct customers. A query filters only on created_at = $1. How does the index help?
Show answer
Answer
Entries are sorted by customer_id first, so created_at values are scattered across the whole index. Without the leading column there is no range to seek to.
Being in the index is not enough, column order matters. Postgres never reorders index columns. Skip scan arrived only in Postgres 18, and even there it pays off only when the leading column has few distinct values.
Source: PostgreSQL Documentation: Multicolumn Indexes, Use The Index, Luke: SQL Indexing and Tuning, Markus Winand