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.

When the WAL segment file WAL_7 is switched, the file is copied to the archival area as Archive log 7.
Fig. 9.20. 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 can be set to any Unix command or tool. This means that you can use the scp command or any other file backup tool to transfer the archive logs to another host, instead of using a simple copy command.

archive_library

In versions 14 or earlier, continuous archiving could only use shell commands.

In version 15, PostgreSQL introduced a loadable library feature that allows you to achieve continuous archiving using a library.

For more information, see the archive_library and basic_archive documentation.

Note

PostgreSQL does not automatically clean up created archive logs. Therefore, you must properly manage the logs when using this feature. If you do nothing, the number of archive logs will continue to grow.

The pg_archivecleanup utility is one of the useful tools for managing archive log files.

You can also use the find command to delete archive logs. For example, the following command would delete all archive logs that were created more than three days ago:

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