How a query becomes points — the four-stage read path, parse to aggregate
A read is four sequential stages — parse selectors, resolve labels to series IDs, decompress the matching chunks for the time range, then aggregate — and only stage 3's cost scales with how far back you look.
Writes are durable. Now flip the system: a query says {method=POST, status=500} and we need to walk from those labels to actual bytes on disk.
Scene 07
How a query becomes points
- Watch
- Try it
- Predict
- Capture
A PromQL query enters on the left. Watch it walk the four stages: parse splits text into selectors and a range, resolve hits a (still-opaque) index that emits a small set of series IDs, decompress unpacks the matching chunks for the [5m] window, and aggregate folds them into one number. The timing bar at the bottom shows where the milliseconds went.
Highlighted lines are the ones running in the diagram right now.
def query(text, t0, t1, fn):selectors, range = parseQuery(text)series_ids = resolveSeries(selectors)points = decompressAndAggregate(series_ids, t0, t1, fn,)return points
def parseQuery(text):ast = promql.parse(text)selectors = []for matcher in ast.label_matchers:selectors.append((matcher.name, matcher.value),)range = ast.range # e.g. [5m], [1h], [30d]return selectors, range
def resolveSeries(selectors):# postings list per (label, value)# cost depends on cardinality, NOT on rangepostings = [index.postings(label, value)for (label, value) in selectors]return intersect(postings) # → {S3, S7}
def decompressAndAggregate(ids, t0, t1, fn):points = []for sid in ids:for chunk in chunksFor(sid):if chunk.overlaps(t0, t1):# delta-of-delta + XOR decodepoints += chunk.decompress()return fn(points) # rate / sum / avg
Where this sits in Build a Prometheus-style time-series database
Scene 07 of 12. A read is four stages — parse, resolve label-selectors to series IDs, decompress the matching chunks, then aggregate. Stage 3 dominates.
Up next. Three of the four stages are clear. Stage 2 is still a black box — how does a database go from {method=POST} to a set of integer series IDs in microseconds?
All 12 scenes in Build a Prometheus-style time-series database · Every curriculum