MVCC
On this page 8 topics
Overview
Topics
- Snapshots
- row versions
- vacuum
- bloat
- visibility
Learn first
2 h of practice (estimate)
Learning outcomes
- Explain MVCC: row versions, snapshots, and visibility.
- Explain why
UPDATEcreates new row versions and what vacuum does. - Detect table and index bloat and long-running transactions that prevent cleanup.
Lessons
Lab and exercises
Lab
MID-014-LAB1 · Observe MVCC
Outcomes: 1–3 · Time: ~2 h With two psql sessions, watch xmin/xmax/ctid change across INSERT, UPDATE, DELETE, and see what each session can see under READ COMMITTED and REPEATABLE READ. Then create bloat with an idle-in-transaction session and a hot update loop, measure it, and fix it. Acceptance: annotated session transcript covering visibility, bloat creation, detection, and the fix.
Exercises (assessment: quiz)
| ID | Outcome | Task |
|---|---|---|
| MID-014-EX01 | 1 | Explain in three sentences why a long read does not block a write. |
| MID-014-EX02 | 1 | Predict what session B sees in each of the two isolation levels after session A commits an update mid-transaction, then verify. |
| MID-014-EX03 | 2 | Update one row three times and show the ctid and xmin each time. |
| MID-014-EX04 | 2 | State what VACUUM does that VACUUM FULL does differently, and when each is appropriate. |
| MID-014-EX05 | 3 | Query pg_stat_user_tables to find the top three tables by dead tuples and their last autovacuum. |
| MID-014-EX06 | 3 | Find the oldest open transaction in pg_stat_activity and set idle_in_transaction_session_timeout for a role. |
Show solutions
- EX01 Readers use a snapshot of old versions; writers create new versions; no row lock is needed to read.
- EX02
READ COMMITTED: sees the new value on the next statement;REPEATABLE READ: keeps the old value until the transaction ends. - EX03
ctidchanges with each update (new physical location);xminis the updating transaction id. - EX04
VACUUMreclaims space for reuse without blocking reads/writes;VACUUM FULLrewrites the table to return space to the OS under an exclusive lock, so use it rarely and in a window. - EX05
SELECT relname, n_dead_tup, last_autovacuum FROM pg_stat_user_tables ORDER BY n_dead_tup DESC LIMIT 3; - EX06
ORDER BY xact_startonpg_stat_activity;ALTER ROLE app SET idle_in_transaction_session_timeout = '30s';(applies to new sessions). - Disabling autovacuum and open transactions in application code are demonstrated in the lab and the lecture demo.
Quiz
Outcomes: O1 MVCC and visibility · O2 row versions and vacuum · O3 bloat and long transactions.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Quiz is the assessment, plus the annotated session transcript from MID-014-LAB1 and exercises MID-014-EX01 to EX06. Pass: quiz 8/10 and a transcript that shows visibility changes, bloat creation, and the fix.
Common mistakes
Disabling autovacuum; leaving transactions open in application code.
Assessment
Quiz.
Career relevance
Isolation (MID-015) and database operations (MID-018).