记录每分钟的WAL发送、应用速度(pg 10及以上可用)
create view v_wal_sent_replay_rate as
WITH a AS (
SELECT pg_stat_replication.sent_lsn,
pg_stat_replication.replay_lsn
FROM pg_stat_replication
), b AS (
SELECT pg_stat_replication.sent_lsn,
pg_stat_replication.replay_lsn
FROM pg_stat_replication,
pg_sleep(60)
)
SELECT to_char(clock_timestamp()::timestamp without time zone,'yyyy-mm-dd hh24:mi'::text) AS db_clock_min,
to_char(clock_timestamp()::timestamp without time zone, 'yyyy-mm-dd hh24:mi:ss'::text) AS db_clock_sec,
round(pg_wal_lsn_diff(b.sent_lsn, a.sent_lsn) / 1024::numeric / 1024::numeric, 2) AS sent_rate_mb,
round(pg_wal_lsn_diff(b.replay_lsn, a.replay_lsn) / 1024::numeric / 1024::numeric, 2) AS replay_rate_mb
FROM a,
b;
create table wal_sent_replay_rate(
db_clock_min character varying(50),
db_clock_sec character varying(50),
sent_rate_mb numeric,
replay_rate_mb numeric,
wal_end_min character varying(50),
wal_cnt integer,
wal_size_mb integer);
vi insert_wal_sent_replay_rate.sh
#!/bin/bash
psql << EOF
insert into wal_sent_replay_rate(db_clock_min,db_clock_sec,sent_rate_mb,replay_rate_mb) select db_clock_min,db_clock_sec,sent_rate_mb,replay_rate_mb from v_wal_sent_replay_rate;
EOF
WAL_END_MIN=`date +"%Y-%m-%d %H:%M"`
WAL_CNT=`find /data/postgres/pg5432/data/pg_wal -maxdepth 1 -type f -mmin -1 -ls|wc -l`
WAL_SIZE_MB=`expr $WAL_CNT \* 16`
psql << EOF
update wal_sent_replay_rate set wal_end_min='$WAL_END_MIN',wal_cnt=$WAL_CNT,wal_size_mb=$WAL_SIZE_MB where db_clock_min='$WAL_END_MIN';
EOF
按分钟
select db_clock_min,wal_size_mb,sent_rate_mb,replay_rate_mb from wal_sent_replay_rate order by 1 desc limit 100;
按天
select substring(db_clock_min from 1 for 10) as day,SUM(wal_size_mb) wal_size_mb,SUM(sent_rate_mb) sent_rate_mb,SUM(replay_rate_mb) replay_rate_mb FROM wal_sent_replay_rate GROUP BY 1 ORDER BY 1 DESC;
按小时
select substring(db_clock_min from 1 for 13) as day,SUM(wal_size_mb) wal_size_mb,SUM(sent_rate_mb) sent_rate_mb,SUM(replay_rate_mb) replay_rate_mb FROM wal_sent_replay_rate GROUP BY 1 ORDER BY 1 DESC;
获取数据后就可以进行图形化展示了~