perf: read only changed data in rebase, merge checks and diff update

Merged
FA
2d ago
+441-127

Rebase, merge checks and diff update read data in proportion to the graph, not to the change

On a branch with one changed attribute, three parts of publishing the change to main read data in proportion to the size of the graph:

  • RebaseBranchQuery matches every relationship of the graph with a pattern that names no relationship type, then keeps the relationships of the branch (#11705).
  • The rebase, the merge, and the Schema Integrity check of a proposed change check the rules of a changed attribute (kind, optional, regex, length, enum, choices) on every node of the kind. The kind check returns one row for each node that has a value, so it also runs once for each page of 5,000 rows (query_size_limit), and each page reads the kind again. On the synthetic dataset below, this was most of the time of the rebase and of the merge (#11706).
  • DiffPropertyNodesQuery matches four relationship types in one pattern, so no index can serve it, and each diff update reads every attribute and relationship vertex of the graph (#11707).

Closes #11705.

Partly addresses #11706: with this change, the merge still runs queries that read every vertex of a label (see the section on the remaining merge queries below).

Addresses #11707: measured on the synthetic dataset only, because the performance tests used below do not time the diff update.

Changes to the rebase, the attribute checks and the diff update

  • Rebase: RebaseBranchQuery (backend/infrahub/core/query/branch.py) reads each of the 8 relationship types on its own, in one direction, through its branch index with USING INDEX SEEK. IS_RESERVED was the only type without a branch index, and this pull request adds it (backend/infrahub/core/graph/index.py). A unit test requires a branch index on every edge type. A branch index alone would not change the 1.12.2 query: a relationship index serves only a pattern that names its type, and the 1.12.2 pattern names none.
  • Attribute checks: ConstraintValidatorDeterminer adds to each attribute constraint raised by a data diff the uuids of the nodes, profiles and templates of the kind where that attribute changed, or no list when the diff identifies none. Only a checker that sets the new reads_listed_nodes flag receives the list, because AggregatedConstraintChecker removes it for the others: the kind, optional, regex, length, enum and choices checkers, and the node uniqueness checker, which already read a list. They read only the listed nodes, through the uuid index of the Node label (SchemaValidatorQuery.match_nodes). The list is passed to the validation task inside quote(). These constraints are used by the rebase, the merge and the Schema Integrity check of a proposed change.
  • What stays the same for the checks: a constraint from a schema change, or on a relationship, has no list and checks every node. When the same constraint comes from a schema change and from the data diff, the merged constraint has no list. The unique check of an attribute receives no list and still compares every node, because a changed value can match a node that is not in the list.
  • Diff update: DiffPropertyNodesQuery (backend/infrahub/core/query/diff.py) reads the IS_PROTECTED, HAS_SOURCE, HAS_OWNER and HAS_VALUE relationships of the time window through their from and to indexes, with a helper it shares with DiffFieldNodesQuery, then finds the node of each changed attribute or relationship once.

This pull request adds one index, on the branch of IS_RESERVED relationships, and no migration or schema change. The API server creates a declared index when it starts, and infrahub db index add creates it without starting the server; Neo4j builds it in the background, and until it is online the rebase reads every IS_RESERVED relationship. The other indexes this pull request uses exist in 1.12.2.

Times on a synthetic dataset of 10,000 devices with 48 interfaces each

These times were measured at 51ac5a0. The later commits change which checks receive a node list, add the profiles and templates of the kind to it, and add the branch index of IS_RESERVED; they do not change the other queries.

Setup, run on one machine (31 CPUs, 77 GB of memory):

  • Infrahub 1.12.2 Community image, with the backend code of this branch mounted for the second column of each pair, and Neo4j 2026.05.0 Community.
  • Schema: MockDevice with 4 attributes, and MockInterface with 6 attributes and a parent relationship to its device.
  • Data on main: 10,000 devices with 48 interfaces each, which is 490,794 nodes and 13,684,466 relationships. Every interface has a description.
  • Each run creates a branch, changes the description of the interfaces, then calls DiffUpdate, BranchRebase and CoreProposedChangeMerge, each with wait_until_completion. The proposed change is created after the rebase, and the merge starts once its checks have finished.
  • Each value is the second of two runs, so the database had already read the data once.
Step1 interface, 1.12.21 interface, this branch4 interfaces on 2 devices, 1.12.24 interfaces on 2 devices, this branch
DiffUpdate5.50 s0.13 s5.60 s0.13 s
BranchRebase234.6 s2.07 s234.6 s2.09 s
CoreProposedChangeMerge247.2 s13.2 s241.8 s13.2 s

Database time of the queries changed by this pull request, from the traces of the 4-interface run:

Query1.12.2This branch
RebaseBranchQuery6.0 s1 ms
Attribute kind check during the rebase222.9 s in 97 queries10 ms in 1 query
Attribute kind check during the merge446.9 s in 194 queries19 ms in 2 queries
DiffPropertyNodesQuery during DiffUpdate5.3 s1 ms

During the merge, the branch merge and the Schema Integrity check of the proposed change both run the kind check, at the same time, so the database time of these queries is larger than the time of the merge.

Times on the large dataset of the performance tests

These runs use the tests of opsmill/infrahub-private-tests#84 with the test-dataset workflow, on the 1.12.2 large backup:

Measurement1.12.2This branch
branch_rebase, branch with 1 changed device19,958 ms7,430 ms
branch_rebase, branch with 50 changed devices36,823 ms4,084 ms
branch_rebase, proposed change branch with 1 changed device48,038 ms9,278 ms

In the Neo4j query log, RebaseBranchQuery took 13,751 ms and 4,266,256 page hits (database page reads) for the 50-device rebase on 1.12.2. On this branch, the two rebases of the same test took 134 ms and 11,040 page hits together. The 1-device rebase on 1.12.2 is not in the log, because the log files had rotated.

Limits of these runs:

  • One run each, on two different runners (ci-runner-huge05, ci-runner-huge04).
  • In each run, the 1-device rebase is the first rebase after the backup restore.
  • TestProposedChangeMerge failed the same way in both runs: after the rebase, the checks of the proposed change did not finish within 1,800 s, and the merge was refused because some checks did not conclude with success. So these runs give no proposed_change_merge time, and the merge was not measured on this dataset.

Merge queries that still read the whole graph

With this branch, on the synthetic dataset, the merge still takes 13.2 s. These queries took 11.1 s of it in the 4-interface run, and their plans (EXPLAIN on the synthetic dataset) start from a scan of a whole label:

  • BulkMergeAttributePropertyEdgesQuery, 4,820 ms: scans every Attribute vertex, and matches 4 relationship types in one pattern.
  • BulkMergeNodeExistenceQuery, 4,685 ms: scans every Node vertex, then every Attribute vertex.
  • BulkMergeRelationshipEdgesQuery, 1,022 ms: scans every Relationship vertex.
  • BulkMergeRelationshipPropertyEdgesQuery, 609 ms: plan not inspected.

DiffMergeMetadataQuery, which #11706 names, took 4 ms in the same run. This pull request does not change these queries; #11712, stacked on this one, changes the first four.

Documentation and changelog updates

  • changelog/+rebase-merge-scale.changed.md.
  • dev/knowledge/backend/merge-schema-checks.md: a section on which nodes an attribute check reads, which checkers receive a list (reads_listed_nodes), and how a checker passes the list to its query.
  • dev/knowledge/backend/database-indexes.md: names DiffPropertyNodesQuery and RebaseBranchQuery as examples of reads through relationship indexes.

Tests and checks

  • test_rebase_query_reads_the_edges_of_the_branch_by_type (backend/tests/component/core/test_branch_rebase.py) asserts that the plan of RebaseBranchQuery has no AllRelationshipsScan operator. It fails with the 1.12.2 query, whose plan has UndirectedAllRelationshipsScan.
  • test_every_edge_type_is_indexed_on_its_times_and_branch (backend/tests/unit/core/graph/test_index.py) now also asserts that every edge type has a range index on branch. It fails for IS_RESERVED without the new index.
  • On the synthetic dataset, with the new index online, EXPLAIN of RebaseBranchQuery shows a DirectedRelationshipIndexSeek on the branch index of each of the 8 types and no notification. With a branch index on all 8 types, EXPLAIN of the 1.12.2 query still starts with UndirectedAllRelationshipsScan.
  • test_validator_checks_only_the_listed_nodes, in the test module of each of the 6 checkers, gives a node list that leaves out a node that breaks the rule and asserts that only the listed one is reported. The 8 cases fail when the node filter is turned off.
  • test_determiner.py now expects the node list on the kind and optional constraints of a data diff and no list on the unique constraint. test_attribute_checks_list_the_changed_profiles_and_templates_of_the_kind asserts that the list holds the changed profiles and templates of the kind.
  • test_proposed_change.py: test_get_proposed_change_schema_integrity_constraints expects the node lists. In test_schema_integrity, the diff summary now lists the person whose name breaks the new regex, because the kind check reads only the listed nodes; the test still expects the regex check and the kind check to report that person.
  • Results at e3c172e:
    • backend/tests/component/core/constraint_validators/, test_branch_rebase.py, backend/tests/component/core/merge/, test_branch_merge.py, backend/tests/component/core/diff/, test_branch_diff.py, backend/tests/component/core/agnostic_retirement/test_on_rebase.py, backend/tests/component/message_bus/operations/requests/test_proposed_change.py and backend/tests/component/proposed_change/: 966 passed, 15 skipped, 1 expected failure.
    • backend/tests/unit/core/graph/test_index.py: 24 passed.
    • ruff check and mypy on backend: no error.
    • On the synthetic dataset, one interface changed, second run: DiffUpdate 0.14 s, BranchRebase 2.09 s, CoreProposedChangeMerge 14.3 s.
uv run pytest backend/tests/component/core/test_branch_rebase.py backend/tests/component/core/constraint_validators/ backend/tests/component/core/agnostic_retirement/test_on_rebase.py
uv run pytest backend/tests/component/message_bus/operations/requests/test_proposed_change.py backend/tests/component/proposed_change/
uv run pytest backend/tests/component/core/diff/ backend/tests/component/core/merge/ backend/tests/component/core/test_branch_merge.py backend/tests/component/core/test_branch_diff.py

Checklist

  • Tests added/updated
  • Changelog entry added (uv run towncrier create ...)
  • External docs updated (if user-facing or ops-facing change): no user-facing documentation describes these steps
  • Internal .md docs updated (internal knowledge and AI code tools knowledge)
  • I have reviewed AI generated content