9.10. Continuous Archiving and Archive Logs

Continuous archiving is a feature that copies WAL segment files to an archival area whenever a WAL segment switch occurs. The archiver (background) process performs this task. The copied file is known as an archive log. This feature is typically utilized for hot physical backup and PITR (Point-in-Time Recovery), which are described in Chapter 10.

The archive_command configuration parameter defines the path to the archival area. For example, the following setting copies WAL segment files to the directory /home/postgres/archives/ at each segment switch:

archive_command = 'cp %p /home/postgres/archives/%f'

In this command, the %p placeholder represents the source WAL segment path, and the %f placeholder represents the archive log filename.

When the WAL segment file WAL_7 is switched, the file is copied to the archival area as Archive log 7.
Figure 9.26. Continuous archiving.

When the WAL segment file WAL_7 is switched, the file is copied to the archival area as Archive log 7.

The archive_command parameter accepts any Unix command or tool. This allows the use of scp or other backup tools to transfer archive logs to remote hosts, rather than relying on simple copy commands.

archive_library

PostgreSQL versions 14 and earlier relied on shell commands for continuous archiving. However, version 15 introduced a loadable library feature to enable continuous archiving through library-based mechanisms.

For further details, consult the documentation on archive_library and basic_archive.

Note

PostgreSQL does not automatically clean up archive logs, so administrators must manage these files. Without intervention, the number of archive logs grows continuously.

The pg_archivecleanup utility is a useful tool for managing archive log files.

Additionally, the find command can remove old archive logs. For instance, the following command deletes logs older than three days:

$ find /home/postgres/archives -mtime +3d -exec rm  -f {} \;