Skip to content

POST /ts/{database}/query materializes the whole range before applying 'limit' #7336

Closed

Description

@robfrank

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, because engine.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 limit over a wide range, in the
    shape Issue5414LastPointTest.descendingScanTouchesOnlyTheNewestBlocks uses AggregationMetrics
  • The existing TimeSeriesQueryHandlerIT.rawQueryReportsTruncation contract must stay green
    byte-for-byte: count, limit, truncated, and the 400 on an out-of-int limit

Activity

  1. added
    server
    timeseries
    on Sep 9, 2026
  2. added this to the 26.10.1 milestone on Sep 10, 2026
  3. self-assigned this
    on Sep 16, 2026
  4. added
    in progress
    on Sep 16, 2026
  5. added a commit that references this issue on Sep 16, 2026
  6. robfrank commented on Sep 16, 2026

    @robfrank
    CollaboratorAuthor

    Fixed in #7679, merged to main.

    POST /ts/{database}/query applied the caller's limit only while serializing, after TimeSeriesEngine.query had 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


    ❤️ 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.

  7. removed
    in progress
    on Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

performanceservertimeseries

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions