Skip to content

BE-921: Batch entity patch reads and locks - #10128

Draft
montekki wants to merge 1 commit into
fs/be-921-resolve-patches-before-persistencefrom
fs/be-921-batch-entity-patch-reads
Draft

montekki wants to merge 1 commit into
fs/be-921-resolve-patches-before-persistencefrom
fs/be-921-batch-entity-patch-reads

Conversation

@montekki

@montekki montekki commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

🌟 Purpose

Preparing a patch request locks entities, reads editions, and loads authorization resources separately for each target. Lock the requested histories and live publication targets together, load their editions in one read, and fetch typed authorization resources together.

🔍 Changes

  1. Replace the per-target query loop in lock_entity_patch_history in entity/batch/mod.rs with one SELECT against targets supplied through unnest(...). Keep deduplication, live publication targets, all decision-time intervals at the transaction timestamp, and FOR NO KEY UPDATE NOWAIT error handling.
  2. Replace the per-edition Read::<Entity>::read_vec loop in load_entity_patch_set with one read using an edition-ID list filter.
  3. Replace the per-edition query loop in read_entity_patch_resources with one ANY($1::UUID[]) query. Keep conversion of database rows into typed EntityPatchResource values inside the helper.
  4. Move policy-component and policy-set construction out of the per-patch loop in resolve_entity_patches. Build them once for the request while continuing to evaluate each patch against its resolved preceding edition.

Build policy sets once for the request, then evaluate each patch against its preceding edition. Deduplicate lock targets, retain the lock-conflict error, and include all decision-time intervals visible at the transaction timestamp.

Validation: 31 selected temporal, patch, draft, and read-only tests passed. Clippy, Rust formatting, embedded SQL formatting, and whitespace checks passed.

@montekki montekki self-assigned this Oct 20, 2026
@github-actions github-actions Bot added area/libs Relates to first-party libraries/crates/packages (area) type/eng > backend Owned by the @backend team labels Oct 20, 2026
@vercel

vercel Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
hash Ready Ready Preview Oct 20, 2026 6:08pm UTC
petrinaut Ready Ready Preview Oct 20, 2026 6:08pm UTC
petrinaut-docs Ready Ready Preview Oct 20, 2026 6:08pm UTC
1 Skipped Deployment
Project Deployment Actions Updated
hashdotdesign-tokens Ignored Ignored Preview Oct 20, 2026 6:08pm UTC

Request Review

@codecov

codecov Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 23 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.95%. Comparing base (9f0bd3e) to head (422ed20).

Files with missing lines Patch % Lines
...e/src/store/postgres/knowledge/entity/batch/mod.rs 0.00% 23 Missing ⚠️
Additional details and impacted files
@@                               Coverage Diff                                @@
##           fs/be-921-resolve-patches-before-persistence   #10128      +/-   ##
================================================================================
- Coverage                                         67.96%   67.95%   -0.01%     
================================================================================
  Files                                              2009     2009              
  Lines                                            206794   206795       +1     
  Branches                                           8801     8801              
================================================================================
- Hits                                             140539   140537       -2     
- Misses                                            64585    64587       +2     
- Partials                                           1670     1671       +1     
Flag Coverage Δ
hash-graph 21.07% <ø> (ø)
hash-graph-api 39.82% <ø> (ø)
hash-graph-atlas 80.01% <ø> (-0.01%) ⬇️
hash-graph-postgres-store 31.82% <0.00%> (-0.01%) ⬇️
hashql-compiletest 28.71% <ø> (ø)
hashql-eval 81.55% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

Copy link
Copy Markdown
Contributor

Benchmark results

hash-graph-benches – Integrations

policy_resolution_large

Function Value Mean Flame graphs
resolve_policies_for_actor user: empty, selectivity: high, policies: 2002 $$29.0 \mathrm{ms} \pm 371 \mathrm{μs}\left({\color{gray}2.92 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: empty, selectivity: low, policies: 1 $$3.58 \mathrm{ms} \pm 45.8 \mathrm{μs}\left({\color{gray}3.42 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: empty, selectivity: medium, policies: 1002 $$13.3 \mathrm{ms} \pm 192 \mathrm{μs}\left({\color{gray}4.61 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: seeded, selectivity: high, policies: 3314 $$46.8 \mathrm{ms} \pm 774 \mathrm{μs}\left({\color{gray}3.43 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: seeded, selectivity: low, policies: 1 $$15.5 \mathrm{ms} \pm 255 \mathrm{μs}\left({\color{gray}3.44 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: seeded, selectivity: medium, policies: 1527 $$25.5 \mathrm{ms} \pm 303 \mathrm{μs}\left({\color{gray}2.62 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: high, policies: 2078 $$29.7 \mathrm{ms} \pm 387 \mathrm{μs}\left({\color{gray}2.43 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: low, policies: 1 $$3.88 \mathrm{ms} \pm 39.2 \mathrm{μs}\left({\color{gray}2.29 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: medium, policies: 1033 $$14.6 \mathrm{ms} \pm 210 \mathrm{μs}\left({\color{red}6.02 \mathrm{\%}}\right) $$ Flame Graph

policy_resolution_medium

Function Value Mean Flame graphs
resolve_policies_for_actor user: empty, selectivity: high, policies: 102 $$3.96 \mathrm{ms} \pm 33.6 \mathrm{μs}\left({\color{gray}0.238 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: empty, selectivity: low, policies: 1 $$3.12 \mathrm{ms} \pm 24.3 \mathrm{μs}\left({\color{gray}1.24 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: empty, selectivity: medium, policies: 52 $$3.51 \mathrm{ms} \pm 28.2 \mathrm{μs}\left({\color{gray}0.508 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: seeded, selectivity: high, policies: 269 $$5.40 \mathrm{ms} \pm 42.4 \mathrm{μs}\left({\color{gray}1.99 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: seeded, selectivity: low, policies: 1 $$3.72 \mathrm{ms} \pm 44.3 \mathrm{μs}\left({\color{gray}2.90 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: seeded, selectivity: medium, policies: 108 $$4.35 \mathrm{ms} \pm 38.6 \mathrm{μs}\left({\color{gray}0.539 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: high, policies: 133 $$4.63 \mathrm{ms} \pm 44.9 \mathrm{μs}\left({\color{gray}0.581 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: low, policies: 1 $$3.63 \mathrm{ms} \pm 33.9 \mathrm{μs}\left({\color{gray}1.26 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: medium, policies: 63 $$4.27 \mathrm{ms} \pm 43.6 \mathrm{μs}\left({\color{gray}0.630 \mathrm{\%}}\right) $$ Flame Graph

policy_resolution_none

Function Value Mean Flame graphs
resolve_policies_for_actor user: empty, selectivity: high, policies: 2 $$2.79 \mathrm{ms} \pm 22.6 \mathrm{μs}\left({\color{gray}1.25 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: empty, selectivity: low, policies: 1 $$2.63 \mathrm{ms} \pm 25.2 \mathrm{μs}\left({\color{gray}1.77 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: empty, selectivity: medium, policies: 2 $$2.76 \mathrm{ms} \pm 25.3 \mathrm{μs}\left({\color{gray}1.21 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: high, policies: 8 $$3.05 \mathrm{ms} \pm 24.7 \mathrm{μs}\left({\color{gray}0.858 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: low, policies: 1 $$2.86 \mathrm{ms} \pm 25.2 \mathrm{μs}\left({\color{gray}1.84 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: medium, policies: 3 $$3.03 \mathrm{ms} \pm 29.4 \mathrm{μs}\left({\color{gray}0.417 \mathrm{\%}}\right) $$ Flame Graph

policy_resolution_small

Function Value Mean Flame graphs
resolve_policies_for_actor user: empty, selectivity: high, policies: 52 $$3.18 \mathrm{ms} \pm 30.3 \mathrm{μs}\left({\color{gray}0.658 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: empty, selectivity: low, policies: 1 $$2.88 \mathrm{ms} \pm 23.6 \mathrm{μs}\left({\color{gray}-0.019 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: empty, selectivity: medium, policies: 26 $$3.17 \mathrm{ms} \pm 32.9 \mathrm{μs}\left({\color{gray}4.08 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: seeded, selectivity: high, policies: 94 $$3.59 \mathrm{ms} \pm 29.1 \mathrm{μs}\left({\color{gray}0.610 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: seeded, selectivity: low, policies: 1 $$3.12 \mathrm{ms} \pm 26.6 \mathrm{μs}\left({\color{gray}-0.753 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: seeded, selectivity: medium, policies: 27 $$3.50 \mathrm{ms} \pm 30.4 \mathrm{μs}\left({\color{gray}4.85 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: high, policies: 66 $$3.53 \mathrm{ms} \pm 31.0 \mathrm{μs}\left({\color{gray}0.176 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: low, policies: 1 $$3.09 \mathrm{ms} \pm 24.3 \mathrm{μs}\left({\color{gray}-0.455 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: medium, policies: 29 $$3.47 \mathrm{ms} \pm 34.9 \mathrm{μs}\left({\color{gray}3.20 \mathrm{\%}}\right) $$ Flame Graph

read_scaling_complete

Function Value Mean Flame graphs
entity_by_id;one_depth 1 entities $$43.3 \mathrm{ms} \pm 409 \mathrm{μs}\left({\color{gray}-1.283 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;one_depth 10 entities $$33.8 \mathrm{ms} \pm 284 \mathrm{μs}\left({\color{gray}-0.654 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;one_depth 25 entities $$37.2 \mathrm{ms} \pm 422 \mathrm{μs}\left({\color{gray}-0.115 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;one_depth 5 entities $$33.5 \mathrm{ms} \pm 381 \mathrm{μs}\left({\color{gray}-0.106 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;one_depth 50 entities $$43.6 \mathrm{ms} \pm 485 \mathrm{μs}\left({\color{gray}-1.373 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;two_depth 1 entities $$51.6 \mathrm{ms} \pm 598 \mathrm{μs}\left({\color{gray}1.13 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;two_depth 10 entities $$41.8 \mathrm{ms} \pm 360 \mathrm{μs}\left({\color{gray}-1.872 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;two_depth 25 entities $$93.1 \mathrm{ms} \pm 783 \mathrm{μs}\left({\color{gray}-1.333 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;two_depth 5 entities $$34.7 \mathrm{ms} \pm 302 \mathrm{μs}\left({\color{gray}-0.100 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;two_depth 50 entities $$261 \mathrm{ms} \pm 1.34 \mathrm{ms}\left({\color{gray}-1.422 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;zero_depth 1 entities $$10.5 \mathrm{ms} \pm 80.6 \mathrm{μs}\left({\color{lightgreen}-5.505 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;zero_depth 10 entities $$10.7 \mathrm{ms} \pm 91.7 \mathrm{μs}\left({\color{gray}-2.529 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;zero_depth 25 entities $$10.7 \mathrm{ms} \pm 80.5 \mathrm{μs}\left({\color{gray}-4.793 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;zero_depth 5 entities $$10.7 \mathrm{ms} \pm 98.0 \mathrm{μs}\left({\color{gray}-0.826 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;zero_depth 50 entities $$10.6 \mathrm{ms} \pm 78.4 \mathrm{μs}\left({\color{gray}-4.847 \mathrm{\%}}\right) $$ Flame Graph

read_scaling_linkless

Function Value Mean Flame graphs
entity_by_id 1 entities $$10.9 \mathrm{ms} \pm 121 \mathrm{μs}\left({\color{gray}-1.189 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id 10 entities $$10.6 \mathrm{ms} \pm 82.0 \mathrm{μs}\left({\color{gray}-0.949 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id 100 entities $$11.2 \mathrm{ms} \pm 114 \mathrm{μs}\left({\color{gray}4.05 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id 1000 entities $$11.1 \mathrm{ms} \pm 102 \mathrm{μs}\left({\color{gray}1.97 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id 10000 entities $$10.9 \mathrm{ms} \pm 81.0 \mathrm{μs}\left({\color{gray}-2.416 \mathrm{\%}}\right) $$ Flame Graph

representative_read_entity

Function Value Mean Flame graphs
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/block/v/1 $$11.2 \mathrm{ms} \pm 100 \mathrm{μs}\left({\color{gray}-3.535 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/book/v/1 $$11.1 \mathrm{ms} \pm 83.6 \mathrm{μs}\left({\color{gray}-3.954 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/building/v/1 $$11.1 \mathrm{ms} \pm 102 \mathrm{μs}\left({\color{lightgreen}-5.637 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/organization/v/1 $$11.1 \mathrm{ms} \pm 113 \mathrm{μs}\left({\color{gray}-0.927 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/page/v/2 $$11.1 \mathrm{ms} \pm 86.1 \mathrm{μs}\left({\color{gray}-0.755 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/person/v/1 $$11.3 \mathrm{ms} \pm 90.5 \mathrm{μs}\left({\color{gray}-0.639 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/playlist/v/1 $$11.2 \mathrm{ms} \pm 117 \mathrm{μs}\left({\color{gray}-2.459 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/song/v/1 $$11.5 \mathrm{ms} \pm 116 \mathrm{μs}\left({\color{gray}2.34 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/uk-address/v/1 $$11.1 \mathrm{ms} \pm 107 \mathrm{μs}\left({\color{gray}-1.682 \mathrm{\%}}\right) $$ Flame Graph

representative_read_entity_type

Function Value Mean Flame graphs
get_entity_type_by_id Account ID: bf5a9ef5-dc3b-43cf-a291-6210c0321eba $$8.23 \mathrm{ms} \pm 61.0 \mathrm{μs}\left({\color{gray}-4.647 \mathrm{\%}}\right) $$ Flame Graph

representative_read_multiple_entities

Function Value Mean Flame graphs
entity_by_property traversal_paths=0 0 $$58.2 \mathrm{ms} \pm 637 \mathrm{μs}\left({\color{gray}-2.631 \mathrm{\%}}\right) $$
entity_by_property traversal_paths=255 1,resolve_depths=inherit:1;values:255;properties:255;links:127;link_dests:126;type:true $$113 \mathrm{ms} \pm 763 \mathrm{μs}\left({\color{gray}-0.680 \mathrm{\%}}\right) $$
entity_by_property traversal_paths=2 1,resolve_depths=inherit:0;values:0;properties:0;links:0;link_dests:0;type:false $$65.5 \mathrm{ms} \pm 550 \mathrm{μs}\left({\color{gray}-3.566 \mathrm{\%}}\right) $$
entity_by_property traversal_paths=2 1,resolve_depths=inherit:0;values:0;properties:0;links:1;link_dests:0;type:true $$76.7 \mathrm{ms} \pm 686 \mathrm{μs}\left({\color{gray}-2.133 \mathrm{\%}}\right) $$
entity_by_property traversal_paths=2 1,resolve_depths=inherit:0;values:0;properties:2;links:1;link_dests:0;type:true $$85.8 \mathrm{ms} \pm 796 \mathrm{μs}\left({\color{gray}-1.387 \mathrm{\%}}\right) $$
entity_by_property traversal_paths=2 1,resolve_depths=inherit:0;values:2;properties:2;links:1;link_dests:0;type:true $$91.5 \mathrm{ms} \pm 757 \mathrm{μs}\left({\color{gray}-1.099 \mathrm{\%}}\right) $$
link_by_source_by_property traversal_paths=0 0 $$44.3 \mathrm{ms} \pm 331 \mathrm{μs}\left({\color{gray}-3.209 \mathrm{\%}}\right) $$
link_by_source_by_property traversal_paths=255 1,resolve_depths=inherit:1;values:255;properties:255;links:127;link_dests:126;type:true $$75.5 \mathrm{ms} \pm 664 \mathrm{μs}\left({\color{gray}-0.368 \mathrm{\%}}\right) $$
link_by_source_by_property traversal_paths=2 1,resolve_depths=inherit:0;values:0;properties:0;links:0;link_dests:0;type:false $$50.8 \mathrm{ms} \pm 460 \mathrm{μs}\left({\color{gray}-4.390 \mathrm{\%}}\right) $$
link_by_source_by_property traversal_paths=2 1,resolve_depths=inherit:0;values:0;properties:0;links:1;link_dests:0;type:true $$61.9 \mathrm{ms} \pm 599 \mathrm{μs}\left({\color{gray}-1.802 \mathrm{\%}}\right) $$
link_by_source_by_property traversal_paths=2 1,resolve_depths=inherit:0;values:0;properties:2;links:1;link_dests:0;type:true $$64.7 \mathrm{ms} \pm 610 \mathrm{μs}\left({\color{gray}0.137 \mathrm{\%}}\right) $$
link_by_source_by_property traversal_paths=2 1,resolve_depths=inherit:0;values:2;properties:2;links:1;link_dests:0;type:true $$64.2 \mathrm{ms} \pm 522 \mathrm{μs}\left({\color{gray}-0.568 \mathrm{\%}}\right) $$

scenarios

Function Value Mean Flame graphs
full_test query-limited $$110 \mathrm{ms} \pm 913 \mathrm{μs}\left({\color{lightgreen}-9.580 \mathrm{\%}}\right) $$ Flame Graph
full_test query-unlimited $$124 \mathrm{ms} \pm 1.05 \mathrm{ms}\left({\color{lightgreen}-6.380 \mathrm{\%}}\right) $$ Flame Graph
linked_queries query-limited $$19.9 \mathrm{ms} \pm 205 \mathrm{μs}\left({\color{red}5.57 \mathrm{\%}}\right) $$ Flame Graph
linked_queries query-unlimited $$525 \mathrm{ms} \pm 1.48 \mathrm{ms}\left({\color{gray}1.37 \mathrm{\%}}\right) $$ Flame Graph

This branch was successfully deployed

3 active deployments
Preview – petrinaut-docs — 422ed204 Deployed Oct 20, 2026 by vercel[bot]
Preview – hash — 422ed204 Deployed Oct 20, 2026 by vercel[bot]
Preview – petrinaut — 422ed204 Deployed Oct 20, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/libs Relates to first-party libraries/crates/packages (area) type/eng > backend Owned by the @backend team

Development

Successfully merging this pull request may close these issues.

1 participant