// SQL snippet

Offset pagination pitfalls

OFFSET reads and throws away the skipped rows, and inserts move pages underneath you.

Main statement
PostgreSQL

The task

Page 400 of your ORDER BY date DESC listing takes seconds, and items sometimes appear on two pages or vanish between clicks.

The snippet

Written for PostgreSQL.

PostgreSQL
SELECT id, customer_id, order_date, amount
FROM orders
ORDER BY order_date DESC, id DESC
LIMIT 20 OFFSET 7980;

Fonctionnement

OFFSET 7980 tells the engine to walk 8000 rows and throw away the first 7980, so deep pages get slower linearly. Any row inserted meanwhile shifts the offset by one, pushing an item from page N to page N+1 (skipped) or pulling one up (duplicated). The sort keys plus a keyset cursor fix both problems; OFFSET stays fine for shallow, mostly-static lists.

Variations

Same list with keyset pagination

SELECT id, customer_id, order_date, amount
FROM orders
WHERE (order_date, id) < ('2026-06-01 12:00:00', 1000)
ORDER BY order_date DESC, id DESC
LIMIT 20;

Questions that come up

Is OFFSET always bad?

No. For the first few pages, admin tables with hundreds of rows, or data that changes rarely, OFFSET is simpler and fast enough. The trouble starts when offsets reach tens of thousands of rows or the list changes while users browse.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.