Repository navigation
holdings tree: one query for the whole tree (WEBBSITE-35) - #128
Merged
Merged
Conversation
orgdata.asp?x=y and holdings.asp?x=y built the ownership tree with one webholdings3 query per generation (#69 took it from one per holder); a 10-level chain made Sentry's N+1 detector fire (WEBBSITE-35, 10 events). Now one statement: a recursive CTE walks the base tables (sholdings -> issue, a superset of the view's edges, 5 ms for a 10-level chain) to every holder within the depth cap, and the view is read once for all of them via = ANY(ARRAY(...)), so each holder is still an index probe. Walking the view itself recursively, or a semi-join on the walk, was slower than before. ORDER BY gains a final `issue` so two share classes of one issuer with the same name sort the same way every time. Proven on production, read-only, old vs new _build_holdings_tree for 47 holders (the largest and the deepest chains) x 5 sorts: 0 differences beyond the order of rows tied on every sort key (5 cases, e.g. ordinary and preference shares of one issuer); total 22.6 s -> 14.8 s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review of #128: the base-table walk passed through listed companies, which webholdings3 drops, so the worst holder's walk reached 3,861 nodes (each a view probe) where the view reaches 708. Pruned: 730 nodes, 180-200 ms. Rechecked on production, read-only: 54 holders (now incl. the 10 worst walks) x 5 sorts, 0 differences; 29.2 s -> 11.0 s in total. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
Author
|
Review finding 1 (walk fans out through ever-listed issuers) fixed in 7c5c624: worst walk 3,861 → 730 nodes (view reaches 708), 180–200 ms. Re-proven on prod: 54 holders incl. the 10 worst × 5 sorts, 0 differences, 29.2 s → 11.0 s. |
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
WEBBSITE-35 fired 10 times: Sentry's N+1 detector caught
orgdata.asp?x=y, which builds the ownership tree with onewebholdings3query per generation, up to 10 levels deep.holdings.asp?x=yshares the helper.What
_build_holdings_treenow fetches the whole tree in one statement:sholdings→issue) to every holder within the depth cap. Those edges are a superset of the view's, so every holder the tree expands is included and the extras are rows the tree never reads. The 10-level walk takes 5 ms on prod.personid = ANY(ARRAY(SELECT DISTINCT node FROM walk)), which is an InitPlan array, so every holder is still an index probe.ORDER BY …, issueis a deterministic tiebreak for share classes of one issuer that tie on every sort key.fetch_generationsis no longer used here.Proof
_build_holdings_treefor 47 holders (the 25 largest plus deep chains, including p=382 from the Sentry event) × 5 sorts = 235 non-empty trees.EXPLAIN ANALYZEon holder 417: 64–73 ms.🤖 Generated with Claude Code