perf(postgres): preserve hash join in observation scoring - #3512
Open
stepanov1975 wants to merge 1 commit into
Open
perf(postgres): preserve hash join in observation scoring#3512stepanov1975 wants to merge 1 commit into
stepanov1975 wants to merge 1 commit into
Conversation
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.
Summary
EXCEPTset differenceFixes #3510.
Problem
On a sufficiently connected PostgreSQL-backed observation graph,
connected_sourcescan be underestimated as one row. PostgreSQL then chooses it as the outer side of a nested-loop join and performs millions of rejected comparisons while scoring otherwise set-wise candidates.A representative warm-cache plan performed about 8.56 million rejected comparisons and took about 4.4 seconds in this path.
Approach
The previous form used
SELECT DISTINCT ... WHERE NOT EXISTS (...). This change expresses the same distinct seed-source exclusion asEXCEPT:The left-side
DISTINCTis intentionally retained to keep the source change minimal. The per-entity lateralORDER BY ... LIMITremains in the same position, before seed-source removal.Results
Across three additional realistic seed sets, the original and rewritten SQL returned identical complete top-300
id -> scoremappings:EXCEPTMedian: 1.753s -> 0.026s (67.4x).
A packaged PostgreSQL full-stack A/B on the same synthetic graph-linked database also returned identical ordered result IDs, entities, chunks, source facts, and non-final score inputs. Tiny final-score drift was reproduced on repeated requests against the same image and came from request-time freshness, not the SQL rewrite. Restart persistence and rollback to the unmodified 0.9.1 image were also exercised successfully.
Testing
uv run pytest tests/test_observation_expansion_scoring.py -q -n0— 3 passed./scripts/hooks/lint.shuv run ty check hindsight_apiThe complete external-LLM test suite was not run locally; the focused and selected suites above exclude tests requiring an external LLM provider.