Skip to content

How to Run PostgreSQL VACUUM and Analyze

DodaTech 2 min read

In this tutorial, you'll learn about How to Run PostgreSQL VACUUM and Analyze. We cover key concepts, practical examples, and best practices.

The Problem

Your PostgreSQL database is growing in size even after deleting rows, queries are slowing down, and pg_stat_user_tables shows n_dead_tup increasing, because autovacuum is not keeping up or has been disabled.

Quick Fix

Run VACUUM Verbose to See Dead Tuples

psql -U postgres -d mydb -c "VACUUM VERBOSE;"
# INFO:  vacuuming "public.orders"
# INFO:  "orders": removed 5000 row versions, 100 pages ...
# DETAIL:  0 dead row versions cannot be removed yet.
# CPU: user: 0.12 s, system: 0.03 s, elapsed: 0.50 s.

VACUUM VERBOSE shows detailed output about dead tuples removed and pages freed. Run this to verify that vacuum is actually reclaiming space.

Run ANALYZE to Update Statistics

psql -U postgres -d mydb -c "ANALYZE VERBOSE;"
# INFO:  analyzing "public.orders"
# INFO:  "orders": scanned 3000 of 3000 pages, ...

ANALYZE updates the table statistics used by the query planner. Run it after significant data changes to ensure efficient query plans.

Check Autovacuum Status and Configuration

psql -U postgres -d mydb -c "
SELECT relname, n_dead_tup, last_autovacuum
FROM pg_stat_user_tables
ORDER BY n_dead_tup DESC LIMIT 5;
"
# relname   | n_dead_tup | last_autovacuum
# orders    |      15000 | 2026-06-23 10:00:00
# products  |       2000 | 2026-06-23 10:00:00

Use pg_stat_user_tables to identify tables with high dead tuple counts that have not been vacuumed recently.

Configure Autovacuum per Table

ALTER TABLE orders SET (autovacuum_vacuum_scale_factor = 0.01);
ALTER TABLE orders SET (autovacuum_vacuum_threshold = 1000);

Override the global autovacuum settings for individual tables that need more aggressive vacuuming. A lower scale_factor triggers vacuum sooner relative to the table size.

Schedule VACUUM with systemd or cron

# /etc/cron.d/postgresql-vacuum
# Run VACUUM ANALYZE daily at 2 AM
0 2 * * * postgres psql -c "VACUUM ANALYZE;" > /dev/null 2>&1

For tables with high churn rates, schedule VACUUM during off-peak hours. The ANALYZE option combines vacuuming with statistics update, eliminating the need for a separate ANALYZE run.

Additional Troubleshooting

# Check the error message and stack trace for more context
echo "Review the full error output to identify the root cause"

If the above steps do not resolve the issue, examine the complete error message and stack trace. Often the key detail is in the middle of the traceback rather than the final line. Search for the error message in the project documentation or issue tracker for additional solutions.

Monitor Bloat with pgstattuple

psql -U postgres -d mydb -c "
CREATE EXTENSION IF NOT EXISTS pgstattuple;
SELECT * FROM pgstattuple('orders');
"
# table_len | dead_tuple_count | dead_tuple_len | free_space
# -----------+------------------+----------------+------------
#   12345678 |             5000 |         250000 |     100000

The pgstattuple extension shows the exact amount of dead tuple bloat at the page level. This helps determine whether a VACUUM or VACUUM FULL is needed.

Prevention

  • Keep autovacuum enabled (it is on by default in PostgreSQL 9.0+)
  • Monitor pg_stat_user_tables.n_dead_tup and last_autovacuum weekly
  • Schedule VACUUM ANALYZE during low-traffic periods for large tables
  • Tune autovacuum_vacuum_scale_factor and autovacuum_vacuum_threshold per table when needed

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro