perf(core): index the values of large attribute kinds by a key

Merged
FA
2d ago
+855-53

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:

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 SubtractionNodeByLabelsScan on AttributeValue&!AttributeValueIndexed and read 2,616,912 database entries in 367 ms.
  • The generator's Python computed attributes create a CoreGraphQLQueryGroup per target, and its JSON parameters value 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 carries value_key, the MD5 hex digest of the serialized value (get_attribute_value_key, to_db). Values of other kinds keep the index on AttributeValueIndexed(value) and get no key.
  • A RANGE index attr_value_key on AttributeValue(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 MERGE on value_key, value and is_default (node creation, attribute add). The update query builds its MERGE from to_db, so it gains the key without a change of its own.
  • A MERGE returns 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 with LIMIT 1, as the lookups they replace did (node creation, kind change). Without it, creating a node with such a value created two Attribute vertices 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 AttributeValueKeyPending in one CALL ... IN TRANSACTIONS pass. 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.
  • 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_key on AttributeValue only. AttributeValueIndexed has its own properties without the key (core/graph/schema.py), so infrahub db constraint add creates one optional STRING constraint, on AttributeValue(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 add on Memgraph would have required a key on every AttributeValue, 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 MERGE instead of a lookup, a CREATE and 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 with Index cursor cannot have transaction state with values and without values simultaneously; it failed 8 CI jobs, and 2 runs out of 5 of test_diff_repository.py::TestDiffRepositorySaveAndLoad::test_get_non_existent_diff locally. The failing query was the node_create_all of a schema node whose List attributes held equal values. With the single MERGE and the LIMIT 1 after it, the 32 tests of TestDiffRepositorySaveAndLoad passed 8 runs out of 8.
  • The MERGE cannot match an indexed value, because indexed values carry no value_key.
  • MD5 is used as a fast lookup key, with usedforsecurity=False as in api/storage/storage.py. A collision would only make a lookup read one more vertex, since the MERGE also compares value and is_default.
  • The CI run on 2dd05455d5 failed in test_graph_constraints.py, because declaring value_key added a constraint on both AttributeValue and AttributeValueIndexed. backend-benchmark failed in the same run with BufferError: Existing exports of data: object cannot be re-sized while setting up its fixtures, as another branch's run did at the same time.
  • develop is also at graph version 83 today. If develop adds 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.

QueryHow it finds a large valueDatabase 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 comparisonSubtractionNodeByLabelsScan of AttributeValue&!AttributeValueIndexed45,018
  • The key seek was planned with empty, stale and freshly sampled index statistics.
  • test_large_value_lookups_seek_the_key_index fails if one of the four queries reads AttributeValue vertices with anything other than a seek of the key index.
  • Migration 084 finds the values without a key with one SubtractionNodeByLabelsScan in its labelling pass. Each batch then reads the labelled values with a NodeByLabelScan of AttributeValueKeyPending, which test_migration_084_batch_read_scans_only_the_labelled_values checks with EXPLAIN. 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 every AttributeValue vertex (25,016 database hits in that database). The same query runs on stable. 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 MERGE of an indexed value scanned the whole AttributeValueIndexed(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_duplicated and test_query_indexed_to_not_indexed_with_duplicated_target_value failed on 2dd05455d5, with 2 Attribute vertices for one attribute and 2 active values for one attribute, and pass with LIMIT 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 key get_attribute_value_key computes 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, mypy on the changed modules, and markdownlint-cli2 on database-schema.md.

Run locally on 104786ced7, after the review:

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.11.12.1 with #11546 and this change
CoreGraphQLQueryGroup create, page hits per call48,412 at 6,500 devices, 101,700 at 12,170104 to 117 from 1,000 to 14,050 devices
CoreGraphQLQueryGroup create, time per call194 ms to 380 ms3 ms to 4 ms
Neo4j CPU per device at about 12,000 devices8.3 s5.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 AttributeValue vertices without the AttributeValueIndexed label 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