PostgreSQL 主从差异查看

本文提供了两种方法来检查PostgreSQL主从数据库之间的同步延迟情况。第一种方法通过比较主从库的数据大小差异来评估延迟,第二种方法则直接测量时间上的差异。这些技巧对于维护数据库系统的高效运行至关重要。

一、大小差异

在主库上执行

select
        application_name,
        pg_size_pretty(pg_xlog_location_diff(pg_current_xlog_location(), replay_location)) as diff
from
        pg_stat_replication;

或者:

select
	application_name,
	client_addr,
	cur_xlog || '/' || cur_offset as cur_xlog,  
	sent_xlog || '/' || sent_offset as sent_xlog,     	     
	replay_xlog || '/' || replay_offset as replay_xlog,
	pg_size_pretty(( ((cur_xlog * 255 * 16 ^ 6) + cur_offset) - ((sent_xlog * 255 * 16 ^ 6) + sent_offset) )::numeric) as master_lag,         
	pg_size_pretty(( ((sent_xlog * 255 * 16 ^ 6) + sent_offset) - ((replay_xlog * 255 * 16 ^ 6) + replay_offset) )::numeric) as slave_lag,                                                                  
	pg_size_pretty(( ((cur_xlog * 255 * 16 ^ 6) + cur_offset) - ((replay_xlog * 255 * 16 ^ 6) + replay_offset) )::numeric) as total_lag
from (
	select
		application_name,
		client_addr,
		('x' || lpad(split_part(sent_location::text,'/', 1), 8, '0'))::bit(32)::bigint as sent_xlog,
		('x' || lpad(split_part(replay_location::text, '/', 1), 8, '0'))::bit(32)::bigint as replay_xlog,
		('x' || lpad(split_part(sent_location::text, '/', 2), 8, '0'))::bit(32)::bigint as sent_offset,
		('x' || lpad(split_part(replay_location::text, '/', 2), 8, '0'))::bit(32)::bigint as replay_offset,
		('x' || lpad(split_part(pg_current_xlog_location()::text, '/', 1), 8, '0'))::bit(32)::bigint as cur_xlog,
		('x' || lpad(split_part(pg_current_xlog_location()::text, '/', 2), 8, '0'))::bit(32)::bigint as cur_offset
	from
		pg_stat_replication
) as s;

二、时间差异

在从库上执行:

select now() - pg_last_xact_replay_timestamp() as replication_delay;


转载于:https://my.oschina.net/aven92/blog/486496

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值