_These are my notes from reading Chapter 10 (Write ahead log) of the [Postgres Internals](https://postgrespro.com/community/books/internals) book_ PostgreSQL uses a write-ahead log (WAL) to ensure data consistency in case of a failure. The WAL is a continuous stream of log entries containing essential information to repeat operations in case of a crash. Postgres generates a LSN (log sequence number) for each item stored in the WAL. Each entry is written to disk before the corresponding data page, ensuring the log can recover data consistency. **Logical and Physical Structure:** The WAL is structured as a stream of log entries of variable length, each preceded by a standard header containing information such as the transaction ID, resource manager to interpret results, checksum, entry length, and a reference to the previous log entry. On disk, the WAL is stored in the `pg_wal` directory as separate files or segments, with a size determined by the `wal_segment_size` parameter. Entries are appended to the current file until it runs out of space, at which point a new file is started. **WAL Cache:** The WAL cache is a buffer that stores WAL entries in memory to reduce the number of disk writes and improve performance. The size of the WAL cache is determined by the `wal_buffers` parameter. New entries are added to the head, and the tail is written to disk. Under low load, the head and tail are almost always the same. **Checkpointing:** Recovery involves replaying the WAL entries to restore the database to a consistent state. To speed up the process, Postgres implements checkpointing. Checkpointing tracks how much of the database pages, transaction info, and other data have been safely written to disk and no longer require a WAL entry. This allows progress to clean up the WAL, and then the recovery process can start from that checkpoint entry. **Configuration and Monitoring:** There are various configurations for the WAL that can be fine-tuned, such as how often the background worker writes the WAL to disk and the maximum WAL file size. My takeaway from this section was to monitor your PostgreSQL instance and make changes based on that.