← Back to PostgreSQL, page 3

postgresql-0058 · Medium mid

Paginate a large table with keyset pagination using a row-value comparison.

Keyset pagination remembers the last row's sort key and asks for rows after it: WHERE (created_at, id) < ($1, $2) ORDER BY created_at DESC, id DESC LIMIT n. The row-value comparison handles ties on created_at correctly, and a matching composite index lets each page start directly at the right place, unlike OFFSET, which reads and discards earlier rows.

Full answer

Test data:

sql
CREATE TABLE posts (id int PRIMARY KEY, created_at date, title text);
INSERT INTO posts SELECT g, date '2026-12-01' - (g / 3), 'post ' || g FROM generate_series(1, 30000) g;
CREATE INDEX posts_created_id ON posts (created_at DESC, id DESC);
ANALYZE posts;
sql
SELECT id, created_at FROM posts ORDER BY created_at DESC, id DESC LIMIT 3;

PostgreSQL returns:

text
 id | created_at
----+------------
 2  | 2026-12-01
 1  | 2026-12-01
 5  | 2026-11-30
(3 rows)
sql
SELECT id, created_at FROM posts WHERE (created_at, id) < ('2026-11-30', 5) ORDER BY created_at DESC, id DESC LIMIT 3;

Result on PostgreSQL 17:

text
 id | created_at
----+------------
 4  | 2026-11-30
 3  | 2026-11-30
 8  | 2026-11-29
(3 rows)
sql
EXPLAIN (COSTS OFF) SELECT id FROM posts WHERE (created_at, id) < ('2026-11-01', 90) ORDER BY created_at DESC, id DESC LIMIT 20;

Result:

text
Limit
  ->  Index Only Scan using posts_created_id on posts
        Index Cond: (ROW(created_at, id) < ROW('2026-11-01'::date, 90))

The first page returns posts 2, 1 and 5, ending at (2026-11-30, 5). The next page asks for rows that sort after that pair and continues with posts 4 and 3, which share 30 November, then post 8 from 29 November; comparing created_at alone would have skipped or repeated the tied rows. The plan is an index-only scan with the row comparison as its index condition, so page 1,000 costs about the same as page 1. Keyset pages cannot jump to an arbitrary page number, which suits infinite scroll and APIs with next-page cursors.

sql
CREATE TABLE posts (id int PRIMARY KEY, created_at date, title text);
INSERT INTO posts SELECT g, date '2026-12-01' - (g / 3), 'post ' || g FROM generate_series(1, 30000) g;
CREATE INDEX posts_created_id ON posts (created_at DESC, id DESC);
ANALYZE posts;

SELECT id, created_at FROM posts ORDER BY created_at DESC, id DESC LIMIT 3;

SELECT id, created_at FROM posts WHERE (created_at, id) < ('2026-11-30', 5) ORDER BY created_at DESC, id DESC LIMIT 3;

EXPLAIN (COSTS OFF) SELECT id FROM posts WHERE (created_at, id) < ('2026-11-01', 90) ORDER BY created_at DESC, id DESC LIMIT 20;

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • Remember the last row's key
  • Row-value comparisons handle ties
  • Composite index in the same order
  • Constant cost per page
  • No random page jumps

Common mistakes

  • Using OFFSET for deep pages
  • Comparing only a non-unique column

Follow-ups

  • How would you paginate backwards?
  • How do you encode the cursor for an API?

Tips

  • Show the tie handling