
WAL retention is a critical topic in PostgreSQL database management. However, DBAs often encounter situations such as:
While external backup tools handle retention policies well, PostgreSQL also includes a simple utility: pg_archivecleanup. This tool is often overlooked but can be very useful.
It supports two primary use cases:
It operates in:
-n)-d)Lists WALs older than a given WAL file:
|
1 2 3 4 |
$ ./pg_archivecleanup -n ~/bigsql/data/pg11/pg_wal 00000001000000000000001E /home/jobin/bigsql/data/pg11/pg_wal/00000001000000000000001B /home/jobin/bigsql/data/pg11/pg_wal/000000010000000000000017 ... |
Removes WALs and logs actions:
|
1 2 3 4 5 |
$ pg_archivecleanup -d /home/postgres/archive 00000002000000000000006B pg_archivecleanup: keeping WAL file "...06B" and later pg_archivecleanup: removing file "...069" pg_archivecleanup: removing file "...06A" ... |
Careful planning is required to avoid losing critical WAL files:
Criteria 1: Preserve WALs from the oldest backup still within retention and PITR requirements.
Criteria 2: Preserve WALs from backup start to completion for consistent restores.
Criteria 3: Preserve WALs needed by standby systems (from restart point).
Criteria 4: Preserve WALs required for crash recovery (from last checkpoint).
Backup tools generate history files with a .backup extension:
0000000200000000000000C9.000002D0.backup
Use this as a reference for cleanup:
|
1 2 3 4 |
$ pg_archivecleanup -d /home/postgres/archive 000000020000000000000069.000003E0.backup pg_archivecleanup: keeping WAL file "...069" and later pg_archivecleanup: removing file "...05D" ... |
Use archive_cleanup_command:
|
1 |
archive_cleanup_command = 'pg_archivecleanup archivelocation %r' |
Note: Multiple standby systems sharing the same archive may require custom logic.
Retrieve checkpoint WAL info:
|
1 2 |
$ pg_controldata -D /var/lib/pgsql/11/data/ Latest checkpoint's REDO WAL file: 000000020000000300000037 |
Most real-world implementations combine multiple criteria using scripts or backup tools. pg_archivecleanup can be used as part of these workflows.
Note: It can also use .partial WAL files as reference.
The pg_archivecleanup source is small (~400 lines) and located in:
/src/bin/pg_archivecleanup/pg_archivecleanup.c
Key functions:
Execution flow:
Initialize → SetWALFileNameForCleanup → CleanupPriorWALFiles
This utility is simple, customizable, and a good candidate for extension via scripts or direct modification.
Enjoy the flexibility PostgreSQL offers for customization. Happy coding.
Resources
RELATED POSTS