9.10. Continuous Archiving and Archive Logs
Continuous archiving is a feature that copies WAL segment files to an archival area at the time when a WAL segment switch occurs. It is performed by the archiver (background) process. The copied file is called an archive log. This feature is typically used for hot physical backup and PITR (Point-in-Time Recovery), which are described in Chapter 10.
The path to the archival area is set by the archive_command configuration parameter. For example, the following parameter would copy WAL segment files to the directory ‘/home/postgres/archives/’ every time a segment switch occurs:
archive_command = 'cp %p /home/postgres/archives/%f'
where, the ‘%p’ placeholder represents the copied WAL segment, and the ‘%f’ placeholder represents the archive log.
The archive_command parameter can be set to any Unix command or tool. This enables the use of scp or other file backup tools to transfer archive logs to remote hosts, instead of relying on simple copy commands.
PostgreSQL versions 14 and earlier relied on shell commands for continuous archiving.
However, version 15 introduced a loadable library feature, enabling continuous archiving through library-based mechanisms.
For further details, consult the documentation on archive_library and basic_archive.
PostgreSQL does not automatically clean up created archive logs, so proper log management is essential when using this feature. Without intervention, the number of archive logs will continuously grow.
The pg_archivecleanup utility is one of the useful tools for managing archive log files.
Additionally, the find command can be used to delete old archive logs.
For instance, to remove logs older than three days:
$ find /home/postgres/archives -mtime +3d -exec rm -f {} \;