Postgres: Paginate query results
The limit & offset arguments
offset are used for pagination.
limit specifies the number of rows to retain from the result set and
offset determines which slice to retain from the results.
You can see the complete specification of the
arguments in the API reference.
The following are examples of different pagination scenarios:
Example: Fetch the first 5 authors from the list of all authors:
Limit results from an offset
Example: Fetch 5 authors from the list of all authors, starting with the 6th one:
Limit results in a nested object
Example: Fetch a list of authors and a list of their first 2 articles:
Keyset cursor based pagination
Cursors are used to traverse across rows of a dataset. They work by returning a pointer to a specific row which can then be used to fetch the next batch of data.
Keyset cursors are a column (or a set of columns) of the data that are
used as the cursor. The column(s) used as the cursor must be unique and
sequential. This ensures that data is read after a specific row rather
than relying on the position of the row in the dataset as done by
offset, and that duplicate records are not fetched again.
For example, consider the following query to fetch a list of authors
where clause used in place of
Here we are fetching authors where the value of
id is greater than 5.
This will always skip the previously fetched results which would have
been ids 1 to 5, ensuring no duplicate results. Column
id is acting as
the cursor here, unique and sequential.
The choice of cursor columns depends on the order of the expected
results i.e. if the query has an
order_by clause, the column(s) used
order_by need to be used as the cursor.
Columns such as
id (auto-incrementing integer/big integer) or
created_at (timestamp) are commonly used as cursors when an order is
not explicit, as they should be unique and sequential.
Keyset cursor based pagination using
where is more performant than
offset because we can leverage database indexes on the columns
that are being used as cursors.
Fetch limited results along with data aggregated over all results (e.g. total count) in the same query
Sometimes, some aggregated information on all the data is required along with a subset of data.
E.g. the total count of results can be returned along with a page of results. The count can then be used to calculate the number of pages based on the limit that is set.
Example: Fetch a list of articles where a certain condition is true and get their count. Then limit the number of articles to return.