Menu

1 / 3

How Stale PostgreSQL Statistics Made a Query 580 Times Slower

DEV Community: database·ImmuneFOMO·7 days ago
#VFijQ0r9
#devtodatabase#devto#dev#analyze#rows
Long article (2506 words) — no AI summary yet.
Reading 0:00
15s threshold

Slow Postgres, part 3 of 8. The SQL and raw numbers for every test are on GitHub .

PostgreSQL 18, sampled every 5 seconds. Gaps: pgbench runs.

A retention job marked 463,418 of five million orders archived , a status ANALYZE had never seen, so Postgres estimated 1 row for it. An endpoint listing one customer's archived orders kept walking the status index, reading 463,418 entries to return one: 29 ms a call instead of 0.05 ms.

One note before the numbers: the AlphaVPS link at the end is a referral link, so I may get a commission at no extra cost to you, and the netcup voucher I offer there comes from my own account.

The short version:

A 1-row estimate for 463,418 archived orders kept a per-customer query on the wrong index: 29 ms a call against 0.05 ms after ANALYZE.

Autovacuum runs ANALYZE once 50 rows plus 10% of the table have changed, whatever the change. The 9.3% backfill sat under that line until 40,000 more rows changed.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.