Replacing offset pagination with stable keyset cursors
OFFSET 50000 forces the database engine to walk and discard fifty thousand index entries before returning twenty rows. As tables grow, deep pages become the slowest queries on the cluster.
Encode the last seen timestamp and unique row identifier as an opaque cursor token. A tuple comparison on a matching composite index seeks directly to the next page in logarithmic time.
Keep sort direction and tie-breaker columns identical between the WHERE clause and ORDER BY clause so the planner uses a single index scan without an extra sort pass.
Test boundary conditions where multiple rows share the exact same timestamp to confirm zero duplicates or skipped items across page turns.