Filling the index leading column with an IN list

With a condition only on the second index column, the index cannot be searched by range. If the leading column has only a few values, fill them in with an IN list to turn a full read into an index search.

SQL

Situation

A customer service screen shows all "orders in the last 7 days" regardless of status. There is an orders_x2(status, ord_dt, ord_id) index, but the query has only an ord_dt condition. The execution plan is a full table read.

Tables

TableColumnDescription
ordersstatusOnly three values: DONE, PAID, CANCELED
ordersord_dtOrder date and time (last date 2025-09-26)
ordersamtOrder amount
IndexColumns
orders_x2status, ord_dt, ord_id

There is no index starting with ord_dt. Orders in the last 7 days are 14,000 rows.

Query

BEFORE · No leading column condition

SELECT ord_id, ord_dt, status, amt
FROM orders
WHERE ord_dt >= DATE '2025-09-20'

Run it and check how many blocks it reads for 14,000 rows.

Approach

1. See why the index cannot be searched by range

orders_x2 is sorted first by status, and by ord_dt within the same status. Orders in the last 7 days are scattered at the ends of the DONE, PAID, and CANCELED sections, so no single search start position can be set.

2. Count the values of the leading column

If there are only three, it can "search each of the three sections by range".

3. Confirm the result stays the same when the condition is added

Check that the value list is complete and whether the column can receive NULLs.

Explanation

Fill the leading column with an IN list

AFTER · IN list added

SELECT ord_id, ord_dt, status, amt
FROM orders
WHERE status IN ('DONE', 'PAID', 'CANCELED')
  AND ord_dt >= DATE '2025-09-20'

Why it got faster

  • Before is a Seq Scan with Buffers about 18,750.
  • In After, Bitmap Index Scan finds only the recent sections of the three status values in orders_x2 (Index Cond: status = ANY ('{DONE,PAID,CANCELED}'), about 80 blocks), and Bitmap Heap Scan reads only the recent blocks where those rows sit. Buffers about 210.
  • PostgreSQL turns the IN (…) list into an array comparison (= ANY) and finds a range per value within one index search.

Evidence that the result is the same

  • status has only three values, and the current data has no NULLs.
  • However, status has no NOT NULL constraint. If a NULL or a new status value comes in later, it silently drops out of the result. Before using this method, confirm that the value list is guaranteed by NOT NULL and CHECK constraints or a code table.

Why this is optimal

  • It reads only the last-7-days sections of the index and only the table blocks where recent data sits.
  • The remaining cost is the table access that fetches amt.

Key points

  • A composite index allows a range search only with an = or IN condition on the leading column.
  • Tuning that adds a condition must also confirm the evidence that the result is the same (NOT NULL, a guaranteed value list). This is the same context as the NULL trap in scenario 8.
  • DB difference: Oracle searches per value with INLIST ITERATOR, and also has INDEX SKIP SCAN, which skips the leading column (Oracle scenario 17). PostgreSQL gained B-tree Skip Scan in 18; in this sandbox (17), the IN list takes its place.