PostgreSQL implements Multi-Version Concurrency Control (MVCC) so readers never block writers and writers never block readers. Instead of overwriting rows in place, UPDATE and DELETE create new row versions (tuples) and mark old versions invisible to future transactions.

How MVCC works#

Every row version stores hidden system columns that determine visibility:

  • xmin: Transaction ID (XID) that inserted the tuple.
  • xmax: Transaction ID that deleted or updated the tuple (0 if still live).
  • ctid: Physical location (page, offset) of the tuple on disk.

Inspect tuple metadata on any table:

SELECT xmin, xmax, ctid, *
FROM my_table
LIMIT 5;

Because UPDATE writes a new tuple and leaves the old one behind until no active snapshot needs it, heavy update workloads generate dead tuples that must be reclaimed by VACUUM.

Interactive How PostgreSQL MVCC Works
HEAP PAGE (accounts · Page 0) TX A (Reader) idle snapshot: none TX B (Writer) idle txid: 101 Tuple v1 · ctid (0,1) · id=1, balance=1000 xmin = 100 | xmax = 0 LIVE · visible to all Tuple v2 · ctid (0,2) · id=1, balance=900 (not created yet) empty slot

Snapshot & Visibility

Isolation levels#

PostgreSQL supports three effective isolation levels (default: READ COMMITTED):

Isolation LevelDirty ReadNon-Repeatable ReadPhantom ReadSerialization Anomaly
READ COMMITTED (default)NoPossiblePossiblePossible
REPEATABLE READNoNoNo (in PG)Possible
SERIALIZABLENoNoNoNo

Check or set the isolation level:

SHOW default_transaction_isolation;

BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
-- queries see a consistent snapshot from the first statement
COMMIT;

Long-running transactions & idle in transaction#

A transaction left open holds back the cluster's visibility horizon (xmin), preventing VACUUM from reclaiming dead tuples across all tables — even tables the transaction never touched.

Find long-running or stuck transactions:

SELECT
  pid,
  usename,
  state,
  now() - xact_start AS xact_age,
  now() - state_change AS state_age,
  left(query, 100) AS query
FROM pg_stat_activity
WHERE xact_start IS NOT NULL
ORDER BY xact_age DESC;

Protect against forgotten sessions in postgresql.conf:

idle_in_transaction_session_timeout = '5min'

Transaction ID (XID) wraparound#

PostgreSQL uses a 32-bit transaction ID counter (~4 billion XIDs, ~2 billion in the past/future). Old tuples must be frozen by VACUUM before a database's age approaches 2 billion, otherwise PostgreSQL shuts down writes to prevent data loss.

Monitor XID age per database:

SELECT
  datname,
  age(datfrozenxid) AS xid_age,
  round(100.0 * age(datfrozenxid) / 2147483647, 2) AS pct_towards_wraparound
FROM pg_database
ORDER BY xid_age DESC;

Alert well before age(datfrozenxid) reaches 1,000,000,000 (50%) so you have plenty of time to investigate blocked autovacuum workers.