I put TimescaleDB under our metrics and then kept doing its job by hand
About five weeks ago we moved our metrics store onto PostgreSQL with the TimescaleDB extension. I wrote the migration, created the hypertables, switched our range queries over to time_bucket, and called it done. Last week it became clear that I had adopted TimescaleDB as a place to put rows and nothing more. Retention, roll-ups, and backfill were all still being done by our own controller code, serialized through a single writer connection, exactly as they had been before the extension existed in the project. That is the opposite of what the database was brought in for, and it was my mistake to make and my mistake to miss, twice.
What the code actually did
TimescaleDB ships the lifecycle of time-series data as background jobs: retention policies that drop whole chunks on a schedule, continuous aggregates that the database refreshes incrementally, compression policies. None of it needs an application writer in the loop.
What we had instead:
- Retention was an hourly sweep in the controller. For the hypertables it called
drop_chunksitself, inside a transaction on our one writer connection, after first counting the rows it was about to drop. For every other time-keyed history (usage hours, test results, runs, commands, and so on) it was plain tables and row-by-rowDELETEs in batches, on that same writer. - Roll-ups were a hand-written aggregate table. Every write of a test run upserted into it, the retention sweep recounted its edge day, and a second controller sweep backfilled it one run per pass.
- Bucketing on read used
time_bucket. This was the one part I got right.
So the hypertable was there, and the thing it exists to make unnecessary was still running alongside it.
What it cost
The clearest cost was an outage. A large aggregation ran inside the writer transaction, took tens of seconds per call, and starved every other write for over an hour. Machine reports, numbering, mirror polling: all stuck behind derived work that should never have touched the application at all.
The second cost was the fix for that outage. I diagnosed the cause correctly, “aggregate work on the serialized writer”, and then responded by making the controller sweep do smaller pieces per pass, and writing that sweep into the design as a requirement. I treated the symptom and codified the disease. A continuous aggregate with a refresh policy would have removed the aggregation from the writer entirely and needed no backfill sweep at all.
The smaller costs add up too: a migration whose own comment records that counting history filled the disk during a rehearsal; a sweep loop, a measurement sweep, chunk-drop bookkeeping, and recount logic that each carry their own tests; and, most importantly, five weeks in which the stated intent for the metrics store was not met while the extension capable of meeting it was deployed the whole time.
Where I should have stopped and asked
Looking back there were three distinct points where the question “what does the database already do for this?” should have been asked, and I walked past all three.
When the design adopted the engine. The design rows I added said where data lives and how it is partitioned and bucketed. They said nothing about who drops it or who aggregates it. An older row stating that the controller prunes its own measurements stayed in force, and everyone downstream, including me, followed it faithfully. Adopting a purpose-built engine without reassigning the operations it performs natively leaves the old assignments authoritative by default. I should have struck the pre-Timescale retention row in the same change and replaced it with: retention of every series is a database policy the controller configures from the operator’s setting; any aggregate over a series is a continuous aggregate; compression is a policy too.
When I ported the sweep. The commit that called create_hypertable also edited the existing prune loop to swap a DELETE for a drop_chunks. That edit was the moment to notice the loop should not survive. A native call appearing inside a hand-rolled mechanism is a signal to remove the mechanism, not adapt it.
When I built the roll-up. A per-test, per-day aggregate over a time-keyed history, maintained by upserts and recounts, is the textbook case for a continuous aggregate. I built it by hand against a plain table, which meant no Timescale feature could even be attached to it later without another migration. I should have filed the design gap and made the source table a hypertable first.
Why it happened
Path dependence, mostly. The sweep predated the database. The migration ported the mechanism and swapped one call inside it, and nobody, me included, re-asked which operations the new engine now owned. Then the incident fix reinforced the pattern instead of breaking it, because optimizing something that exists is easier to reach for than deleting it.
What I am changing
- Retention becomes a TimescaleDB retention policy per series. The controller’s only job is to set or alter the policy when the operator’s setting changes and to verify on start that the policy matches. The sweep goes away for every table that qualifies.
- The test for “qualifies” is simple: anything pruned by age alone is a series and belongs in a hypertable under a policy. Records pruned by some other condition (open alerts, unfinished operations, running instances) stay with the controller, and the design will say so explicitly rather than by omission.
- Roll-ups become continuous aggregates with refresh policies, with real-time aggregation over the unrefreshed tail on read. No application code maintains a derived table. No backfill sweep.
- Compression gets a policy while I am there, since the same reasoning applies.
- The design rows that currently require a controller sweep get rewritten, not amended, so there is no stale row left for the next implementer to correctly follow into the same hole.
The general lesson
When a system adopts a purpose-built engine, every operation that engine performs natively must be either assigned to the engine in writing or explicitly retained by the application with a stated reason. Application code that reimplements a feature its dependency ships is custom code where none should exist, however well it is written and tested. And when an incident’s root cause is “this work runs in the wrong place”, a fix that keeps the work in the same place in smaller pieces is a cheap fix, and cheap fixes to placement problems are a red flag I will treat as one from now on.