Files
HealthLog/tests/integration
Marc-André Bombeck 8f1af43891 fix(measurements): keep per-type reads on the live index whatever the statistics say
The per-type reads descend measurements_live_covering_idx once per type and
stop at their cap, but the planner chose that descent only when its row
estimates favoured it. The type is a lateral parameter, so one generic
estimate covered every type; an account whose rows arrived after the last
ANALYZE was expected to hold about one row; and statistics taken while the
table held one type made the type condition look free. In those states the
planner walked the type-less (user_id, measured_at) index through every row
of the account once per type, or read every row of each type into a sort.
On a 9,300-row account that was 33,900 row visits for 6,300 rows returned,
1.66 million for the span probe and 845,000 for the dashboard's latest
reading per type.

One shared fragment now holds the descent: the account and the cap reach
the planner through scalar subqueries, and the type is matched as a closed
range it is also ordered on, so only an index leading (user_id, type,
measured_at) can return the rows without a sort. readNewestRowsPerType,
readLiveMeasurementSpan, the dashboard's readLatestEver and the latest-instant
probe of readLatestCanonical use it, and readLatestReadingsPerType reads
through readNewestRowsPerType.

The plan guard moves to its own file, which builds four statistics states on
purpose (none, predating the account over many types and over one type, and
analysed with other accounts) with autovacuum off, and holds every read to
its bound in each. The previous guard depended on statistics another file
had left behind, so it failed in the full suite and passed alone.

Refs #1398
2026-11-17 20:51:39 +01:00
..