AdvancedPerformance & Indexes

Vacuum and table statistics

The query

SQL
-- Check table bloat and last vacuum/analyze
SELECT
  schemaname,
  relname AS table_name,
  n_live_tup AS live_rows,
  n_dead_tup AS dead_rows,
  ROUND(n_dead_tup * 100.0 / NULLIF(n_live_tup + n_dead_tup, 0), 1) AS dead_pct,
  last_vacuum,
  last_autovacuum,
  last_analyze
FROM pg_stat_user_tables
ORDER BY dead_pct DESC NULLS LAST;

-- Manual vacuum and analyze
VACUUM (ANALYZE, VERBOSE) employees;
Tested against PostgreSQL 16

Note

Dead tuples from UPDATE/DELETE bloat tables. VACUUM reclaims space. ANALYZE updates statistics for planner.

Tables referenced