PostgreSQL OFFSET Reads Every Skipped Row, Not Just the Ones You Want

A developer tested PostgreSQL 17 pagination performance on a 10-million-row table and found that OFFSET does not skip rows — it reads and discards every one before the target page. A query using OFFSET 9,999,980 to fetch just 20 rows touched 110,659 buffer pages, equivalent to nearly the entire index and heap combined. Benchmark results showed OFFSET query time scaled linearly with depth, reaching over 790 ms at the deepest page. By contrast, a keyset cursor approach returned results in a flat 0.016–0.027 ms regardless of how deep into the table the query reached. The findings illustrate that OFFSET-based pagination belongs to a fundamentally different — and far costlier — complexity class than cursor-based alternatives.
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