Pcornel
Browse topics
Options
Recent
Favorites
Databases · Technical reference

MVCC and Vacuum

Direct answer

MVCC keeps old row versions for concurrent readers. In PostgreSQL, VACUUM removes dead versions and prevents transaction ID wraparound; autovacuum does this automatically.

When to use this

Use it when tables bloat, autovacuum lags, or wraparound warnings appear.

First 30 seconds

1. Dead tuples: SELECT relname,n_dead_tup,last_autovacuum FROM pg_stat_user_tables ORDER BY n_dead_tup DESC;.

2. Oldest transaction: SELECT … FROM pg_stat_activity ORDER BY xact_start;.

3. Wraparound age: SELECT datname, age(datfrozenxid) FROM pg_database;.

4. Run VACUUM (VERBOSE) t; if needed.

5. Tune autovacuum.

Operational verification

updates/deletes create dead tuples → vacuum reclaims → visibility map → freeze

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
MariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformMariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
Scope / prerequisiteUse it when tables bloat, autovacuum lags, or wraparound warnings appear.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

Capture the concrete output, symptom, packet, log or counter before changing anything.

Step 1 How to verify Verify boundary

Watch dead tuples and autovacuum activity over time. Find and end idle in transaction sessions. Tune per-table autovacuum thresholds for hot tables.

Step 2 Common failures Narrow the fault

An open transaction for hours causes bloat across the database. VACUUM FULL rewrites and locks the table. Ignoring wraparound warnings can force the database into read-only protection.

Key points

Long-running or idle-in-transaction sessions stop vacuum from cleaning. Tune autovacuum per table for busy tables. Wraparound warnings are urgent.

Key checks
  • SELECT relname,n_dead_tup,last_autovacuum FROM pg_stat_user_tables ORDER BY n_dead_tup DESC; — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -S <server> -Q "SELECT @@VERSION" — SQL Server reachable and version
ReferenceWhat it is1 section · depth on demand

How it works

Readers see the version valid for their snapshot. Vacuum marks dead versions reusable and updates the visibility map, which also enables index-only scans. InnoDB keeps old versions in undo logs and purges them in the background.

ReferenceImportant terms & boundaries3 sections · depth on demand

Vacuum in PostgreSQL

SELECT relname, n_dead_tup, last_autovacuum FROM pg_stat_user_tables ORDER BY n_dead_tup DESC LIMIT 10;
SELECT max(age(datfrozenxid)) FROM pg_database;   -- transaction ID wraparound risk
VACUUM (VERBOSE, ANALYZE) orders;

Updates and deletes leave dead tuples that autovacuum cleans. Long-running transactions and abandoned replication slots block cleanup, causing bloat. Tune autovacuum per busy table; VACUUM FULL rewrites the table with an exclusive lock.

In depth

Why PostgreSQL needs vacuum

An UPDATE writes a new row version and marks the old one expired; a DELETE only marks it. Old versions stay in the table until no running transaction can still see them; then VACUUM marks their space reusable. Vacuum cannot remove a version that the oldest open snapshot might still need — anywhere in the cluster, including replicas with hot_standby_feedback and stale replication slots.

Finding what holds vacuum back

SELECT pid, state, now() - xact_start AS xact_age, left(query, 60)
FROM pg_stat_activity WHERE xact_start IS NOT NULL ORDER BY xact_start LIMIT 5;

SELECT slot_name, active, xmin, catalog_xmin FROM pg_replication_slots;
SELECT gid, prepared FROM pg_prepared_xacts;          -- forgotten two-phase transactions

Transaction ID wraparound

Transaction IDs are 32-bit; rows must be frozen before their XID age reaches about 2 billion. Autovacuum forces an aggressive vacuum when a table's age exceeds autovacuum_freeze_max_age (default 200 million). If freezing cannot keep up, PostgreSQL warns, and close to the limit it stops assigning new transaction IDs until a vacuum completes.

SELECT datname, age(datfrozenxid) FROM pg_database ORDER BY 2 DESC;
SELECT relname, age(relfrozenxid) FROM pg_class WHERE relkind = 'r' ORDER BY 2 DESC LIMIT 5;

Tuning for hot tables

The default trigger is 50 rows + 20 % of the table changed. For a large, busy table lower it per table:

ALTER TABLE events SET (autovacuum_vacuum_scale_factor = 0.02, autovacuum_vacuum_cost_limit = 2000);

Bloat that already exists is not returned to the OS by plain VACUUM; VACUUM FULL (exclusive lock) or pg_repack (online) rewrites the table.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

Common mistakes

  • Long transactions blocking vacuum.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terminology layer

Terms & aliases

Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.

Aliases that resolve here
mvcc and vacuumpostgresql vacuummvcc bloat
Confirmation

Confirm action

Continue?