Postgresql 中文操作指南
pg_receivewal
pg_receivewal —— 从 PostgreSQL 服务器流式传输预写式日志
pg_receivewal — stream write-ahead logs from a PostgreSQL server
Description
pg_receivewal 用于从正在运行的 PostgreSQL 集群流式传输预写式日志。预写式日志使用流式复制协议进行流式传输,并写入本地文件目录。此目录可用作用于执行恢复的归档位置,并使用当时恢复(请参阅 Section 26.3 )。
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 Section 26.3).
pg_receivewal 实时流式传输预写式日志,就像在服务器上生成日志一样,并且不会像 archive_command 和 archive_library 那样等待段完成。出于此原因,在使用 pg_receivewal 时无需设置 archive_timeout 。
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 archive_command and archive_library do. For this reason, it is not necessary to set archive_timeout when using pg_receivewal.
与 PostgreSQL 备用服务器的 WAL 接收器不同,pg_receivewal 在默认情况下仅在 WAL 文件关闭时才刷新 WAL 数据。必须指定选项 —synchronous 以实时刷新 WAL 数据。由于 pg_receivewal 不应用 WAL,因此当 synchronous_commit 等于 remote_apply 时,你不应该允许它成为同步备用。如果是这样,它将看起来像一个永不赶上的备用,并将导致事务提交阻塞。为避免这种情况,你应该为 synchronous_standby_names 配置适当的值,或为与其不匹配的 pg_receivewal 指定 application_name ,或将 synchronous_commit 的值更改为除 remote_apply 之外的其他值。
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 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 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.
预写式日志通过常规的 PostgreSQL 连接进行流式传输,并使用复制协议。必须使用具有 REPLICATION 权限(请参阅 Section 22.2 )或超级用户的用户建立连接,并且 pg_hba.conf 必须允许复制连接。服务器还必须配置 max_wal_senders ,将其设置为足够高以使至少有一个会话可用于流。
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 Section 22.2) or a superuser, and pg_hba.conf must permit the replication connection. The server must also be configured with max_wal_senders set high enough to leave at least one session available for the stream.
pg_receivewal 开始时会计算预写日志流式传输的起点:
The starting point of the write-ahead log streaming is calculated when pg_receivewal starts:
如果连接丢失,或者连接无法以非致命性错误初始化,pg_receivewal 将无限期地重试连接,并且在可行时立即重新建立流式传输。如需避免此类行为,请使用 -n 参数。
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.
在没有致命性错误的情况下,pg_receivewal 将运行,直至被 SIGINT ( Control + C ) 或 SIGTERM 信号终止。
In the absence of fatal errors, pg_receivewal will run until terminated by the SIGINT (Control+C) or SIGTERM signal.
Options
-
-D _directory—directory=_directory
-
Directory to write the output to.
-
This parameter is required.
-
-
-E _lsn—endpos=_lsn
-
Automatically stop replication and exit with normal exit status 0 when receiving reaches the specified LSN.
-
If there is a record with LSN exactly equal to lsn, the record will be processed.
-
-
—if-not-exists
-
Do not error out when —create-slot is specified and a slot with the specified name already exists.
-
-
-n_—no-loop_
-
Don’t loop on connection errors. Instead, exit right away with an error.
-
-
—no-sync
-
This option causes pg_receivewal to not force WAL data to be flushed to disk. This is faster, but means that a subsequent operating system crash can leave the WAL segments corrupt. Generally, this option is useful for testing but should not be used when doing WAL archiving on a production deployment.
-
This option is incompatible with —synchronous.
-
-
-s _interval—status-interval=_interval
-
Specifies the number of seconds between status packets sent back to the server. This allows for easier monitoring of the progress from server. A value of zero disables the periodic status updates completely, although an update will still be sent when requested by the server, to avoid timeout disconnect. The default value is 10 seconds.
-
-
-S _slotname—slot=_slotname
-
Require pg_receivewal to use an existing replication slot (see Section 27.2.6). When this option is used, pg_receivewal will report a flush position to the server, indicating when each segment has been synchronized to disk so that the server can remove that segment if it is not otherwise needed.
-
When the replication client of pg_receivewal is configured on the server as a synchronous standby, then using a replication slot will report the flush position to the server, but only when a WAL file is closed. Therefore, that configuration will cause transactions on the primary to wait for a long time and effectively not work satisfactorily. The option —synchronous (see below) must be specified in addition to make this work correctly.
-
-
—synchronous
-
Flush the WAL data to disk immediately after it has been received. Also send a status packet back to the server immediately after flushing, regardless of —status-interval.
-
This option should be specified if the replication client of pg_receivewal is configured on the server as a synchronous standby, to ensure that timely feedback is sent to the server.
-
-
-v_—verbose_
-
Enables verbose mode.
-
-
-Z _level-Z _method[:_detail_]—compress=_level—compress=_method[:_detail__]_
-
Enables compression of write-ahead logs.
-
The compression method can be set to gzip, lz4 (if PostgreSQL was compiled with —with-lz4) or none for no compression. A compression detail string can optionally be specified. If the detail string is an integer, it specifies the compression level. Otherwise, it should be a comma-separated list of items, each of the form keyword or keyword=value. Currently, the only supported keyword is level.
-
If no compression level is specified, the default compression level will be used. If only a level is specified without mentioning an algorithm, gzip compression will be used if the level is greater than 0, and no compression will be used if the level is 0.
-
The suffix .gz will automatically be added to all filenames when using gzip, and the suffix .lz4 is added when using lz4.
-
以下命令行选项控制数据库连接参数。
The following command-line options control the database connection parameters.
-
-d _connstr—dbname=_connstr
-
Specifies parameters used to connect to the server, as a connection string; these will override any conflicting command line options.
-
The option is called —dbname for consistency with other client applications, but because pg_receivewal doesn’t connect to any particular database in the cluster, database name in the connection string will be ignored.
-
-
-h _host—host=_host
-
Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix domain socket. The default is taken from the PGHOST environment variable, if set, else a Unix domain socket connection is attempted.
-
-
-p _port—port=_port
-
Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections. Defaults to the PGPORT environment variable, if set, or a compiled-in default.
-
-
-U _username—username=_username
-
User name to connect as.
-
-
-w_—no-password_
-
Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password.
-
-
-W_—password_
-
Force pg_receivewal to prompt for a password before connecting to a database.
-
This option is never essential, since pg_receivewal will automatically prompt for a password if the server demands password authentication. However, pg_receivewal will waste a connection attempt finding out that the server wants a password. In some cases it is worth typing -W to avoid the extra connection attempt.
-
pg_receivewal 可以执行以下两种操作之一来控制物理复制槽:
pg_receivewal can perform one of the two following actions in order to control physical replication slots:
-
—create-slot
-
Create a new physical replication slot with the name specified in —slot, then exit.
-
-
—drop-slot
-
Drop the replication slot with the name specified in —slot, then exit.
-
还可以使用其他选项:
Other options are also available:
-
-V_—version_
-
Print the pg_receivewal version and exit.
-
-
-?_—help_
-
Show help about pg_receivewal command line arguments, and exit.
-
Exit Status
pg_receivewal 在 SIGINT 或 SIGTERM 信号终止时将退出,状态为 0。(这是结束它的正常方式。因此它不是错误。)对于致命错误或其他信号,退出状态将为非零。
pg_receivewal will exit with status 0 when terminated by the SIGINT or SIGTERM signal. (That is the normal way to end it. Hence it is not an error.) For fatal errors or other signals, the exit status will be nonzero.
Environment
此实用工具与大多数其他 PostgreSQL 实用工具类似,它使用 libpq 所支持的环境变量(请参阅 Section 34.15 )。
This utility, like most other PostgreSQL utilities, uses the environment variables supported by libpq (see Section 34.15).
环境变量 PG_COLOR 指定是否在诊断消息中使用颜色。可能的值是 always 、 auto 和 never 。
The environment variable PG_COLOR specifies whether to use color in diagnostic messages. Possible values are always, auto and never.
Notes
在使用 pg_receivewal 代替 archive_command 或 archive_library 作为主 WAL 备份方法时,强烈建议使用复制槽。否则,由于服务器没有任何来自 archive_command 或 archive_library 或复制槽的信息,在未备份的情况下,服务器可以自由回收或删除预写日志文件,表明 WAL 流已经存档到何处。但是,请注意,如果接收方跟不上提取 WAL 数据的进度,复制槽将会填满服务器的磁盘空间。
When using pg_receivewal instead of archive_command or archive_library as the main WAL backup method, it is strongly recommended to use replication slots. Otherwise, the server is free to recycle or remove write-ahead log files before they are backed up, because it does not have any information, either from archive_command or archive_library or the replication slots, about how far the WAL stream has been archived. Note, however, that a replication slot will fill up the server’s disk space if the receiver does not keep up with fetching the WAL data.
如果源集群启用了组权限,pg_receivewal 将保留接收的 WAL 文件上的组权限。
pg_receivewal will preserve group permissions on the received WAL files if group permissions are enabled on the source cluster.
Examples
从 mydbserver 服务器传输预写日志并将其存储在本地目录 /usr/local/pgsql/archive 中:
To stream the write-ahead log from the server at mydbserver and store it in the local directory /usr/local/pgsql/archive:
$ pg_receivewal -h mydbserver -D /usr/local/pgsql/archive