Cursor Pagination in Postgres: Why OFFSET Gets Slow

Cursor Pagination in Postgres: Why OFFSET Gets Slow

So everybody is saying OFFSET is slow, and we too, but how much slow, we didn't have a number, and on our laptop here is Postgres 18, so why not to make a million fake articles and just look, one index on created_at together with id and nothing else.

CREATE INDEX CONCURRENTLY articles_feed
  ON articles (created_at DESC, id DESC);

Our numbers are these. Page one 0.022 ms. Offset hundred thousand, 6.4. Offset nine hundred thousand, 58 ms and 9,185 buffers. The 6.4 did not scare us, what we did not like is the line rows=100020 in the index scan, because we asked for 20, so the other hundred thousand it was reading only for throwing in the garbage, and if a bot is going through your pages one by one then your database does this garbage work again on each of them, deeper and deeper.

After that same question but without OFFSET, we give to the query the created_at and id of the last row we saw and say give us smaller than this, and answer was 0.064 ms with 4 buffers, which is funny, because page one also had 4 buffers, so deep or not deep it costs the same.

SELECT id, title, created_at
FROM articles
WHERE (created_at, id) < ('2026-09-30 14:02:11.482913+00', 88121)
ORDER BY created_at DESC, id DESC
LIMIT 20;

Then we made a mistake on purpose, well, a thing which looks not like a mistake at all, we wrote the condition with OR, created_at smaller, or equal and id smaller, school logic, same result. And 40 ms. What? In the plan is written Rows Removed by Filter 900000, so it was going from the top of the index again and checking every row, and all what we were winning is gone. With the brackets form it lands in Index Cond. We tried it only on this one version and this one table, so check on yours.

About the id in the end, we thought to show why it must be there, so 500 rows got exactly the same created_at, plus one old row, and after the first twenty we asked for everything what is older than that time, and Postgres gave us one row, one, where are the other 480, they are just skipped and nobody will tell you. When id is also in the cursor, 481 comes back and this is right.

Microseconds was the nasty one. Our 100 rows had difference only in microseconds, cursor after the first page stopped on .482081 and the next query found 80 rows, good, then we cut the value to .482, the thing which JavaScript date is doing to it, and found zero. Zero! Your frontend will think this was the last page. So the time has to travel as a text with all six digits, don't let anything round it.

So for ourselves we decided, admin page with 300 rows stays on OFFSET, there people like clicking page 7 and the cursor can't do this trick, but a feed or public API we would start on cursor, otherwise one day we have to break somebody's client, we mean it is in our API design post too, and the Ruby which is lower makes the cursor to Base64 and back, with garbage it returns nil, and Base64 is not a lock, anyone reads it, and for indexes we have the partial and covering one.

class ArticleCursor
  def self.encode(article)
    data = [article.created_at.utc.iso8601(6), article.id]
    Base64.urlsafe_encode64(data.to_json, padding: false)
  end

  def self.decode(value)
    time, id = JSON.parse(Base64.urlsafe_decode64(value))
    [Time.iso8601(time), Integer(id)]
  rescue ArgumentError, TypeError, JSON::ParserError
    nil
  end
end
def index
  scope = Article.order(created_at: :desc, id: :desc)

  if params[:after].present?
    time, id = ArticleCursor.decode(params[:after])
    unless time
      return render json: { error: "invalid_cursor" }, status: :bad_request
    end

    scope = scope.where("(created_at, id) < (?, ?)", time, id)
  end

  rows = scope.limit(21).to_a
  page = rows.first(20)
  cursor = ArticleCursor.encode(page.last) if rows.size > 20

  render json: { data: page, next: cursor }
end