Skip to main content
Cursor pagination lets you run a query once and then read its results page by page. The broker saves the full result after the first API call. Each later page is read from that saved copy, so the query is never run again.
At a glance
  • First API call: POST /query/sql?getCursor=true&numRows=N runs the query and returns page 1 plus a requestId, brokerHost and brokerPort.
  • Subsequent calls: GET /responseStore/{requestId}/results?offset=X&numRows=N returns any page you ask for. Through the StarTree proxy, also send the FORWARD_HOST and FORWARD_PORT headers.
  • Cleanup: stored results expire and are deleted automatically.

Overview

Without cursors, paging through a large result usually means sending the same query again and again with a different LIMIT and OFFSET. Each of those calls makes Pinot do the full query work again. With cursors, the broker runs the query one time, saves the full result, and hands you pages of it on request. Use cursor pagination when:
  • A query returns more rows than you want to send to a client in one response.
  • You are building a UI with “next page” or “load more” controls.
  • You are exporting or processing a large result in batches.
  • You want every page to come from the same snapshot of results.

How it works

The broker runs your query once, saves the whole result in its response store, and then serves pages from that saved copy. Servers are only involved in the first API call. On StarTree Cloud, calls go through the StarTree proxy, which forwards follow-up calls to the broker named in the FORWARD_HOST and FORWARD_PORT headers.
  1. Your app sends the query with getCursor=true. The broker runs it on the servers like any other query.
  2. The broker saves the full result to its response store and gives it an ID (requestId).
  3. The broker returns the first page, the requestId, the total row count, and its own address in brokerHost and brokerPort.
  4. For each later page, your app asks the same broker for rows starting at an offset. The broker reads them from the stored result. The query is not run again.
  5. After the expiration time, the broker deletes the stored result automatically.

Prerequisites

Cursor pagination must be enabled on your environment’s brokers. Ask StarTree Support to enable it. Support can also tune the page size and how long stored results are kept. See Broker configuration for the settings involved and Sizing and tuning for how to choose values.
How expiration and the cleaner work together. A stored result expires after the expiration time, and the cleaner deletes expired results on its next run. For example, with a 15-minute expiration and a cleaner that runs every 10 minutes, a result is removed between 15 and 25 minutes after the query. Ask for an expiration longer than your clients need to page through a result.

Paging through results

A first API call opens the cursor, and subsequent calls fetch the remaining pages. In all examples, replace xxx.startree.cloud with your broker URL, and <YOUR_TOKEN>, <your_table>, <REQUEST_ID>, <BROKER_HOST> and <BROKER_PORT> with your own values. The examples use a result of 17 rows read with a page size of 10. Page 1 returns rows 0 to 9 and page 2 returns the remaining 7.

Run the query and open a cursor

Send the query with POST /query/sql, adding getCursor=true and your page size numRows as URL parameters. Put the SQL in the JSON body as usual.
getCursor and numRows must go in the URL. They are not query options, so putting them inside queryOptions in the body does nothing.
The response holds the first page of data in resultTable, plus the cursor fields: A first-page response with a page size of 3 looks like this (trimmed):

Fetch the next pages

Call GET /responseStore/{requestId}/results with the offset of the first row you want and the page size in numRows. Through the StarTree proxy, add the FORWARD_HOST and FORWARD_PORT headers with the brokerHost and brokerPort values from the first response.
To walk through the whole result, increase offset by your page size each time (0, 10, 20, …) until offset reaches numRowsResultSet. The last page may hold fewer rows than you asked for, and its numRows field shows the actual count.

Reach the right broker

The stored result lives only on the broker that ran the query. Other brokers do not have it. Use one of these two options for every follow-up call:
  1. Call the same broker directly: send every page fetch to the broker named in brokerHost and brokerPort in the first response.
  2. Use the StarTree proxy URL with forward headers: send the call to the proxy broker URL with FORWARD_HOST: <brokerHost> and FORWARD_PORT: <brokerPort>. The proxy forwards the call to exactly that broker.
The proxy does not route by requestId. Without forward headers, the proxy sends /responseStore calls to a random broker. That works only when the cluster has a single broker. With several brokers, a call can land on a broker that does not hold the result and fails with “not found”.
How the proxy picks the target broker for a /responseStore call: Sending only FORWARD_HOST fails with this error:
Use the values exactly as returned. The proxy does not check FORWARD_HOST against its broker list; it forwards to whatever address you send. Copy brokerHost and brokerPort from the first response. brokerHost is usually an internal cluster hostname. That is expected, because the proxy resolves it inside the cluster.

Stop at the total

An offset equal to or larger than numRowsResultSet returns an error, not an empty page. With 17 stored rows, valid offsets are 0 to 16.
The same error comes back for offset=17, because it equals the total, and the broker returns it as an HTTP 500 error. Stop paging once offset reaches numRowsResultSet. An offset inside the range with a large numRows is fine: it simply returns the rows that are left.

Clean up (optional)

Stored results are deleted automatically after they expire. To free the space right away when you are done, delete the cursor. Through the proxy, this call also needs the forward headers.

Parameters

There are two kinds of settings: request parameters and headers you send with each call, and broker configuration that sets the page size and storage behavior.

Request parameters and headers

Broker configuration

These are the cursor settings a broker reads. To change any of them, contact StarTree Support. Durations accept the units d, h, m and s, and combinations such as 1h30m. With the defaults, a stored result can stay on disk for up to 2 hours (expiration plus one cleaner cycle).

Good to know

  • Stay on the same broker. Always fetch pages from the broker that ran the query: either call the broker in brokerHost directly, or call the StarTree proxy URL with both FORWARD_HOST and FORWARD_PORT headers. The proxy does not track which broker owns a requestId, so without these headers a call can land on a different broker and fail with “not found”.
  • Results expire. A stored result lives for the expiration time, and may stay up to one more cleaner cycle before it is deleted. After that, run the query again to get a new cursor.
  • Check broker disk size. If your cursor QPS is very high, check the broker disk size. Stored results stay on the broker’s disk until they expire, so a high volume of cursor queries could fill the disk. See Estimate broker disk.
  • Pick a sensible page size. Each page fetch re-reads the stored result, so very small pages over a very large result are slower. Pages of hundreds or thousands of rows work better than pages of 10.
  • The query still runs in full. Cursors page the delivery of results, not the query itself. The query’s LIMIT and the broker’s memory limits still apply, and only read (SELECT) queries can open a cursor.
  • Results are a snapshot. Every page comes from the result saved when the query ran, so new data ingested later does not appear until you run the query again.

Sizing and tuning

These values are not standard. The numbers in this section are examples to give a rough idea only. Every workload is different: query rate, result size and paging behavior vary from customer to customer. Measure your own workload and share it with StarTree Support so the brokers are sized and configured to match.

Estimate broker disk

Each cursor query writes its full result to the disk of the broker that ran it. A result can stay on disk for up to expiration + cleaner.frequencyPeriod before it is deleted.
Example (illustrative only): N = 100 per minute, B = 2, X = 5 MB, E = 15, C = 10:
The stored file is JSON, so it is usually larger than the raw data. Measure X from bytesWritten on real queries rather than estimating it.
Keep an extra buffer of 3× to 5×. Base it on two things: the total size of your query results, and how much your cursor QPS can burst above the normal rate N. Use the higher end when result sizes vary a lot (some results are much larger than the average X) or when traffic comes in large bursts. The buffer also covers uneven load across brokers, temp files written during a save, and a broker outage that moves its load to fewer brokers.

Choose expiration and cleaner period

  • expiration (E): set it to the longest time a client needs to page through one result (p99), plus a margin. A paging session that outlives E fails with “not found”.
  • cleaner.frequencyPeriod (C): keep it small next to E, about E/3 to E/2. The worst-case lifetime of a result is E + C, so a smaller C keeps disk use close to the estimate. A cleaner run only scans and deletes expired results, so running it often is cheap.
  • The defaults (1h / 1h) keep results up to 2 hours, which uses about 4–5× more disk than 15m / 10m at the same query rate.

How many cursor queries a broker can handle

There is no fixed cursor-specific QPS limit. Capacity depends on four things:
  1. Query capacity: every first call (getCursor=true) is a full query, so normal broker and server capacity and table query quotas apply.
  2. Disk: use the estimate in Estimate broker disk.
  3. Broker memory: the broker builds and writes the whole result before it returns page 1. Many large results at once put pressure on broker heap.
  4. Page-fetch load: page fetches read from the stored files, so many fetches over large results cost broker CPU and disk I/O.
Load test with your expected query rate and result sizes to find the real limit for your cluster.
Test at peak QPS and watch the brokers. Run the load test at your peak cursor QPS, including expected bursts, with realistic result sizes and paging patterns. During the test, monitor every broker and confirm it stays stable:
  • CPU: no sustained saturation; usage settles at a steady level.
  • Memory: JVM heap does not keep growing, no long or frequent GC pauses, no out-of-memory errors.
  • Disk: response-store usage levels off once the cleaner starts deleting expired results.
  • Latency: first-call and page-fetch latency stay within your targets.
If CPU or memory is not stable at peak, add brokers, increase broker resources, or reduce result sizes before going to production. Keep monitoring these metrics after go-live.
Set a disk alert. Alert when the response-store volume reaches about 80% full. If the real query rate or result size is higher than estimated, the alert fires before the broker disk fills.