Offset Pagination: Calculate Pages and Reduce Missing or Duplicate Rows
Offset pagination skips the first N rows of an ordered result and returns a set number of rows. It is easy to expose page numbers, but results can vary without a unique order, and changes between requests can make rows disappear or repeat. Deep offsets can also cost more to compute.
Offset pagination skips the first N rows of an ordered result and returns a set number of rows. It is easy to expose page numbers, but results can vary without a unique order, and changes between requests can make rows disappear or repeat. Deep offsets can also cost more to compute.
Convert a page number to an offset
For a one-based page number p and page size s, the number of rows to skip is (p - 1) × s. With a page size of 20, page 3 skips 40 rows. Validate that the page is at least 1 and the size is positive, and enforce API-defined maximum page size and offset.
In PostgreSQL 17, LIMIT sets the maximum number of returned rows and OFFSET sets how many rows to skip first:
SELECT id, created_at, total
FROM orders
ORDER BY created_at DESC, id DESC
LIMIT 20 OFFSET 40;This illustrates page 3 of 20 items; it is not a tested query against a real table or index.
Make ordering deterministic
Without an explicit ORDER BY, a relational query does not guarantee row order. PostgreSQL warns that predictable subsets with LIMIT and OFFSET require a unique ordering. Sorting only by a value that can tie, such as created_at, does not fix the relative order of tied rows. Add a unique key such as id as the final tie-breaker.
Keep sort direction fixed as well. Changing ORDER BY or filters between page requests breaks the assumption that the client is traversing one collection. With multiple sort columns, keep each direction and null-ordering policy consistent with the API contract.
Data changes between pages
Even with deterministic ordering, an offset refers to a position in the current result. If a row is inserted near the front after page 1, the next request may repeat the previous page’s last row. If an earlier row is deleted, an unread row may shift before the page boundary and be skipped. Updating a sort key can also move a row across pages.
Offset works best for lists that change little or for interfaces where a small amount of drift during page navigation is acceptable. For an export or batch job that needs a complete collection as of one point in time, use an API snapshot/export feature or a consistent read boundary supported by the storage system. This does not mean keeping a database transaction open across HTTP requests; check the storage documentation for connection, locking, snapshot-retention, and isolation costs.
Deep-page cost and limits
The server may need to compute rows that an offset skips, so large offsets can be inefficient. PostgreSQL documents this limitation. Elasticsearch’s current guide says deep from/size paging can require each shard to keep prior hits in memory, and its default result window is 10,000; it points to search_after for deeper traversal. Those details describe Elasticsearch defaults and do not apply automatically to other databases or search engines.
Set a maximum offset and page size, and return a consistent error when callers exceed the limit. If deep traversal is common, consider a continuation/cursor or keyset contract that resumes after the last sort key. Before changing methods, measure with representative data and the actual query plan; do not assume a universal performance multiplier.
Total counts and the last page
An exact total may require a separate aggregate or storage calculation and can be costly. Microsoft Azure REST API Guidelines recommend avoiding a total count for this reason. First decide whether the page-number interface truly needs the total. Choose a signal the API can support: an approximate or optional count, or an indicator that another page exists.
A response with fewer items than requested is not necessarily an error or evidence of concurrent changes: it may be the last page, or the server may allow smaller responses. Specify the final-page representation, hasMore, or next-link meaning in the API contract instead of inferring it from item count alone.
Implementation checklist
Document whether page numbering starts at 0 or 1 and match the offset formula.
Validate page size, page number, offset range, and integer overflow.
Include a unique tie-breaker in `ORDER BY`; preserve filters and sorting across pages.
Explain possible omissions/repeats during changes and whether point-in-time consistency is provided.
Check deep offsets and count calculations with representative data and query plans; set limits.
Compare token/keyset paging or a separate export contract when deep traversal or consistent extraction is the main need.
Sources
[PostgreSQL 17: LIMIT and OFFSET](https://www.postgresql.org/docs/17/queries-limit.html)
[Elasticsearch: Paginate search results](https://www.elastic.co/docs/reference/elasticsearch/rest-apis/paginate-search-results)
[Microsoft Azure REST API Guidelines: Collections](https://github.com/microsoft/api-guidelines/blob/vNext/azure/Guidelines.md#collections)