Postgresql 检查数据库主从复制进度的操作

时间:2022-10-17 08:51:48

如何查看主从复制的状态,且备库应用落后了多少字节

这些信息要在主库中查询

查看流复制的信息可以使用主库上的视图

?
1
select pid,state,client_addr,sync_priority,sync_state from pg_stat_replication;

pg_stat_replication中几个字断记录了发送wal的位置及备库接收到的wal的位置、

sent_location--发送wal的位置

write_location--备库接收到的wal的位置

flush_location—备库写wal日志到磁盘的位置

replay_location—备库应用日志的位置

查看备库落后主库多少字节

?
1
2
select pg_xlog_location_diff(pg_current_xlog_location(),replay_location)/1024/1024 as MB from pg_stat_replication;
select pg_xlog_location_diff(pg_current_xlog_location(),replay_location)/1024/1024/1024 as GB from pg_stat_replication;

级联复制

?
1
select pg_xlog_location_diff(pg_last_xlog_replay_location(),replay_location)/1024/1024/1024 as GB from pg_stat_replication;

补充:pgsql之查看主备复制延迟

查看复制延迟:

10.0及以上:

?
1
2
3
4
5
6
7
8
9
10
11
SELECT
  pg_wal_lsn_diff(A .c1, replay_lsn) /(1024 * 1024) AS slave_latency_MB,
  pg_wal_lsn_diff(A .c1, sent_lsn) /(1024 * 1024) AS send_latency_MB,
  pg_wal_lsn_diff(A .c1, flush_lsn) /(1024 * 1024) AS flush_latency_MB,
  state,
  backend_start,
  now()::timestamp with time zone
 FROM pg_stat_replication, pg_current_wal_lsn() AS A(c1)
 WHERE client_addr='192.168.46.173' and application_name = 'standby1'
 ORDER BY slave_latency_MB, send_latency_MB DESC
 LIMIT 1;

注:

192.168.46.173 表示从库ip地址。

pg_wal_lsn_diff(lsn pg_lsn, lsn pg_lsn):计算两个预写式日志位置间的差别。

pg_current_wal_lsn():获得当前预写式日志写入位置

小于10.0版本:

?
1
2
3
4
5
6
7
8
9
10
11
SELECT
  pg_xlog_location_diff(A .c1, replay_lsn) /(1024 * 1024) AS slave_latency_MB,
  pg_xlog_location_diff(A .c1, sent_lsn) /(1024 * 1024) AS send_latency_MB,
  pg_xlog_location_diff(A .c1, flush_lsn) /(1024 * 1024) AS flush_latency_MB,
  state,
  backend_start,
  now()::timestamp with time zone
 FROM pg_stat_replication, pg_current_xlog_location AS A(c1)
 WHERE client_addr='192.168.46.173' and application_name = 'standby1'
 ORDER BY slave_latency_MB, send_latency_MB DESC
 LIMIT 1;

注:

192.168.46.173 表示从库ip地址。

pg_xlog_location_diff(lsn pg_lsn, lsn pg_lsn):计算两个预写式日志位置间的差别。

pg_current_xlog_location ():获得当前预写式日志写入位置

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/zhangchen881222/article/details/80668837