Postgresql, EPAS/Replication

Streaming Replication

havy 2025. 6. 18. 16:14

Streaming Replication은 WAL 파일이 완성될 때까지 기다리지 않고 WAL 레코드를 실시간으로 복제하는 방식이다.

트랜잭션이 Commit 된 후 Standby 노드에 적용되기까지 약간의 지연이 발생할 수 있지만 File-based Log Shipping 방식에 비해 현저히 적으며, DDL,DML 전부 복제 가능하다.

아직 동기화되지 않은 WAL 레코드가 포함된 WAL 파일이 삭제될 경우에는 더 이상 복제를 진행할 수 없기 때문에 Replication을 재구성해야 한다.

 

Master 노드의 WAL 레코드를 Standby 노드로 전달하기 위해 각 노드에는 아래와 같은 프로세스가 존재한다.

  • Master 노드: WAL Sender
  • Standby 노드: WAL Receiver

 

[Master]

1) Replication 계정 생성

 

edb=# create user repl with replication password 'repl';
CREATE ROLE


edb=# \du
                                   List of roles
       Role name       |                         Attributes
-----------------------+------------------------------------------------------------
 aq_administrator_role | No inheritance, Cannot login                              +
                       | Profile default
 capture_admin         | No inheritance, Cannot login                              +
                       | Profile default
 enterprisedb          | Superuser, Create role, Create DB, Replication, Bypass RLS+
                       | Profile default
 repl                  | Replication                                               +
                       | Profile default

2) Replication Slot 생성

edb=# select * from pg_create_physical_replication_slot('repl_slot_01');
  slot_name   | lsn
--------------+-----
 repl_slot_01 |
(1 row)

edb=# select slot_name, slot_type, active from pg_replication_slots;
  slot_name   | slot_type | active
--------------+-----------+--------
 repl_slot_01 | physical  | f
(1 row)

 

3) Config 파일 설정

 

vi postgresql.conf
listen_addresses = '*'      
port = 5444  
wal_level = replica     minimal: Crash나 immediate shutdown으로 인해 복구에 필요한 정보만 기록

replica: Postgresql 9.6버전부터 archive와 hot_standby 내용 통합
    - archive: minimal+WAL 보관에 필요한 정보를 추가
    - hot_standby: archive+standby 노드에서 읽기 전용 쿼리를 실행하는 데 필요한 정보


logical: replica+Logical decoding을 지원하는 데 필요한 정보를 추가. 저장되는 데이터 양이 많아져 WAL 파일의 크기가 증가함.
재기동 필요
wal_keep_size = MB     Standby에서 설정
WAL 파일이 보관되는 pg_wal 디렉터리에 저장 가능한 WAL 파일들의 총 크기를 지정 (Default 16MB)
max_wal_sender = WAL 파일을 전송할 수 있는 최대 개수를 설정
max_replication_slots =     replication slots의 최대 개수를 설정
데이터베이스의 복제를 관리하고, 복제 클라이언트가 뒤쳐지는 경우에도 필요한 WAL 데이터를 유지하는 데 사용됨
hot_standby = on     Standby에서 설정
데이터베이스가 Standby 상태로 Replication이 이루어지는 Standby 노드에서는 반드시 ON으로 설정해야 함

 

vi pg_hba.conf

Master 노드의 pg_hba.conf에서 Slave 노드에서 repl 유저가 로그인하고 WAL Record를 streaming 할 수 있도록 허용 (Slave IP를 적어주면 됨)

host    replication     repl            192.168.56.152/24       trust

 

4) EPAS 재기동

 

[Slave]

1) pg_basebackup 수행

$ pg_basebackup -h 192.168.56.151  -D /var/lib/edb/as16/data/ -U repl -P -v -X stream -S repl_slot_01 -R


pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 0/3000028 on timeline 1
pg_basebackup: starting background WAL receiver
57528/57528 kB (100%), 1/1 tablespace
pg_basebackup: write-ahead log end point: 0/3000138
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: syncing data to disk ...
pg_basebackup: renaming backup_manifest.tmp to backup_manifest
pg_basebackup: base backup completed

 

2) EPAS 기동

 

3)postgresql.auto.conf 확인
프라이머리 DB 정보가 있다.

primary_conninfo = 'user=repl passfile=''/var/lib/edb/.pgpass'' channel_binding=prefer host=192.168.56.151 port=5444 sslmode=prefer sslcompression=0 sslcertmode=allow sslsni=1 ssl_min_protocol_version=TLSv1.2 gssencmode=prefer krbsrvname=postgres gssdelegation=0 target_session_attrs=any load_balance_hosts=disable'
primary_slot_name = 'repl_slot_01'

 

 

 

[Replication 상태 확인]

마스터 노드에서
edb=# select * from pg_stat_activity where usename='repl';
-[ RECORD 1 ]----+-----------------------------------------------------------
datid            |
datname          |
pid              | 1893
leader_pid       |
usesysid         | 16384
usename          | repl
application_name | walreceiver
client_addr      | 192.168.56.152
client_hostname  |
client_port      | 15188
backend_start    | 07-MAY-25 16:32:48.610399 +09:00
xact_start       |
query_start      | 07-MAY-25 16:32:48.61433 +09:00
state_change     | 07-MAY-25 16:32:48.614359 +09:00
wait_event_type  | Activity
wait_event       | WalSenderMain
state            | active
backend_xid      |
backend_xmin     |
query_id         |
query            | START_REPLICATION SLOT "repl_slot_01" 0/4000000 TIMELINE 1
backend_type     | walsenderedb=# select * from pg_stat_activity;
.
.
.-[ RECORD 4 ]----+-----------------------------------------------------------
datid            |
datname          |
pid              | 1893
leader_pid       |
usesysid         | 16384
usename          | repl
application_name | walreceiver
client_addr      | 192.168.56.152
client_hostname  |
client_port      | 15188
backend_start    | 07-MAY-25 16:32:48.610399 +09:00
xact_start       |
query_start      | 07-MAY-25 16:32:48.61433 +09:00
state_change     | 07-MAY-25 16:32:48.614359 +09:00
wait_event_type  | Activity
wait_event       | WalSenderMain
state            | active
backend_xid      |
backend_xmin     |
query_id         |
query            | START_REPLICATION SLOT "repl_slot_01" 0/4000000 TIMELINE 1
backend_type     | walsender

 

슬레이브 노드에서
edb=# select * from pg_stat_wal_receiver;
-[ RECORD 1 ]---------+----------------------------
pid                   | 1724
status                | streaming
receive_start_lsn     | 0/4000000
receive_start_tli     | 1
written_lsn           | 0/4000060
flushed_lsn           | 0/4000060
received_tli          | 1
last_msg_send_time    | 07-MAY-25 16:35:48.677619 +09:00
last_msg_receipt_time | 07-MAY-25 16:35:48.678058 +09:00
latest_end_lsn        | 0/4000060
latest_end_time       | 07-MAY-25 16:32:48.614482 +09:00
slot_name             | repl_slot_01
sender_host           | 192.168.56.151
sender_port           | 5444
conninfo              | user=repl passfile=/var/lib/edb/.pgpass channel_binding=prefer dbname=replication host=192.168.56.151 port=5444 fallback_application_name=walreceiver sslmode=prefer sslcompression=0 sslcertmode=allow sslsni=1 ssl_min_protocol_version=TLSv1.2 gssencmode=prefer krbsrvname=postgres gssdelegation=0 target_session_attrs=any load_balance_hosts=disable
edb=# select * from pg_stat_activity;
.
.
.
-[ RECORD 5 ]----+---------------------------------
datid            |
datname          |
pid              | 1724
leader_pid       |
usesysid         |
usename          |
application_name |
client_addr      |
client_hostname  |
client_port      |
backend_start    | 07-MAY-25 16:32:48.607383 +09:00
xact_start       |
query_start      |
state_change     |
wait_event_type  | Activity
wait_event       | WalReceiverMain
state            |
backend_xid      |
backend_xmin     |
query_id         |
query            |
backend_type     | walreceiver[enterprisedb@vbox:/var/lib/edb/as16/data]$ps -ef |grep postgres
enterpr+    1719       1  0 16:32 ?        00:00:00 /usr/edb/as16/bin/edb-postgres -D /var/lib/edb/as16/data
enterpr+    1720    1719  0 16:32 ?        00:00:00 postgres: logger
enterpr+    1721    1719  0 16:32 ?        00:00:00 postgres: checkpointer
enterpr+    1722    1719  0 16:32 ?        00:00:00 postgres: background writer
enterpr+    1723    1719  0 16:32 ?        00:00:00 postgres: startup recovering 000000010000000000000004
enterpr+    1724    1719  0 16:32 ?        00:00:00 postgres: walreceiver streaming 0/4000060
enterpr+    1735    1538  0 16:36 pts/0    00:00:00 grep --color=auto postgres

'Postgresql, EPAS > Replication' 카테고리의 다른 글

Logical Replication  (1) 2025.06.17