1. 개요
선행된 포스팅에서 PG_AUTO_FAILOVER 노드 구성에 대해 살펴볼 수 있었습니다.
이제 JDBC를 활용하여 간단한 커넥션 테스트를 해보고자 합니다.
2. Connection TEST
1) Connection formation 확인
[postgres@postgres-01 ~]$ pg_autoctl show uri
Type | Name | Connection String
-------------+---------+-------------------------------
monitor | monitor | postgres://autoctl_node@xxx.xxx.xxx.xxx:15432/pg_auto_failover?sslmode=require
formation | default | postgres://xxx.xxx.xxx.100:15432,xxx.xxx.xxx.101:15432/postgres?target_session_attrs=read-write&sslmode=require
상기의 코드에서 formation을 활용 할 예정입니다.
2) JDBC Command
jdbc:postgresql://xxx.xxx.xxx.100:15432,xxx.xxx.xxx.101:15432/postgres?targetServerType=master
노드의 address를 순차적으로 나열한 다음, targetServerType 파라미터를 설정합니다.
- any : 모든 DB 노드에 연결 (이 경우 loadBalanceHosts 설정을 좀 더 부여할 수 있을 듯 합니다.)
- master : Primary에 연결
- Slave : Standby에 연결
- prefer : Standby 노드에 우선적으로 붙으나, 사용 불가할 경우 Primary로 연결
3) DB 접속
-[ RECORD 10 ]---+-----------------------------------------------------------------------------
datid | 5
datname | postgres
pid | 90583
leader_pid |
usesysid | 10
usename | postgres
application_name | PostgreSQL JDBC Driver
client_addr | xxx.xxx.xxx.xxx
client_hostname |
client_port | 52348
backend_start | 2023-xx-xx 13:10:58.590644+09
xact_start |
query_start | 2023-xx-xx 13:10:58.617542+09
state_change | 2023-xx-xx 13:10:58.61756+09
wait_event_type | Client
wait_event | ClientRead
state | idle
backend_xid |
backend_xmin |
query_id |
query | SET application_name = 'PostgreSQL JDBC Driver'
backend_type | client backend
-[ RECORD 11 ]---+-----------------------------------------------------------------------------
4) switchover
[postgres@localhost pgsql]$ pg_autoctl perform switchover
[postgres@localhost pgsql]$ pg_autoctl show state
Name | Node | Host:Port | TLI: LSN | Connection |
Reported State | Assigned State
--------+-------+---------------------+----------------+--------------+-------------
--------+--------------------
Active | 1 | xxx.xxx.xxx.100:15432 | 1: 0/3000148 | read-only |
secondary | secondary
Standby | 2 | xxx.xxx.xxx.101:15432 | 1: 0/3000148 | read-write |
primary | primary
5) DB접속 테스트 2
-[ RECORD 10 ]---+-----------------------------------------------------------------------------
datid | 5
datname | postgres
pid | 90583
leader_pid |
usesysid | 10
usename | postgres
application_name | PostgreSQL JDBC Driver
client_addr | xxx.xxx.xxx.xxx
client_hostname |
client_port | 52348
backend_start | 2023-xx-xx 13:10:58.590644+09
xact_start |
query_start | 2023-xx-xx 13:10:58.617542+09
state_change | 2023-xx-xx 13:10:58.61756+09
wait_event_type | Client
wait_event | ClientRead
state | idle
backend_xid |
backend_xmin |
query_id |
query | SET application_name = 'PostgreSQL JDBC Driver'
backend_type | client backend
-[ RECORD 11 ]---+-----------------------------------------------------------------------------
3. 정리
Node의 Primary / Standby가 변경된 상황에서도 Active node로 연결되는지 살펴보았습니다.
테스트 결과 JDBC 연결 시 targetServerType 파라미터를 적용한 대로 접속할 수 있음을 확인할 수 있었습니다.
Oracle과 같이 Distributed Transaction을 구현할 수가 없으므로,
(read only 환경에서는 commit를 할 수 없으므로)
세션을 분할하는 방법을 고려해볼 수 있을 듯 합니다.
(예를 들어, select만 하는 경우라면 read only인 Standby node로 연결되도록.)