perf(merge): read the edges of the merged branch through the branch index

Merged
FA
2d ago
+183-15

Merge queries that read the whole graph

This pull request is stacked on #11709 and targets its branch.

On the synthetic dataset described in #11709 (10,000 devices with 48 interfaces each, 490,794 nodes and 13,684,466 relationships), the merge of a proposed change with one changed interface description still took about 13 s with #11709. Four bulk merge queries took 11.1 s of it in the 4-interface run, and their plans started from a scan of a whole label:

  • BulkMergeNodeExistenceQuery: every Node vertex, then every Attribute vertex.
  • BulkMergeRelationshipEdgesQuery: every Relationship vertex.
  • BulkMergeAttributePropertyEdgesQuery: every Attribute vertex. Its pattern matches 4 relationship types at once, so no index can serve it.
  • BulkMergeRelationshipPropertyEdgesQuery: every Relationship vertex, with the same 4-type pattern.

This addresses the part of #11706 that #11709 leaves open.

Changes to the four bulk merge queries

All in backend/infrahub/core/diff/query/bulk_merge.py:

  • BulkMergeNodeExistenceQuery hints the branch index of IS_PART_OF and of HAS_ATTRIBUTE. These reads no longer name the labels their edge type implies (Node and Root for IS_PART_OF, Node for HAS_ATTRIBUTE): with the Node label on HAS_ATTRIBUTE, the planner joined the index seek with a scan of every Node vertex to check it. The only IS_PART_OF edges that do not link a Node to the Root link a Branch, and they have no branch property.
  • BulkMergeRelationshipEdgesQuery hints the branch index of IS_RELATED.
  • BulkMergeAttributePropertyEdgesQuery reads each of HAS_VALUE, IS_PROTECTED, HAS_OWNER and HAS_SOURCE (VERSIONED_VALUE_EDGE_TYPES) through its branch index, joined with UNION ALL, then finds the node of each attribute. The HAS_VALUE read does not name the Attribute label, which that edge type implies.
  • BulkMergeRelationshipPropertyEdgesQuery does the same for IS_PROTECTED, HAS_OWNER and HAS_SOURCE. It no longer reads HAS_VALUE, which never starts at a Relationship vertex.
  • The writes that follow these reads are unchanged.
  • BulkMergeCardinalityOneResolutionQuery is unchanged: it starts from the resolutions it receives and finds their nodes by uuid.

This pull request adds no index, no migration and no schema change. It uses the branch indexes that #11709 relies on.

Times on the synthetic dataset

Same setup as in #11709. The values are the second of two runs.

Step1 interface, #117091 interface, this branch4 interfaces on 2 devices, #117094 interfaces on 2 devices, this branch
DiffUpdate0.14 s0.11 s0.13 s0.13 s
BranchRebase2.09 s2.07 s2.09 s2.08 s
CoreProposedChangeMerge14.3 s2.14 s13.2 s3.13 s

The #11709 values come from e3c172e for 1 interface and from 51ac5a0 for 4 interfaces; the commits in between do not change these queries.

Database time of the four queries in the 4-interface run, from the traces:

Query#11709This branch
BulkMergeAttributePropertyEdgesQuery4,820 ms3 ms
BulkMergeNodeExistenceQuery4,685 ms1 ms
BulkMergeRelationshipEdgesQuery1,022 ms1 ms
BulkMergeRelationshipPropertyEdgesQuery609 ms1 ms

With this branch at 35a3b5a, the merge spends 0.15 s in 121 database queries; the rest of its 3.1 s is outside the database. At 7a3a52d, the second 1-interface run took 2.10 s for the merge, and the four queries 1 to 2 ms each. EXPLAIN of the four queries on the synthetic dataset at 7a3a52d shows a relationship index seek on branch for each read, and no scan of a label.

The merge was not measured on the large dataset of the performance tests: as described in #11709, the proposed change merge test of opsmill/infrahub-private-tests#84 fails before the merge.

Tests and checks

  • backend/tests/component/core/diff/query/test_bulk_merge_query_plans.py loads 300 people with a car each on main and refreshes the planner statistics. For each of the four queries, it asserts that the plan has no scan of every Node, Attribute or Relationship vertex, of every relationship of a type, or of a whole index, and that it seeks the branch index of the source branch for exactly the expected edge types. On a graph of a few nodes, the planner already uses the branch index without a hint, so the test needs that data; it takes about 25 s.
  • The 4 cases fail with the queries of #11709. Removing only the IS_PART_OF hint does not fail the test, because on this data the planner still seeks that index without the hint.
  • Results at 7a3a52d:
    • backend/tests/component/core/diff/, backend/tests/component/core/merge/, test_branch_merge.py, backend/tests/component/core/agnostic_retirement/ and backend/tests/component/core/changelog/: 422 passed.
    • mypy on backend: no issues.
uv run pytest backend/tests/component/core/diff/ backend/tests/component/core/merge/ backend/tests/component/core/test_branch_merge.py

Documentation and changelog updates

  • dev/knowledge/backend/database-indexes.md: a rule to leave out a label that the edge type already implies on a hinted read, and the bulk merge queries that read the source branch named as examples of reads of one branch.
  • No changelog fragment: the fragment of #11709 states that merging a branch or a proposed change is faster on large databases, which covers this change.

Checklist

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