Repository navigation
Description
Summary
Sibling of #7322, found by that issue's completeness sweep. #7322 put the last-point path on the
bounded TimeSeriesEngine.queryDescending(..., limit). The head path has the same shape and is
still unbounded.
PostTimeSeriesQueryHandler.executeRawQuery reads a caller-supplied limit, then:
final List<Object[]> rows = engine.query(fromTs, toTs, columnIndices, tagFilter);
...
final int count = limit > 0 ? Math.min(rows.size(), limit) : rows.size();TimeSeriesEngine.query merges every shard's full range into one ArrayList and sorts it, so
{"from": 0, "to": 9999999999999, "limit": 10} over a type holding millions of samples costs
O(N) heap and O(N log N) time to serialize ten rows.
The handler's own source already says so:
Checked here, before the JSON is built, so the ceiling at least keeps the second and larger copy
of the range out of the heap - it cannot keep the first one out, becauseengine.query()above
materializes the whole range before any limit is known. That is a bound on the fetch, and it
belongs in the engine, not in this handler.
Why it was out of scope for #7322
#7322's fix is queryDescending(..., 1, ...), which walks blocks newest-first. This endpoint
returns rows ascending, so the descending scan answers a different question and cannot be
substituted. The engine has iterateQuery, a lazy ascending merge whose memory is
O(shards x block) rather than O(rows) - but the handler takes a List, and the hard
maxResultRows ceiling in the middle of the method is written against a materialized list whose
size is already known.
Consequence
An operator polling /ts/query with a small limit and a wide time range - the natural way to
ask "show me the ten most recent rows" over HTTP - pays for the entire range on every call. The
maxResultRows guard added in #5719 refuses to serialize an oversized result, but only after the
allocation it is meant to prevent has already happened.
Suggested fix
Move the bound into the fetch: consume iterateQuery (or a new bounded ascending form on
TimeSeriesEngine) and stop at limit, deciding at the same time how truncated and the
maxResultRows refusal are determined without a fully materialized list - reading one row past the
limit is enough to tell a cut response from a complete one.
Verification
- A test asserting block-level work stays bounded for a small
limitover a wide range, in the
shapeIssue5414LastPointTest.descendingScanTouchesOnlyTheNewestBlocksusesAggregationMetrics - The existing
TimeSeriesQueryHandlerIT.rawQueryReportsTruncationcontract must stay green
byte-for-byte:count,limit,truncated, and the 400 on an out-of-intlimit
Activity
fix(#7336): bound the fetch of POST /ts/{database}/query instead of t…
robfrank commentedon Sep 16, 2026
Fixed in #7679, merged to
main.POST /ts/{database}/queryapplied the caller'slimitonly while serializing, afterTimeSeriesEngine.queryhad already merged every shard's full range into one sorted list - so asking for ten rows over a wide range cost O(N) heap and O(N log N) time regardless. The bound now belongs to the fetch: a new ascending row-capped path stops reading once the limit is satisfied, so the cost tracks what you asked for instead of what the range holds.Still open
TimeSeriesQueryRPC has the identical defect, and a comment inArcadeDbGrpcServicecurrently claims the opposite.queryAscendingentry point this PR added exists for it./tsquery and Prometheus remote-read routes have no row limit and nomaxResultRowsceiling at all. A different defect shape: no bound, rather than a bound applied late.WARNINGlogged on a default-cap truncation no longer states the total row count - counting it was the O(N) walk this change removes.❤️ Thanks for helping make ArcadeDB better! ArcadeDB is free and open source, sustained by its community. If it's useful to you or your company, please consider becoming a sponsor to keep development going.