StoicBuilder

HandbookXI the TabulariumPagination

XIthe Tabularium, Model the data

Pagination

1 min readUpdated 28 November

In plain words

Pagination splits a long list into pages, either by offset (skip 40, take 20) or by cursor (take 20 after this row), so responses stay small and fast however many rows exist.

Where you see it

A Harbor customer has 30,000 invoices. GET /v1/invoices returns 50 at a time with a next_cursor value, and the client sends it back as ?cursor= for the next 50 until has_more is false. Stripe's API pages the same way, with starting_after and has_more.

Cursor pagination in SQLdatabase
-- first pageselect * from invoices where team_id = $1order by created_at desc, id desc limit 50; -- next page: continue after the last row you returnedselect * from invoices where team_id = $1 and (created_at, id) < ($2, $3)order by created_at desc, id desc limit 50;
The id breaks ties between invoices created in the same instant, so no row is skipped or shown twice.

What goes wrong

  1. Offsets on a big, busy table

    offset 100000 still makes the database walk past 100,000 rows, and when rows are added while someone pages, items shift and get skipped or repeated. Use a cursor for long or changing lists.

  2. Sorting by a column that isn't unique

    Ordering only by created_at lets rows with the same timestamp swap places between requests. Add the id as a final tiebreaker.

  3. No upper limit on page size

    ?limit=1000000 turns a paginated endpoint back into a dump of the whole table. Set a default page size and a maximum, and cap anything larger.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
Add cursor pagination to the invoices list endpoint: order by created_at and id, return next_cursor and has_more, and default to 50 per page with a maximum of 100. Make sure an index covers the filter and the sort, and add a test that pages through 250 invoices without skipping or repeating any.

Quick check

Sign in to earn +10 XP for it

A Harbor customer exports invoices page by page using offsets while new invoices keep arriving. Some invoices appear twice in the export. Why?

Got a prompt that touches pagination?

Paste it. You get a better prompt first, then the cards that explain it.

Check your own prompt

Free, no sign-up.