Skip to content

fix(remote): earn a statistics re-dump at a fifth, not a half - #232

Merged
veksen merged 1 commit into
mainfrom
tune-size-drift-ratio
Aug 13, 2026
Merged

fix(remote): earn a statistics re-dump at a fifth, not a half#232
veksen merged 1 commit into
mainfrom
tune-size-drift-ratio

Conversation

@veksen

@veksen veksen commented Aug 13, 2026

Copy link
Copy Markdown
Member

Goal

Queries should be costed against statistics that match the database they run on. The analyzer keeps a project's stored statistics current by re-dumping when the source has moved; this changes how far it has to move before that happens.

What

Before: a table had to grow by half, or shrink to half, before the analyzer re-dumped the source's statistics. Between re-dumps a table could sit at 1.5x its recorded size, and every query against it was costed a third light.

After: a fifth of movement is enough. A table at 1.25x its recorded size now earns a re-dump instead of being called current.

The re-dump itself is unchanged, as is the 24-hour floor that fires when nothing has drifted.

How

src/remote/stats-drift.ts is the whole change: DEFAULT_SIZE_DRIFT_RATIO goes from 0.5 to 0.2.

The number is bounded on both sides. Underneath, the check reads pg_class.reltuples, which Postgres only rewrites when autovacuum analyzes the table, by default once it has changed by autovacuum_analyze_scale_factor, 10%. A threshold below that reads sampling noise rather than data. Above, the threshold is how wrong a cost may be before it is worth acting on, and 0.5 tolerated a cost a third light.

0.2 also makes the growth and shrink sides close to even. The comparison is |now - before| / before, so 0.5 fired on 1.5x growth but needed a 2x shrink; at 0.2 the two are 1.25x and 1.2x.

Tests

src/remote/stats-drift.test.ts: a 1.25x move drifts, a 1.1x move does not. The near-miss case that names the closest table sits at 15% and 5%, either side of the new threshold.

Full suite passes (443 tests, 43 files), npm run typecheck clean.

Size Drift decided a snapshot was still current until a table's reltuples
moved by half. That is coarser than the signal it reads: pg_class.reltuples
is rewritten when autovacuum analyzes the table, which by default happens
once it has changed by 10%, so four fifths of the available resolution went
unused. It also let a table sit at 1.5x its snapshot size while every query
against it was costed a third light.

Move the ratio to 0.2 — two ticks of the underlying signal, and small enough
that the growth/shrink asymmetry in |now - before| / before stops mattering.

Co-Authored-By: Claude <noreply@anthropic.com>

@github-actions github-actions 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.

Query Doctor — 6 successful checks

 Cost regression — No query went up more than 5%
 Untested data access — No changed data-access file without a test
 New query — No new queries
 New query with index recommendation — No new query ships an index recommendation
 Schema drift — No schema changes
 High-value nudge — No index or rewrite past the threshold


More details via MCP → get_ci_run({ runId: "019ffd57-3a69-7252-aba4-ab993ff5800c" }) · view run · docs
3 queries read against main on assumed statistics of 10,000,000 rows per table. Sync production stats for costs measured against your real data.

@veksen
veksen merged commit aff59d6 into main Aug 13, 2026
6 checks passed
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