Deep-page pagination → keyset pagination

Replace OFFSET pagination, which slows down on later pages, with reading from the last key of the previous page.

SQL

Situation

An admin screen lists paid orders newest first, 20 per page. The first pages are fast, but once a user pages to around page 5,000, one page takes tens to hundreds of milliseconds. When several people page through the same query at once, DB load spikes.

Tables

TableColumnTypeDescription
ordersstatusvarcharabout 140,000 rows are PAID
ordersord_dttimestampOrder date and time
ordersord_idintegerOrder ID (distinguishes orders at the same time)
IndexColumns
orders_x2status, ord_dt, ord_id

Rows 100,001–100,020

BEFORE · OFFSET pagination

-- Page 5,001 at 20 rows per page
SELECT *
FROM orders
WHERE status = 'PAID'
ORDER BY ord_dt DESC, ord_id DESC
OFFSET 100000 LIMIT 20

Run it and check how many rows it read and discarded to return 20.

Approach

1. Count the discarded rows

OFFSET 100000 means skip the first 100,000 rows, but the DB actually reads those 100,000 rows, counts them in order, and discards them. The later the page, the more it reads and discards, in proportion.

2. Ask "from where", not "which position"

Knowing the last row of the previous page (ord_dt, ord_id), read 20 rows starting below that key. The index is sorted by key, so it can go straight to that position.

3. Check that the sort key is unique

With ord_dt alone, several orders can share the same time, so rows drop or repeat at the boundary. Add ord_id to make the order unique.

Explanation

OFFSET → keyset pagination

AFTER · Keyset pagination

SELECT *
FROM orders
WHERE status = 'PAID'
  AND (ord_dt, ord_id) < (:last_dt, :last_id)
ORDER BY ord_dt DESC, ord_id DESC
LIMIT 20

Why it got faster

  • Before reads all 140,577 paid orders (Bitmap Heap Scan, Buffers about 19,400), sorts them, counts to row 100,020, and returns only the last 20.
  • In After, Index Scan Backward goes to the position just before ('PAID', :last_dt, :last_id) in orders_x2 and reads only 20 rows. Buffers 8. It is the same no matter how deep the page.
  • (ord_dt, ord_id) < (…, …) is a row comparison. PostgreSQL uses it directly as an index search condition. Writing it out as in Oracle, ord_dt <= :d AND (ord_dt < :d OR ord_id < :id), yields the same plan.

Why this is optimal

  • For 20 rows on screen, it reads only the blocks from the index root down to the leaf and the table blocks holding the 20 rows.

Variation: OFFSET on an early page

TRY · OFFSET 1000

SELECT *
FROM orders
WHERE status = 'PAID'
ORDER BY ord_dt DESC, ord_id DESC
OFFSET 1000 LIMIT 20
  • On early pages the planner reads only 1,020 rows with Index Scan Backward and stops (Buffers about 160). Even the same OFFSET query switches to a full sort at some point as the skipped rows grow. Keyset pagination stays constant regardless of page position.

Key points

  • OFFSET pagination reads every skipped row too. The deeper the page, the slower it gets.
  • Keyset pagination needs an index on the sort key and a unique sort key (ord_dt, ord_id), and cannot "jump to page n" (only next/previous).
  • DB difference: Oracle uses a ROWNUM inline view or OFFSET … FETCH; PostgreSQL uses OFFSET … LIMIT. Using the row comparison (a, b) < (x, y) directly for index search is a PostgreSQL strength.