🔍 문제 상황
PostgreSQL streaming replication 환경에서 failback(다시 primary 전환) 을 시도하던 중, 서버 기동이 실패하면서 아래와 같은 에러 로그가 발생했습니다.
- 이중화 확인
-- 이중화 확인
postgres=# select pg_is_in_recovery();
pg_is_in_recovery
-------------------
f
(1 row)
-- 이중화 확인
postgres=# SELECT * FROM pg_stat_replication;
pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | backend_xmin | state | sent_lsn | write_lsn | flush_lsn | replay_lsn | write_lag | flush_lag | replay
_lag | sync_priority | sync_state | reply_time
-----+----------+---------+------------------+-------------+-----------------+-------------+---------------+--------------+-------+----------+-----------+-----------+------------+-----------+-----------+-------
-----+---------------+------------+------------
(0 rows)
-- 이중화 확인
postgres=# SELECT pg_last_wal_receive_lsn(), pg_last_wal_replay_lsn();
pg_last_wal_receive_lsn | pg_last_wal_replay_lsn
-------------------------+------------------------
|
(1 row)
-- 이중화 확인
postgres=# SELECT pg_current_wal_lsn();
pg_current_wal_lsn
--------------------
0/1A0000D8
(1 row)
- DB 재기동
[postgres@gwon-sub pgsql17]$ $PG_HOME/bin/pg_ctl -D $PGDATA start
waiting for server to start....2025-07-24 06:17:08.118 GMT [168431] LOG: redirecting log output to logging collector process
2025-07-24 06:17:08.118 GMT [168431] HINT: Future log output will appear in directory "/postgres/app/postgres/pgsql17/pg_log".
stopped waiting
pg_ctl: could not start server
Examine the log output.
- 해당 로그 확인
-- 로그 확인
2025-07-24 05:59:42.042 GMT [168148] LOG: starting PostgreSQL 17.5 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 11.5.0 20240719 (Red Hat 11.5.0-5.0.1), 64-bit
2025-07-24 05:59:42.042 GMT [168148] LOG: listening on IPv4 address "10.0.0.117", port 5432
2025-07-24 05:59:42.111 GMT [168148] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2025-07-24 05:59:42.120 GMT [168152] LOG: database system was shut down in recovery at 2025-07-24 05:58:29 GMT
2025-07-24 05:59:42.123 GMT [168152] LOG: entering standby mode
2025-07-24 05:59:42.123 GMT [168152] FATAL: requested timeline 3 is not a child of this server's history
2025-07-24 05:59:42.123 GMT [168152] DETAIL: Latest checkpoint is at 0/1A0C5A38 on timeline 2, but in the history of the requested timeline, the server forked off from that timeline at 0/19000000.
2025-07-24 05:59:42.131 GMT [168148] LOG: startup process (PID 168152) exited with exit code 1
2025-07-24 05:59:42.131 GMT [168148] LOG: aborting startup due to startup process failure
2025-07-24 05:59:42.216 GMT [168148] LOG: database system is shut down
👉 이 로그는 서버2(standby) 에서 PostgreSQL이 시작은 되었지만 실패
✅ 즉, 복제 서버(standby) 가 요청하는 타임라인이, 현재 서버의 WAL 히스토리와 맞지 않기 때문에 발생
📛 Standby는 timeline 2의 백업본을 기준으로 시작하려 해서 이력을 잇지 못하고 실패한 것
🧠 원인 요약
- 페일백 시에 Primary 서버가 새로운 timeline (예: timeline 3) 으로 전환됨.
- 그러나 Standby는 이전 timeline (예: timeline 2) 의 데이터를 갖고 있음.
- 이 경우 WAL 동기화가 불가능하므로, standby로 복제 연결 실패.
✅ 해결 방법
🔁 정상적인 basebackup으로 서버2 초기화 필요
- 서버1(현재 primary)에서 서버2로 pg_basebackup을 다시 수행해야 합니다.
1. Standby 서버 PostgreSQL 정지(Standby)
[postgres@gwon-sub data]$ $PG_HOME/bin/pg_ctl -D $PGDATA stop
2. Standby 서버 기존 데이터 디렉토리 제거 또는 백업(Standby)
[postgres@gwon-sub pgsql17]$ mv data data_old
[postgres@gwon-sub pgsql17]$ mkdir data
[postgres@gwon-sub pgsql17]$ chown postgres:dba data
[postgres@gwon-sub pgsql17]$ chown 700 data
3. Primary 서버에서 Standby 서버로 basebackup 수행(Standby)
[postgres@gwon-sub pgsql17]$ pg_basebackup -h 10.0.0.61 -U repl_user -D $PGDATA -Fp -Xs -P -R
4. Standby 서버 PostgreSQL 재기동(Standby)
[postgres@gwon-sub pgsql17]$ $PG_HOME/bin/pg_ctl -D $PGDATA start
💡마무리
이 문제는 PostgreSQL 스트리밍 복제에서 자주 마주치는 이슈 중 하나라고 합니다.
타임라인 불일치는 단순 재기동으로는 해결되지 않기 때문에 복제 구조를 이해하고 적절한 조치가 필요할 것 같습니다.
'🗄️ DB_이야기 > # 🐘 PostgreSQL' 카테고리의 다른 글
| 🐘[PostgreSQL] 환경변수 및 DB 기동/정지 정리 (2) | 2025.07.28 |
|---|---|
| 🐘 [PostgreSQL] 기동 실패 에러: "could not bind IPv4 address" (3) | 2025.07.24 |
| 🐘[PostgreSQL] 수동 장애 전환(Fail-Over)과 복구(Fail-Back) (2) | 2025.07.24 |
| 🐘 [PostgreSQL] PostgreSQL 17 고가용성(HA) 이중화 설치 가이드(2) (4) | 2025.07.17 |
| 🐘 [PostgreSQL] PostgreSQL 17 고가용성(HA) 이중화 설치 가이드(1) (0) | 2025.07.16 |