Repository navigation
Darling analysis: the plan advisory read groups each plan's rows before it ranks them, so a week of history no longer spills to disk (#9495) - #9496
Open
erikdarlingdata wants to merge 2 commits into
Conversation
… week of history no longer spills to disk (#9495)
erikdarlingdata
marked this pull request as ready for review
November 24, 2026 17:01
|
Non-blocking notes from the review of #9496:
|
There was a problem hiding this comment.
Verdict: LGTM
Claims
- VERIFIED — The old
candranked every in-window row with DENSE_RANK/ROW_NUMBER —RowRankedSqlinDarling/Darling.Tests/PlanAdvisoryGroupLiveTests.cs(the old statement) and the removed lines of the diff hunk atPgFactCollector.QueryPerf.cs:1699. - VERIFIED — A new
grpstep groups by database, query hash,COALESCE(query_plan_hash, plan_handle),query_plan_xmlandquery_plan_digest, andcandnow ranks the groups —Darling/PerformanceMonitor.Darling.Analysis/PgFactCollector.QueryPerf.cs:1699-1746. - VERIFIED — The FROM/WHERE moved into
grpunchanged, including thequery_plan_xml IS NOT NULL OR query_plan_digest IS NOT NULLfilter and its comments —PgFactCollector.QueryPerf.cs:1716-1725; the diff touches nothing in those lines except the word "rank"→"group" and "sort"→"carry" in one comment. - VERIFIED —
hits,misses,demoted, the final select and the parameters are byte for byte the same — the diff has only the two hunks, and both end beforehits AS MATERIALIZED. - VERIFIED — The group's cost is
CASE WHEN bool_or(cost IS NULL) THEN NULL ELSE max(cost) END, so a group holding a NULL-cost row sorts first under DESC as the row did —PgFactCollector.QueryPerf.cs:1711. PostgreSQL's DESC defaults to NULLS FIRST. - VERIFIED — Every later step reads only plan_id, plan_rank, query_plan_xml, query_plan_digest and cost from
cand—PgFactCollector.QueryPerf.cs:1747-1820. - VERIFIED — The five new tests exist and assert what the body says: NULL cost ranks first, the demoted other-digest path, inline-text grouping, ten distinct plans with one hot plan, and old-vs-new equality on a 22-plan seed —
PlanAdvisoryGroupLiveTests(APlan_WhoseCostliestRowHasNoCost_StillRanksFirst,APlan_WhoseCostliestDigestHasNoDimensionRow_IsRepresentedByItsOtherDigest,RowsWithInlinePlanText_GroupByTheirOwnText,TenDistinctPlansComeBack_WhenOneHotPlanHasManySnapshots,TheGroupedStatement_ReturnsTheRowRankedStatementsReply_OnOneSeed). - UNVERIFIED — Revert proof: with a plain
max(), the NULL-cost test and the comparison test fail. Reading the seed agrees (plan 1 at 700 loses to eleven plans at 1,020 or more), but the run itself needs a live PostgreSQL. - UNVERIFIED — EXPLAIN: 105 ms with no temp files against 827 ms with an 18,816 kB external merge, on 268,800 rows — needs a live store.
- UNVERIFIED — Release build has 0 errors;
*PlanAdvisory*13,*PgFactCollector*18 and*AnalysisPlanAdvisory*2 pass; the pre-push Guard lines are green — needs a build and a live run. - UNVERIFIED — Release note: "about two and a half times faster" — no measurement in the tree or the body gives 2.5x (see Divergence).
Riskiest lines
Darling/PerformanceMonitor.Darling.Analysis/PgFactCollector.QueryPerf.cs:1711— the group's cost must keep the NULL-first ordering of the per-row rank — covered byPlanAdvisoryGroupLiveTests.APlan_WhoseCostliestRowHasNoCost_StillRanksFirstDarling/PerformanceMonitor.Darling.Analysis/PgFactCollector.QueryPerf.cs:1726— the GROUP BY must keep the digest and the inline text apart, or a digest with no dimension row would merge with one that has a plan and the demoted path would break — covered byPlanAdvisoryGroupLiveTests.APlan_WhoseCostliestDigestHasNoDimensionRow_IsRepresentedByItsOtherDigestDarling/PerformanceMonitor.Darling.Analysis/PgFactCollector.QueryPerf.cs:1743-1744— plan_id and plan_rank now partition on the projectedg.plan_key; a different key would change which plans count as distinct — covered byPlanAdvisoryGroupLiveTests.TenDistinctPlansComeBack_WhenOneHotPlanHasManySnapshotsandTheGroupedStatement_ReturnsTheRowRankedStatementsReply_OnOneSeed
Traps near this diff
- "Two copies of one definition pinned EQUAL to each other fail only after BOTH have drifted (#3684). Alias, do not copy." — does not apply —
PgDrillDownCollector.PlanAdvisoryXmlSqlis an alias (PgDrillDownCollector.Plans.cs:146,= PgFactCollector.PlanAdvisorySql). The test'sRowRankedSqlis a frozen reference to the old statement that the new one is compared with, not a second live definition. - "
MAX(max_dop)grouped byquery_hashis the highest DOP ANY plan for that statement text ever ran at ... An aggregate across plans needs provenance" — does not apply — themax()atPgFactCollector.QueryPerf.cs:1711is taken within one plan key and one digest/text, so it cannot mix plans. It picks the one representative row that the old ROW_NUMBER would have ranked first. - "Ruling (#5833): tests are load-bearing ... Never add one [a census or pin] ... never answer a trap with a test that reads source text" — does not apply — every new test runs the statement against a seeded store and compares replies (
PlanAdvisoryGroupLiveTests.RowsAsync). None reads source text. - "A test that reads the clock twice measures the CI scheduler ... One anchor owns both ends of an interval." — does not apply in effect.
RowsAsyncandInsertAsynceach callLiveClock.Now(), but the window is 7 days back to 5 minutes ahead and the rows sit 30 minutes or more in the past, so the drift between calls cannot move a row across a window edge. - "Lite and Darling ship in parity (CONTRIBUTING's two-store rule): a fix to one SKU's reader ... without the counterpart's is a finding" — does not apply — this is a PostgreSQL-only plan-shape change with an unchanged reply. Lite's twin (
Lite/Analysis/DuckDbFactCollector.QueryPerf.cs:687) is a different statement with no dimension probes, and its reply is not changed.
Divergence (blind read vs PR body)
- The release note says "about two and a half times faster", but the body's own EXPLAIN shows 827 ms → 105 ms (~7.9x). Neither the body nor the tree has a 2.5x figure (non-blocking; see the PR comment).
- The body says the new statement writes no temp files, and the only evidence is the one EXPLAIN in the body. The existing sort-space test (
PlanAdvisoryReadLiveTests.TheFetch_SortsThinRows_AndReturnsTheViewBasedStatementsRows) adds up only Sort nodes'Sort Space Used, so it would not see a HashAggregate spill ingrp. The body doesn't claim that test covers it, so this is a gap in coverage, not a contradiction.
Otherwise the body's account matches the diff.
Reviewer checklist
- Run the seven-day read on a TimescaleDB store with several chunks, with EXPLAIN (ANALYZE, BUFFERS). Confirm the
grpHashAggregate stays atBatches: 1with no temp read/write, including on a store with legacy rows whose ownquery_plan_xmlis set. - Run
PlanAdvisoryGroupLiveTestsand the existingPlanAdvisoryReadLiveTests/PlanAdvisoryDigestProbeLiveTestsagainstDARLING_TEST_PG(CI or local). - Fix the release note's speed-up figure, or say where it comes from.
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.
Claude posting for Erik Darling.
The change
PgFactCollector.PlanAdvisorySqlused to rank everyquery_statsrow of the window withDENSE_RANK()andROW_NUMBER()before it kept one row per plan. At seven days that is every snapshot row of the server, and the sort spilled to disk.The statement now reads the window through a
grpstep first. It groups the rows by database, query hash, plan key (COALESCE(query_plan_hash, plan_handle)),query_plan_xmlandquery_plan_digest.candranks those groups instead of the rows. The FROM and WHERE of the oldcandmoved intogrpunchanged, including thequery_plan_xml IS NOT NULL OR query_plan_digest IS NOT NULLfilter and its comments.hits,misses,demoted, the final select, the parameters and the reply are byte for byte the same.The cost of a group is
CASE WHEN bool_or(cost IS NULL) THEN NULL ELSE max(cost) END. A NULL cost sorts first underDESC, so a group with a NULL-cost row ranks first, as that row does today.Type checks. Every later step reads only
plan_id,plan_rank,query_plan_xml,query_plan_digestand the cost fromcand, and all of those still come out ofcandwith the same meaning.query_plan_xmlistextandquery_plan_digestisbytea, so both have an equality operator andGROUP BYworks. Neither is thexmltype.Tests
New class
PlanAdvisoryGroupLiveTests(live, own scratch store):demotedpath)query_plan_xmlgroup by their own textRevert proof. With the
CASEreplaced by a plainmax(),APlan_WhoseCostliestRowHasNoCost_StillRanksFirstand the old-vs-new comparison both fail. With theCASEback, all five pass.EXPLAIN
Seeded 268,800 rows (400 plans, 672 snapshots each) on a scratch store, seven day window. The scratch store runs without TimescaleDB, so
query_statsis a plain table here and the three-chunk seed was not possible. The row counts and spill below are what the grouped form changes. The per-chunk sort shape from the issue is not reproduced here.New statement, 105 ms, no temp files:
Old statement, 827 ms:
Runs
ci-select.py local-classes --base origin/devlistsPlanAdvisoryGroupLiveTestsonly, which passes.*PlanAdvisory*13 passed,*PgFactCollector*18 passed,*AnalysisPlanAdvisory*2 passed.pre-push.sh: both Guard lines green (Darling 5004, Lite 1248, 0 failed). ThenGATE STOPbecause the selection is the whole suite. CI decides the rest.Left as a draft until a field check of the seven day read.
Fixes #9495
Release note: the plan advisory check reads a week of history about two and a half times faster and no longer writes temporary files.