pg_rewind

pg_rewind — PostgreSQL 데이터 디렉터리를 자신으로부터 파생된 또 다른 데이터 디렉터리에게 맞춰 동기화

요약

pg_rewind [옵션...] { -D | --target-pgdata } 디렉터리 위치 { --source-pgdata=디렉터리 위치 | --source-server=연결정보 }

설명

pg_rewind는 클러스터의 또 다른 복사본에 맞춰 PostgreSQL 클러스터를 동기화 해주는 도구이다, 클러스터의 타임라인이 분기된 후라 할지라도. 이를 사용하는 일반적인 시나리오는 장애복구 후에 '이전의 master 서버'를 '새로운 master 서버'의 standby 로 되돌려준다.

되감기 작업이 끝난 대상 데이터 디렉터리는 원본 데이터 디렉터리의 베이스 백업본과 비슷하다. 새로운 베이스 백업을 하거나, rsync 같은 도구를 사용할 때와 다른 점은, pg_rewind 명령을 사용하면 똑 같은 파일은 복사하지 않는다는 점이다. 오직 해당 릴레이션의 변경된 블록만 복사된다. 또한, 대상 데이터 디렉터리에는 없는 릴레이션 파일과, 환경 설정 파일, WAL 파일은 그대로 복사 된다. 데이터베이스가 크고, 자료 변경분이 그리 크지 않은 경우, 이 되감기 작업은 다른 구축 방법보다 빠르다.

pg_rewind는 source/target 클러스간에 타임라인 이력을 조사하여 분기지점을 알아낸다. 그리고 target 클러스터의 pg_wal 디렉터리내에서 분기되었던 시점으로 되돌릴 전체 WAL 찾아내려 한다. 분기지점은 target, source 또는 그 이전의 공통된 클러스터 타임라인들 중에서 찾을 수 있다. 전형적인 장애복구 시나리오에서 target 클러스터는 분기된지 그리 오래 시간이 흐르지 않는다. 이러면 문제가 없지만, 만약에 target 클러스터가 분기된 이후에도 오래도록 운영되었다면 source 클러스터쪽에서 오래된 WAL파일들이 없을 수 있다. 이 경우, 직접 그 대상 WAL 아카이브 파일을 pg_wal 디렉터리로 복사할 수도 있다, 또는 pg_rewind -c 옵션을 사용해서 복사할 수도 있다. pg_rewind 명령은 페일오버 용도로만 사용하는 것이 아니라, 대기 서버가 운영 서버로 전환되었다가 다시 대기 서버로 바꾸려고 할 때도 사용될 수 있다.

pg_rewind 수행 후, target 서버는 자료 정합성을 맞추기 위해 WAL 재반영 작업이 필요하다. 서버가 시작되면, 아카이브 복구 모드로 진입하고, 원본 데이터베이스 서버의 마지막 체크 포인트 이후의 source 서버에서 만들어진 모든 WAL을 재생한다. 이때, 필요한 WAL 파일 없다면, pg_rewind 명령으로는 이 파일들을 복사할 수 없다. 이런 상황에서는 target 서버가 실행 될때 target 데이터 디렉터리 안에 recovery.signal 파일 만들고, postgresql.conf 파일에서 복구에 필요한 적당한 restore_command 설정을 할 수도 있다.

pg_rewind를 수행하기 위한 요구사항은 target 서버의 postgresql.conf 에서 wal_log_hints 옵션이 활성화되어 있거나, initdb 로 클러스터를 초기화할때 체크섬이 활성화 되어 있어야 한다. 이것들의 활성화값은 모두 기본값은 아니다. full_page_writes 값은 on 되어 있어야 한다. 이건 기본값으로 활성화되어 있다.

주의

If pg_rewind 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.

As pg_rewind 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 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 pg_rewind. 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는 다음 명령어 줄 옵션을 사용한다.

-D directory
--target-pgdata=directory

이 옵션은 source 서버로 부터 동기화하는 target 데이터 디렉터리를 가리킨다. target 서버는 pg_rewind 수행전에 완전히 내려가 있어야 한다.

--source-pgdata=directory

target 서버를 동기화시킬 source 서버의 데이터 디렉터리를 가리킨다. 이 옵션을 사용하려면 source 서버가 완전히 내려가 있어야 한다.

--source-server=connstr

Specifies a libpq connection string to connect to the source PostgreSQL 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 pg_rewind on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and not in recovery mode.

-R
--write-recovery-conf

Create standby.signal and append connection settings to postgresql.auto.conf in the output directory. --source-server is mandatory with this option.

-n
--dry-run

target 디렉터리에 실제 수정없이 모든 것을 수행해 본다.

-N
--no-sync

By default, pg_rewind will wait for all files to be written safely to disk. This option causes pg_rewind to return without waiting, which is faster, but means that a subsequent operating system crash can leave the synchronized data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation.

-P
--progress

진행상태를 표시한다. 이 옵션을 활성화 시키면 정확하진 않지만 source 클러스터로부터 데이터 복제하는 진행상태를 볼 수 있다.

-c
--restore-target-wal

Use restore_command defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the pg_wal directory.

--debug

pg_rewind를 디버깅하는 개발자들에게 유용한 디버깅 메시지를 출력한다.

--no-ensure-shutdown

pg_rewind requires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly, pg_rewind starts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option, pg_rewind 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.

-V
--version

버전 정보를 표시하고 종료한다.

-?
--help

도움말을 표시하고 종료한다.

환경 변수

--source-server 옵션이 사용되면, pg_rewindlibpq에서 지원되는 환경 변수도 사용할 수 있다.(33.14절 참조).

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

주석

When executing pg_rewind using an online cluster as source, a role having sufficient permissions to execute the functions used by pg_rewind on the source cluster can be used instead of a superuser. Here is how to create such a role, named rewind_user here:

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;

When executing pg_rewind using an online cluster as source which has been recently promoted, it is necessary to execute a CHECKPOINT after promotion such that its control file reflects up-to-date timeline information, which is used by pg_rewind to check if the target cluster can be rewound using the designated source cluster.

어떻게 동작되는가?

이 생각은 기본적으로은 시스템 수준에서 모든 변경 사항을 source 클러스터에서 target 클러스터로 복사한다는데 있다.

  1. target 클러스터의 WAL을 검사한다. 범위는, target 클러스터로부터 source 클러스터의 타임라인이 나뉘어진 지점 바로 앞의 마지막 체크포인트된 지점에서 시작한다. 각 WAL 레코드들을 통해서 변경된 데이터 블록들을 기록한다. 그러면 source 클러스터를 분기시킨 후에 target 클러스터에서 변경된 데이터 블록의 전체 목록을 만들 수 있다. If some of the WAL files are no longer available, try re-running pg_rewind with the -c option to search for the missing files in the WAL archive.

  2. 1번에서 얻은 목록을 사용해서 source 클러스터에서 target 클러스터로 변경된 블록 전체를 복사한다. 이때 직접 시스템 파일에 접근하는 (--source-pgdata)이나 SQL (--source-server) 옵션 중 하나를 사용한다. 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.

  3. pg_xact이나 설정파일들, WAL 조각 파일들 모두 source 클러스터에서 target 클러스터로 복사한다. (변경되지 않는 릴레이션 파일을 제외하곤 모두) 베이스 백업과 비슷하게, pg_dynshmem/, pg_notify/, pg_replslot/, pg_serial/, pg_snapshots/, pg_stat_tmp/, pg_subtrans/ 디렉터리의 내용은 빠진다. 또한 backup_label, tablespace_map, pg_internal.init, postmaster.opts, postmaster.pid 파일과, pgsql_tmp로 시작하는 모든 디렉터리와 파일들도 생략한다.

  4. Create a backup_label file to begin WAL replay at the checkpoint created at failover and configure the pg_control file with a minimum consistency LSN defined as the result of pg_current_wal_insert_lsn() when rewinding from a live source or the last checkpoint LSN when rewinding from a stopped source.

  5. When starting the target, PostgreSQL replays all the required WAL, resulting in a data directory in a consistent state.