본문 바로가기
PostgreSQL

PostgreSQL 14.2 File-based Log Shipping Replication - Failover

by 울라불라오미짱 2023. 6. 2.
반응형

Failover란?

primary 서버에 장애가 생겼을 때, 예비 서버(예: standby 서버) 로 대체하여 운영하는 것을 Failover(장애 극복)라고 합니다.

 

File-based Log Shipping Replication을 구성하고 primary 서버에 문제가 생겼을 때, 어떻게 failover를 해야 하는지 알아봅시다!

 

File-based Log Shipping Replication 글을 안읽고 오셨다면! 먼저 가서 읽고 오시는걸 추천드립니다 ㅎㅎ

2023.06.02 - [PostgreSQL] - PostgreSQL 14.2 File-based Log Shipping Replication 구축하기

 

PostgreSQL 14.2 File-based Log Shipping Replication 구축하기

PostgreSQL의 고가용성을 높이기 위한 방법 중, 가장 기본적인 방법인 File-based Log Shipping Replication에 대해 알아보자! File-based Log Shipping Replication 이란? primary 서버에서 만드는 WAL 파일을 정기적으로 st

omi-1101.tistory.com

File-based Log Shipping Replication Failover

1. primary 서버 장애 발생

primary 서버에 장애 발생을 가정하여 PostgreSQL 서버를 내립니다.

pg_ctl stop

2. standby 서버 promote

그 후, standby 서버에서 pg_ctl promote 명령어로 standby 모드를 종료합니다.

pg_ctl promote
waiting for server to promote.... done
server promoted

pg_ctl promote 명령을 받아 archive recovery가 종료하였고, 접속 및 쿼리 수행이 가능한 서버(즉, primary)가 되었습니다. 아래 로그를 봅시다!

2023-06-02 16:59:48.778 KST [9529] LOG:  received promote request
2023-06-02 16:59:48.778 KST [9529] LOG:  redo done at 0/F000110 system usage: CPU: user: 0.06 s, system: 2.60 s, elapsed: 9525.00 s
2023-06-02 16:59:48.778 KST [9529] LOG:  last completed transaction was at log time 2023-06-02 15:02:18.781894+09
2023-06-02 16:59:48.791 KST [9529] LOG:  restored log file "00000001000000000000000F" from archive
cp: cannot stat ‘/test/pg/14/archive/00000002.history’: No such file or directory
2023-06-02 16:59:48.818 KST [9529] LOG:  selected new timeline ID: 2
2023-06-02 16:59:48.842 KST [9529] LOG:  archive recovery complete
cp: cannot stat ‘/test/pg/14/archive/00000001.history’: No such file or directory
2023-06-02 16:59:48.850 KST [9527] LOG:  database system is ready to accept connections

프로세스 확인

ps -ef | grep postgres
test       9527      1  0 14:21 ?        00:00:00 /usr/pgsql-14/bin/postgres
test       9528   9527  0 14:21 ?        00:00:00 postgres: logger
test       9533   9527  0 14:21 ?        00:00:00 postgres: checkpointer
test       9534   9527  0 14:21 ?        00:00:00 postgres: background writer
test       9535   9527  0 14:21 ?        00:00:00 postgres: stats collector
test      67922   9527  0 16:59 ?        00:00:00 postgres: walwriter
test      67923   9527  0 16:59 ?        00:00:00 postgres: autovacuum launcher
test      67924   9527  0 16:59 ?        00:00:00 postgres: archiver failed on 00000002.history
test      67925   9527  0 16:59 ?        00:00:00 postgres: logical replication launcher
  • postgresql.conf에 설정되어 있는 archive_command 값에 의해 오류가 발생할 수 있습니다.
  • archive_mode = off 또는 archive_command 경로를 적절히 수정하면 정상적으로 작동합니다.

저는 아카이브 모드를 off 해주고 pg_ctl restart를 통해 PostgreSQL 서버를 재시작 해주겠습니다!

만약 커맨드만 수정할 경우엔 restart 해줄 필요 없고 reload만 해주면 정상 작동 합니다.

ps -ef | grep postgres
test      74611      1  0 17:17 ?        00:00:00 /usr/pgsql-14/bin/postgres
test      74612  74611  0 17:17 ?        00:00:00 postgres: logger
test      74614  74611  0 17:17 ?        00:00:00 postgres: checkpointer
test      74615  74611  0 17:17 ?        00:00:00 postgres: background writer
test      74616  74611  0 17:17 ?        00:00:00 postgres: walwriter
test      74617  74611  0 17:17 ?        00:00:00 postgres: autovacuum launcher
test      74618  74611  0 17:17 ?        00:00:00 postgres: stats collector
test      74619  74611  0 17:17 ?        00:00:00 postgres: logical replication launcher

이렇게 하면 failover가 완료 됐고 정상적으로 서버 운영이 가능해졌습니다! recovery 모드도 아닌 것도 다음과 같이 확인할 수 있습니다!

postgres=# select pg_is_in_recovery();
 pg_is_in_recovery
-------------------
 f
(1 row)
반응형