Postgresql 中文操作指南

pg_archivecleanup

pg_archivecleanup —— 清理 PostgreSQL WAL 存档文件

pg_archivecleanup — clean up PostgreSQL WAL archive files

Synopsis

pg_archivecleanup [ option …​] archivelocation oldestkeptwalfile

pg_archivecleanup [option…​] archivelocation oldestkeptwalfile

Description

pg_archivecleanup 设计为用作 archive_cleanup_command ,用以在运行备用服务器时清理 WAL 文件存档(参见 Section 27.2 )。pg_archivecleanup 也可用作清理 WAL 文件存档的独立程序。

pg_archivecleanup is designed to be used as an archive_cleanup_command to clean up WAL file archives when running as a standby server (see Section 27.2). pg_archivecleanup can also be used as a standalone program to clean WAL file archives.

要配置备用服务器以使用 pg_archivecleanup,请将其放入其 postgresql.conf 配置文件中:

To configure a standby server to use pg_archivecleanup, put this into its postgresql.conf configuration file:

archive_cleanup_command = 'pg_archivecleanup archivelocation %r'

其中 archivelocation 是要从中删除 WAL 段文件的目录。

where archivelocation is the directory from which WAL segment files should be removed.

archive_cleanup_command 中使用时,所有逻辑上在 %r 参数值之前的所有 WAL 文件都将从 archivelocation 中删除。这最大限度减少了需要保留的文件数,同时保留崩溃重启功能。如果 archivelocation 是此特定备用服务器的临时暂存区,那么使用此参数是合适的,但当 notarchivelocation 旨在作为长期 WAL 存档区,或当多个备用服务器从同一存档位置恢复时,使用此参数不合适。

When used within archive_cleanup_command, all WAL files logically preceding the value of the %r argument will be removed from archivelocation. This minimizes the number of files that need to be retained, while preserving crash-restart capability. Use of this parameter is appropriate if the archivelocation is a transient staging area for this particular standby server, but not when the archivelocation is intended as a long-term WAL archive area, or when multiple standby servers are recovering from the same archive location.

在用作独立程序时,逻辑上在 oldestkeptwalfile 之前的 WAL 文件都将从 archivelocation 中删除。在此模式下,如果您指定了 .partial.backup 文件名,则只会将文件前缀用作 oldestkeptwalfile 。这种处理方式 .backup 文件名允许您删除在特定基准备份之前存档的所有 WAL 文件,且不会报错。例如,以下示例将删除所有旧于 WAL 文件名 000000010000003700000010 的文件:

When used as a standalone program all WAL files logically preceding the oldestkeptwalfile will be removed from archivelocation. In this mode, if you specify a .partial or .backup file name, then only the file prefix will be used as the oldestkeptwalfile. This treatment of .backup file name allows you to remove all WAL files archived prior to a specific base backup without error. For example, the following example will remove all files older than WAL file name 000000010000003700000010:

pg_archivecleanup -d archive 000000010000003700000010.00000020.backup

pg_archivecleanup:  keep WAL file "archive/000000010000003700000010" and later
pg_archivecleanup:  removing file "archive/00000001000000370000000F"
pg_archivecleanup:  removing file "archive/00000001000000370000000E"

pg_archivecleanup 假设 archivelocation 是服务器所有者用户可以读写的目录。

pg_archivecleanup assumes that archivelocation is a directory readable and writable by the server-owning user.

Options

pg_archivecleanup 接受以下命令行参数:

pg_archivecleanup accepts the following command-line arguments:

  • -d

    • Print lots of debug logging output on stderr.

  • -n

    • Print the names of the files that would have been removed on stdout (performs a dry run).

  • -V_—​version_

    • Print the pg_archivecleanup version and exit.

  • -x extension

    • Provide an extension that will be stripped from all file names before deciding if they should be deleted. This is typically useful for cleaning up archives that have been compressed during storage, and therefore have had an extension added by the compression program. For example: -x .gz.

  • -?_—​help_

    • Show help about pg_archivecleanup command line arguments, and exit.

Environment

环境变量 PG_COLOR 指定是否在诊断消息中使用颜色。可能的值是 alwaysautonever

The environment variable PG_COLOR specifies whether to use color in diagnostic messages. Possible values are always, auto and never.

Notes

pg_archivecleanup 设计为在用作独立实用程序时与 PostgreSQL 8.0 和更高版本一起使用,或用作存档清理命令时与 PostgreSQL 9.0 和更高版本一起使用。

pg_archivecleanup is designed to work with PostgreSQL 8.0 and later when used as a standalone utility, or with PostgreSQL 9.0 and later when used as an archive cleanup command.

pg_archivecleanup 用 C 语言编写,具有易于修改的源代码,其中有专门指定用于修改以符合您自身需求的部分

pg_archivecleanup is written in C and has an easy-to-modify source code, with specifically designated sections to modify for your own needs

Examples

在 Linux 或 Unix 系统上,您可能使用:

On Linux or Unix systems, you might use:

archive_cleanup_command = 'pg_archivecleanup -d /mnt/standby/archive %r 2>>cleanup.log'

其中归档目录实际位于备用服务器上,以便 archive_command 通过 NFS 访问它,但文件是备用文件的本地文件。这将:

where the archive directory is physically located on the standby server, so that the archive_command is accessing it across NFS, but the files are local to the standby. This will: