フェイルオーバー後にpg_rewindで旧プライマリをレプリカとして再参加させる
PostgreSQLレプリケーションコマンド
# Prerequisite (must be set BEFORE the incident): wal_log_hints = on OR data checksums; full_page_writes = on
pg_controldata $PGDATA | grep -E 'wal_log_hints|checksum'
# 1) The old primary must be stopped:
pg_ctl -D $PGDATA stop -m fast
# 2) Dry run first:
pg_rewind --target-pgdata=$PGDATA --source-server='host=<new_primary_host> port=5432 user=postgres dbname=postgres' --progress --dry-run
# 3) Actual rewind + standby configuration (standby.signal + primary_conninfo):
pg_rewind --target-pgdata=$PGDATA --source-server='host=<new_primary_host> port=5432 user=postgres dbname=postgres' --progress --write-recovery-conf
# 4) Review postgresql.auto.conf copied from the source (port, primary_slot_name), then start:
pg_ctl -D $PGDATA start概要
タイムラインの分岐点以降に変更されたブロックを新プライマリからコピーし、旧プライマリのデータディレクトリを新プライマリと一貫させる。フルのbasebackupなしで数分でレプリカにする。
使用する場面
フェイルオーバー/昇格の後に旧プライマリが復帰し、分岐した変更を持ったまま新しいタイムラインから遅れているときに。
注意
フェイルオーバー後に旧プライマリに残り、新プライマリに届かなかったトランザクションは完全に削除される。pg_rewindは構成ファイル(postgresql.conf、postgresql.auto.conf、pg_hba.conf)もソースからコピーするため、起動前にポートとスロットの設定を修正する。分岐点以降に必要なWALがソースにもう存在しない場合は、-c(--restore-target-wal)でアーカイブから取得する。ソース接続にはスーパーユーザー、またはドキュメントに記載された関数権限を持つロールが必要。Patroniではuse_pg_rewind: trueで自動的に行われる。