Skip to content

fix(api): cap the result size of PromQL table-function queries - #3846

Open
bcanvural wants to merge 1 commit into
ClickHouse:mainfrom
bcanvural:fix/promql-table-function-size-cap
Open

bcanvural wants to merge 1 commit into
ClickHouse:mainfrom
bcanvural:fix/promql-table-function-size-cap

Conversation

@bcanvural

@bcanvural bcanvural commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Closes #3836

Problem

With promqlBackend: table-function, PromQL queries ran with max_result_rows (100,000) as the only result limit. prometheusQueryRange() returns one row per series with all its samples, so the row limit does not bound the size. One wide range query without aggregation returned a body too large for the API: Invalid string length, then the process was OOM-killed. The HTTP backends stop reading at 8 MB (PROMETHEUS_MAX_RESPONSE_BYTES) and fail the same query cleanly.

Change

  • max_result_bytes (8 MB, PROMETHEUS_MAX_RESULT_BYTES) next to max_result_rows in PROMQL_TABLE_FUNCTION_SETTINGS. This covers range and instant queries.
  • ClickHouse counts these bytes in memory. The JSON it sends is about 3 times larger, so a result just below the limit is about 27 MB of JSON. That is far below the size that crashed the API.
  • A query over the limit now fails with a 400: Limit for result exceeded, max bytes: 8.00 MiB, current bytes: 11.23 MiB.

Testing

  • New int tests: /query_range and /query on the table-function path send max_result_bytes. Both fail without the change.
  • Checked by hand on ClickHouse 26.8 with 3,000 series × 241 samples over 1 h at a 15 s step:
    • Without the limit, the query returns a 38 MB JSON body.
    • With the limit, ClickHouse fails the query with TOO_MANY_ROWS_OR_BYTES (HTTP 500) before it sends any data, and the API returns the 400 above.

Note

Low Risk
Defensive query limit on the PromQL table-function path; reduces OOM risk without changing auth or data models.

Overview
Adds an 8 MB byte cap (PROMETHEUS_MAX_RESULT_BYTES) on PromQL evaluations that use the ClickHouse table-function backend (prometheusQuery / prometheusQueryRange). Row limits alone were unsafe because each result row is a full series with all samples, which could OOM the API on wide range queries.

The limit is applied via ClickHouse max_result_bytes in PROMQL_TABLE_FUNCTION_SETTINGS (instant and range queries). Oversized results fail with a Limit for result exceeded style error instead of crashing the process, aligning memory bounds with the existing 8 MB cap on proxied HTTP Prometheus responses.

Integration tests assert /query and /query_range on the table-function path send max_result_bytes to ClickHouse.

Reviewed by Cursor Bugbot for commit 67ea546. Bugbot is set up for automated code reviews on this repo. Configure here.

prometheusQueryRange() returns one row per series with all its samples, so
max_result_rows did not bound the response. One wide range query could return
a body large enough to run the API out of memory. Set max_result_bytes (8 MB)
next to max_result_rows, like the 8 MB read limit of the HTTP backends.

Closes ClickHouse#3836
@bcanvural
bcanvural requested a review from a team as a code owner November 27, 2026 09:39
@vercel

vercel Bot commented Nov 27, 2026

Copy link
Copy Markdown

@bcanvural is attempting to deploy a commit to the HyperDX Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot

changeset-bot Bot commented Nov 27, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 67ea546

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@hyperdx/api Patch
@hyperdx/app Patch
@hyperdx/otel-collector Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

Copy link
Copy Markdown
Contributor

PR Review

✅ No issues found.

@github-actions

Copy link
Copy Markdown
Contributor

Deep Review

✅ No critical issues found.

This is a tight, well-scoped fix: a single ClickHouse max_result_bytes (8 MB) setting added to PROMQL_TABLE_FUNCTION_SETTINGS, applied to both the range and instant table-function paths. The error-to-HTTP mapping is already sound — both /query_range and /query handlers wrap the call in try/catch and return a 400, so a TOO_MANY_ROWS_OR_BYTES overflow surfaces cleanly as status: error rather than crashing the process. The byte cap is a strict improvement over the previously unbounded result size.

🟡 P2 -- recommended

  • packages/api/src/routers/api/__tests__/prometheus.int.test.ts:1409 -- the new test only asserts that max_result_bytes is forwarded to ClickHouse; the headline behavior (an over-limit result being rejected and mapped to a 400) is never exercised, so a regression in the error-to-400 path would go uncaught.
    • Fix: add a focused test that forces overflow — e.g. evaluate a query with a tiny max_result_bytes override, or unit-test the overflow-error → 400 mapping — to prove the cap changes behavior, not just that a setting is passed.
🔵 P3 nitpicks (2)
  • packages/api/src/routers/api/__tests__/prometheus.int.test.ts:1410 -- the assertion compares the forwarded setting against the same imported PROMETHEUS_MAX_RESULT_BYTES constant, so a change that mis-sizes the cap to a uselessly large value would edit code and test together and still pass; it only guards against the setting being dropped.
    • Fix: assert a concrete literal (e.g. String(8 * 1024 * 1024)) so an accidental change to the constant's value is caught.
  • packages/api/src/controllers/timeseriesEngine.ts:413 -- PROMETHEUS_MAX_RESULT_BYTES and promql.ts's PROMETHEUS_MAX_RESPONSE_BYTES are both 8 * 1024 * 1024, both documented as "8 MB" to keep the two backends aligned, but live as independent literals that can silently drift.
    • Fix: if the alignment is intended to hold, derive one from the other or add a cross-reference assertion; otherwise leave a note that the match is coincidental, not contractual.

Reviewers (9): correctness, testing, maintainability, project-standards, reliability, kieran-typescript, previous-comments, agent-native, learnings.

Testing gaps: Error-path coverage for the result-size cap (ClickHouse rejecting the body → router 400 mapping) is absent; only the happy path and setting-forwarding are exercised.

No finding blocks merge automatically. A maintainer will expect P0/P1 findings in code this PR changes to be fixed; P2/P3 are your call -- fix or reply. Never fix findings about surrounding code here; reply instead. Do not widen the PR. How to respond

@bcanvural

Copy link
Copy Markdown
Contributor Author

Thanks. No code change for these:

  • P2 (no over-limit test): going over 8 MiB needs about 730,000 samples seeded in the test. This file already notes that the router's limits are constants "far above anything a test can exceed". The ClickHouse error goes through the same try/catch to a 400 as the missing-table test on this path. I checked the over-limit case end to end by hand (see the PR description): the API returns 400 Limit for result exceeded, max bytes: 8.00 MiB, current bytes: 11.23 MiB.
  • P3 (literal in the assertion): the test guards against the setting being dropped, which is the bug in PromQL table-function backend has no response size limit: one wide range query can OOM the API #3836. The value itself is reviewed where it is defined.
  • P3 (two 8 MB constants): they measure different things. One is ClickHouse's result size in memory, the other the HTTP body we read. The comment on PROMETHEUS_MAX_RESULT_BYTES says the JSON is about 3 times larger. Deriving one from the other would suggest they are equal, and they are not.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PromQL table-function backend has no response size limit: one wide range query can OOM the API

1 participant