10
Recovery
During server startup after a crash
1 Find LSN
0
corresponding to the start of the last completed
checkpoint
2 Apply each WAL record starting from LSN
0
,
if the record’s LSN is greater than the page's LSN
3 Abort transactions that were started but not committed
4 Overwrite unlogged tables using their init files
5 Perform a checkpoint
xid
checkpoint
checkpoint crash
required WAL files
start
of recovery
If the server fails, at the subsequent start, the startup process detects this by
looking into the pg_control file to find the status different from "shut down“.
Automatic recovery is done in this case.
The process first reads the LSN of the last completed checkpoint record
from that same pg_control file. From this record, the process determines
LSN0, the starting position of that checkpoint.
(In case of recovery from a backup, the backup_label file is present, and the
checkpoint record is read from that file instead. For more details, see the
DBA3 course.)
Next, the startup process reads the WAL records one by one, starting from
the determined position, sequentially applying records to pages, if necessary
(the need is determined by comparing LSN of the on-disk page with LSN of
the WAL record). Page modifications occur in the buffer cache, just as they
do during normal operations.
Records related to CLOG pages restore the status of transactions.
Transactions not committed by the end of the recovery are considered
aborted; their changes are not visible in any data snapshots.
WAL records are applied to files in a similar way: for example, if it is clear
from a record that the file should be created, but it does not exist, the file is
created.
At the end of the process, all unlogged tables are overwritten using the
templates stored in the init files. At this point, the startup process completes
the recovery and performs a checkpoint.