Menu

1 / 3

Cursor-Based Pagination in PostgreSQL (Keyset vs OFFSET)

DEV Community: database·Mathias Paulenko·about 6 hours ago
#FGfRifOu
#devtodatabase#devto#dev#cursor#rows
Long article (2944 words) — no AI summary yet.
Reading 0:00
15s threshold

The deep-page problem, measured

I didn't want to publish another “OFFSET is slow” post, so I put both queries on PostgreSQL 17.12 in Docker and measured them over 1.2 million rows. Then I tried to break the cursor implementation in the ways real data breaks it: duplicate timestamps, backward navigation, NULL sort keys, mixed sort directions, filter changes, deletes, and rows that arrive late.

The setup was boring on purpose: page size 20, three runs per query, and only rows matching this filter:

status = 'published' AND deleted_at IS NULL

Enter fullscreen mode

Exit fullscreen mode

When I say depth below, I mean how many rows the client has already consumed before asking for the next page.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.