Repository navigation
fix(usage): realign skip indexes that drifted from their definition - #14591
Conversation
ensureIndexes() added indexes with ADD INDEX IF NOT EXISTS, which matches by name only, so an index whose name is defined but whose expression or type is not was never corrected. It also left a second shape in place: production fra1 and nyc3 still carry `index-resource` on `resourceType`, because the `resource` column was renamed to `resourceType` but its index kept the old name. The definition no longer has `index-resource`, so migrate() added `index-resourceType` next to it and both now index the same column. migrate() now reads the live indexes from system.data_skipping_indices and, comparing expressions by their identifiers with the existing tokenizer, drops a defined name whose expression or type differs, and drops an index under another name that duplicates a defined one. The defined index is then added and materialized, as modifyColumns() already does, so the parts the dropped index covered are indexed again. A converged table issues no drops or materializations, so a rerun is a no-op. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Review complete. No blocking issues — approved ✅; 1 nitpick below. 🧹 Nitpicks (1) — 🟢 1 low
The change aligns live ClickHouse skip indexes with the adapter's declared schema: it now detects drifted or missing indexes and issues drop/re-add/MATERIALIZE statements to converge the table, with a new e2e test covering the schema reconciliation and a changelog entry documenting it.
Reviewed commit: 63d08a5 |
🟢 Tier S · Ready to mergeThe ClickHouse usage migration now compares live skip-index expressions and types with the definitions, removes drifted or duplicate indexes, and materializes replacements. The schema tests cover mismatched definitions, renamed duplicate indexes, preservation of unrelated indexes, and idempotent reruns.
📂 Walkthrough · 3
Reviewed |
Security rulesNo new WARNING or ERROR findings from security rules. 19 existing findings tracked in
|
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
What does this PR do?
Usage::migrate()(and sosetup()) adds skip indexes withADD INDEX IF NOT EXISTS, which matches by name only. Two shapes therefore survive every migration:index-resourceTypeonservice, orindex-resourceIdasset(0)where the definition saysbloom_filter. The name exists, so nothing happens.projects_usage_gauges(asset) andprojects_usage_events(asbloom_filter) still haveindex-resourceonresourceType. The column used to be calledresource. It was renamed toresourceType, but its index kept its old name. The library now definesindex-resourceTypeonresourceTypeand noindex-resource, somigrate()addedindex-resourceTypenext to the old index. Both now index the same column, and every merge maintains both.Note: on main the gauges definition has no
index-resourceonresource.Metric::getGaugeIndexes()coversservice, resourceType, resourceId, resourceInternalId, teamId, teamInternalId, ordinal. The onlyindex-resourceonresourceI found is on staging, which still has a legacyresourcecolumn. This PR leaves that index alone, because it duplicates nothing.The fix is in
ensureIndexes(). It reads the live indexes fromsystem.data_skipping_indices, using the querymodifyColumns()already ran (nowgetSkipIndexes()). It compares each one with the definition: the expression by its identifiers, using thegetIdentifiers()tokenizer from #14561 (no regex), and the type with spaces ignored. Then:ADD INDEX IF NOT EXISTS, as before);MATERIALIZE INDEX, asmodifyColumns()does), so the parts the dropped index covered are indexed again.Indexes that match their definition are not touched. Indexes that duplicate nothing, such as an operator's own index, are left as they are. On a converged table,
migrate()issues noDROP INDEXand noMATERIALIZE INDEX, so a rerun adds nothing tosystem.mutations. Granularity is not compared.If a run is interrupted after the drop, the next run adds the index again, and merges build it for older parts. That is the same recovery
modifyColumns()documents.Production
Running this against production is an operator decision. I have not run it anywhere except the package's local ClickHouse. On fra1 and nyc3, the next
task-usage-setupwould dropindex-resourceon both tables and runMATERIALIZE INDEX index-resourceType. That is an asynchronous mutation that builds the index for every existing part, which on the largest tables takes a while. Until it finishes, older parts are read without that skip index, which is slower but returns the same results. Regions without the stale index see no change.Test Plan
Both new tests run against a real ClickHouse 25.11 (
bin/monorepo test usage). Each starts from the mismatched shape.ensureIndexes()by name)ClickHouseSchemaTest::testMigrateRealignsSkipIndexesWhoseExpressionOrTypeDriftedindex-resourceTypeonserviceandindex-resourceIdasset(0)index-resourceTypestill onserviceMATERIALIZE INDEXissued for each,index-serviceuntouched, rerun adds no mutationClickHouseSchemaTest::testMigrateDropsAnIndexThatDuplicatesADefinedIndexUnderAnotherNameset(0)) and events (bloom_filter):index-resourceonresourceType, plus an unrelatedindex-customonmetricindex-resourcestill presentindex-resourcedropped,index-resourceTypepresent and materialized,index-customkept, rerun adds no mutationbin/monorepo test usage: 112 unit tests pass, and 277 E2E tests pass, up from 275 with the two new ones. The suite's 5 deprecations (Query::contains()inUsageBase) and 1 notice (file_get_contents()inClickHouseTest) are the same on main.bin/monorepo check usage(Pint, Rector, PHPStan) passes.Related PRs and Issues
index-resourceonresourceType.Checklist
🤖 Generated with Claude Code