Skip to content

Darling store: build the restart-rows index server-led so one-server reads seek their own range - #10447

Merged
erikdarlingdata merged 1 commit into
devfrom
fix/10440-restart-rows-server-index
Dec 4, 2026
Merged

erikdarlingdata merged 1 commit into
devfrom
fix/10440-restart-rows-server-index

Conversation

@erikdarlingdata

@erikdarlingdata erikdarlingdata commented Dec 4, 2026 •

Copy link
Copy Markdown
Owner

Claude posting for Erik Darling

Fixes #10440

Change

The start path builds idx_query_stats_server_restart_rows_time on collect.query_stats (server_id, collection_time) WHERE sample_interval_seconds = 0 in place of the time-led idx_query_stats_restart_rows_time. It is a PgTableTuning start-path index under the shared start build budget (#9795), the same as its siblings, so there is no migration and no schema version change.

The old index is dropped on the next statement with the guarded drop shape (its own transaction, the 5 s lock timeout, #4247), and only once the new index exists and is valid (s_replacedBy, the #9171 shape). A start whose new build fails or runs out of the shared build time keeps the old index, and the next start builds the new one and then drops the old. A store is never left with neither.

The procedure_stats restart-row indexes are unchanged.

What the reads use

DailySummarySql reads the restart rows only in its credited-pairs count, at four call sites, and every one of them holds server_id = $1. Through the time-led index each of them fetched every server's restart rows in the window to keep one server's. The server-led index seeks one server's range. The Top Queries grid's restart read is a one-server read too.

The cross-server read, IntervalRollupRestartRows.QueryStatsRestartRowsSql (the shape of the Custom Views restart arm), has no server bound. It reads the same pages through the new index on PostgreSQL 16, which has no btree skip scan, because the index holds only restart rows and chunk exclusion bounds the scan to the window's chunks.

Lab

TimescaleDB 2.32.1, 11 servers, 5.2 M query_stats rows in 6 hour chunks, 3.9 GB of heap. Cold is the median of 3 after a container restart and a page cache drop, warm the median of 5. Rows were equal on every read, old index against new and PostgreSQL 16 against 18. r1 and r1b are the two runs.

Read PostgreSQL Run Old index, cold (warm) ms New index, cold (warm) ms
Top Queries grid restart rows 18 r1 5,312.3 (273.6) 1,875.4 (250.7)
Top Queries grid restart rows 18 r1b 5,325.6 (265.6) 1,835.6 (247.3)
Daily Summary credited pairs 18 r1 5,015.2 (199.2) 966.6 (184.0)
Daily Summary credited pairs 18 r1b 4,884.9 (197.2) 989.2 (187.6)
Daily Summary credited pairs 16 r1b 4,897.9 (183.9) 930.5 (185.2)
Cross-server restart rows 18 r1 4,247.7 (9.9) 4,305.5 (10.3)
Cross-server restart rows 18 r1b 4,415.3 (9.6) 4,437.2 (9.9)
Cross-server restart rows 16 r1b 4,126.3 (9.9) 4,226.7 (9.7)

The cross-server read takes 7,607 pages through the old index and 7,614 through the new one. Keeping both indexes cost 5 to 8% more WAL a row on 21,600-row batches, and with both present the planner still took the old index for 8 of 10 grid loops, so the change replaces the index rather than adding one. The new index holds the same rows as the old one, so the insert path keeps one restart-row index on query_stats.

Build time and write wait

The build is a plain CREATE INDEX on the hypertable, so an insert into collect.query_stats waits for the whole build. Over the lab's 3.9 GB of heap the build took 15.2 s (cold, median of 3, about 3.9 s a GB), and an insert started 0.5 s in waited 14.7 s. At 24 GB of uncompressed heap that projects to about 93 s, inside the 300 s shared start build budget. The first start after the upgrade pauses collection for that one bounded build on a large store, per the #9795 budget. A store whose query_stats is empty builds it at once, outside the budget.

Rollback

An older build's start re-creates the old index under its own budget, about one more 93 s build on the largest store, and leaves the new one in place. No data changes. Upgrading again drops the old index and keeps the new one as it is.

Lite

DuckDB has no counterpart index, so Lite is unchanged.

Tests

  • TuningStartPathOneBuildTests has three new live tests for the drop guard.
    • A start that ends with the new index valid drops the old one.
    • A start whose new build runs out of the shared build time (forced with the test build clock) keeps the old index. The hourly pass keeps it too, and the next start builds the new index and drops the old.
    • A store with both indexes ends a start with only the new one, the same index (its OID is unchanged).
  • RestartRowIndexLiveTests reads the key, predicate and validity of the start path's indexes from pg_index, checks that the old index is gone, and asserts that QueryStatsRestartRowsSql plans on the new index.
  • DailySummaryCreditedPairsLiveTests builds the index from the start path's own statement, and both plan tests assert that the credited-pairs restart read is served by the new index.
  • TuningIndexReaderPairingTests pairs the new index with DailySummarySql.RangeSqlPlainStore on server_id. TuningHourlyCreateLiveTests and the existing start-path tests name the new index.
  • PgTableTuningTests no longer pins the CREATE text of the query_stats restart index, because RestartRowIndexLiveTests reads its key and predicate from pg_index on a live store. Its drop count is 7.
  • Red proofs, in a box clone only:
    • Dev's PgTableTuning.cs with only the new constant added fails AStartThatBuildsTheServerLedRestartRowIndex_DropsTheTimeLedOne at its new-index assert. That start applies 51 of 51 statements and never builds the server-led index.
    • With the s_replacedBy entry taken out, AStartThatDefersTheServerLedRestartRowIndex_KeepsTheTimeLedOne_AndTheNextStartReplacesIt fails at its old-index assert after the deferred start. The old index is dropped while the new one waits, which leaves the table with neither.
  • Linux build box gate, against a TimescaleDB 2.32.1 store on PostgreSQL 17. It ran every Darling.Tests file that names PgTableTuning, DailySummarySql or IntervalRollupRestartRows: 80 files by grep, 78 of them with tests, all 78 run, 758 tests. 74 files were clean. The other 4 fail the same way on dev. ExcludedDatabasesTests and ViewerCalendarRetentionPortTests need WPF assemblies that Linux does not have. MemoryPressureEventsCountedOncePerEventLiveTests and TopQueriesRankingSpill8549LiveTests reopen the store from a connection string that no longer carries the box store's password.
  • Windows build box gate at 2b34175: Darling FactCollectorFailureReportingTests 3 of 3, Darling Stage=Guard 5,109 of 5,109, the classes in the four files Linux could not run clean (ExcludedDatabasesPickerTests, ExcludedDatabasesStoreLiveTests, ViewerCalendarRetentionPortTests, MemoryPressureEventsCountedOncePerEventLiveTests, TopQueriesRankingSpill8549LiveTests, 21 tests) all pass, and Lite Stage=Guard 1,270 of 1,270, with 0 build warnings.

The start path builds idx_query_stats_server_restart_rows_time on
(server_id, collection_time) WHERE sample_interval_seconds = 0 in place of
the time-led idx_query_stats_restart_rows_time, under the shared start build
budget. The old index's guarded drop runs only once the new index exists and
is valid (s_replacedBy), so a start whose build is deferred or fails keeps it.

DailySummarySql's credited-pairs restart read and the Top Queries grid's are
one-server reads and seek one server's range. The cross-server read reads the
same pages through the new index on PostgreSQL 16.

Tests: three start-path tests for the drop guard (replace, deferral keeps the
old index and the next start replaces it, both present ends with the new one),
the plan tests on the new index, pg_index reads of the key and predicate.
@erikdarlingdata
erikdarlingdata marked this pull request as ready for review December 4, 2026 12:38
Comment on lines +776 to +779
Top Queries grid) fetched every server's restart rows of the window through an index on the time alone, to keep one
server's. The cross-server read (IntervalRollupRestartRows) reads the same rows through this index: it holds only restart
rows, and chunk exclusion bounds the scan to the window's chunks, so without btree skip scan (before PostgreSQL 18) it
reads as many pages as through the time-led index. The time-led index it replaces

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Non-blocking, a note on the comment rather than the code. "chunk exclusion bounds the scan to the window's chunks, so … it reads as many pages as through the time-led index" holds when the window covers whole chunks, which is what the lab had (6 h chunks). Without skip scan (before PG 18), a btree scan led by server_id has no bound on collection_time. It walks every leaf of each surviving chunk's partial index and checks the time quals as index filters. So a window much shorter than the chunk, such as the Custom Views restart arm over a few hours (ComposeCompiler.HourlyRawEdgesRelation, ComposeCompiler.cs:231-233) on a daily chunk, now reads the whole chunk's restart-row leaves, where the time-led index read only the window's slice. Heap fetches stay the same, and the index holds only restart rows, so the extra cost is small. Still, the sentence would be more accurate as "about as many pages when the window covers whole chunks".

@claude claude 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.

Verdict: LGTM

Claims

  • VERIFIED — The start path builds idx_query_stats_server_restart_rows_time on (server_id, collection_time) WHERE sample_interval_seconds = 0 in place of the time-led index — PgTableTuning.cs:786; the old CREATE statement is gone from Statements.
  • VERIFIED — The new index is under the shared start build budget (#9795) — PgTableTuning.cs:333 swaps it into s_startBuildBudget.
  • VERIFIED — No migration and no schema version change — the diff touches no migration or schema file (gh pr diff --name-only).
  • VERIFIED — The old index is dropped with the guarded-drop shape, and only once the new index exists and is valid — PgTableTuning.cs:787 GuardedDrop(QueryStatsRestartRowIndexName), :538 s_replacedBy entry, and the ReplacedIndexOf → IndexValidSql probe in ApplyAsync.
  • VERIFIED — The procedure_stats restart-row indexes are unchanged — PgTableTuning.cs:788 and :805 are untouched.
  • VERIFIED — DailySummarySql reads restart rows only in credited pairs, at four call sites, each holding server_id = $1 — DailySummarySql.cs:496, :693, :897, :1081; their predicates are at :439, :650, :880, :1030.
  • VERIFIED — The Top Queries grid's restart read is one-server — ViewerDataService.QueryStats.cs:588-592 and :677-681 (server_id = $1 … sample_interval_seconds = 0).
  • VERIFIED — Re-upgrading after an older build re-created the old index keeps the new index as it was (same OID) and drops the old — AStoreWithBothRestartRowIndexes_EndsAStartWithOnlyTheServerLedOne.
  • VERIFIED — Lite has no counterpart index — no restart_rows index anywhere under Lite/.
  • VERIFIED — Three new live drop-guard tests in TuningStartPathOneBuildTests — AStartThatBuildsTheServerLedRestartRowIndex_DropsTheTimeLedOne, AStartThatDefersTheServerLedRestartRowIndex_KeepsTheTimeLedOne_AndTheNextStartReplacesIt, AStoreWithBothRestartRowIndexes_EndsAStartWithOnlyTheServerLedOne.
  • VERIFIED — RestartRowIndexLiveTests reads key, predicate and validity from pg_index, checks the old index is gone, and asserts QueryStatsRestartRowsSql plans on the new index — RestartRowIndexLiveTests.cs:52-54, :142, :112.
  • VERIFIED — DailySummaryCreditedPairsLiveTests builds the index from the start path's own statement, and both plan tests assert the new index — DailySummaryCreditedPairsLiveTests.cs:41-42, :112, :166.
  • VERIFIED — The pairing test uses RangeSqlPlainStore on server_id, and the drop count is 7 — TuningIndexReaderPairingTests.cs:44; seven GuardedDrop statements at PgTableTuning.cs:582, 590, 601, 611, 620, 787, 1061.
  • UNVERIFIED — Lab timings, page counts (7,607 vs 7,614), the 5-8% WAL cost of keeping both indexes, and the planner preferring the old index when both exist — live measurements on a rig, which a read-only tree cannot reproduce.
  • UNVERIFIED — Build time of 15.2 s over 3.9 GB and the ~93 s projection at 24 GB — a live timing.
  • UNVERIFIED — Red proofs (new-index assert fails on dev; old-index assert fails without the s_replacedBy entry) — confirming them needs a build and a live store. By reading, the deferral test does depend on the s_replacedBy entry.
  • UNVERIFIED — Linux and Windows gate runs (758 tests; Stage=Guard counts) — CI or box results.

Riskiest lines

  1. Darling/PerformanceMonitor.Darling.Storage/PgTableTuning.cs:538 — the s_replacedBy entry; without it, a deferred build would leave query_stats with neither restart-row index — covered by AStartThatDefersTheServerLedRestartRowIndex_KeepsTheTimeLedOne_AndTheNextStartReplacesIt
  2. Darling/PerformanceMonitor.Darling.Storage/PgTableTuning.cs:786 — the new key order and partial predicate decide whether one-server reads can seek — covered by RestartRowIndexLiveTests.BothIndexes_ExistWithThePartialPredicate_AndTheSetupIsIdempotent (asserts btree (server_id, collection_time) and the WHERE from pg_index)
  3. Darling/PerformanceMonitor.Darling.Storage/PgTableTuning.cs:787 — the guarded drop must follow the create so one start both builds and drops — covered by AStartThatBuildsTheServerLedRestartRowIndex_DropsTheTimeLedOne

Traps near this diff

  • "Ruling (#5833): tests are load-bearing. … Never add one, and never answer a trap with a test that reads source text or asserts how code is spelled" — does not apply — the new tests are live behaviour tests (TuningStartPathOneBuildTests.cs:497-565). The two source-reading pins touched were already there and are edited, not added (PgTableTuningTests.cs:77 drop count, TuningIndexReaderPairingTests.cs:44). The exact-text pin for this index is removed (PgTableTuningTests.cs:57).
  • "Lite and Darling ship in parity (CONTRIBUTING's two-store rule): a fix to one SKU's reader, tool, alert or description without the counterpart's is a finding" — does not apply — PgTableTuning is the PostgreSQL store's index tuning, and Lite (DuckDB) has no restart-row index (grep over Lite/ is empty). No reader, tool, alert or description changed.
  • "A live test that seeds at the wall clock (DateTime.UtcNow, SQL now()) and asserts an hour or day bucket splits its seed across two buckets" — does not apply — the new start-path tests assert index existence and OIDs, not buckets. RestartRowIndexLiveTests seeds at a fixed anchor (RestartRowIndexLiveTests.cs:86), and DailySummaryCreditedPairsLiveTests uses LiveClock.Now() (:65).
  • "Two copies of one definition pinned EQUAL to each other fail only after BOTH have drifted (#3684). Alias, do not copy." — does not apply — DailySummaryCreditedPairsLiveTests now takes the start path's own statement (DailySummaryCreditedPairsLiveTests.cs:41-42) instead of a hand-copied CREATE.

Divergence (blind read vs PR body)

The body's account matches the diff on every claim graded above. One nuance, posted inline as non-blocking: the body and the comment at PgTableTuning.cs:776-779 say the cross-server read "reads the same pages" because chunk exclusion bounds the scan. That holds for windows that cover whole chunks, as in the lab's 6 h chunks. Before PG 18, a window much shorter than its chunk (the Custom Views restart arm, ComposeCompiler.cs:231-233) walks the whole chunk's partial-index leaves instead of a time slice. Heap fetches are unchanged and the index holds only restart rows, so this is a wording point, not a defect.

Reviewer checklist

  • Confirm the live suites (RestartRowIndexLiveTests, DailySummaryCreditedPairsLiveTests, TuningStartPathOneBuildTests) ran green in CI against a TimescaleDB store with DARLING_TEST_PG set; they skip without it.
  • On a large production-like store, watch the first start after upgrade: the server-led build fits the 300 s shared budget, and the old index is gone after the start that builds the new one.
  • Optionally, EXPLAIN (ANALYZE, BUFFERS) the Custom Views restart arm on PG 16/17 with daily chunks and a few-hour window, to confirm the extra index leaf reads are negligible.

@erikdarlingdata
erikdarlingdata merged commit 9cd86fb into dev Dec 4, 2026
28 checks passed
@erikdarlingdata
erikdarlingdata deleted the fix/10440-restart-rows-server-index branch December 4, 2026 12:47
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