Overview
PostgreSQL implements MVCC by keeping multiple versions of a row in the table itself. An UPDATE does not change a row in place: it writes a new version and marks the old one as ended. Each transaction reads the versions visible to its snapshot, so readers and writers do not block each other (locking-and-mvcc compares engines).
Core concepts
- Every row version carries
xmin(the transaction id that created it) andxmax(the id that deleted or replaced it,0if none). - A snapshot records which transaction ids were still running when it was taken. A version is visible if its
xmincommitted before the snapshot and itsxmaxis absent, aborted, or not yet visible. - Read Committed takes a new snapshot per statement; Repeatable Read and Serializable keep one for the whole transaction (isolation-levels).
- Commit status is stored in
pg_xact; the first reader to check a row sets hint bits on it to avoid looking again. - Old versions are dead tuples once no snapshot can see them. They stay until
VACUUMreclaims the space (pg-vacuum-bloat). - Transaction ids are 32-bit and compared circularly, so very old rows must be frozen before about two billion transactions pass, or the server will refuse writes to avoid wraparound.
Worked example
Verified on PostgreSQL 17 (pageinspect extension):
CREATE TABLE t(id int PRIMARY KEY, v text);
INSERT INTO t VALUES (1,'a'),(2,'b');
SELECT ctid, xmin, xmax, * FROM t;
-- (0,1) | 741 | 0 | 1 | a
-- (0,2) | 741 | 0 | 2 | b
UPDATE t SET v = 'a2' WHERE id = 1;
SELECT ctid, xmin, xmax, * FROM t ORDER BY id;
-- (0,3) | 742 | 0 | 1 | a2 <- new version, new ctid
-- (0,2) | 741 | 0 | 2 | b
SELECT lp, t_xmin, t_xmax, t_ctid FROM heap_page_items(get_raw_page('t', 0));
-- 1 | 741 | 742 | (0,3) <- old version: ended by 742, points to the new one
-- 2 | 741 | 0 | (0,2)
-- 3 | 742 | 0 | (0,3)The old version of row 1 is still on the page, ended by transaction 742. Transaction ids will differ on your server.
How it works (internals)
- Updates are insert + mark. Every index entry that points at the old row version must also be handled; HOT (heap-only tuple) updates avoid new index entries when no indexed column changes and the new version fits on the same page (pg-vacuum-bloat).
- Locks vs. versions. Writers still take row-level locks to stop two transactions updating the same row; a second updater waits for the first to commit, then (under Read Committed) re-evaluates its
WHEREon the new version. SELECT ... FOR UPDATElocks rows without changing them;SKIP LOCKEDandNOWAITchange the waiting behaviour.
Pitfalls and production notes
- Long-running transactions (including idle ones,
idle in transaction) keep old snapshots alive and prevent cleanup everywhere, causing bloat. Setidle_in_transaction_session_timeoutand monitorxact_startinpg_stat_activity. - Replication slots and hot-standby feedback also hold back cleanup (pg-replication-failover).
- Watch
age(datfrozenxid); alert well before the 2 billion limit. - Counting rows (
count(*)) must visit versions to check visibility, so it is not constant time.
Practice questions
- Why is
UPDATEon a wide row more expensive than the changed bytes suggest? - How can a stuck session on a replica cause bloat on the primary?
- What makes an old row version removable?
References
- PostgreSQL manual, Concurrency Control and Routine Vacuuming: https://www.postgresql.org/docs/current/mvcc.html
- PostgreSQL manual,
pageinspect: https://www.postgresql.org/docs/current/pageinspect.html
Related
pg-vacuum-bloat · isolation-levels · locking-and-mvcc · pg-architecture