Ownership in the sort key makes test_file_mappings inserts non-idempotent
Background
shared.test_file_mappings records which source files each test exercises. The coverage export in gitlab_quality-test_tooling writes it from gitlab-org/gitlab CI about every two hours.
Its consumers:
- Four materialized views in
code_coverage, refreshing hourly, behind the sole-coverage, risk-summary and flaky-impact dashboards - Four dashboards in runbooks, reading the table directly: two CI health, two coverage
It surfaced while investigating observer's ingestion backlog, on the theory that these views were starving observer of memory. That theory holds. query_log appeared to refute it, but it only records server-side rejections, and about 95% of observer's export failures time out client-side and never reach it. Measured in Cloud Logging instead, 514 of 661 failures in 24 hours fall in minutes 00-08, the window where all four views used to refresh together.
Problem
The table holds 151.8M rows to store 11.6M mappings, and it grows on its own. Nothing changed on 2026-08-22. It crossed a size threshold.
The chain runs like this:
- ClickHouse decides whether two rows are the same row by comparing their sort key. Ours is
(ci_project_path, section, stage, test_file, source_file). - Two of those columns describe who owns the test, and ownership changes whenever stages.yml is reorganised.
- So a re-owned mapping arrives looking like a brand new row, and the old one stays forever.
- Because the engine cannot collapse those duplicates, the exporter has to. Before each run it reads back every mapping for the project, roughly 17.6M rows, and skips the ones that have not changed.
- That read grows with the table. Past a point it exceeds the memory cap, and the rescue clause inserts everything anyway.
- Which makes the table bigger, and the next read more expensive.
WARN -- [CodeCoverage] Failed to fetch existing mappings: ClickHouse query failed: code: 500,
error: {"exception": "Code: 241. DB::Exception: (total) memory limit exceeded: ...
INFO -- [CodeCoverage] Successfully pushed 11567802 new/changed records
to shared.test_file_mappings (skipped 0 duplicates)Two runs on 2026-08-28 added 23.1M rows between them.
Three multipliers stack to the 8.6x:
| multiplier | cause |
|---|---|
| 1.86x | ownership sits in the sort key, so re-owned rows fork instead of replacing |
| 2.40x | monthly partitioning, and merges never cross a partition boundary |
| 1.93x | duplicates the merges have not caught up on |
Two things are already broken because of it:
| what | state |
|---|---|
| The two CI health dashboards | anyLast(group) returns an arbitrary owner whenever a test has more than one, and about 80% of test files do |
| The two coverage dashboards | They filter on the stamped columns across seven sites, so their section, stage and group pickers match fewer and fewer rows as the table converts to blank, with no error |
| Three of the four materialized views | Failing their refreshes with out-of-memory errors, 28 in the retained window, 2.6 to 3.8 GiB each |
The view failures cluster in the first two minutes of the hour, when all four refresh at once: 17 of the 28. system.view_refreshes reports every one as healthy, because it keeps only the latest attempt. Counts are summed across both replicas, since query_log is per-replica.
Proposed solution(s)
Make the inserts idempotent. The exporter's read-back then stops being load-bearing, and its failure stops mattering.
Ownership is a property of the category, not of a mapping, and ownership is the half that churns. Four steps, each safe on its own:
- Resolve ownership in the four runbooks dashboards from
shared.category_ownersrather than the columns stamped on each row, deduped the way the gem'sCategoryOwnersTablenow does it.ci-health-incidentsandci-health-incident-detailreadanyLast(group);code-coverage-actionablesandcode-coverage-health-checkfilter on the columns across seven sites. A plain join oncategorywould reproduce the ambiguity, since 54 of 178 categories carry several rows. Works against today's schema, and must ship before step 2. - Stop the gem sending
group,stageandsection. - Rebuild the table on
ORDER BY (ci_project_path, test_file, source_file), without the monthly partitioning and without those three columns.categorystays as a plain column, so changing it replaces the row rather than forking it. - Narrow the exporter's read-back to the test files in the batch, so its cost stops growing with the table.
Step 3 is the substantial one. ORDER BY cannot be altered in place, so it means a new table, a backfill over 151.8M rows, and a swap, run by hand against ClickHouse Cloud.
What makes this durable rather than more headroom: once step 3 lands, the skip in step 4 is only an optimisation. If it fails, it inserts duplicates the engine quietly collapses. Even re-inserting everything on every run would settle at roughly 17.6M rows, which also takes the heaviest views from about 303M rows read to 35M.
Alternatives considered:
| alternative | what it buys | why it is not the fix |
|---|---|---|
| Stagger the view refreshes | Relieves the collision at the top of each hour | Headroom only, and the loop stays intact. Worth holding in reserve |
| Shorten the three-month TTL | Halves the table for one statement | Trades retention, and the loop stays intact |
| Incremental aggregation for the views | The right long-term shape for an hourly full-table scan | Stays correct only once the source is deduplicated, so it belongs after this work |