PostgreSQL WAL产生量、发送速度、应用速度监控脚本

1. 创建视图

记录每分钟的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;

2. 创建记录表

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);

3. 创建shell脚本

  • 将每分钟的WAL发送、应用速度插入记录表
  • 统计每分钟WAL产生量,更新至记录表(会有秒级的误差)

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

4. 监控效果

按分钟

select db_clock_min,wal_size_mb,sent_rate_mb,replay_rate_mb from wal_sent_replay_rate  order by 1 desc limit 100;

PostgreSQL WAL产生量、发送速度、应用速度监控脚本_第1张图片

按天

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;

PostgreSQL WAL产生量、发送速度、应用速度监控脚本_第2张图片

按小时

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;

获取数据后就可以进行图形化展示了~

你可能感兴趣的:(PostgreSQL,监控,灾备,WAL产生量,发送速度,应用速度,主从延迟,主从lag)