Skip to content
WebsiteHosting.com

comparison Data & storage

OFFSET versus keyset pagination

Inserts or deletes can shift rows past an offset, causing repeats or omissions. A complete keyset cursor follows saved values instead, but it can still miss a row whose sort key changes. Neither method freezes the list.

Latest recorded source check · P0247

What it means

OFFSET 2 skips the first two rows in the next request’s result, not necessarily the two rows you just read. For predictable pages, use an order that uniquely determines each row’s position. PostgreSQL 18: LIMIT and OFFSET

A complete keyset cursor instead saves the ordering values of the last row you read. The next request selects rows that sort strictly after those values. Neither method by itself gives the two requests a shared snapshot.

Try it

Start with Rows per page set to 2, Change between page requests set to None, and Saved cursor completeness set to complete.

OFFSET versus keyset pagination

Page through an ordered synthetic table, insert/delete a row, then request the next page using offset or a complete unique cursor; expose repeats and omissions.

Simulation

Synthetic inputs only. This is not a measurement of your hardware or service.

Computed under the stated assumptions

OFFSET skips positions in the changed ordered rows. Keyset uses the saved full tuple boundary; changed sort keys can still move an unread row behind it.

OFFSET versus keyset pagination: synthetic scenario inputs

Scenario: Starting example

Each preset replaces all fields. Editing any field preserves the other values exactly, including invalid entries.

Saved tuple boundary versus changed row positions

First page
A · B
Saved complete cursor
id: B; key: 20
Next page with OFFSET
C · D
Next page with keyset
C · D
OFFSET repeated IDs
None
Keyset repeated IDs
None
Original unread IDs skipped behind OFFSET
None
Original unread IDs moved behind cursor
None
Ordered rows after the intervening change
Unique IDSort key
A10
B20
C30
D40
E50

Evaluated evidence

Rows per page (rows)
2
Change between page requests
none
Saved cursor completeness
complete

Finite synthetic rows, non-null integer keys and uppercase ASCII IDs. Separate reads observe the intervening change; this is not a fixed snapshot, query optimizer or performance measurement.

About this model

Every input is local synthetic evidence. Numbers stay as raw text until this model validates them. The shown evaluated values come from the same result as the explanation.

Presets replace the entire scenario. Reset restores all starting fields, scenario selection and model state; it does not change the open or closed state of this help or article FAQ.

No external request, device command, stored-input history or measurement is performed.

Read the five synthetic rows in ascending (key, ID) order: (10, A), (20, B), (30, C), (40, D), (50, E). The keys are non-null integers, and the unique uppercase IDs use ASCII order. Page one is A, B, so the saved cursor is (20, B).

For non-null values, an ordered row comparison uses the first pair of values that differ. PostgreSQL 18: Row Constructor Comparison Here, a row qualifies if its key is greater than 20, or if its key is 20 and its ID sorts after B. With no change, both methods return C, D.

Now try each Change between page requests separately. In each case, page one stays A, B and the second request sees the changed order:

  • insert-X-15: The order becomes A, X, B, C, D, E. OFFSET 2 skips A and X, so it returns B, C, repeating B. X sorts before (20, B), so the keyset page is still C, D.
  • delete-A: The order becomes B, C, D, E. OFFSET 2 now skips B and the unread C, returning D, E. The saved boundary still precedes C, so keyset returns C, D.
  • move-C-15: Change C’s key from 30 to 15. The order becomes A, C, B, D, E. Offset returns B, D. Keyset returns D, E: C still exists, but it now sorts before (20, B). The complete cursor misses C too.
  • insert-Z-20: Both methods return Z, C. Z ties B on the key, but (20, Z) sorts after (20, B). Testing only for a key greater than 20 would skip Z.

With Saved cursor completeness set to Sort key only (incomplete), the simulation reports unknown, even with no change: 20 alone does not identify the boundary within a tie. That differs from an invalid Rows per page entry. Only the whole-number strings 1, 2, and 3 are accepted; 0 and 2.000000000000000001 produce no page result. Reset restores the default scenario and all three inputs, returns the step and simulated time to zero, and clears history, comparisons, and errors.

In practice

These examples compare page contents, not speed. PostgreSQL notes that rows skipped by OFFSET still have to be computed; that does not establish a fixed cost for a given offset. PostgreSQL 18: LIMIT and OFFSET

Choose the boundary that matches what “next” means for your reader. An offset counts positions in the current order. A complete cursor continues after saved values, so check whether an unread row can move behind them. If a report must reproduce the list as it stood on page one, require a fixed view across requests as well as a unique order; changing the pagination method alone does not meet that requirement.

Frequently asked questions

Editorial questions based on the listed sources and explicitly labeled local examples.

Does keyset pagination create a snapshot?

No snapshot is created in this simulation. Each page reads the current synthetic rows, so an intervening change remains visible even with a cursor.

Why include a unique tie-breaker?

Pagination needs an unambiguous order. The tuple (key, ID) includes the unique tie-breaker, and its comparison distinguishes rows with equal key values.

Can changing a sort key cause omissions?

Yes in the example: moving unread row C behind the saved cursor makes it absent from the next keyset page. A cursor does not freeze changing keys.

Does a large offset always have the same cost?

No fixed cost is claimed. PostgreSQL notes that skipped rows still have to be computed; actual work depends on the query, plan and data.

Sources & model boundaries

Primary references used for this explanation. The interactive example is an educational model, not a live check.

  1. PostgreSQL 18: LIMIT and OFFSET

    7.6 LIMIT and OFFSET. Pagination needs a predictable unique order; OFFSET skips rows of the result, and skipped rows still have to be computed.

    Checked .

  2. PostgreSQL 18: Row and Array Comparisons

    9.25.5 Row Constructor Comparison. Ordered row comparisons proceed lexicographically; the first unequal pair determines the comparison, subject to documented NULL rules.

    Checked .

Report an issue. Include the article URL and ID, not passwords or access keys.