๐ Introduction
Have you ever wondered how PostgreSQL can handle hundreds of concurrent updates and still feel blazing fast?
If Postgres tried to update every piece of data directly on disk at commit time, performance would slow down dramatically. Why? Because accessing and writing to random pages in memory and on disk is costly.
Instead, Postgres uses a smarter approach.
If youโre curious about the deep internals of how Postgres manages this trick โ and want to walk away with a clear mental model of what happens when you run an UPDATE
โ keep reading. By the end of this article, youโll be proficient in one of the most important mechanisms inside Postgres.
โย TL;DR
- PostgreSQL uses Write-Ahead Logging (WAL) to guarantee:
- โก Speed of writes (sequential log writes instead of random I/O)
- ๐ Durability (your data is safe once committed)
- ๐ Crash recovery (WAL can replay changes after a failure)
- Updating a row in Postgres = modify in shared buffers โ log in WAL โ flush WAL โ commit โ later write data file.
๐ What is WAL?
<aside>
๐ก
WAL (Write-Ahead Logging) is a fundamental technique in databases where all changes are first written to a sequential log before being applied to the actual data files.
</aside>
- Sequential writes are fast: Writing to a log file is much cheaper than random writes to many data pages.
- Crash safety: If the database crashes, WAL can be replayed to bring the data files back to a consistent state.
- Deferred writes: Data pages in memory (shared buffers) donโt need to be flushed immediately โ WAL ensures the change is durable.
โย Without WAL:
- Every transaction would need to flush all modified data pages to disk before commit.
- This would be painfully slow, especially for random I/O.
โ
ย With WAL:
- Only a small sequential log write is needed at commit.