バージョンごとのドキュメント一覧

pg_rewind

pg_rewind <refpurpose>synchronize a <productname>PostgreSQL</productname> data directory with another data directory that was forked from it</refpurpose> PostgreSQLのデータディレクトリを、そこから派生した他のデータディレクトリと同期する

概要

pg_rewind [option...] { -D | --target-pgdata } directory { --source-pgdata=directory | --source-server=connstr }

説明

<title>Description</title>

<application>pg_rewind</application> is a tool for synchronizing a PostgreSQL cluster with another copy of the same cluster, after the clusters' timelines have diverged. A typical scenario is to bring an old primary server back online after failover as a standby that follows the new primary. pg_rewindは、PostgreSQLのクラスタのタイムラインが分岐した後、クラスタをその複製のクラスタに同期するためのツールです。 典型的なシナリオとしては、フェイルオーバー後、新しいプライマリに追従するスタンバイとして、古いプライマリサーバをオンラインに戻す、というのがあります。

After a successful rewind, the state of the target data directory is analogous to a base backup of the source data directory. Unlike taking a new base backup or using a tool like <application>rsync</application>, <application>pg_rewind</application> does not require comparing or copying unchanged relation blocks in the cluster. Only changed blocks from existing relation files are copied; all other files, including new relation files, configuration files, and WAL segments, are copied in full. As such the rewind operation is significantly faster than other approaches when the database is large and only a small fraction of blocks differ between the clusters. 巻き戻し(rewind)が成功すれば、ターゲットデータディレクトリの状態はソースデータディレクトリのベースバックアップと類似したものになります。 新しいベースバックアップを取ったり、rsyncのようなツールを使うのとは異なり、pg_rewindはクラスタ内の変更されていないリレーションブロックの比較やコピーを必要としません。 既存のリレーションファイルのうちの変化のあったブロックだけがコピーされます。それ以外のすべてのファイルは、新しいリレーションファイルや設定ファイル、WALセグメントを含め、すべてのファイルがコピーされます。 そのため、データベースが大きく、クラスタ間で変更されているブロックの割合が小さい場合には、巻き戻し操作は他の方法に比べて極めて高速になります。

<application>pg_rewind</application> examines the timeline histories of the source and target clusters to determine the point where they diverged, and expects to find WAL in the target cluster's <filename>pg_wal</filename> directory reaching all the way back to the point of divergence. The point of divergence can be found either on the target timeline, the source timeline, or their common ancestor. In the typical failover scenario where the target cluster was shut down soon after the divergence, this is not a problem, but if the target cluster ran for a long time after the divergence, its old WAL files might no longer be present. In this case, you can manually copy them from the WAL archive to the <filename>pg_wal</filename> directory, or run <application>pg_rewind</application> with the <literal>-c</literal> option to automatically retrieve them from the WAL archive. The use of <application>pg_rewind</application> is not limited to failover, e.g., a standby server can be promoted, run some write transactions, and then rewound to become a standby again. pg_rewindはソースとターゲットクラスタ内のタイムラインヒストリーを調べ、それらがどの時点で異なるものになったのかを調べます。 差異が発生した分岐点までずっと遡ることにより、ターゲットクラスタ内のpg_walディレクトリ内の分岐点に到達するWALを見つけようとします。 変化の分岐点は、ターゲット側のタイムライン中、ソース側のタイムライン中、あるいはそれら共通の祖先の中に見つかる可能性が高いです。 分岐点のあと間をおかずシャットダウンされたような典型的なフェイルオーバーのシナリオにおいては、これは特に問題になりません。 しかし、分岐点の後にターゲットクラスタが長時間運用されていた場合には、古いWALファイルはもう存在しないかもしれません。 この場合は、WALアーカイブから手動でpg_walディレクトリにコピーできます。 あるいは、-cオプションを付けてpg_rewindを実行し、WALアーカイブから自動的に取り出すこともできます。 pg_rewindの利用は、フェイルオーバーに留まりません。 たとえば、スタンバイサーバが昇格してから書き込みトランザクションを実行し、再びスタンバイになるために巻き戻すこともできます。

After running <application>pg_rewind</application>, WAL replay needs to complete for the data directory to be in a consistent state. When the target server is started again it will enter archive recovery and replay all WAL generated in the source server from the last checkpoint before the point of divergence. If some of the WAL was no longer available in the source server when <application>pg_rewind</application> was run, and therefore could not be copied by the <application>pg_rewind</application> session, it must be made available when the target server is started. This can be done by creating a <filename>recovery.signal</filename> file in the target data directory and by configuring a suitable <xref linkend="guc-restore-command"/> in <filename>postgresql.conf</filename>. pg_rewindを実行した後、データディレクトリが整合のとれた状態になるためにはWALリプレイの完了が必要です。 ターゲットサーバは再起動すると、アーカイブリカバリに入り、分岐点の前の最後のチェックポイント以降にソースサーバで生成されたWALをすべてリプレイします。 pg_rewindが実行された時にWALの中にソースサーバにないものがあり、pg_rewindのセッションではコピーできなかった場合は、ターゲットサーバが起動した時にWALを読む込めるようになっていなければなりません。 recovery.signalファイルをターゲットデータディレクトリに置き、postgresql.confに適切なrestore_commandを設定することで、これを達成できます。

<application>pg_rewind</application> requires that the target server either has the <xref linkend="guc-wal-log-hints"/> option enabled in <filename>postgresql.conf</filename> or data checksums enabled when the cluster was initialized with <application>initdb</application>. Neither of these are currently on by default. <xref linkend="guc-full-page-writes"/> must also be set to <literal>on</literal>, but is enabled by default. pg_rewindを使用するには、ターゲットサーバ上でpostgresql.confwal_log_hintsオプションが有効になっているか、initdbでクラスタを初期化した時にデータチェックサムが有効になっていなければなりません。 どちらもデフォルトではonではありません(無効です)。 full_page_writeson(有効)でなければなりませんが、これはデフォルトで有効です。

警告: 巻き戻し中の失敗

<title>Warning: Failures While Rewinding</title>

If <application>pg_rewind</application> fails while processing, then the data folder of the target is likely not in a state that can be recovered. In such a case, taking a new fresh backup is recommended. 処理中にpg_rewindが失敗した場合、ターゲットのデータフォルダーはリカバリ可能な状態でない可能性があります。 このような場合は、最新のバックアップを取ることをお勧めします。

As <application>pg_rewind</application> copies configuration files entirely from the source, it may be required to correct the configuration used for recovery before restarting the target server, especially if the target is reintroduced as a standby of the source. If you restart the server after the rewind operation has finished but without configuring recovery, the target may again diverge from the primary. pg_rewindはソースから設定ファイルをそのままコピーするので、特にターゲットをソースのスタンバイとして再導入する場合には、ターゲットサーバを再起動する前にリカバリのために使われる設定を修正することが必要かもしれません。 巻き戻し操作は終わったもののリカバリの設定をせずにサーバを再起動すると、ターゲットは再びプライマリから分岐するでしょう。

<application>pg_rewind</application> will fail immediately if it finds files it cannot write directly to. This can happen for example when the source and the target server use the same file mapping for read-only SSL keys and certificates. If such files are present on the target server it is recommended to remove them before running <application>pg_rewind</application>. After doing the rewind, some of those files may have been copied from the source, in which case it may be necessary to remove the data copied and restore back the set of links used before the rewind. pg_rewindは直接書き込めないファイルが見つかるとすぐに失敗します。 たとえば、ソースサーバとターゲットサーバが読み取り専用のSSLキーと証明書に同じファイルマッピングを使用する場合に発生します。 そのようなファイルがターゲットサーバ上に存在する場合、それらを削除してからpg_rewindを実行することをお勧めします。 巻き戻しを行った後、それらのファイルの一部がソースからコピーされている可能性があります。 その場合は、コピーされたデータを削除して、巻き戻し前に使用していたリンクのセットを元に戻す必要があります。

オプション

<title>Options</title>

<application>pg_rewind</application> accepts the following command-line arguments: pg_rewindは以下のコマンドラインオプションを受け付けます。

-D directory
--target-pgdata=directory

This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running <application>pg_rewind</application> このオプションは、同期するターゲットデータディレクトリを指定します。 ターゲットサーバは、pg_rewindを実行する前に、正常にシャットダウンされていなければなりません。

--source-pgdata=directory

Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down. 同期するソースサーバのデータディレクトリへのファイルシステム上のパスを指定します。 このオプションを使用する場合は、ソースサーバは正常にシャットダウンされていなければなりません。

--source-server=connstr

Specifies a libpq connection string to connect to the source <productname>PostgreSQL</productname> server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used by <application>pg_rewind</application> on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections. ターゲットサーバに同期するソースPostgreSQLサーバに接続するlibpq接続文字列を指定します。 接続は、ソースサーバ(詳しくは注釈を参照)でpg_rewindで使われる関数を実行する権限を持つロールまたはスーパーユーザロールでの通常の(レプリケーションでない)接続でなければなりません。 このオプションはソースサーバが実行中であることと接続を受け付けることを必要とします。

-R
--write-recovery-conf

Create <filename>standby.signal</filename> and append connection settings to <filename>postgresql.auto.conf</filename> in the output directory. <literal>&#45;-source-server</literal> is mandatory with this option. 出力ディレクトリでstandby.signalを作成し、postgresql.auto.confに接続設定を追加します。 このオプションでは--source-serverは必須です。

-n
--dry-run

Do everything except actually modifying the target directory. ターゲットディレクトリを実際に更新する以外はすべてのことを行います。

-N
--no-sync

By default, <command>pg_rewind</command> will wait for all files to be written safely to disk. This option causes <command>pg_rewind</command> to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation. デフォルトでは、pg_rewindはファイルがすべて安全にディスクに書き込まれるのを待ちます。 このオプションにより、pg_rewindは待つことなく戻るようになります。これは速いのですが、直後にオペレーティングシステムがクラッシュした場合、データディレクトリの破損が残るかもしれません。 一般に、このオプションはテストするためには有用ですが、稼働用のインストレーションでは使うべきではありません。

-P
--progress

Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster. 進行状況のレポートを有効にします。このオプションを有効にすると、データをソースクラスタからコピーする際のおおよその進行状況をレポートします。

-c
--restore-target-wal

Use <varname>restore_command</varname> defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the <filename>pg_wal</filename> directory. WALファイルがpg_walディレクトリにもはや存在しない場合、ターゲットクラスタ設定内で定義されているrestore_commandを使ってWALファイルをWALアーカイブから取り出します。

--config-file=filename

Use the specified main server configuration file for the target cluster. This affects <application>pg_rewind</application> when it uses internally the <application>postgres</application> command for the rewind operation on this cluster (when retrieving <varname>restore_command</varname> with the option <option>-c/&#45;-restore-target-wal</option> and when forcing a completion of crash recovery). ターゲットクラスタ用に指定されたメインサーバ設定ファイルを使用します。 これは、pg_rewindがこのクラスタ上での巻き戻し操作のためにpostgresコマンドを内部的に使用している場合(-c/--restore-target-walオプションを指定してrestore_commandを取得している場合や、クラッシュリカバリの完了を強制する場合)に影響します。

--debug

Print verbose debugging output that is mostly useful for developers debugging <application>pg_rewind</application>. 主に開発者がpg_rewindをデバッグするのに役立つ冗長なデバッグ出力を印字します。

--no-ensure-shutdown

<application>pg_rewind</application> requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, <application>pg_rewind</application> starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, <application>pg_rewind</application> skips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case. pg_rewindは、巻き戻す前にターゲットサーバが正常にシャットダウンされていることを要求します。 デフォルトでは、ターゲットサーバが正常にシャットダウンされていなければ、pg_rewindはターゲットサーバをシングルユーザモードで起動し、まずクラッシュリカバリを完了して停止します。 このオプションを渡すことで、pg_rewindはこれを飛ばして、サーバが正常にシャットダウンされていない場合にはすぐにエラーを発生します。 その場合、ユーザが自身で状況を扱うことが期待されます。

-V
--version

バージョン情報を表示して終了します。

-?
--help

ヘルプを表示して終了します。

環境

<title>Environment</title>

When <option>&#45;-source-server</option> option is used, <application>pg_rewind</application> also uses the environment variables supported by <application>libpq</application> (see <xref linkend="libpq-envars"/>). --source-serverオプションを使用する場合、pg_rewindlibpqで利用できる環境変数を使用します(34.15を参照)。

The environment variable <envar>PG_COLOR</envar> specifies whether to use color in diagnostic messages. Possible values are <literal>always</literal>, <literal>auto</literal> and <literal>never</literal>. 環境変数PG_COLORは診断メッセージで色を使うかどうかを指定します。 可能な値はalwaysautoneverです。

注釈

<title>Notes</title>

When executing <application>pg_rewind</application> using an online cluster as source, a role having sufficient permissions to execute the functions used by <application>pg_rewind</application> on the source cluster can be used instead of a superuser. Here is how to create such a role, named <literal>rewind_user</literal> here: オンラインのクラスタをソースとして使用してpg_rewindを実行するとき、スーパーユーザの代わりにソースのクラスタ上でpg_rewindで使われる関数を実行できる権限を持ったロールを使うことができます。 rewind_userという名前のこのようなロールの作り方を以下に示します。

CREATE USER rewind_user LOGIN;
GRANT EXECUTE ON function pg_catalog.pg_ls_dir(text, boolean, boolean) TO rewind_user;
GRANT EXECUTE ON function pg_catalog.pg_stat_file(text, boolean) TO rewind_user;
GRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text) TO rewind_user;
GRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text, bigint, bigint, boolean) TO rewind_user;

どうやって動くのか

<title>How It Works</title>

The basic idea is to copy all file system-level changes from the source cluster to the target cluster: 基本的なアイデアは、ファイルシステムレベルの変更を、すべてをソースクラスタからターゲットクラスタにコピーする、というものです。

  1. Scan the WAL log of the target cluster, starting from the last checkpoint before the point where the source cluster's timeline history forked off from the target cluster. For each WAL record, record each data block that was touched. This yields a list of all the data blocks that were changed in the target cluster, after the source cluster forked off. If some of the WAL files are no longer available, try re-running <application>pg_rewind</application> with the <option>-c</option> option to search for the missing files in the WAL archive. ソースクラスタのタイムライン履歴がターゲットクラスタから分岐した時点より前の最後のチェックポイントから始めて、ターゲットクラスタのWALログをスキャンします。 各々のWALレコードについて、変更されたデータブロックを記録します。 これにより、ソースクラスタが分岐した以降に、ターゲットクラスタで変更されたすべてのデータブロックのリストが作成されます。 WALファイルの中にもう存在しないものがある場合は、失われたファイルをWALアーカイブで探すよう-cオプションを付けてpg_rewindを再実行してみます。

  2. Copy all those changed blocks from the source cluster to the target cluster, either using direct file system access (<option>&#45;-source-pgdata</option>) or SQL (<option>&#45;-source-server</option>). Relation files are now in a state equivalent to the moment of the last completed checkpoint prior to the point at which the WAL timelines of the source and target diverged plus the current state on the source of any blocks changed on the target after that divergence. ファイルシステムへの直接アクセス(--source-pgdata)かSQL (--source-server)を使って、変更のあったすべてのブロックを、ソースクラスタからターゲットクラスタにコピーします。 これでリレーションファイルは、ソースとターゲットのWALタイムラインが分岐した時点より前で最後に完了したチェックポイントの時点に加えて、分岐後にターゲットで変更されたブロックを含んだソースでの現在の状態に相当する状態になります。

  3. Copy all other files, including new relation files, WAL segments, <filename>pg_xact</filename>, and configuration files from the source cluster to the target cluster. Similarly to base backups, the contents of the directories <filename>pg_dynshmem/</filename>, <filename>pg_notify/</filename>, <filename>pg_replslot/</filename>, <filename>pg_serial/</filename>, <filename>pg_snapshots/</filename>, <filename>pg_stat_tmp/</filename>, and <filename>pg_subtrans/</filename> are omitted from the data copied from the source cluster. The files <filename>backup_label</filename>, <filename>tablespace_map</filename>, <filename>pg_internal.init</filename>, <filename>postmaster.opts</filename>, and <filename>postmaster.pid</filename>, as well as any file or directory beginning with <filename>pgsql_tmp</filename>, are omitted. 新しいリレーションファイルやWALセグメント、pg_xactや設定ファイルなどを含めて、それ以外のファイルをすべてソースクラスタからターゲットクラスタにコピーします。 ベースバックアップと同様に、ディレクトリpg_dynshmem/pg_notify/pg_replslot/pg_serial/pg_snapshots/pg_stat_tmp/、および、pg_subtrans/の内容は、ソースクラスタからコピーされるデータから省かれます。 pgsql_tmpで始まるすべてのファイルやディレクトリは省かれます。 ファイルbackup_labeltablespace_mappg_internal.initpostmaster.opts、および、postmaster.pidも同様です。

  4. Create a <filename>backup_label</filename> file to begin WAL replay at the checkpoint created at failover and configure the <filename>pg_control</filename> file with a minimum consistency LSN defined as the result of <literal>pg_current_wal_insert_lsn()</literal> when rewinding from a live source or the last checkpoint LSN when rewinding from a stopped source. フェイルオーバーで作成されたチェックポイントでWALリプレイを開始するためにbackup_labelファイルを作成し、動作中のソースから巻き戻す場合にはpg_current_wal_insert_lsn()の結果として定義される最小の整合のとれたLSNを、停止したソースから巻き戻す場合には最後のチェックポイントLSNをpg_controlファイルに設定します。

  5. When starting the target, <productname>PostgreSQL</productname> replays all the required WAL, resulting in a data directory in a consistent state. ターゲットが起動すると、PostgreSQLは必要なWALをすべてリプレイします。それにより、データディレクトリが整合のとれた状態になります。