How Stale PostgreSQL Statistics Made a Query 580 Times Slower

https://hackernoon.imgix.net/images/RxIFGDejXQUaj54NhArKLyi3a272-1a1e99aa03d.png

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

Endpoint time per call

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...

Copyright of this story solely belongs to hackernoon.com. To see the full text click HERE

Read more