Skip to content

perf(conversations): Fix the subtree walk's cost estimate - #3838

Merged
BYK merged 7 commits into
perf/status-reads-jit-offfrom
perf/subtree-plan-cost
Nov 23, 2026
Merged

BYK merged 7 commits into
perf/status-reads-jit-offfrom
perf/subtree-plan-cost

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #3785. Its base moves to main when #3785 merges.

conversationSubtree put the root-index lookup inside the recursive CTE's start rows. Postgres guesses each recursive step from those rows, so for a root with a big tree it expected hundreds of rows per step. That estimate inflated the plan cost of every statement that reads a subtree, and pushed some of them over the default JIT threshold.

Now the self rows and the root members (from the root index) sit outside the recursion. The walk starts only from selected rows that are not roots. The results are the same.

With #3785's role-wide jit_above_cost, these reads no longer JIT either way. The fix still gives the planner a truer row count for joins over a subtree.

Cost estimates at 114,375 seeded Conversations (13,000 roots with 0–100 children each):

  • CTE, 100-child root: 80,273 → 4,829 (366 → 92 rows estimated, 102 actual).
  • CTE, leaf child: 22,706 → 4,587.
  • Detail statements, 100-child root (state row, tree totals, watermarks, tree state): 83k–119k → 6k–15k.
  • Versions, 100-child root: 176,994 → 96,855.

The SQL budget fixture gets explainNodes, which returns the plan nodes of each statement that a call sends, so a test can read one planner estimate.

Open gap: a set of many roots is still estimated high, because Postgres expects every selected id to start a walk. For 50 roots, the CTE goes from 77k to 81k, versions from 173k to 178k, and the feed tree state from 98k to 120k. That puts the feed tree state over the default JIT threshold, so this change depends on #3785.

via @BYK.

--

View Session [Sentry]

sentry-junior Bot and others added 3 commits November 19, 2026 17:06
Postgres guesses each recursive step from the rows that start the walk.
The root lookup was part of that start, so a root with a big tree made
the planner expect thousands of rows per step. That estimate pushed every
statement that reads a subtree over jit_above_cost. The root lookup and
the selected rows now sit outside the recursive CTE, and the walk starts
from selected children only.

The detail state row and the subtree usage and event watermark reads run
in readWithoutJit like the other status reads.

Co-Authored-By: BYK <[email protected]>
…perf/subtree-plan-cost

Co-Authored-By: BYK <[email protected]>

# Conflicts:
#	packages/junior/tests/integration/api/conversations/list-answer.test.ts
#	packages/junior/tests/integration/api/conversations/tree-state.test.ts
With the subtree estimate fixed, a read of one Conversation plans far
below jit_above_cost, so the detail state row and the detail tree reads
need no JIT-off transaction. A feed page of ids still plans above it, so
the tree state, tree totals and subtree watermarks of more than one id
run in readWithoutJit.

Co-Authored-By: BYK <[email protected]>
@sentry-junior
sentry-junior Bot requested a review from BYK November 19, 2026 22:54
@sentry-junior
sentry-junior Bot added this pull request to stack #3839 November 19, 2026 22:54
@vercel

vercel Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
junior-docs Ready Ready Preview Nov 23, 2026 2:47pm UTC

Request Review

@github-actions github-actions Bot added the risk: high PR risk score: high label Nov 19, 2026
The feed runs its JIT-off reads in parallel, so their BEGIN and COMMIT rounds do not always line up. The feed budgets allow 2 rounds per JIT-off read.

Co-Authored-By: BYK <[email protected]>
#3785 now raises the app role's jit_above_cost instead of
wrapping reads in readWithoutJit, so this branch keeps only the subtree
estimate fix and its plan test.

Co-Authored-By: BYK <[email protected]>
@sentry-junior sentry-junior Bot changed the title perf(conversations): Keep subtree reads below the JIT threshold perf(conversations): Fix the subtree walk's cost estimate Nov 20, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 36f2db3. Configure here.

@BYK
BYK enabled stacked auto-merge (squash) November 20, 2026 21:24
stacked auto-merge was automatically disabled November 23, 2026 14:38

Pull Request is not mergeable

main's sql-budget fixture already explains statements and walks their
plan nodes. Add explainNodes there and use it, instead of a second plan
schema in the tree state test.

Co-Authored-By: BYK <[email protected]>
@BYK
BYK merged commit 1533fa1 into main Nov 23, 2026
56 checks passed
@BYK
BYK deleted the perf/subtree-plan-cost branch November 23, 2026 15:52

This branch was successfully deployed

1 active deployment
Preview – junior-docs — bd23c93a Deployed Nov 23, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: high PR risk score: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant