Pagination and Filtering
Download JSONThis document covers PostgREST pagination, cursor pagination, column selection and embedding, filter operators, ordering, RPC pagination, and the Prefer headers that affect list responses. All table reads (GET /rest/v1/{table}) speak PostgREST's query language. RPCs that accept result_limit and result_offset (for example search_enterprise_products) paginate through arguments instead.
Pagination
| Parameter | Meaning |
|---|---|
limit=N | Max rows returned (default 1000 server-side; set explicitly: the default is not a contract) |
offset=M | Rows to skip |
GET /rest/v1/erp_products?tenant_id=eq.b2a3b20c-9816-5518-92db-f9395c063acd&limit=50&offset=100&select=id,name
For exact counts, send Prefer: count=exact: the total arrives in the Content-Range response header (0-1/14057). Count queries are expensive on large tables; do not attach count=exact to hot paths.
Deterministic paging requires order: always include an order when using offset, or rows can shift between pages as concurrent writes land.
Cursor pagination (the sync-job pattern)
Offset paging degrades as the offset grows and breaks under concurrent inserts. For bulk syncs, page on a sort key instead: filter gt the last-seen key and keep a stable order. A real sort key from this schema is erp_products.created_at
GET /rest/v1/erp_products
?tenant_id=eq.b2a3b20c-9816-5518-92db-f9395c063acd
&select=id,name,created_at
&order=created_at.desc
&created_at=lt.2026-10-11T02:55:04.425511+00:00
&limit=500
The first page (order=created_at.desc&limit=2 with count=exact) returned Content-Range: 0-1/14057: 14,057 tenant products, two newest shown with their created_at timestamps. Each subsequent request takes the last row's created_at as the next lt bound. Two details that matter
created_atis not unique: two rows can share a timestamp. For a strict guarantee of no skips and no duplicates, order on(created_at, id)and carry both as the cursor:order=created_at.desc,id.descwithcreated_at=lt.…&id=lt.…combined in anor=of the tie-break. For this catalog, timestamp ties are rare enough that most sync jobs accept the theoretical overlap and dedupe onid.- The timestamps are
timestamptz: include the+00:00offset or the server parses them in your session timezone and your cursor silently drifts.
Column selection
select= takes a comma-separated column list. It also drives embedding (joins)
GET /rest/v1/commerce_catalog_items?limit=1&select=id,name,brand,commerce_prices(price),commerce_product_media(url,is_primary)
One row returned exactly this shape
[{
"id": "6a7af36e-e679-456a-bcbb-57c39f0dec3a",
"name": "Super Snouts Dog Broad Chew Joint 6 Count",
"brand": "Super Snout Hemp",
"commerce_prices": [{ "price": 19.660000 }],
"commerce_product_media": [
{ "url": "https://cdn.shopify.com/…/733810234906.jpg?v=1731369018", "is_primary": true },
{ "url": "https://cdn.shopify.com/…/733810234906-4.jpg?v=1731369016", "is_primary": false }
]
}]
Embedding follows foreign keys in one round trip. Never use N+1 queries when an embed exists. To filter on an embedded relation's column (for example only primary media), add commerce_product_media.is_primary=eq.true as a query param: the embedded table becomes a join predicate.
Note the price renders as 19.660000: numeric, not a rounded decimal. Format for display; never parse money into a float.
Filter operators
| Operator | Meaning | Example |
|---|---|---|
eq | equals | status=eq.pending |
neq | not equals | status=neq.archived |
gt / gte | greater (than or equal) | created_at=gte.2026-10-01 |
lt / lte | less (than or equal) | price=lte.50 |
like / ilike | pattern (% wildcard) | name=ilike.%bed% |
in | set membership | status=in.(pending,shipped) |
is | null check | deleted_at=is.null |
not | negation prefix | status=not.eq.archived |
Combine with &. OR logic uses or=(a.eq.1,b.eq.2): parenthesized, comma-separated. Values with spaces or special characters must be URL-encoded; a bare % in ilike needs %25 encoding through most HTTP clients (curl sends it raw, which PostgREST accepts: other clients may not).
Ordering
order=column.desc.nullslast: direction (asc or desc) and null placement (nullsfirst or nullslast) are explicit. Multi-column: order=priority.desc,created_at.asc. If you do not specify null placement, Postgres puts nulls first on descending sorts: say what you want.
RPC pagination
search_enterprise_products takes result_limit and result_offset as JSON body (POST) or query params (GET): see the argument list in Request and Response Examples. get_bulk_availability takes the full ID array in one call (product_ids is required); chunk arrays at about 500 IDs per call so a single request does not carry the whole PLP grid.
POST /rest/v1/rpc/search_enterprise_products
{
"search_query": "orthopedic dog bed",
"result_limit": 5,
"result_offset": 0,
"p_tenant_id": "b2a3b20c-9816-5518-92db-f9395c063acd"
}
An RPC can also be called as GET with query params (/rpc/search_enterprise_products?search_query=…&result_limit=5), but POST with a JSON body is the reliable form: GET hits URL-length limits on long queries and some clients mangle the encoding.
Embedding pitfalls
- To-many embeds multiply.
commerce_product_media(url)returns an array per catalog item; selecting two to-many embeds on one request fans out the row count and the payload. Use one embed per request on hot paths. - Embed filters are inner joins. Adding
commerce_product_media.is_primary=eq.trueas a query param turns the embed into a filter: catalog items with no primary media vanish from the result set. If you want "items with their primary image where one exists," that is a left join PostgREST cannot express in one request; fetch the items and filter client-side, or use two calls. - Select inside the embed is independent.
commerce_prices(price)returns onlypriceper price row: but it returns every price row for the item (all lists, all history) unless you add the price-list predicate. The checkout route's query pinsprice_list_idto theRETAILlist andvalid_to >= now(); copy that predicate.
Prefer headers worth knowing
| Header | Effect |
|---|---|
Prefer: count=exact | Content-Range carries the total |
Prefer: return=representation | POST and PATCH return the written row or rows: the default for POST; send return=minimal to skip it |
Prefer: tx=rollback | Runs the request in a rolled-back transaction: the safe way to dry-run a write |
tx=rollback lets you verify a POST or PATCH end to end (validation, constraints, triggers) against the API without changing anything. Use it when testing write payloads for the first time.
Rules
- Every list endpoint a client exposes must pass an explicit
limit. The server default is not a contract. ilike.%…%scans do not use indexes: fine for admin search, not for hot paths. Hot paths use the search RPCs.- Filtering on
tenant_idis mandatory for service-role callers (see Overview). The tenant UUID isb2a3b20c-9816-5518-92db-f9395c063acdfor the marketplace. - Offset paging is for humans clicking "next". Cursor paging is for machines syncing data. Use the right one.
Prefer: count=exactis a separate query plan: profile before attaching it to anything hot.