The ORDER BY index trap: reading 2 million rows to skip a sort

To pick the 20 canceled orders of January 2023 with the highest order IDs, the planner reads the PK backward to skip the sort and discards 1.94 million rows by filter. It does not know that the matching rows are clustered at the opposite end of the PK order. Turning the ORDER BY key into an expression, reading 1,830 rows through the condition index and sorting them, cuts Buffers from 28,753 to 579.

SQL

Situation

On a cancellation analysis screen, choosing a period shows 20 canceled orders of that period in descending order ID order. Recent periods return instantly, but choosing an old period such as January 2023 takes 0.5 seconds each time.

Tables

TableColumnsDescription
ordersord_idOrder ID. Matches insertion order, and therefore ord_dt order
ordersstatus, ord_dt1,830 canceled orders in January 2023
IndexColumns
orders_pkord_id
orders_x2status, ord_dt, ord_id

Query

BEFORE · 20 canceled orders of January 2023

SELECT ord_id, ord_dt, cust_id, amt
FROM orders
WHERE status = 'CANCELED'
  AND ord_dt < TIMESTAMP '2023-02-01'
ORDER BY ord_id DESC
LIMIT 20

Look at Rows Removed by Filter of Index Scan Backward using orders_pk.

Approach

1. See what the planner tried to save

With LIMIT 20, the planner counts the cost of a plan that returns the first 20 rows quickly only in proportion to "the fraction that must be passed before meeting 20 rows". Reading orders_pk backward yields descending ord_id order without a sort, so it can stop after meeting 20 matching rows.

2. See what that estimate assumes

The planner assumes the matching rows are spread evenly across the ord_id order. In the actual data, ord_id and ord_dt are in the same order, and the condition is the oldest dates. Consider where the first matching row appears when reading backward.

3. Find a path that reads less even with a sort

Check how many rows match in total and whether an index finds those rows directly. Compare whether reading them all and sorting is cheaper than the PK reverse scan. PostgreSQL has no index hints, so find a way to keep the planner from choosing a plan that skips the sort through the PK.

Explanation

Turn the ORDER BY key into an expression to block sort avoidance

AFTER · ORDER BY column transformed

SELECT ord_id, ord_dt, cust_id, amt
FROM orders
WHERE status = 'CANCELED'
  AND ord_dt < TIMESTAMP '2023-02-01'
ORDER BY ord_id + 0 DESC
LIMIT 20

Why it got faster

  • Before reads orders_pk backward and checks status and date in the heap for each row. The January 2023 orders are at the very end of the PK (the smallest side), so 1,938,674 rows are discarded by filter before the first row appears. Buffers 28,753.
  • In After, ord_id + 0 is not an index column, so the sort cannot be skipped through PK order. The planner reads the 1,830 rows of the range status = 'CANCELED' AND ord_dt < 2023-02-01 through orders_x2 and picks the top 20 with top-N heapsort. Buffers 579. The result order is the same.

Why this is optimal

  • All 1,830 matching rows must be examined to pick 20, whatever the read order. orders_x2 is ordered by ord_dt, ord_id while ORDER BY is on ord_id alone, so 20 rows cannot be picked directly in index order.
  • top-N heapsort scans the 1,830 rows while keeping only 20, so it finishes in 27kB of memory.

Variation: with a recent period

TRY · Since September 2025

SELECT ord_id, ord_dt, cust_id, amt
FROM orders
WHERE status = 'CANCELED'
  AND ord_dt >= TIMESTAMP '2025-09-01'
ORDER BY ord_id DESC
LIMIT 20
  • When the matching rows are at the high end of the PK, the same PK reverse scan discards 752 rows and finds 20. Buffers 15. The same plan is very fast or very slow depending on the period. If the screen accepts any period, run both cases before settling on the statement.

Key points

  • With LIMIT, the planner prefers an index that skips the sort. The cost of that plan depends on where the matching rows lie in the index order, and the planner assumes they are spread evenly.
  • When the sort key and the condition column are correlated (here ord_id and ord_dt), this assumption misses badly. Suspect it when Rows Removed by Filter is far larger than the result row count.
  • In PostgreSQL, which has no hints, block sort avoidance by turning the ORDER BY key into an expression such as + 0. This is the counterexample showing that the sort avoidance of scenario 2 is not always a gain.