Cursor pagination can silently drop rows due to JS Date precision loss
A common Node.js approach to keyset (cursor) pagination can quietly skip rows, sometimes ending pagination after the first page. The problem stems from JavaScript Date objects holding millisecond precision while Postgres timestamps store microseconds. Rows inserted in the same millisecond can then be mishandled when the cursor is compared. The suggested fix is to keep the cursor as the exact database text value, such as created_at::text, so the comparison stays exact. Other options include using millisecond-precision timestamps with an id tiebreaker, or paginating on id alone when ids are monotonic. The author recommends testing by walking every page against a real database to confirm no rows are missing or repeated.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in