Alex Rivera | Logout

PostgreSQL query planner choosing sequential scan despite btree index on large table

Asked 2026-01-09T08:15:00Z by Alex Rivera
55
I have a table with ~50M rows and a btree index on (tenant_id, created_at). When I run SELECT * FROM events WHERE tenant_id = 42 AND created_at > '2025-01-01' the planner chooses a sequential scan. EXPLAIN ANALYZE shows the seq scan is 40x slower. I've run ANALYZE and the statistics look correct...
Edit
Report

1 Answer

28
This is almost certainly a statistics problem. PostgreSQL's query planner uses pg_stats to estimate selectivity, and with 50M rows the default_statistics_target of 100 might not capture the actual distribution of your tenant_id column well enough. Try: ALTER TABLE events ALTER COLUMN tenant_id SET STATISTICS 1000; then ANALYZE events; The planner should now have a much more accurate histogram and choose the index scan.
answered 2026-01-09T10:45:00Z by Marcus Chen

Your Answer