Status: Accepted
Context#
Rollups were kept at full resolution for ROLLUP_RETENTION (30 days) and then
deleted. A tenant asking how this month compared with the same month last year
got nothing back, and QUERY_MAX_RANGE was 31 days because there was nothing
older to reach. Keeping every one-minute window for a year multiplies storage
by twelve for data almost always read at hourly or daily steps.
Three designs were credible:
- Keep everything longer. No new code, but storage and index size grow with the retention period, and the hot table every write touches grows with it.
- Downsample on a schedule into a second table, reading the old windows, writing the hours, then deleting the windows. Simple to write, but between the insert and the delete a window is counted twice, a crash between them leaves it counted twice for good, and two concurrent runs both archive it.
- Move windows atomically. One statement deletes a chunk of expired
windows and merges them into the archive, so a window is either in
rollupsor in an archived hour, never both and never neither.
Decision#
Atomic moves, implemented in store.CompactRollups
into the rollups_archive table of migration 0010.
- One statement per chunk. A CTE deletes up to 10,000 expired windows
(
FOR UPDATE SKIP LOCKED, so concurrent aggregators take different rows), groups what it deleted by series and hour, and upserts the hours. The delete and the merge commit together or not at all. - The merge is the same merge the aggregator already does. Counts and sums
add, minimum and maximum widen, the latest event's
lastwins (ties broken toward the greater value, as for live windows), and histogram buckets add element-wise throughfluxgate_array_add. Merging is associative and commutative, so the order chunks, compactors and late replays arrive in does not change the result. - Late replays still count once. A delivery replayed after its hour was archived writes a new full-resolution window, which the next pass moves into the existing hour. The delivery ledger, which is retained independently, still suppresses duplicates.
- Reads take both tiers. Queries, label listings and stepped queries read
rollups UNION ALL rollups_archive; the two never hold the same window. A step finer than an hour over archived data carries the existing warning that stored windows straddle the step. - The window must divide an hour. Configuration rejects an archive with an
AGGREGATOR_WINDOW_SIZEthat does not, since a window crossing an hour boundary would be credited entirely to the hour it started in.
Consequences#
Measured. Against PostgreSQL, three concurrent compactors moved 10,050 windows across chunk boundaries and every observation was counted exactly once. When the loop was changed to stop on the number of hours written rather than windows consumed, the same test found 169 windows instead of 10,050, and a merge that overwrote counts instead of adding them was caught too. Archived hours read exactly as the windows they replaced did -- count, sum, minimum, maximum, last value and every histogram bucket -- for hourly and coarser steps. The packaged verifier checks the same property through the restricted aggregator role.
What it costs. One table with the same indexes as rollups; a second scan
in every read, which the tenant/metric/time index keeps narrow; and a year of
hourly rows, one sixtieth of a year at one-minute windows. Quantiles over
archived hours are as accurate as the fixed histogram layout always was, since
buckets merge losslessly.
What it does not cover. There is one archive resolution. Daily rollups for
multi-year history would be a third tier compacted the same way. Sub-hour
detail older than ROLLUP_RETENTION is gone by design; a tenant who needs it
needs a longer ROLLUP_RETENTION, not this.
When to revisit. If reads spanning both tiers become a measurable share of query latency, a view maintained per hour or a partitioned table would let the planner skip a tier. If tenants need years of history, add a daily tier before lengthening the hourly one.