PG判断主库与备库的方式

通过进程查看

  • 主库
    ps -ef | grep postgres
    在主库中可以看到有 walsender进程
    PG判断主库与备库的方式_第1张图片

  • 备库
    ps -ef | grep postgres
    在主库中可以看到有 walreceiver进程
    PG判断主库与备库的方式_第2张图片

通过视图查看

执行select * from pg_stat_replication ;在主库上有结果,在备库上无结果;
执行select * from pg_stat_wal_receiver,在备库上查询有结果,在主库上查询无结果:
在主库上查看是有结果的
在备库上查询无结果

  • 主库
[postgres@lxm ~]$ psql
Password for user postgres:
psql (13beta1)
Type "help" for help.

postgres=# \x
Expanded display is on.
postgres=# select * from pg_stat_replication ;
-[ RECORD 1 ]----+------------------------------
pid              | 21584
usesysid         | 16391
usename          | repuser
application_name | walreceiver
client_addr      | 192.168.0.61
client_hostname  |
client_port      | 12736
backend_start    | 2020-06-16 04:00:41.269905+08
backend_xmin     |
state            | streaming
sent_lsn         | 0/120000D8
write_lsn        | 0/120000D8
flush_lsn        | 0/120000D8
replay_lsn       | 0/120000D8
write_lag        |
flush_lag        |
replay_lag       |
sync_priority    | 0
sync_state       | async
reply_time       | 2020-06-11 19:50:27.009871+08
spill_txns       | 0
spill_count      | 0
spill_bytes      | 0

postgres=# select * from pg_stat_wal_receiver ;
(0 rows)

postgres=#

  • 备库
postgres=# \q
[postgres@lxm1 ~]$ psql
Password for user postgres:
psql (13beta1)
Type "help" for help.

postgres=# \x
Expanded display is on.
postgres=# select * from pg_stat_replication ;
(0 rows)

postgres=# select * from pg_stat_wal_receiver ;
-[ RECORD 1 ]---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
pid                   | 4236
status                | streaming
receive_start_lsn     | 0/11000000
receive_start_tli     | 1
written_lsn           | 0/120000D8
flushed_lsn           | 0/120000D8
received_tli          | 1
last_msg_send_time    | 2020-06-16 04:11:14.812036+08
last_msg_receipt_time | 2020-06-11 19:50:57.082737+08
latest_end_lsn        | 0/120000D8
latest_end_time       | 2020-06-16 04:00:43.843597+08
slot_name             |
sender_host           | 192.168.0.60
sender_port           | 5432
conninfo              | user=repuser password=******** channel_binding=disable dbname=replication host=192.168.0.60 port=5432fallback_application_name=walreceiver sslmode=disable sslcompression=0 gssencmode=disable krbsrvname=postgres target_session_attrs=any

postgres=#

通过系统函数查看

  • 主库
postgres=# select * from pg_is_in_recovery();
-[ RECORD 1 ]-----+--
pg_is_in_recovery | f


  • 备库

postgres=# select * from pg_is_in_recovery();
 pg_is_in_recovery
-------------------
 t
(1 row)


查看控制文件

  • 主库
[postgres@lxm ~]$ pg_controldata $PGDATA | grep cluster
Database cluster state:               in production
[postgres@lxm ~]$

  • 备库
[postgres@lxm1 ~]$ pg_controldata $PGDATA | grep cluster
Database cluster state:               in archive recovery

通过配置文件

  • 在PG12之前,通过recovery.conf文件判断, 该文件会存在于备库上
  • PG12以及以后,通过standy.signal文件来判断, 该文件存在于备库上

你可能感兴趣的:(PostgreSQL)