postgresql_16:7.2.14.1 Description
PostgreSQL 16 > Reference > PostgreSQL Client Applications > pg_receivewal > Description
pg_receivewal is used to stream the write-ahead log from a running PostgreSQL cluster. The write-ahead log is streamed using the streaming replication protocol, and is written to a local directory of files. This directory can be used as the archive location for doing a restore using point-in-time recovery (see (see postgresql_16:4.11.3)).
pg_receivewal streams the write-ahead log in real time as it's being generated on the server, and does not wait for segments to complete like (see guc-archive-command) and (see guc-archive-library) do. For this reason, it is not necessary to set (see guc-archive-timeout) when using pg_receivewal.
Unlike the WAL receiver of a PostgreSQL standby server, pg_receivewal by default flushes WAL data only when a WAL file is closed. The option --synchronous must be specified to flush WAL data in real time. Since pg_receivewal does not apply WAL, you should not allow it to become a synchronous standby when (see guc-synchronous-commit) equals remote_apply. If it does, it will appear to be a standby that never catches up, and will cause transaction commits to block. To avoid this, you should either configure an appropriate value for (see guc-synchronous-standby-names), or specify application_name for pg_receivewal that does not match it, or change the value of synchronous_commit to something other than remote_apply.
The write-ahead log is streamed over a regular PostgreSQL connection and uses the replication protocol. The connection must be made with a user having REPLICATION permissions (see (see postgresql_16:4.7.2)) or a superuser, and pg_hba.conf must permit the replication connection. The server must also be configured with (see guc-max-wal-senders) set high enough to leave at least one session available for the stream.
The starting point of the write-ahead log streaming is calculated when pg_receivewal starts: First, scan the directory where the WAL segment files are written and find the newest completed segment file, using as the starting point the beginning of the next WAL segment file. If a starting point cannot be calculated with the previous method, and if a replication slot is used, an extra READ_REPLICATION_SLOT command is issued to retrieve the slot's restart_lsn to use as the starting point. This option is only available when streaming write-ahead logs from PostgreSQL 15 and up. If a starting point cannot be calculated with the previous method, the latest WAL flush location is used as reported by the server from an IDENTIFY_SYSTEM command.
If the connection is lost, or if it cannot be initially established, with a non-fatal error, pg_receivewal will retry the connection indefinitely, and reestablish streaming as soon as possible. To avoid this behavior, use the -n parameter.
In the absence of fatal errors, pg_receivewal will run until terminated by the SIGINT (ControlC) or SIGTERM signal.