Sub topic 13
Retention: Keeping the WAL Your Backups Need
Assume you know basic PostgreSQL administration, Linux file permissions, and how a physical base backup works. Retention means deciding how long to keep base backups and archived WAL files so that every retained backup can still be recovered.
Why WAL retention matters
A base backup is only the starting copy of a recovery. PostgreSQL replays archived WAL from the backup's recovery starting point until the requested recovery target.
For example, suppose you keep a base backup from Monday and want to recover it to Thursday. You must keep every required WAL segment between the backup and Thursday. If one segment is missing, recovery stops at the gap.
| Item | Role in recovery | Retention question |
|---|---|---|
| Base backup | Starting copy of the cluster | How many restore points must it support? |
| Archived WAL | Changes replayed after the backup | Does it cover the full recovery range? |
| Backup metadata | Identifies required WAL and timeline | Is it retained with the backup? |
A safe policy keeps each base backup together with all WAL needed to reach its latest allowed recovery target. A backup without that WAL is not a complete recovery option.
Design a retention policy
Start with recovery objectives:
- Recovery window: how far back you must recover, such as 35 days.
- Backup frequency: how often you create full physical backups.
- Target storage: local disk, object storage, or another archive.
- Safety margin: extra time for delayed discovery, copying, or restore testing.
A simple policy might keep weekly base backups for eight weeks and all archived WAL needed by those backups. The oldest retained backup determines the oldest WAL that can be removed. After deleting that backup, its dependent WAL can also become removable, but only if no newer retained backup needs it.
Keep backup metadata, including the backup label or the metadata recorded by your backup tool. It identifies the backup's WAL starting point and timeline.
wal_keep_size does not replace archive retention. It controls WAL kept in the primary data directory for standby needs. A replication slot can also retain WAL, but it does not tell you which archived files are required by a base backup. Monitor slots separately so an inactive slot does not fill the primary disk.
Delete archive files safely
Manual cleanup should use backup inventory and verified dependency calculations. A safer process is:
- List every retained base backup and its recovery start information.
- Identify the oldest WAL required by those backups.
- Confirm that a newer backup does not need the files you plan to remove.
- Delete only WAL older than that boundary.
- Verify that the archive still contains a continuous sequence after cleanup.
- Record the deletion and update the backup inventory.
Never remove WAL while a base backup is still being created. Also avoid deleting files that are still being copied into the archive. Archive commands should write atomically, or use a temporary name followed by a rename, so cleanup cannot mistake a partial file for a completed segment.
Use dedicated retention tooling
Tools such as pgBackRest, Barman, and WAL-G store backup metadata and can apply retention rules to backups and their dependent WAL. They also support repositories, checksums, compression, encryption, and remote storage.
Manual scripts can work for a small system, but they must handle interrupted uploads, missing files, concurrent backups, timelines, and failed deletions. A dedicated tool is usually safer when recovery objectives or backup history become important.
Exercise
You retain a base backup from day 1 and another from day 8. What must be true before deleting WAL from day 1 through day 7?
Show answer
The day 1 backup must no longer be retained, and the day 8 backup must not require any of those WAL segments. Verify this using backup metadata and a restore test rather than file age alone.