How do you do clean pagination?

Hi :wave:

I am trying to get the data paginated, and I am trying all the methods and actions but still stuck while doing that. here is the methods I tried


   read :list do
      pagination do
        keyset? true
        offset? true
        countable true
        default_limit 10
      end
    end
 read :list do
      pagination do
        offset? true
        countable true
        default_limit 10
      end

      filter expr(true)
    end

I found all the result variables like count,more, before, after but while reading the documentation i found that the methods gives counts expect i got “nil” and for list i am doing this,

  1. List item
 allcontect =
      People
      |> Ash.Query.for_read(:list)
      |> Ash.read!()
  1. List item with count and pagination
 page = 1
    limit = 10
    offset = (page - 1) * limit

    allcontect =
      People
      |> Ash.Query.for_read(:list)
      |> Ash.Query.limit(limit)
      |> Ash.Query.offset(offset)
      |> Ash.read!()

It give me only first 10 records but I am not able to fetch others while handling next_page and previous_page events.

Take a look at the pagination guide: Pagination — ash v3.5.6

Pagination is done via Ash.Query.page or via the page option to Ash.read, i.e

query
|> Ash.Query.page(limit: 10, offset: 20, count?: true)

or

Ash.read!(page: [limit: 10, offset: 20, count?: true])

Thanks, it’s very help-full for me i used this,

Ash.read!(page: [limit: 10, offset: 20, count?: true])

this is working actually and i tested for thousands of data but i am wondering it can help me to handle millions of data or not?

As long as your offset or keyset page id is indexed that is fine (and PKs generally are).

1 Like

Thanks! That makes sense.

Look into keyset pagination. It will perform well on practically any amount of data with properly indexed data

1 Like

Link (v3.5.6 as of 2025-APR-18): Ash Keyset Pagination

btw, is this relating to cursor pagination? :thinking: I can’t find it mentioned anywhere.

Or, since we are here on this topic, is keyset pagination the same as e.g. “give me 20 records where ID is greater than 157”?

1 Like

Yes. I should add “also known as cursor pagination” somewhere on that page to make it easier to find.

edit: done

2 Likes