mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-15 06:52:04 +00:00
The shared canonical-source CTE sorted every raw reading in its window by a CASE expression no index can supply; on the benchmark account the pulse pick over ten years sorted 260,000 rows and spilled to disk. Its order depends only on (type, source), so the pick now groups the window to its distinct (type, day, source) triples first and runs DISTINCT ON over those. The picked source per day is the same by construction; an integration test runs the old statement beside the new one over a fixture with overlapping sources, sources off the ladder, tombstones, empty days and readings either side of midnight, under default and reordered ladders, for every reader shape, and requires identical rows. Every app session now sets jit=off. Postgres compiled the larger folds to machine code, which took 100 to 140 ms of a 220 ms statement and saved nothing for statements that run once and are bound by reading rows. JIT never changes a result.