Skip to content

holdings tree: one query for the whole tree (WEBBSITE-35) - #128

Merged
gfrmin merged 3 commits into
masterfrom
orgdata-holdings-one-query
Nov 12, 2026
Merged

gfrmin merged 3 commits into
masterfrom
orgdata-holdings-one-query

Conversation

@gfrmin

@gfrmin gfrmin commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Why

WEBBSITE-35 fired 10 times: Sentry's N+1 detector caught orgdata.asp?x=y, which builds the ownership tree with one webholdings3 query per generation, up to 10 levels deep. holdings.asp?x=y shares the helper.

What

_build_holdings_tree now fetches the whole tree in one statement:

  • A recursive CTE walks the base tables (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.
  • The view is then read once, with personid = ANY(ARRAY(SELECT DISTINCT node FROM walk)), which is an InitPlan array, so every holder is still an index probe.
  • Two attempts measured slower than master: recursing through the view, and a semi-join on the walk.
  • ORDER BY …, issue is a deterministic tiebreak for share classes of one issuer that tie on every sort key.
  • fetch_generations is no longer used here.

Proof

  • Stub test (item 19): 1 query instead of 3, and the cross-holding layout is unchanged. Red on master.
  • Production, read-only: old and new _build_holdings_tree for 47 holders (the 25 largest plus deep chains, including p=382 from the Sentry event) × 5 sorts = 235 non-empty trees.
    • 0 differences apart from the order of rows tied on every sort key. There were 5 such cases, e.g. the ordinary and preference shares of one issuer, both named the same.
    • Total time went from 22.6 s on master to 14.8 s.
  • EXPLAIN ANALYZE on holder 417: 64–73 ms.

🤖 Generated with Claude Code

Guy Freeman and others added 3 commits November 12, 2026 22:49
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>
@gfrmin

gfrmin commented Nov 12, 2026

Copy link
Copy Markdown
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.

@gfrmin
gfrmin merged commit 42f7c97 into master Nov 12, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant