Repository navigation
perf(query): bind rate/gauge window bounds to hit the compile cache - #496
Merged
Merged
Conversation
vishr
enabled auto-merge
November 16, 2026 01:13
vishr
force-pushed
the
fix/compile-cache-params
branch
from
November 16, 2026 01:17
937241e to
30667cf
Compare
Rollup rate panels and metric rate/gauge panels rendered their start, end and rollup-freshness nanosecond bounds directly into the SQL text, so every time window produced a unique statement. The engine's snapshot-compile cache is keyed by that text, so each refresh missed and paid json_serialize_sql, a Go decode, rewriteKeys and json_deserialize_sql (twice for metrics) on the read connection inside the panel's slot. Emit those bounds as ? placeholders and append their values to the compiled Args in the left-to-right order the placeholders appear, so the values ride as bind parameters while the statement text is identical across windows of the same panel. The snapshot binder already tolerates placeholders (window bounds in the WHERE clause were already parameters, and the serialize/rewrite/deserialize round trip preserves positional parameter order); this extends the same treatment to coveredSecondsSQL, metricSpreadCTE, metricCarryCandidatesCTE and metricGaugeCTE. Bucket widths and window durations stay literals because they are constant for a fixed relative range, so they do not vary the text. Results are unchanged: each placeholder binds the exact value that was the literal. Also make the compile cache an LRU that evicts the single least-recently- used entry at capacity instead of clearing all 128, so a burst of one-off statements no longer discards a dashboard's stable, repeatedly-run ones. Measured across 200 sliding one-hour windows of the same metric rate panel: compile-cache hit rate rises from ~0.5% to 100% and end-to-end read latency drops from ~102ms to ~66ms per refresh, with no result change. Adds TestRatePanelCompiledTextIsWindowStable (text identical and placeholder count equals arg count across windows), TestCompileCacheEvictsOneEntryNotAll, and a hit-rate benchmark. Closes #479
vishr
force-pushed
the
fix/compile-cache-params
branch
from
November 16, 2026 02:28
30667cf to
f232683
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Rollup
ratepanels and all metric rate/gauge panels rendered thestart,end, andFreshTonanosecond window bounds as literals directly into the SQL text (coveredSecondsSQLinrollup_compile.go;metricSpreadCTE/metricCarryCandidatesCTE/metricGaugeCTEinmetric_buckets.go). The snapshot-compile cache (file_snapshot.gocompileSnapshot, keyed by statement text) therefore missed on every window, payingjson_serialize_sql→ Go decode →rewriteKeys→json_deserialize_sql(twice for metrics) on the read connection inside the panel slot. The cache also cleared wholesale when it reached 128 entries, evicting stable entries.Closes #479.
Fix
Bind the nanosecond bounds as
?parameters instead of rendering them as literals, so the compiled statement text is identical across time windows of the same panel and the compile cache hits. Each CTE builder returns its window values in the exact left-to-right order of the placeholders it emits, and callers concatenate SQL and args in lockstep — the same discipline the existingbuildWhere/metricSeriesCTEwindow placeholders already use. Bucket widths, window durations, and bucket origin stay literals because they are constant for a fixed relative range and do not vary the text. Every bound binds the exact value that was previously the literal, cast to the same type (::BIGINT/::DOUBLE), so results are byte-identical.Switch the compile cache to an LRU.
snapshotQueriesis now amap[string]*list.Elementover acontainer/listMRU list (stdlib, no new dependency) guarded bysnapshotMu; at capacity (128) it evicts the single least-recently-used entry instead of clearing all of them.Correctness
Positional
?survive thejson_serialize_sql→rewriteKeys→json_deserialize_sqlpipeline with indices intact (the snapshot-source CTEsbindSnapshotprepends contain only literals, never placeholders, so they do not shift indices). The decisive evidence is that the existing golden numeric tests —TestRollupCoveredSecondsAndInterval,TestMetricRateUsesCoveredElapsedTime,TestHistogramRateUsesPerSeriesCoveredElapsedTime, the gauge/carry/spread suites — all pass unchanged; a wrong positional order would bind wrong values and change those numbers.New tests:
TestRatePanelCompiledTextIsWindowStable— rollup rate, metric rate, and metric gauge-avg panels produce identical compiled text across different windows, with?count equal to args count and differing args.TestCompileCacheEvictsOneEntryNotAll— fills the cache to 128, overflows by one, and asserts all 128 survivors hit and only the single LRU entry missed (fails against the old wholesale clear).Measured improvement (Apple M3 Max, 200 sliding 1h windows, same metric rate panel)
(Local micro-benchmark proving the mechanism; full throughput validation follows the usual Hetzner/release process.)
Verification
just test "./internal/query/..."andjust test "./internal/engine/...",just vet, andjust check-static(gofmt, deadcode, behavioral test names) all green. Arbitrary-SQL read-only single-SELECT guarantee intact — the new parameters are engine-internal window bounds, never user text.