Chronix

Query execution

How a Chronix query runs: layered pruning with blooms and zone maps, the inverted tag index, bounded-memory scans, caches and DataFusion.

On this page

A query reads as little as it can. It narrows the measurement's segments by time, tag index, series bloom and column statistics; inside each surviving segment it skips row groups by time, tag bloom and zone map; and only then decodes the projected columns. Scans run in bounded memory, one time-disjoint bucket at a time. SQL is DataFusion on top of the same scan; PromQL and the native QueryBuilder use it directly.

Pruning, level by level

LevelUsesSkipsMetric
Segment: timethe catalog entry's [min, max] timestampsegments outside the rangechronix_segments_pruned_by_time_and_index_total
Segment: tagsthe inverted tag indexsegments it can prove lack a filtered key=value(same)
Segment: seriesa bloom filter of the segment's series keyssegments without the queried series—
Segment: statsper-column catalog statisticssegments whose filtered tag column is all null—
Row group: timeper-row-group timestamp min/maxrow groups outside the rangechronix_segment_rg_pruned_by_time_range_total
Row group: tagsa bloom per tag column per row grouprow groups that cannot hold the valuechronix_segment_rg_pruned_by_bloom_total
Row group: fieldszone maps (min/max per column)row groups a numeric predicate rules outchronix_segment_rg_pruned_by_zone_map_total

Chronix::execute_with_stats returns PruningStats beside the result — segments_total, pruned_by_time, pruned_by_bloom, pruned_by_stats, segments_remaining — so a slow query can be explained. EXPLAIN in SQL and the PromQL explain route show the same plan from the outside.

Pruning only ever fails towards more work. The tag index is derived state, updated after the catalog, so a segment can be queryable before it is indexed. The index therefore answers the negative question — can it prove this segment lacks a pair? — and a segment it has not seen is scanned, not skipped. Zone maps follow the same rule: a decimal predicate's bounds are widened by one mantissa unit, so a value binary floating point cannot represent exactly never prunes the row group that holds it.

The structures behind it

  • Catalog. Every segment's measurement, time range, row count, size and per-column statistics. Crash-safe through an append-only manifest with CRC-32C framing and periodic snapshots; open replays the snapshot and the manifest after it.
  • Series blooms and the tag index. Rebuilt at open from each segment's .series sidecar — the segment's distinct series keys — so nothing decodes a segment to start. A missing or corrupt sidecar is rebuilt once from the segment's tag columns. The blooms use double hashing (two hashes produce all k positions) at a 1 % target false-positive rate.
  • Row-group metadata. Each column block carries min/max, null and distinct counts and, for tag columns, a bloom. A validity bitmap stored unencrypted answers "does this field have a value here?" without decoding or decrypting it, which is what keeps /labels exact.

Bounded-memory scans

Segments are written series-major, so their row groups are not in time order and cannot be merged lazily row group by row group. Time-partitioned segments rarely overlap, though, so execute_iter groups the surviving segments into time-disjoint buckets and merges one bucket at a time: memory is the largest bucket, not the result. Segments that all overlap become one bucket — slower, never wrong.

PlanPeak memory
Scanone bucket plus one 64 Ki-row output chunk
Aggregateone accumulator per group and field
Downsampleone open time bucket
Limitthe rows taken; stopping the iterator stops the reads

Within a bucket a k-way merge resolves duplicates last write wins per series and timestamp. Distinct series that share a timestamp are never merged. export_parquet drives the same iterator, so a gateway can export a window larger than its memory; execute_stream collects it and enforces max_query_result_bytes.

Order and limits. The scan's output is ascending in _time, and SQL knows it: ORDER BY _time [DESC] LIMIT n is answered by the scan itself — descending reads walk the buckets newest first — so "the latest ten rows" reads one bucket, not the table.

Parallelism. How many threads a scan may use is the database's parallelism setting: serial in ChronixConfig::small, otherwise the scan reads a bucket's segments in parallel. Segments are memory-mapped; the page cache is shared by every reader.

The native query API

QueryBuilder builds three plan shapes over a scan:

let plan = QueryBuilder::new()
    .measurement("cpu")
    .tag("host", "web-01")
    .range(start, end)
    .aggregate(AggFn::Mean)
    .group_by(&["region"])
    .build()?;
PlanResult
Scanthe rows, time-ordered and deduplicated
AggregateSum, Min, Max, Mean, Count, First, Last over the range, optionally grouped by tags
Downsamplethe same functions per time bucket, using the TimeBucket calendar SQL and rollups share

First and Last pick by timestamp, not by arrival order. Grouping chooses a sort-based path for low cardinality and a hash-based one otherwise; EXPLAIN names the choice.

SQL

SQL is Apache DataFusion 55 with each measurement as a table, scoped to the caller's namespace. Chronix adds:

  • The scan (ChronixExec): time-range and tag filters, projection, field predicates for zone maps, the _time ordering above, and statistics for the optimizer.
  • Bounded memory: a fair spill pool of per_query_memory_limit (256 MiB by default), spilling to a directory under the data directory, and target_partitions equal to the parallelism budget.
  • Epoch literals: WHERE _time >= 1700000000000000000 is rewritten to a timestamp literal before type coercion, so integer epochs work as they do on every other surface.
  • Read-only HTTP: /api/v1/chronix/sql refuses DDL, DML, COPY and session statements at the logical plan, including inside EXPLAIN.
  • Discovery: SHOW TABLES, SHOW COLUMNS and information_schema answer from the namespace's own measurements only.
  • Functions: time bucketing, counter rates, rolling statistics, STL, forecasting and anomaly scores — see the SQL reference.

Caches

CacheHoldsKeyed byKept coherent by
Last-value cache (opt-in: enable_last_value_cache)the newest point per seriesseries keyupdated on insert; series removed on delete, retention, GC and archive
Segment cachedecoded column blockssegment, row group, columnsegments are immutable; entries go when a segment is compacted away or retired

The segment cache is bounded by memory and uses W-TinyLFU admission: a frequency sketch admits a block only if it is used more often than the block it would evict, so one large scan cannot flush the hot set. Concurrent misses on the same block load it once.

Edit this page on GitHub · Updated

Esc