페일오버 후 pg_rewind로 이전 primary를 replica로 재합류
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하는 일
timeline 분기 시점 이후 변경된 블록을 새 primary에서 복사하여 이전 primary의 데이터 디렉터리를 새 primary와 일치시킨다. 전체 basebackup 없이 몇 분 안에 replica로 만든다.
사용 시점
페일오버/promote 후 이전 primary가 복귀했지만 분기된 변경으로 새 timeline보다 뒤처져 있을 때.
주의
페일오버 후 이전 primary에 남아 새 primary에 도달하지 못한 트랜잭션은 영구적으로 제거된다. pg_rewind는 원본의 설정 파일(postgresql.conf, postgresql.auto.conf, pg_hba.conf)도 복사하므로 시작 전에 포트와 slot 설정을 수정한다. 분기 시점 이후 필요한 WAL이 원본에 더 이상 없으면 -c(--restore-target-wal)로 아카이브에서 가져온다. 원본 연결에는 superuser 또는 문서에 나열된 함수 권한을 가진 롤이 필요하다. Patroni에서는 use_pg_rewind: true로 자동 수행된다.