Database Query Planner Statistics
Your database doesn't "guess" how to run a query. It calculates the Cost of every possible execution path. It does this using Histograms stored in the system catalog.
1. How Histograms Work
Postgres samples your data to build a frequency distribution. If your is skewed (e.g., one has 90% of rows), the planner uses the histogram to realize that an Index Scan is actually slower than a Sequential Scan.
2. The 'ANALYZE' Trap
If you run too rarely, your stats are stale. The planner will think a query returns 10 rows when it actually returns 10 million. Result: Slow query, high memory usage, system crash.
