WAL archiving
WAL archiving preserves write-ahead-log segments for later recovery before normal log recycling can remove them.
PostgreSQL 18 uses WAL segment files, normally 16 MiB (16,777,216 bytes); initdb can select another segment size. With archive_mode on and wal_level at replica or higher, completed segments are passed to archive_command or archive_library. The shell command reports success with exit status zero; the library's archive_file_cb reports success by returning true. PostgreSQL then treats the file as archived and may remove or recycle the original. [1] [5] [6] [7]
WAL segment archiving processes completed segments, so a quiet server can leave committed changes in an open segment. Local commit durability therefore does not by itself prove archive coverage. archive_timeout measures elapsed time since the previous segment switch and requires database activity. Forced early switches still produce full-length files. [3]
For scale, a day with exactly 1,440 completed 16 MiB segments produces 24,159,191,040 bytes (22.5 GiB, about 24.16 GB) of uncompressed segment files. This is illustrative file-volume arithmetic, not an upper bound: workload-driven completion or manual pg_switch_wal calls can produce more segments. It is not a measurement of useful WAL records, network traffic or physical archive allocation. [1] [6]
Archive steps should refuse to overwrite an existing file with different contents, and should return success for an identical, fully persisted copy, because a crash can make the server archive the same segment twice. File names can be up to 64 characters long and must be preserved as given. [1]
Cleanup ignores timeline prefixes, compares segment positions and retains the cutoff to protect parent-timeline segments. [9] [10]
Relations
Advantages
- Together with a base backup, the archive allows recovery to replay changes up to a chosen point after that backup, not only to the moment the backup was taken. [1]
Limitations
- A successful archive command must mean the file is safely preserved, not merely that copying was attempted. [1]
- archive_timeout schedules segment switches, not completed transfers; treating it alone as an end-to-end RPO bound is unjustified. [3]
- last_archived_wal does not prove all earlier segments were archived: ordering is not guaranteed. Some failures that restart the archiver are not counted in pg_stat_archiver. [4, 1]
- Using pg_archivecleanup with %r as a standby's archive_cleanup_command is intended for that standby's transient staging area, not a long-term archive or one shared by recovering standbys. A standby restart boundary does not establish that WAL needed by every retained backup has been preserved. [8, 1]
- A PostgreSQL 16.15 probe covered 23 synthetic files in 10 cases, not valid WAL or restorability. At cutoff 000000020000000000000010, a later segment on timeline 1 remained but an earlier segment on timeline 3 was listed. [11]
- Version 16.15 accepts -n but rejects --dry-run and -b; the reviewed 18.0 source supports both newer options. A backup-history filename used as a cutoff is distinct from a backup-history candidate selected for cleanup. [9, 10, 11]
Citations
- PostgreSQL 18: Continuous Archiving and Point-in-Time Recovery
- PostgreSQL 18: archive_timeout
- PostgreSQL 18: pg_stat_archiver
- PostgreSQL 18: initdb WAL segment-size option
- PostgreSQL REL_18_0 initdb.c: binary WAL segment-size conversion
- PostgreSQL 18: archive module callback success and retry
- PostgreSQL 18: pg_archivecleanup and standby staging scope
- PostgreSQL 16.15: timeline-independent cleanup and short options
- PostgreSQL 18.0: cleanup selection and backup-history option
- Hesela: PostgreSQL 16.15 native dry-run filename probe, 10 cases
Further reading
Sources & caveats
- PostgreSQL 18 documentation: continuous archiving section 25.3.1, archive_timeout in section 19.5.3, archive callback section 49.2.3, initdb --wal-segsize and pg_archivecleanup Description; reviewed 2026-11-28. The pg-stats citation retains its earlier access date.
- REL_18_0 initdb.c passes the segment-size option multiplied by 1024 * 1024 to the backend; MiB makes that binary unit explicit.
- Worked example: 1,440 * 16 * 1,048,576 = 24,159,191,040 bytes; divide by 1,073,741,824 for 22.5 GiB, or by 1,000,000,000 for 24.15919104 GB. The segment count is an assumption, not a bound implied by a 60-second timeout. No PostgreSQL timing or recovery experiment was performed.
- Correction on 2026-11-28: removed the earlier 1,440-segment upper-bound claim and ambiguous 23 GB figure; separated shell exit status from library boolean success.
- Native probe: Ubuntu pg_archivecleanup 16.15-0ubuntu0.24.04.1, executed 2026-11-28. Seven selection cases plus three rejection controls used -n in a disposable synthetic directory; every filename, size and SHA256 stayed unchanged.
- Results publish commands, output and binary/probe hashes. PostgreSQL 18.0 was source-reviewed, not executed. No recovery or deletion experiment was performed.