Repository navigation
Darling store: build the restart-rows index server-led so one-server reads seek their own range - #10447
Conversation
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.
| 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 |
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
Verdict: LGTM
Claims
- VERIFIED — The start path builds
idx_query_stats_server_restart_rows_timeon(server_id, collection_time) WHERE sample_interval_seconds = 0in place of the time-led index — PgTableTuning.cs:786; the old CREATE statement is gone fromStatements. - 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), :538s_replacedByentry, and theReplacedIndexOf→IndexValidSqlprobe inApplyAsync. - 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_rowsindex 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 assertsQueryStatsRestartRowsSqlplans 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
RangeSqlPlainStoreonserver_id, and the drop count is 7 — TuningIndexReaderPairingTests.cs:44; sevenGuardedDropstatements 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_replacedByentry) — confirming them needs a build and a live store. By reading, the deferral test does depend on thes_replacedByentry. - UNVERIFIED — Linux and Windows gate runs (758 tests; Stage=Guard counts) — CI or box results.
Riskiest lines
- Darling/PerformanceMonitor.Darling.Storage/PgTableTuning.cs:538 — the
s_replacedByentry; without it, a deferred build would leavequery_statswith neither restart-row index — covered byAStartThatDefersTheServerLedRestartRowIndex_KeepsTheTimeLedOne_AndTheNextStartReplacesIt - 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(assertsbtree (server_id, collection_time)and the WHERE frompg_index) - 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, SQLnow()) 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 usesLiveClock.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_PGset; 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.
Claude posting for Erik Darling
Fixes #10440
Change
The start path builds
idx_query_stats_server_restart_rows_timeoncollect.query_stats (server_id, collection_time) WHERE sample_interval_seconds = 0in place of the time-ledidx_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_statsrestart-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_statsrows 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.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 INDEXon the hypertable, so an insert intocollect.query_statswaits 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 whosequery_statsis 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
TuningStartPathOneBuildTestshas three new live tests for the drop guard.RestartRowIndexLiveTestsreads the key, predicate and validity of the start path's indexes frompg_index, checks that the old index is gone, and asserts thatQueryStatsRestartRowsSqlplans on the new index.DailySummaryCreditedPairsLiveTestsbuilds 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.TuningIndexReaderPairingTestspairs the new index withDailySummarySql.RangeSqlPlainStoreonserver_id.TuningHourlyCreateLiveTestsand the existing start-path tests name the new index.PgTableTuningTestsno longer pins the CREATE text of thequery_statsrestart index, becauseRestartRowIndexLiveTestsreads its key and predicate frompg_indexon a live store. Its drop count is 7.PgTableTuning.cswith only the new constant added failsAStartThatBuildsTheServerLedRestartRowIndex_DropsTheTimeLedOneat its new-index assert. That start applies 51 of 51 statements and never builds the server-led index.s_replacedByentry taken out,AStartThatDefersTheServerLedRestartRowIndex_KeepsTheTimeLedOne_AndTheNextStartReplacesItfails 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.PgTableTuning,DailySummarySqlorIntervalRollupRestartRows: 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.ExcludedDatabasesTestsandViewerCalendarRetentionPortTestsneed WPF assemblies that Linux does not have.MemoryPressureEventsCountedOncePerEventLiveTestsandTopQueriesRankingSpill8549LiveTestsreopen the store from a connection string that no longer carries the box store's password.