Always run EXPLAIN with ANALYZE and BUFFERS
EXPLAIN alone shows the planner's guess. EXPLAIN (ANALYZE, BUFFERS) shows what actually happened, including how many pages came from cache and how many from disk. The gap between the estimated and actual row counts is where the bad plans hide.
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) SELECT ...;If the actual rows are ten times the estimate, run ANALYZE on the table before you touch the query.
postgresqlperformance