Sub topic 4
Making `archive_command` safe
Assume you can edit postgresql.conf, restart PostgreSQL when required, and inspect Linux file permissions. You should also understand that PostgreSQL treats a zero exit status from archive_command as proof that the WAL file is safely archived.
What makes an archive command reliable
PostgreSQL supplies two substitutions:
%pis the full path to the WAL segment in thepg_waldirectory.%fis the WAL filename, without its directory path.
The command must copy the complete file to durable storage. It must return success only after the copy has finished. If it returns a nonzero status, PostgreSQL keeps the WAL segment and retries the command.
A safe command has these properties:
| Property | Why it matters |
|---|---|
| Does not overwrite an existing file | A retry must not silently replace an archived segment |
| Handles spaces and shell characters | Quoting prevents a path from being split or interpreted |
| Uses a temporary file | Readers never see a partly copied WAL segment |
| Returns failure when storage is unavailable | PostgreSQL can retry instead of losing the segment |
| Uses a destination with enough capacity | A full archive stops successful archiving |
The archive destination should be separate from the active data directory. A second disk, a mounted backup volume, or an object-storage tool is safer than another directory on the same filesystem.
A safer wrapper script
Put the copying logic in a script instead of placing complex shell logic directly in postgresql.conf:
#!/bin/sh
set -eu
source=$1
filename=$2
archive_dir=/mnt/archive
destination=$archive_dir/$filename
temporary=$destination.tmp.$$
umask 077
trap 'rm -f "$temporary"' EXIT
if [ -f "$destination" ]; then
if cmp -s "$source" "$destination"; then
exit 0
fi
echo "archive collision: $destination differs from $source" >&2
exit 1
fi
cp -- "$source" "$temporary"
sync -f "$temporary"
mv "$temporary" "$destination"
trap - EXITMake the script executable and configure PostgreSQL to call it:
archive_command = '/usr/local/bin/archive-wal "%p" "%f"'The script treats an existing identical file as success. This matters when PostgreSQL retries after the archive operation completed but its success response was lost. If the existing file differs, the script fails loudly rather than hiding possible corruption.
The temporary filename prevents another process from reading a partial copy. The final mv is atomic when the temporary file and destination are on the same filesystem. The sync command asks the operating system to flush the temporary file before it becomes visible under its final name.
Shell quoting and permissions
Quote both %p and %f in the configuration. PostgreSQL normally supplies simple names, but quoting protects the command if the path contains spaces or shell-significant characters.
The PostgreSQL operating-system user needs read permission on the WAL source and write, create, and rename permission in the archive directory. Test the command as that user, not as an interactive administrator. Also check that the archive filesystem is mounted before PostgreSQL starts.
Exercise
Why should a wrapper compare an existing archive file before returning success?
Show answer
A retry may find a file created by an earlier attempt. An identical file proves the segment is already archived, while a different file indicates a collision or possible corruption that must not be ignored.