Keyset pagination

Page through big tables without the slowdown of large OFFSET values.

sql intermediate #pagination#performance
Apri nel playground
sql-keyset-pagination.sql
-- first page
SELECT id, title, created_at FROM posts
ORDER BY created_at DESC, id DESC
LIMIT 20;

-- next page: pass the last row's (created_at, id)
SELECT id, title, created_at FROM posts
WHERE (created_at, id) < ('2026-10-20 09:15:00', 4812)
ORDER BY created_at DESC, id DESC
LIMIT 20;

-- supporting index
CREATE INDEX IF NOT EXISTS idx_posts_created_id ON posts (created_at DESC, id DESC);