Skip to article
TOOLS. SYSTEMS. BETTER DECISIONS.
Stack & Method

The journal

What Is an Offset in Pagination? Follow Six Rows Before and After an Insert

Trace a PostgreSQL limit and offset example, including exactly which row repeats when the sorted list changes between two requests.

Two stacks of blank index cards lie on a dark desk.
One group represents rows skipped before you read the next slice.
Stack & MethodStack & Method

At a glance

An offset skips a number of ordered matches before the query returns its next slice. A new item before the next slice can shift one record onto both pages, even with identical sort keys.

Compare the offset with a cursor

An offset tells a paginated query how many ordered rows to skip. With a page size of three, the start of the first one-based page has offset zero, and the start of the second has offset three. The offset refers to positions in the current result, so those positions may move between requests.

Get a stable order before numbering positions

Use a list with a repeatable sort. Here a set of six fictional tasks is ordered by priority and then by ID. Priority can repeat, so ID settles ties. The sorted IDs are 10, 20, 30, 40, 50 and 60. A limit of three with offset zero returns 10, 20 and 30.

When the same query uses offset three, it skips the first three matches in that ordered list. The next three are 40, 50 and 60. PostgreSQL’s documentation notes that without an order that gives you a unique sequence, different limit and offset queries are free to return inconsistent subsets. Do not assume, for example, that equal-priority tasks will always come back in insertion order.

The first three IDs are 10, 20 and 30, and the next three are 40, 50 and 60.
Limit three returns up to three rows. The offset locates their starting window.

For the SQL definition of skipping and limiting, see LIMIT and OFFSET in PostgreSQL 16.

Sources: LIMIT and OFFSET in PostgreSQL 16 · SELECT in PostgreSQL 16

Insert one item before the next request

In a local PostgreSQL example, inserting priority-one task 5 after the first query changed the sorted order to 5, 10, 20, 30, 40, 50 and 60. Offset three still skipped three items. It now returned 30, 40 and 50. Task 30 appeared on the first page already, and 60 moved beyond this particular window. The tiebreaker was identical in every query. The result set was what changed.

Try the same check using an empty list and a last page with fewer than three items. The SQL limit is a maximum. Offset counts all matching rows before the slice, so a page after the end can be empty even when other pages contain results.

Readback from the local PostgreSQL fixture.
RequestLimit / offsetReturned IDs
Page one3 / 010 · 20 · 30
Page two on original data3 / 340 · 50 · 60
Page two after inserting ID 53 / 330 · 40 · 50
After insertion of ID 5, page two contains 30, 40 and 50, so ID 30 was seen on page one too.
Sorting stays the same. The slice begins at a new record because the list changed.

Read how a later statement can see newly committed rows in Transaction isolation in PostgreSQL 16.

Sources: Transaction isolation in PostgreSQL 16 · LIMIT and OFFSET in PostgreSQL 16

Choose what the reader expects to remain stable

On a frozen report, offset can make a jump to a numbered page clear. For a live stream or a large export you may need different behavior. A cursor anchored to the last item or a stable dataset snapshot can help, depending on what sort keys can change and how the data is read. An offset API should state its page numbering and any restrictions on deep pages.

Record one fixture before making your choice. Count what is skipped, write the returned IDs and repeat after an insert. You can then decide whether a repeated row is acceptable for your interface or whether the list needs a different contract.

Rows describe fixed results, live results and the final page.
Make the numbering and behavior at the end explicit in the consumer’s contract.
A row of blank cards leaves a narrow slot at one end.
Adding something early can move the visible boundary for the next page.

Test the contract

  • Choose a unique ordering for the query.
  • Record the numbering convention and page size.
  • Test empty, final and changed result sets.
  • Decide whether a repeated or skipped item is acceptable.

Choose the boundary the client follows

Offset

Starts at a numbered position within the current sorted result.

Cursor

Uses a saved boundary from the last record according to the API’s contract.

For the supported SQL shape and ordering, see SELECT in PostgreSQL 16.

For the related task, see Compare the main API pagination choices.

Sources: Transaction isolation in PostgreSQL 16 · LIMIT and OFFSET in PostgreSQL 16

Frequently asked questions

Is offset the number of pages to skip?

Offset counts ordered rows. The page number needs to be converted according to the API’s page size and numbering.

What does the limit specify?

It is the maximum number of rows the query can return after skipping the offset.

Why can a large offset be slower?

PostgreSQL still has to compute the skipped rows. Measure the actual query before choosing how to page deeply.

How do I avoid a position shift in a frozen export?

Read the same fixed dataset for the export. Determine how your application can consistently keep that snapshot.

Stack & MethodStack & Method

See how a cursor defines its next boundary

Compare the end of one page with the start of the next.

Compare the offset with a cursor

Continue with a related guide

References

Your privacy choices

Optional analytics helps us improve these guides. It stays off until you allow it. Privacy details