MVCC
Multi-version concurrency control (MVCC) in SQL is a technique where the database keeps several versions of a row, so that each transaction can see a consistent snapshot without blocking others. It is why, in PostgreSQL, a long report and a busy stream of updates can run at the same time: the report reads the versions that were current when it started, while the updates create new versions. In this lesson, we will see that an UPDATE really writes a new row version, learn how a snapshot decides which version each transaction sees, clean up old versions with VACUUM, and compare PostgreSQL's approach with SQL Server's and MySQL's.
In One Minute
- An
UPDATEin PostgreSQL writes a new version of the row; the old version stays until it's no longer needed. - Each row version records which transaction created it (
xmin) and which deleted it (xmax). - A transaction's snapshot decides which versions it can see.
- Because of versions, readers don't block writers and writers don't block readers.
- Old versions become dead rows;
VACUUMreclaims their space.
An UPDATE Writes a New Version
Every row has a hidden column, ctid, giving its physical location: (page, slot). Before any change, SQL Basics is the fourth row on the first page:
SELECT ctid, course_id, title, fee_usd
FROM courses
ORDER BY course_id; ctid | course_id | title | fee_usd
-------+-----------+-------------------+---------
(0,1) | 1 | Java Basics | 450.00
(0,2) | 2 | Java OOP | 500.00
(0,3) | 3 | Java Collections | 550.00
(0,4) | 4 | SQL Basics | 400.00
(0,5) | 5 | JavaScript Basics | 450.00
(0,6) | 6 | Git Basics | 250.00
(6 rows)Now update its fee, and look again:
UPDATE courses SET fee_usd = 410 WHERE course_id = 4;
SELECT ctid, course_id, title, fee_usd
FROM courses
WHERE course_id = 4;UPDATE 1
ctid | course_id | title | fee_usd
-------+-----------+------------+---------
(0,7) | 4 | SQL Basics | 410.00
(1 row)Explanation: SQL Basics moved from slot (0,4) to (0,7). PostgreSQL didn't change the row in place; it wrote a new version at a new location and marked the old one as replaced. Updating again would create yet another version. The old version at (0,4) still exists on disk for now, for any transaction that started before the update and must still see the fee as $400.00.
Snapshots: Who Sees Which Version
Each version is stamped with the transaction that created it (xmin) and, once replaced or deleted, the transaction that ended it (xmax). A transaction's snapshot is the list of transactions that had committed when it started. A version is visible to a transaction if its creator committed before the snapshot and its deleter hadn't.
| Version of SQL Basics | Created by | Ended by | Visible to a report that started before the update? | After? |
|---|---|---|---|---|
| fee 400.00 at (0,4) | the original load | the update | Yes | No |
| fee 410.00 at (0,7) | the update | (still current) | No | Yes |
That's how isolation works without making the report wait: it simply reads the 400.00 version.
Dead Rows and VACUUM
Once no running transaction can need an old version, it becomes a dead row: wasted space until VACUUM cleans it up. Updating every row of a 1,000-row table doubles the space it uses, because each row now has a dead old version and a live new one:
CREATE TABLE study_marks (id INTEGER PRIMARY KEY, mark INTEGER) WITH (autovacuum_enabled = false);
INSERT INTO study_marks SELECT g, 0 FROM generate_series(1, 1000) AS g;
UPDATE study_marks SET mark = 1;
SELECT pg_size_pretty(pg_relation_size('study_marks')) AS after_update;
VACUUM study_marks;
SELECT pg_size_pretty(pg_relation_size('study_marks')) AS after_vacuum;
VACUUM FULL study_marks;
SELECT pg_size_pretty(pg_relation_size('study_marks')) AS after_vacuum_full;CREATE TABLE
INSERT 0 1000
UPDATE 1000
after_update
--------------
72 kB
(1 row)
VACUUM
after_vacuum
--------------
72 kB
(1 row)
VACUUM
after_vacuum_full
-------------------
40 kB
(1 row)Explanation: Plain VACUUM marks the dead rows' space as free for future rows, but doesn't shrink the file, so the size stays the same. VACUUM FULL rewrites the table without dead rows, shrinking it from 72 kB to 40 kB, but it locks the table completely while it works. In real databases, the autovacuum background process runs plain VACUUM automatically; here it was turned off for the demonstration.
How Other Databases Do It
| Database | Where old versions live | Cleanup |
|---|---|---|
| PostgreSQL | In the table itself, next to new versions | VACUUM (autovacuum) |
| MySQL (InnoDB) | In a separate undo log; the table holds the newest version | Purge thread |
| Oracle | In undo segments | Automatic |
| SQL Server | Locks by default; with snapshot isolation, versions in tempdb | Automatic |
PostgreSQL's design makes rollbacks instant and reads simple, at the cost of needing VACUUM. Undo-log designs keep tables compact but make long-running reads rebuild old versions from the log. Tabs showing each database's version are coming to this course.
Common Mistakes
Turning off autovacuum. Dead rows accumulate and queries slow down.
Using VACUUM FULL routinely. It blocks all access to the table. Plain VACUUM is usually enough.
Long idle transactions. They stop cleanup across the database.
Thinking UPDATE changes a row in place. In PostgreSQL it creates a new version.
Interview Corner
What is MVCC?
Multi-version concurrency control: the database keeps multiple versions of rows so each transaction reads a consistent snapshot, and readers and writers don't block each other.
Why does PostgreSQL need VACUUM?
Because updates and deletes leave old row versions in the table. Once no transaction needs them, VACUUM marks their space for reuse and keeps table statistics and transaction ids healthy.
What's the difference between VACUUM and VACUUM FULL?
VACUUM frees dead rows' space for reuse without locking out reads and writes, and doesn't shrink the file. VACUUM FULL rewrites the table to its minimal size but takes an exclusive lock.
Practice
Challenge: Update SQL Basics' fee twice (to 410, then 420), and show its ctid afterwards. Compare with its original location, (0,4).
Show solution
UPDATE courses SET fee_usd = 410 WHERE course_id = 4;
UPDATE courses SET fee_usd = 420 WHERE course_id = 4;
SELECT ctid, course_id, fee_usd
FROM courses
WHERE course_id = 4;UPDATE 1
UPDATE 1
ctid | course_id | fee_usd
-------+-----------+---------
(0,8) | 4 | 420.00
(1 row)Each update wrote a new version: (0,4), then (0,7), then (0,8). The two older versions are now dead rows, waiting for VACUUM.
Key Takeaways
| You want to | Use |
|---|---|
| See a row's location | SELECT ctid, * FROM t |
| Free dead rows for reuse | VACUUM t; (autovacuum does it for you) |
| Shrink a bloated table | VACUUM FULL t; (locks the table) |
Was this tutorial helpful?