Keyset Pagination Query
Paginate with a WHERE clause on the last seen key instead of OFFSET, keeping each page fast and stable as new rows arrive.
Code
SELECT id, title, created_at
FROM articles
WHERE (created_at, id) < ($1, $2)
AND published = true
ORDER BY created_at DESC, id DESC
LIMIT $3;
-- The caller passes the previous page's created_at and id as cursor values,
-- plus the page size as the limit, so no OFFSET scan is required.Report a problem with this exampleOpens a pre-filled issue on GitHub.