Repository navigation
List phrase signatures and kind words in one pass instead of rescanning the base once per group - #1108
Merged
WaylandYang merged 1 commit intoOct 9, 2026
Conversation
…ng the base once per group Signed-off-by: evolv3ai <hello@evolv3.ai>
WaylandYang
approved these changes
Oct 9, 2026
WaylandYang
left a comment
Contributor
There was a problem hiding this comment.
Thank you. Both rewrites are what #1096 asked for, with the tiebreak and the test.
Checked at 3b61214:
- On three real local bases the output of both queries is the same before and after, in order and as a multiset (31, 5 and 3 phrase signatures; 23, 6 and 2 kind words).
- The store suite and the alignment tests pass with the database required, including
tied_signatures_come_in_a_fixed_order. - The local bases are small, so the speed-up is yours to vouch for; the plans in the description show both
SubPlans gone.
Merging. Thanks again for the measurements and for carrying the fix upstream.
This was referenced Oct 9, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
#1096.
phrase_bindings::signaturesandtype_bindings::signaturesbuild each group's sample arrays with correlatedARRAY(SELECT …)subqueries, and each one rescans the whole CTE once per group. Cost grows with groups × rows.align_phrasesruns the signature query twice per run and the kind-word query during its typed recompute, so on a large base these statements dominate a run (measurements in the issue: 195 s → 0.8 s and 80 s → 0.4 s).What changes
row_number()), then take the count and the first three examples and quotes in oneGROUP BYwitharray_agg … FILTER (WHERE rn <= 3). Chunk text is read only for the rows kept. TheliveCTE (including the per-statement evidence pick) is unchanged.ORDER BYgainsg.subject_type_id, g.object_type_id, g.object_is_value, so rows that tie on(count, phrase)have a fixed order.grouped,spellings/words,phrase_counts/phrases), joined back bykind_word. Its output is unchanged, including order.How it was checked
tied_signatures_come_in_a_fixed_order: a group of five statements (examples are the first three byrecorded_at, each quote stays with its example, a statement without evidence gives""), three groups tied on(count, phrase)that differ only in type ids /object_is_value(fixed order on repeated calls), and kind words with four spellings (top three by frequency, examples in creation order, phrases by frequency). Against the old query it fails on the tied rows' order.phrase_signature_evidenceanda_kind_word_binds_to_a_classtests pass unchanged. So does the wholeutopia-storesuite withUTOPIA_DATABASE_URLandUTOPIA_TEST_REQUIRE_DB=1against a scratch pgvector:pg16, plusutopia-server -- phrase_alignment type_alignment implication.cargo fmt --all --checkandcargo clippy --workspace --all-targets -- -D warningsare clean.(count, phrase)ties, which now follow the new tiebreak.EXPLAIN ANALYZE, before and after
Synthetic base on pgvector/pg16 with default settings, 1 CPU: 15,000 open statements in 12,100 signature groups (100 of them with more than three rows), 6,000 entities, 1,500 kind words.
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF).phrase_bindings::signaturesSubPlans (CTE Scan on picked, loops=12100 each)SubPlantype_bindings::signaturesSubPlans (loops=1500 each)SubPlanSignature query, before
Signature query, after
Kind-word query, before
Kind-word query, after
Fixture
This change was drafted with Claude Code (Anthropic) and reviewed by me before submitting.
Before review
git commit -s)cargo fmt --all --check,cargo clippy --workspace --all-targets -- -D warningsandcargo test --workspacepasscrates/utopia-store/was tested withUTOPIA_DATABASE_URLset (those tests skip without it)