The one thing to understand first
The <a class="sev1-termlink" href="https://thesev1database.com/glossary/query-planner/" title="Query planner">optimizer chooses plans by estimating how many rows each operation produces. Those estimates come entirely from statistics gathered by ANALYZE into the pg_statistic catalog (human-readable via pg_stats). Bad statistics → bad estimates → bad plans. Almost every “mysteriously slow query” traces back here.
The planner is only as smart as its newest ANALYZE, and it assumes your columns are independent until you tell it otherwise. Those two facts — stale stats and the independence assumption — explain the overwhelming majority of catastrophically wrong row estimates you will ever debug.
What ANALYZE collects
For each column, ANALYZE samples rows (default 300 × statistics_target) and computes: