欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-09 22:21:27
如何查看主从复制的状态,且备库应用落后了多少字节这些信息要在主库中查询查看流复制的信息可以使用主库上的视图select pid,state,client_addr,sync_priority,sync...

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

这些信息要在主库中查询

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

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—备库应用日志的位置

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

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;

级联复制

select pg_xlog_location_diff(pg_last_xlog_replay_location(),replay_location)/1024/1024/1024 as gb from pg_stat_replication;

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

查看复制延迟:

10.0及以上:

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版本:

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 ():获得当前预写式日志写入位置

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