Boss lesson · Unit 04
Inside Postgres
Row versions, the write-ahead log, VACUUM and replicas
What you practise
- Pages and the heap, the write-ahead log and crash recovery, MVCC row versions and why
UPDATEwrites a new row, dead tuples andVACUUM, the buffer cache, streaming replication and read replicas
Try a question from this lesson
Pick one
You UPDATE a balance from 100 to 70. A transaction that began earlier must keep reading 100 from its snapshot. So where is the 100 now?
Correct answer
Still in the table, as an old version of the row
Why
Postgres does not overwrite. The UPDATE wrote a second version of the row next to the first, and the old one stays for as long as any snapshot might still need it.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Rows have versions
An UPDATE writes a new version of the row and marks the old one as ended.
Each version records the transaction that created it (xmin) and the one that ended it (xmax). A snapshot simply picks the version that was current when it was taken.
This is MVCC: multi-version concurrency control.
xmin xmax name balance
100 205 Ana 100 <- old
205 - Ana 70 <- newConcept
The write-ahead log
Rewriting data pages on disk at every commit would be slow: the pages are scattered all over the file.
So each change is first appended to one sequential file, the write-ahead log (WAL). COMMIT returns once the log is safely on disk. The data pages are written later, in the background.
After a crash, Postgres replays the log.
10 more exercises in the app
- 2× type the answer
- 5× multiple choice
- 1× true or false
- 1× order the lines
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.