Analysis: bound the freshness query, let window sums skip chunks, share the Overview's catalog
ci / build-test (push) Successful in 2m34s

Three things the 1,000-meter x 10-year measurement found, each proved by
EXPLAIN or a statement count before and after. No tally moves.

Freshness had two jobs in one unbounded query. The mark -- when a meter
last delivered -- is now stored on meter_rollup_state and maintained by
every recompute, with a one-pass backfill in the migration, so an
import-only meter keeps its years-old last activity without reading a
single raw row. The rhythm that decides stale versus live is sampled
inside a 90-day window and only for meters that actually have a live
source; a source silent for longer than that is re-read unbounded, so it
is still called stale by its own rhythm rather than by a default. The
portfolio query went from 13.8 ms planning plus 36.1 ms execution across
all 123 reading chunks to 0.58 plus 0.44 ms across four.

Window sums took their time bounds only from the unnest join, so the
planner could not exclude chunks: a 1,960-window case scanned 1.39 M rows
in parallel and spilled a 45 MB sort. Repeating the overall min and max as
constants makes it five chunks and nested-loop index scans, 121.5 ms to
8.7 ms.

The Overview read the catalog three times, once for the quantities and
once for each of its two bills. One context and one catalog snapshot now
feed all three: 31 statements per load to 23.

The final timings on an idle machine are in docs/ANALYSIS_REPORT.md: the
brief's target request (100 meters, ten years, monthly) is 286 ms against
two seconds, and a startup rebuild of 1,000 meters is 279 s.
This commit is contained in:
Florian Schmidt
2026-09-20 11:16:48 +02:00
parent ec51419f64
commit 5a6f34a467
21 changed files with 1857 additions and 87 deletions
@@ -178,6 +178,13 @@ public sealed class MeterRollupState
/// <summary>What the meter's amounts measure (D-20).</summary>
public QuantityKind Kind { get; set; }
/// <summary>
/// The stamp of the meter's latest raw reading when its analysis data was built, or null when it has none
/// (A-40). Every write path recomputes the meter inline, so this is the freshness mark of D-18 without a
/// query over <c>reading</c> — whose chunks a reader would otherwise have to plan across on every request.
/// </summary>
public DateTimeOffset? LastReadingAt { get; set; }
/// <summary>
/// When the meter's analysis data last changed: a rollup or coverage row was added, changed or removed, or
/// one of the columns above changed. A recompute that reproduces the stored data leaves it alone, so it