perf(core): index the values of large attribute kinds by a key
Why
Before Infrahub writes an attribute value, it looks for an equal value vertex to reuse. A value of a large attribute kind (TextArea, JSON, List) has no AttributeValueIndexed label and no index, so that lookup reads every AttributeValue vertex of the database:
- Creating a node runs the lookup twice for each such attribute (
core/query/node.pyon stable). - Updating such an attribute merges on
AttributeValue {value, is_default}, which has no index either (core/query/attribute.py).
Measured on a local run of the xx-large dataset generator of infrahub-private-tests with Infrahub 1.12.1, with 1,289,603 AttributeValue vertices of which 36,108 were not indexed:
- One lookup ran as
SubtractionNodeByLabelsScanonAttributeValue&!AttributeValueIndexedand read 2,616,912 database entries in 367 ms. - The generator's Python computed attributes create a
CoreGraphQLQueryGroupper target, and its JSONparametersvalue is unique per group. Creating one group read 48,412 pages at 6,500 devices and 101,700 pages at 12,170 devices, and took 194 ms and 380 ms.
The local measurements come from Neo4j's query log and are not linkable.
What changed
- Only a value of a large kind, the values without
AttributeValueIndexed, now carriesvalue_key, the MD5 hex digest of the serialized value (get_attribute_value_key,to_db). Values of other kinds keep the index onAttributeValueIndexed(value)and get no key. - A RANGE index
attr_value_keyonAttributeValue(value_key)(core/graph/index.py). A RANGE index only holds vertices that have the property, so indexed values are not in it. - Node creation and attribute adds find or create such a value with a single
MERGEonvalue_key,valueandis_default(node creation, attribute add). The update query builds itsMERGEfromto_db, so it gains the key without a change of its own. - A
MERGEreturns every vertex that matches. Concurrent writes can leave two equal values, and migration 084 gives both the same key. Node creation and the kind change to a large kind keep one match withLIMIT 1, as the lookups they replace did (node creation, kind change). Without it, creating a node with such a value created twoAttributevertices with the same name, and a kind change gave the attribute two active values. - Cypher cannot compute the hash:
- A kind change to a large kind first reads the distinct values to convert, hashes them in Python, and passes them to its query as a map (
attribute_kind_update.py). - Migration 084 labels the values without a key
AttributeValueKeyPendingin oneCALL ... IN TRANSACTIONSpass. It then reads at most 1000 labelled values at a time, hashes them in Python, and sets each key and removes the label in one write. Last, it creates the index (m084_index_attribute_value_keys.py). A run that stops part way leaves the label, and the next run continues from it. Like migration 079, it only runs on Neo4j.
- A kind change to a large kind first reads the distinct values to convert, hashes them in Python, and passes them to its query as a map (
- Saving an attribute compares the stored value without
value_key, so an unchanged value stored before this change is not written again (core/attribute.py). - The graph schema declares
value_keyonAttributeValueonly.AttributeValueIndexedhas its own properties without the key (core/graph/schema.py), soinfrahub db constraint addcreates one optionalSTRINGconstraint, onAttributeValue(value_key). - Memgraph constraints can only require a property to exist. An optional property now gets no Memgraph constraint (
core/graph/constraints.py). Before,infrahub db constraint addon Memgraph would have required a key on everyAttributeValue, indexed values included. - Database schema documents the property, how an existing value is found, and why a lookup keeps one match. It also documents the temporary label that an interrupted migration 084 can leave.
What stayed the same: indexed values, the GraphQL API and the stored values themselves.
How to review
- One
MERGEinstead of a lookup, aCREATEand a second lookup. The first version of this PR looked the value up through the key index before and after creating it. Neo4j 2026.05 enterprise sometimes rejected that query withIndex cursor cannot have transaction state with values and without values simultaneously; it failed 8 CI jobs, and 2 runs out of 5 oftest_diff_repository.py::TestDiffRepositorySaveAndLoad::test_get_non_existent_difflocally. The failing query was thenode_create_allof a schema node whose List attributes held equal values. With the singleMERGEand theLIMIT 1after it, the 32 tests ofTestDiffRepositorySaveAndLoadpassed 8 runs out of 8. - The
MERGEcannot match an indexed value, because indexed values carry novalue_key. - MD5 is used as a fast lookup key, with
usedforsecurity=Falseas inapi/storage/storage.py. A collision would only make a lookup read one more vertex, since theMERGEalso comparesvalueandis_default. - The CI run on
2dd05455d5failed intest_graph_constraints.py, because declaringvalue_keyadded a constraint on bothAttributeValueandAttributeValueIndexed.backend-benchmarkfailed in the same run withBufferError: Existing exports of data: object cannot be re-sizedwhile setting up its fixtures, as another branch's run did at the same time. developis also at graph version 83 today. Ifdevelopadds its own migration 084 before this reaches it, this one needs a new number in the stable-to-develop merge.
Index use, checked with EXPLAIN and PROFILE
Checked locally on Neo4j 2026.05 enterprise with 20,000 indexed and 5,000 large values. The plans are not linkable.
| Query | How it finds a large value | Database hits of the lookup |
|---|---|---|
Node creation (NodeCreateAllQuery) | Seek of AttributeValue(value_key) | 3 to 5 |
Attribute update (AttributeUpdateValueQuery) | Seek of AttributeValue(value_key) | 1 |
Attribute add (AttributeAddQuery) | Seek of AttributeValue(value_key) | 1 |
Kind change to a large kind (AttributeKindUpdateMigrationQuery) | Two seeks of AttributeValue(value_key) | 1 and 18 |
Lookup on stable, for comparison | SubtractionNodeByLabelsScan of AttributeValue&!AttributeValueIndexed | 45,018 |
- The key seek was planned with empty, stale and freshly sampled index statistics.
test_large_value_lookups_seek_the_key_indexfails if one of the four queries readsAttributeValuevertices with anything other than a seek of the key index.- Migration 084 finds the values without a key with one
SubtractionNodeByLabelsScanin its labelling pass. Each batch then reads the labelled values with aNodeByLabelScanofAttributeValueKeyPending, whichtest_migration_084_batch_read_scans_only_the_labelled_valueschecks withEXPLAIN. The run time of the migration was not measured.
Not changed by this PR, and measured in the same database:
- A kind change to an indexed kind looks for an existing value with
OPTIONAL MATCH (existing_av:AttributeValue ...), a scan of everyAttributeValuevertex (25,016 database hits in that database). The same query runs onstable. Making it a seek puts two lookups of one index in one query, the pattern that failed on Neo4j 2026.05 enterprise above, so it is left for a separate change. - The
MERGEof an indexed value scanned the wholeAttributeValueIndexed(value)index right after a bulk load, and seeked again once Neo4j resampled the index statistics.
How to test
uv run pytest backend/tests/unit/core/graph
uv run pytest backend/tests/component/core/test_attribute_value_key.py backend/tests/component/core/migrations/graph/test_084_index_attribute_value_keys.py backend/tests/component/core/migrations/schema/test_attribute_kind_update.py backend/tests/component/core/graph/test_graph_constraints.py
Run locally on 5397e81094:
- The new tests
test_create_links_one_value_when_equal_keyed_values_are_duplicatedandtest_query_indexed_to_not_indexed_with_duplicated_target_valuefailed on2dd05455d5, with 2Attributevertices for one attribute and 2 active values for one attribute, and pass withLIMIT 1. - A kind change of Number, Boolean and empty Text values to TextArea gave each value the MD5 of its serialized form (
5,true,false,NULL), which is the keyget_attribute_value_keycomputes for it (a temporary test, not committed). - Full backend component suite on Neo4j 2026.05 enterprise: 5,867 passed, 20 skipped, 9 expected failures (
xfail), 1 unexpected pass (xpass), no failures (pytest -n 10 backend/tests/component). - Backend unit tests: 5,126 passed. The 155 failures and 30 errors are all in
backend/tests/unit/git, where this workspace's global git hook refuses the commits the tests make. ruff,mypyon the changed modules, andmarkdownlint-cli2ondatabase-schema.md.
Run locally on 104786ced7, after the review:
test_migration_084_keys_values_across_batcheskeys 1,001 values in two batches of at most 1,000, and checks that no value keeps the label. On41f32d9073, it failed when the loop was changed to stop after the first batch.test_migration_084_keys_values_left_labelled_by_an_interrupted_runkeys values left labelled by an earlier run together with values that have no label.test_migration_084_batch_read_scans_only_the_labelled_valuesfails when the batch read of41f32d9073is put back, because that read is aSubtractionNodeByLabelsScanofAttributeValue&!AttributeValueIndexed.test_084_index_attribute_value_keys.pyandtest_attribute_value_key.py: 9 passed.ruffandmypyon the migration, andmarkdownlint-cli2ondatabase-schema.md.
The full component suite was not run again after the review.
Not run locally: the integration and functional suites.
Measured with #11546 on 1.12.1
A local xx-large dataset run of 1.12.1 with #11546 and the first version of this change (the 1000-character prefix key, same index):
| 1.12.1 | 1.12.1 with #11546 and this change | |
|---|---|---|
CoreGraphQLQueryGroup create, page hits per call | 48,412 at 6,500 devices, 101,700 at 12,170 | 104 to 117 from 1,000 to 14,050 devices |
CoreGraphQLQueryGroup create, time per call | 194 ms to 380 ms | 3 ms to 4 ms |
| Neo4j CPU per device at about 12,000 devices | 8.3 s | 5.2 s |
Neo4j CPU per device still grows with the data in that run, from node_list_get_attribute and ip_prefix_reconcile, which neither change touches.
Impact & rollout
- Backward compatibility: the upgrade runs migration 084. It reads the
AttributeValuevertices without theAttributeValueIndexedlabel once to label the values without a key, then hashes those values 1000 at a time. - Performance: finding an equal large value becomes an index seek. Each value of a large kind gains a 32-character property.
- Config/env changes: none.
Checklist
- Tests added/updated
- Changelog entry added (
uv run towncrier create ...) - External docs updated (if user-facing or ops-facing change): not needed
- Internal .md docs updated (internal knowledge and AI code tools knowledge)
- I have reviewed AI generated content