Blog

Keyset Pagination: Stop Making PostgreSQL Count Past Old Rows

continue from a value, not a page number
A cursor continuing directly from one sorted database row while offset pages pile up behind

OFFSET 500000 LIMIT 50 asks the database to find and discard half a million rows before returning fifty. The page is small; the work is not.

Continue After The Last Key

For newest-first results with a unique tie-breaker:

select id, created_at, amount
from payments
where (created_at, id) < ($1, $2)
order by created_at desc, id desc
limit 50;

The cursor contains the last created_at and id from the previous response. A matching index supports the traversal.

create index payments_feed_order
on payments (created_at desc, id desc);

The unique id tie-breaker matters. Timestamps alone can repeat, producing missing or duplicated rows between pages.

Cursor Is An API Contract

Encode cursor fields as an opaque token so clients do not depend on storage representation. Validate it, version it if the ordering contract may evolve, and reject a cursor used with incompatible filters.

Concurrent inserts before the cursor do not shift later pages as they do with offsets. Updates to ordering fields can still move records; immutable ordering keys make feeds more stable.

Page Numbers May Be The Wrong Promise

Keyset pagination cannot jump cheaply to arbitrary page 7,842. Most user flows do not need that. They need “next,” “previous,” or infinite scroll.

For small admin tables, offset is perfectly acceptable. Change when depth and latency justify it, not because cursor pagination sounds senior.