perf(migrations): find each value vertex once in the attr kind update
Why
Changing an attribute's kind between an indexed kind (Text, Number and others) and TextArea, JSON or List runs a migration. The migration moves each attribute to a value vertex of the new kind. It looked up that vertex once for each distinct value, and again once for each attribute:
- Toward TextArea, JSON or List,
valuehas no index, so each lookup scanned everyAttributeValuevertex in the database. - Toward an indexed kind, the first lookup also scanned every
AttributeValuevertex, because the query checked theAttributeValueIndexedlabel in aWHEREclause instead of in the pattern.
The cost grew with (distinct values + attributes) × all value vertices. Changing a Number attribute to TextArea on 3,000 nodes took 90.5 million database hits.
Goal: the cost of the migration grows with the number of attributes it changes.
Non-goals:
- The user-branch read after a kind update that #11647 pins with
test_a_kind_update_on_a_user_branch_keeps_the_values_read_on_that_branchis not fixed here. - No new database index.
Based on #11647, which is based on #11638.
What changed
Behavioral changes:
- Changing an attribute's kind to or from TextArea, JSON or List takes time in proportion to the number of attributes it changes.
- The nodes the migration selects, the edges it writes and the branches it writes them on are unchanged.
Implementation notes:
- The query groups the selected attributes by
(is_default, value)and finds or creates one value vertex for each group. - Toward an indexed kind, each group's vertex is found with one lookup on the
AttributeValueIndexedvalue index. - Toward TextArea, JSON or List:
- one pass over the selection collects the values being migrated;
- a second pass joins the selected attributes, by grouping on
(is_default, value), with one scan of the non-indexed value vertices that hold one of those values.
- Each direction has its own query template.
render_selection()is unchanged from #11647, so the count of nodes the migration will change still matches the write.
How to review
backend/infrahub/core/migrations/schema/attribute_kind_update.py:TOWARD_INDEXED_QUERYandTOWARD_LARGE_QUERY.EDGE_UPDATES_QUERYis the previous edge-write block, except thatnew_avcomes from the grouping instead of aMATCHfor each attribute.- The grouping holds one entry for each selected attribute in query memory, where the previous query used a second pass over the selection. The measurements under "Impact & rollout" show the peak query memory is lower than before.
Alternatives measured and not kept:
- Toward a large kind, reading the selection once and collecting the rows first was 4% to 26% faster. It used two to four times the query memory: 458 MB against 115 MB at 200,000 attributes with 100 values.
- Toward an indexed kind, two streaming passes with an index lookup for each attribute were 2.3 to 3.8 times slower than grouping, with no clear memory saving.
How to test
uv run pytest backend/tests/component/core/migrations/schema/test_attribute_kind_update.py \
backend/tests/component/core/migrations/schema/test_migration_second_run.py \
backend/tests/component/core/migrations/schema/test_node_kind_update_generated_kinds.py \
backend/tests/component/core/migrations/schema/test_migration_selection_count.py
Local result: 126 passed, 0 failed, 10 expected failures. All 10 expected failures are already on #11647.
test_a_kind_update_on_a_user_branch_keeps_the_values_read_on_that_branch is a non-strict expected failure, and it sometimes passes on #11647 as well. In one run of the four modules with the query from #11647, both of its cases passed.
The new test test_kind_update_reuses_value_vertices runs on the default branch and on a user branch. It changes three attributes to TextArea, then changes one of them back to Number and changes a TextArea attribute to Text. It checks the graph and does not read values back through the API. It asserts:
- the exact number of value vertices for each
(value, is_default, indexed)key, so an existing vertex is reused and a missing one is created exactly once; - that the integer 1 and the string "1", and the default 2 and the explicit 2, get separate vertices;
- one open active
HAS_VALUEedge for each node, attribute and branch; - that
verify_graphreports no violation.
The test passes against the previous query. It fails against a version that leaves is_default out of the grouping key, and against a version that lets the large-kind scan reuse indexed vertices.
Impact & rollout
- Backward compatibility: no change to the graph that the migration produces.
- Performance:
PROFILEdatabase hits and wall time, measured locally.- Setup: Neo4j 2026.10.1 Enterprise with a 4 GiB heap.
- Data: synthetic nodes on
main, one attribute each, with unique integer values. - "Unrelated values" means 20,000 extra non-indexed value vertices.
- Each run was rolled back, so both queries ran against the same data.
| Change | Attributes | Before: hits | Before: time | After: hits | After: time |
|---|---|---|---|---|---|
| Number to TextArea | 3,000 | 90.5M | 10.4 s | 513K | 0.98 s |
| Number to TextArea | 10,000 | 502M | 29.6 s | 1.71M | 1.21 s |
| Number to TextArea | 10,000, with unrelated values | 1.10B | 109.6 s | 1.77M | 1.56 s |
| TextArea to Number | 3,000 | 32.0M | 2.90 s | 336K | 0.46 s |
| TextArea to Number | 10,000 | 352M | 19.9 s | 1.12M | 0.58 s |
| TextArea to Number | 10,000, with unrelated values | 752M | 38.1 s | 1.12M | 0.78 s |
- Memory: at 200,000 attributes with 100 distinct values, peak query memory was 190.5 MB before and 115 MB after, in both directions.
- The transaction heap was about 1 GB for both queries, mostly the transaction's writes.
- With 200,000 distinct values, the new query used 219 to 236 MB of query memory. The previous query was not run at that size; its cost there is quadratic.
- The migration runs in one transaction, so
CALL ... IN TRANSACTIONSis not available. Splitting the query would not reduce the write state, so the query is not batched.
- Config/env changes: none.
- Deployment notes: no new index and no data migration.
Checklist
- Tests added/updated
- Changelog entry added (
uv run towncrier create ...) - External docs updated (if user-facing or ops-facing change)
- Internal .md docs updated (internal knowledge and AI code tools knowledge)
- I have reviewed AI generated content
🤖 Generated with Claude Code