One entry at the index end per value: GROUP BY MAX → skipping with recursive WITH

Getting the latest order time per status with GROUP BY status reads all 2 million orders. There are only three status values, so using WITH RECURSIVE to find the next status value in the index one at a time, and reading only one entry at the index end for each value's MAX, cuts Buffers from 18,752 to 24.

SQL

Situation

An operations monitoring screen shows the last order time per order status every minute. The result is three rows, yet every run reads the whole orders table.

Tables

TableColumnsDescription
ordersstatusThree values: DONE, PAID, CANCELED
ordersord_dtOrder timestamp
IndexColumns
orders_x2status, ord_dt, ord_id

There is no separate table holding the status codes.

Query

BEFORE · GROUP BY status

SELECT status, MAX(ord_dt) AS last_dt
FROM orders
GROUP BY status

Approach

1. See how many blocks a single value needs

For MAX(ord_dt) with WHERE status = 'PAID', the planner rewrites it as ORDER BY ord_dt DESC LIMIT 1 and reads the one entry at the end of the PAID range of orders_x2 with Index Only Scan Backward. Grouped with GROUP BY, this rewrite is not available and all rows are read.

2. Consider where to get the list of status values

Computing MAX per value requires the list of status values first. Hard-coding the values in the statement misses new statuses when they appear. In the index, "the first value greater than the current value" can be found with MIN(status) … WHERE status > current value by descending to a single spot in the index. Repeat this until no value remains.

3. Check the PostgreSQL version

Up to PostgreSQL 17 there is no Skip Scan that reads by skipping over values of the leading index column. So the repetition has to be written as a statement.

Explanation

Skip through status values with WITH RECURSIVE, then MAX per value

AFTER · WITH RECURSIVE + MIN/MAX

WITH RECURSIVE s (status) AS (
  SELECT MIN(status) FROM orders
  UNION ALL
  SELECT (SELECT MIN(o.status) FROM orders o WHERE o.status > s.status)
  FROM s
  WHERE s.status IS NOT NULL
)
SELECT s.status,
       (SELECT MAX(o.ord_dt) FROM orders o WHERE o.status = s.status) AS last_dt
FROM s
WHERE s.status IS NOT NULL

Why it got faster

  • Before reads 2 million orders with a Seq Scan and gathers them into three groups with HashAggregate. Buffers 18,752.
  • The Recursive Union in After finds the first value (CANCELED) with Index Only Scan + Limit of one row, then finds the next values (DONE, PAID) once each in the same way. The last probe returns NULL and the recursion ends (WorkTable Scan loops=4).
  • The MAX(ord_dt) attached to each value runs three times as a SubPlan, each reading the one entry at the end of its range with Index Only Scan Backward (Heap Fetches: 0). Total Buffers 24.

Why this is optimal

  • The cost is proportional to the number of distinct values, not the row count. Each value takes only two index descents (finding the next value, finding the MAX).
  • With tens of thousands of distinct values, the probe count grows accordingly and becomes more expensive than a full read. This method is for few distinct values and many rows.
  • Heap Fetches: 0 is because the Visibility Map marks every page as "all-visible". On a frequently updated table, heap checks may be added.

Key points

  • GROUP BY + MIN/MAX reads all rows even when there are few groups. With an index that starts with the group key, it can be turned into one entry at the index end per value.
  • Without a value list, WITH RECURSIVE finds "the next value" in the index one at a time. This technique is called a Loose Index Scan.
  • The planner rewrites a single-MIN/MAX subquery as a LIMIT 1 index scan. Confirm Index Only Scan under Limit in the execution plan.