如何分辨主、备
看进程
- 主库 – walwriter
[root@pg01 PG_12_201909212]# ps -ef| grep wal
postgres 21157 21151 0 15:57 ? 00:00:00 postgres: walwriter
postgres 21168 21151 0 15:57 ? 00:00:00 postgres: walsender repuser 192.168.56.102(38473) streaming 0/2A0001C0
- 备库 – walreceiver
[root@pg02 ~]# ps -ef | grep wal
postgres 13383 13369 0 14:08 ? 00:00:01 postgres: walreceiver streaming 0/2A0001C0
函数方法
一句话判断哪个是主库、哪个是备库,返回的值:
f
为主库
t
为备库
postgres=# select pg_is_in_recovery();
pg_is_in_recovery
-------------------
f
(1 row)
那我这个就是主库喽~
检查流复制同步情况
- 先确定主库传到哪儿了
- 在确定备库接收到哪儿了
- 最后确定备库应用到哪儿了
检查主库传输
确定主库传到什么位置了
postgres=# select pg_current_wal_lsn();
pg_current_wal_lsn
--------------------
0/2A0001C0
(1 row)
检查备库恢复
- 确定备库接收到哪儿了
postgres=# select pg_last_wal_receive_lsn();
pg_last_wal_receive_lsn
-------------------------
0/2A0001C0
(1 row)
- 确定备库应用到哪儿了
postgres=# select pg_last_wal_replay_lsn();
pg_last_wal_replay_lsn
------------------------
0/2A0001C0
(1 row)
- 最近事务应用的时间
postgres=# select pg_last_xact_replay_timestamp();
pg_last_xact_replay_timestamp
-------------------------------
2020-03-05 15:20:22.125688+08
(1 row)