# 4.2 The Recovery Parade (Crash Recovery) ![The Recovery Parade](assets/arch_recovery_parade.png) Imagine a sudden, violent thunderclap. The power in the depot flickers and dies. The elephant, who was in the middle of three different tea-parties, falls asleep instantly. When the power returns and the elephant wakes up, he has **amnesia**. He doesn't remember what was in his head, what was on his desk (Memory), or which suitcases he was currently moving. "Where am I? Who am I? And where is my jam?" he wonders. ## The Morning After The Filing Cabinet (the Frozen Pantry) is now in a "dirty" state. Some suitcases are halfway moved, some are missing, and the whole depot is a mess. But the elephant doesn't panic. He reaches into his pocket and pulls out the **Pocket Diary (WAL)**. The diary survived the storm because of the `fsync` handshake we talked about in **[[Chapter 4/4.1 - The Pocket Diary (WAL & fsync)|The Pocket Diary]]**. ## The Redo Log The elephant begins the **Recovery Parade**. He starts at the very last "safe point" (the last **[[Chapter 4/4.1 - Write-Ahead Logging#The Checkpoint|Checkpoint]]**) and reads every single scribble in the diary from that point forward. Because every scribble has a unique **Log Sequence Number (LSN)**, the elephant never gets confused about the order of events. He pulls the first `XLogRecord` out of the 16MB segment file, looks at its LSN, and walks over to the Shipping Container in the Frozen Pantry basement. When he opens the container, he checks the `pd_lsn` stamped on the wobbly control panel. If the container's LSN is *older* than the scribble's LSN, he knows the cabinet missed the update when the power went out! _"I changed Row 5 to Red..."_ -> The elephant checks Row 5. If it's still Blue, he makes it Red. He then updates the container's `pd_lsn` to match the scribble, proving he did the work. _"I moved Suitcase 10 to Shelf B..."_ -> He moves the suitcase. It’s like a playback of a very confusing movie! He "redoes" every single successful action that was confirmed before the power went out. He also looks for any tasks that were **not** finished—those where the user hadn't received a thumbs-up yet. For these, he performs an **Undo** (or simply ignores them), ensuring that the depot returns to a perfectly consistent state, as if the uncommitted actions never existed. ## The Miracle of Consistency This parade is why Postgres is so famously reliable. You can pull the plug, kick the server, or have a catastrophic kernel crash, and as long as the Pocket Diary is intact, the elephant will eventually rebuild the world exactly as it was promised to be. This recovery process happens automatically every time Postgres starts up. If you see a message saying "database system was not shut down cleanly; automatic recovery in progress," don't worry—that's just the elephant finishing his morning reading. --- [[Chapter 4/4.1 - The Pocket Diary (WAL & fsync)|← 4.1 - Write-Ahead Logging]] | [[Chapter 4/4.0 - Safety Without Sweating (The Write-Ahead Log)|↑ 4.0 - Safety Without Sweating]] | [[Chapter 4/4.3 - The Town Crier (Logical Replication)|4.3 - Publications & Subscriptions →]]