Why Postgres ignored my index (and the ANALYZE that fixed it)
A 40-second query that should have been 8ms. The plan was right; the statistics were four months stale.
This query does a seq scan on a 40M row table even though there's an index on `created_at`: ```sql select * from events where created_at > now() - interval '1 hour' order by created_at desc limit 100; ```
Get the plan with real numbers first: ```sql explain (analyze, buffers) select * from events where created_at > now() - interval '1 hour' order by created_at desc limit 100; ``` The thing to compare is `rows=` (the estimate) against `actual rows=`. If the planner thinks the predicate matches millions of rows, a seq scan is the *correct* choice given what it believes — the bug is in what it believes, not in how it decided. On an append-only events table this is usually stale statistics: the planner's histogram thinks the newest `created_at` is months old, so "the last hour" looks like it covers