PostgreSQL WAL: how change records support crash recovery
A write-ahead log (WAL) records a database change before the database writes that change to its data files. PostgreSQL can keep the log for a committed change durable without flushing every affected table and index page on every commit, then replay the log to restore a consistent state after a failure. This article explains WAL in PostgreSQL 18, including checkpoints, archiving, and operational limits. Other databases use similar structures, such as redo logs, but their file formats and guarantees differ.
This article focuses on PostgreSQL 18 behavior. It also gives a bounded, high-level comparison with MySQL 8.4 InnoDB redo logs. The examples are hypothetical; no database, configuration, or recovery procedure was run.
Why write the log before the data files?
Tables and indexes are spread across many disk pages. A transaction that changes several rows and indexes could trigger many scattered writes if every changed page had to be persisted immediately. WAL appends change records in sequence. The central rule is that the record describing a change must reach durable storage before the corresponding data page is written there. Because log writes are generally sequential, this can be more efficient than persisting each affected data page on every commit. PostgreSQL 18: Write-Ahead Logging
Consider a hypothetical order transaction that updates an order row and a stock index. PostgreSQL creates the WAL records needed for the change. When it reports the commit, it follows the configured durability rules to flush the log to storage. The table page itself may be written later. If power fails before that page reaches the data file, PostgreSQL can use the preserved WAL during restart to redo changes since the last checkpoint and bring the data files back into a consistent state. This ordering reduces the dangerous gap that could exist if a page were changed before its log record was saved.
Durability still depends on storage honoring flush requests. If the operating system or storage controller reports success before data has actually reached nonvolatile media, the WAL can also be lost or corrupted. PostgreSQL documents risks such as write-cache behavior and partially written pages. Database settings alone cannot guarantee that the storage hardware honors durability requests. PostgreSQL 18: Reliability
How does recovery replay the log after a failure?
PostgreSQL stores WAL segments in pg_wal under the data directory. A segment is normally 16 MB, although its size can be chosen when the cluster is initialized. Records contain information appropriate to the kind of change. During recovery, PostgreSQL reads forward from the location identified by a checkpoint and reapplies the required changes. The first modification of a page after a checkpoint may also put a full-page image into WAL, which can help repair a page that was only partly written. PostgreSQL 18: WAL Internals
Follow this hypothetical timeline. A checkpoint completes at 10:00. An order transaction commits at 10:03, but the changed data page has not yet been written to the data file. The server stops unexpectedly at 10:04. On restart, PostgreSQL reads WAL from the checkpoint position and redoes the committed change. A transaction interrupted before commit must not be exposed to users as complete; recovery also restores transaction state as it replays page changes. This example illustrates the sequence; it is not a report of a crash test.
What is a checkpoint's relationship to WAL?
A checkpoint is a recovery reference point that records that earlier changes have been applied to the data files. PostgreSQL writes dirty pages to disk and records a checkpoint entry in WAL. Recovery can start from that location instead of scanning every earlier record. Shorter checkpoint intervals can reduce the amount of WAL to replay after a failure, but they also cause dirty pages to be written more often. They can generate additional WAL, including full-page images on the first page modification after each checkpoint. So “more frequent checkpoints are always safer and faster” is not a sound tuning rule. PostgreSQL 18: WAL Configuration Guidance
max_wal_size is a soft threshold that can trigger an automatic checkpoint, not an absolute cap on the pg_wal directory. Actual usage can exceed it under conditions such as archive failure or old WAL being retained for a replication slot. If WAL keeps accumulating, disk space can run out and the database may no longer be able to write normally. Monitor checkpoint logs, WAL disk usage, and archive lag together. PostgreSQL documents how checkpoint intervals, max_wal_size, and archiving affect one another. PostgreSQL 18: WAL Runtime Configuration
How is WAL used for backup and replication?
WAL first supports crash recovery, but archived WAL can also be combined with a base backup to restore to a chosen time or send changes to a standby. PostgreSQL continuous archiving preserves a file-system-level base backup together with the WAL segments generated after it. During recovery, the base backup is restored and the sequence of logs is replayed to move the database forward to the target time. A logical dump such as pg_dump has a different format and cannot serve as the base backup for this WAL-replay method. PostgreSQL 18: Continuous Archiving and Point-in-Time Recovery
WAL archiving works on completed segments. When traffic is light, a recently committed change may take some time to reach archive storage. If an archive command fails, PostgreSQL may be unable to recycle the local segment until it is safely archived. Persistent failures can therefore increase local pg_wal usage. Operators should monitor archive successes and failures, the time of the last successful archive, accumulated lag, and free disk space. A very short archive_timeout can force frequent segment switches and waste storage.
Replication and backup are not substitutes for each other. Streaming replication sends changes to a standby and can help with failover or read scaling, but an accidental deletion can also be propagated. An archive does not identify a logically harmful change by itself either. Point-in-time recovery requires a selected recovery target, a suitable base backup, and an unbroken WAL sequence through that target. Keep backups independently of replicas and test the restore procedure.
Does WAL mean the same thing as a logical change log?
WAL usually refers to a database's internal physical recovery log, but a database can also decode log records and expose changes to other systems. In PostgreSQL, wal_level=logical adds the information required for logical decoding. Exposing row changes in a form a subscriber can understand may be part of logical replication or change data capture (CDC). This differs from physical replication, which copies and applies WAL in the database's physical format. Enabling logical decoding does not by itself create a complete CDC pipeline or a long-term retention system. PostgreSQL 18: WAL Runtime Configuration
WAL is not an audit log or an application event log. It is recorded for internal recovery, so a business that needs to explain over time who changed a price and why may need a separate audit or domain-event design. Do not treat WAL as the only long-term history. Its retention and replayability depend on checkpoints, archives, database versions, and base backups.
What should operators check?
Is `pg_wal` usage continually growing? Investigate archive failures, replication slots retaining old logs, and unusually large write workloads. Do not delete WAL files manually.
Is archiving failing or falling behind? Check supported status information such as `pg_stat_archiver`, then inspect remote storage connectivity, permissions, capacity, and the archive command's exit status. `pg_wal` can grow while the problem remains unresolved.
Does restart recovery take a long time? Review the amount of WAL to replay, checkpoint settings, and storage throughput together. Tightening checkpoints to reduce recovery time can increase routine write load.
Is a restore needed? Confirm the base backup's time, continuity of retained WAL, desired recovery target, and plan for validating business data afterward. Verify the procedure in an isolated recovery environment; do not experiment by deleting files from a production instance.
WAL makes changes recoverable by persisting a log before data pages. It is not itself a backup and does not automatically undo a harmful logical change. The recovery objective depends on WAL working together with checkpoints, reliable storage, continuous archiving, and a restore process that has been independently verified.
Scope and references
Product behavior in this article is based on the PostgreSQL 18 documentation. The sources below cover WAL ordering and recovery, internal files, reliability, checkpoints, runtime settings, and archiving. MySQL 8.4 InnoDB redo-log documentation was also read for comparison: its redo log likewise reapplies changes not yet reflected in data files after a crash, but this article does not assume its layout or detailed behavior matches PostgreSQL.
[PostgreSQL 18: Write-Ahead Logging](https://www.postgresql.org/docs/18/wal-intro.html)
[PostgreSQL 18: Reliability](https://www.postgresql.org/docs/18/wal-reliability.html)
[PostgreSQL 18: WAL Internals](https://www.postgresql.org/docs/18/wal-internals.html)
[PostgreSQL 18: WAL Runtime Configuration](https://www.postgresql.org/docs/18/runtime-config-wal.html)
[PostgreSQL 18: WAL Configuration Guidance](https://www.postgresql.org/docs/18/wal-configuration.html)
[PostgreSQL 18: Continuous Archiving and Point-in-Time Recovery](https://www.postgresql.org/docs/18/continuous-archiving.html)
[MySQL 8.4: InnoDB Redo Log](https://dev.mysql.com/doc/refman/8.4/en/innodb-redo-log.html)
Product behavior is limited to the documented versions above. Before applying settings, check the official documentation for the installed server because defaults can change by version and distribution. No commands were run on a production server, and no real recovery was tested.