What is VACUUM and autovacuum?

IntermediateConceptPostgreSQL
Answer

Short answer

VACUUM removes dead row versions left behind by updates and deletes (because of MVCC), so the space can be reused, and refreshes visibility information. Autovacuum is the background process that runs VACUUM and ANALYZE automatically when enough rows have changed. It must be left on and healthy.

Commands

VACUUM orders;                 -- reclaim dead rows for reuse (doesn't lock reads/writes)
VACUUM (VERBOSE, ANALYZE) orders;
VACUUM FULL orders;            -- rewrites the table and returns space to the OS — LOCKS the table!
ANALYZE orders;                -- refresh planner statistics

Is autovacuum keeping up?

SELECT relname, n_live_tup, n_dead_tup, last_autovacuum, last_autoanalyze
FROM pg_stat_user_tables ORDER BY n_dead_tup DESC LIMIT 10;

Lots of dead tuples and an old last_autovacuum on a busy table → tune it (e.g. lower autovacuum_vacuum_scale_factor for that table) or look for long transactions blocking it.

Transaction ID wraparound

Postgres must vacuum every table periodically to "freeze" old rows. If that's prevented for a very long time, the database will eventually stop accepting writes to protect data. Monitoring age(datfrozenxid) prevents this rare but serious outage.

What to say in the interview

"VACUUM cleans dead tuples created by MVCC and ANALYZE refreshes statistics; autovacuum does both automatically and must stay enabled. I monitor dead tuples and last_autovacuum, tune per-table thresholds for hot tables, avoid VACUUM FULL on live tables, and watch for wraparound."

All Databases (MySQL, PostgreSQL, MongoDB) questions Roadmap: PostgreSQL basics

More on PostgreSQL