perf: read only changed data in rebase, merge checks and diff update
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:
RebaseBranchQuerymatches 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). DiffPropertyNodesQuerymatches 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 itsbranchindex withUSING INDEX SEEK.IS_RESERVEDwas the only type without abranchindex, and this pull request adds it (backend/infrahub/core/graph/index.py). A unit test requires abranchindex on every edge type. Abranchindex 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:
ConstraintValidatorDetermineradds 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 newreads_listed_nodesflag receives the list, becauseAggregatedConstraintCheckerremoves 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 theuuidindex of theNodelabel (SchemaValidatorQuery.match_nodes). The list is passed to the validation task insidequote(). 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 theIS_PROTECTED,HAS_SOURCE,HAS_OWNERandHAS_VALUErelationships of the time window through theirfromandtoindexes, with a helper it shares withDiffFieldNodesQuery, 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:
MockDevicewith 4 attributes, andMockInterfacewith 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,BranchRebaseandCoreProposedChangeMerge, each withwait_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.
| Step | 1 interface, 1.12.2 | 1 interface, this branch | 4 interfaces on 2 devices, 1.12.2 | 4 interfaces on 2 devices, this branch |
|---|---|---|---|---|
DiffUpdate | 5.50 s | 0.13 s | 5.60 s | 0.13 s |
BranchRebase | 234.6 s | 2.07 s | 234.6 s | 2.09 s |
CoreProposedChangeMerge | 247.2 s | 13.2 s | 241.8 s | 13.2 s |
Database time of the queries changed by this pull request, from the traces of the 4-interface run:
| Query | 1.12.2 | This branch |
|---|---|---|
RebaseBranchQuery | 6.0 s | 1 ms |
| Attribute kind check during the rebase | 222.9 s in 97 queries | 10 ms in 1 query |
| Attribute kind check during the merge | 446.9 s in 194 queries | 19 ms in 2 queries |
DiffPropertyNodesQuery during DiffUpdate | 5.3 s | 1 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:
- Run 45236175559: 1.12.2 release image.
- Run 45243258797: image built from
infrahub-enterprise-v1.12.2with this branch at 51ac5a0.
| Measurement | 1.12.2 | This branch |
|---|---|---|
branch_rebase, branch with 1 changed device | 19,958 ms | 7,430 ms |
branch_rebase, branch with 50 changed devices | 36,823 ms | 4,084 ms |
branch_rebase, proposed change branch with 1 changed device | 48,038 ms | 9,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.
TestProposedChangeMergefailed 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 noproposed_change_mergetime, 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 everyAttributevertex, and matches 4 relationship types in one pattern.BulkMergeNodeExistenceQuery, 4,685 ms: scans everyNodevertex, then everyAttributevertex.BulkMergeRelationshipEdgesQuery, 1,022 ms: scans everyRelationshipvertex.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: namesDiffPropertyNodesQueryandRebaseBranchQueryas 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 ofRebaseBranchQueryhas noAllRelationshipsScanoperator. It fails with the 1.12.2 query, whose plan hasUndirectedAllRelationshipsScan.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 onbranch. It fails forIS_RESERVEDwithout the new index.- On the synthetic dataset, with the new index online,
EXPLAINofRebaseBranchQueryshows aDirectedRelationshipIndexSeekon thebranchindex of each of the 8 types and no notification. With abranchindex on all 8 types,EXPLAINof the 1.12.2 query still starts withUndirectedAllRelationshipsScan. 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.pynow 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_kindasserts that the list holds the changed profiles and templates of the kind.test_proposed_change.py:test_get_proposed_change_schema_integrity_constraintsexpects the node lists. Intest_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.pyandbackend/tests/component/proposed_change/: 966 passed, 15 skipped, 1 expected failure.backend/tests/unit/core/graph/test_index.py: 24 passed.ruff checkandmypyonbackend: no error.- On the synthetic dataset, one interface changed, second run:
DiffUpdate0.14 s,BranchRebase2.09 s,CoreProposedChangeMerge14.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