postgresql是使用Streaming Replication来实现热备份的,热备份的作用如下:

  • 灾难恢复
  • 高可用性
  • 负载均衡,当你使用Streaming Replication来实现热备份(hot standby)的时候,可以再standby上执行查询语句,也只允许执行select

那么,当我们有大量使用了流复制的机器之后, 监控 Streaming Replication 的正常运行,在我们的部署中是非常重要的。

那么,我们会有下面的监控问题:

  1. 如何更好的监控流复制(Streaming Replication)
  2. 监控它们最好的方法是什么
  3. 除了使用 Master的pg_stat_replication视图 监控,还有什么在standby上可用的方法来监控流复制
  4. 如何计算 replication 滞后时间,以秒、分钟为单位。

针对上面几个常见的问题,下面是一些我认为比较有用的方法。

  1. master/primary server 上的pg_stat_replication 视图

     pid:              walsender process的进程ID
    usesysid: 执行流复制的用户的OID
    usename: 执行流复制的用户的用户名
    application_name: 连接到master的Application name
    client_addr: standby/streaming replication的ip地址
    client_hostname: Hostname of standby.
    client_port: standby上的TCP port
    backend_start: 从数据第一次连接master的时间
    state: 当前WAL sender状态 i.e streaming
    sent_location: Last transaction location sent to standby.
    write_location: Last transaction written on disk at standby
    flush_location: Last transaction flush on disk at standby.
    replay_location: Last transaction flush on disk at standby.
    sync_priority: standby服务器的优先级
    sync_state: standby的同步类型( async/synchronous)(异步/同步). e.g.:
    postgres=# \x
    Expanded display is on.
    postgres=# select * from pg_stat_replication;
    -[ RECORD 1 ]----+------------------------------
    pid | 19597
    usesysid | 16384
    usename | repl
    application_name | walreceiver
    client_addr | 210.61.161.183
    client_hostname |
    client_port | 50474
    backend_start | 2015-02-04 11:07:27.137356+08
    state | streaming
    sent_location | 4/E059E560
    write_location | 4/E059E560
    flush_location | 4/E059E560
    replay_location | 4/E059BEB0
    sync_priority | 0
    sync_state | async
  2. select pg_is_in_recovery();, 这个函数在standby执行,会告诉你,是否处于recovery 模式!

     e.g.
    # standby处于复制状态,返回 t, 否则返回 f
    postgres=# select pg_is_in_recovery();
    pg_is_in_recovery
    -------------------
    t
    (1 row) # 下面不是standby的例子
    postgres=# select pg_is_in_recovery();
    pg_is_in_recovery
    -------------------
    f
    (1 row)
  3. select pg_last_xlog_replay_location();, 同样是在standby上执行,显示recovery 过程中的最近一个事务。

     e.g.
    postgres=# select pg_last_xlog_replay_location();
    pg_last_xlog_replay_location
    ------------------------------
    0/27099838
    (1 row)
  4. select pg_last_xlog_receive_location();, standby上执行,standby最后接收到的事务日志,并且已经同步写到硬盘的.

     e.g.
    postgres=# select pg_last_xlog_receive_location();
    pg_last_xlog_receive_location
    -------------------------------
    0/2709CB70
    (1 row)
  5. select pg_last_xact_replay_timestamp();, standby上执行,recovery过程中最后一个事务执行的时间

     e.g.
    postgres=# select pg_last_xact_replay_timestamp();
    pg_last_xact_replay_timestamp
    -------------------------------
    2015-02-09 19:48:57.916245+08
    (1 row)

接下来的问题,是如何正确的在master和standby上监控Streaming Replication:

standby上的监控:

  1. select pg_is_in_recovery(); 判断是否处于recovery模式

  2. 查看recovery的延时情况:

     SELECT CASE WHEN pg_last_xlog_receive_location() = pg_last_xlog_replay_location()
    THEN 0
    ELSE EXTRACT (EPOCH FROM now() - pg_last_xact_replay_timestamp())
    END AS log_delay; # 如果receive和replay是同一个位置,延时为0;否则当前时间减去最后一个事务的时间为延时
    log_delay
    -----------
    0
    (1 row)
  3. pg_last_xact_replay_timestamppg_last_xlog_replay_location 判断recovery是否处于工作状态。当Streaming Replication在复制的时候,replay_timestamp和pg_last_xlog_replay_location会一直增长。

     postgres=# select pg_last_xact_replay_timestamp();
    pg_last_xact_replay_timestamp
    -------------------------------
    2015-02-09 20:53:54.48081+08
    (1 row) postgres=# select pg_last_xact_replay_timestamp();
    pg_last_xact_replay_timestamp
    -------------------------------
    2015-02-09 20:53:55.456179+08 postgres=# select pg_last_xlog_replay_location();
    pg_last_xlog_replay_location
    ------------------------------
    5/723E528
    (1 row) postgres=# select pg_last_xlog_replay_location();
    pg_last_xlog_replay_location
    ------------------------------
    5/72514B8
    (1 row)

master上的监控:

  1. 查看pg_stat_replication 中的状态呢,使用

     postgres=# select * from pg_stat_replication;
    -[ RECORD 1 ]----+------------------------------
    pid | 19597
    usesysid | 16384
    usename | repl
    application_name | walreceiver
    client_addr | 210.61.161.183
    client_hostname |
    client_port | 50474
    backend_start | 2015-02-04 11:07:27.137356+08
    state | streaming
    sent_location | 5/64046A8
    write_location | 5/64046A8
    flush_location | 5/64046A8
    replay_location | 5/64027B0
    sync_priority | 0
    sync_state | async
  2. 在master判断recovery的滞后程度,以字节为单位

     postgres=# select pg_xlog_location_diff(sent_location, replay_location) from pg_stat_replication;
    pg_xlog_location_diff
    -----------------------
    1968
    (1 row) postgres=# select pg_xlog_location_diff(sent_location, replay_location) from pg_stat_replication;
    pg_xlog_location_diff
    -----------------------
    1488

参考:

Postgresql The Statistics Collector

System Administration Functions

PostgreSQL 9.3 Streaming Replication 状态监控的更多相关文章

  1. postgresql Streaming Replication监控与注意事项

    一监控Streaming Replication集群 1 pg_stat_replication视图(主库端执行) pid Wal sender process的进程ID usesysid 执行流复制 ...

  2. Streaming replication slots in PostgreSQL 9.4

    Streaming replication slots are a pending feature in PostgreSQL 9.4, as part of the logical changese ...

  3. 配置PostgreSQL Streaming Replication集群

    运行环境: Primary: 192.168.0.11 Standby: 192.168.0.21, 192.168.0.22 OS: CentOS 6.2 PostgreSQL: 9.1.2 版本以 ...

  4. PostgreSQL Streaming Replication的FATAL ERROR

    磨砺技术珠矶,践行数据之道,追求卓越价值回到上一级页面: PostgreSQL集群方案相关索引页     回到顶级页面:PostgreSQL索引页[作者 高健@博客园  luckyjackgao@gm ...

  5. PostgreSQL的streaming replication

    磨砺技术珠矶,践行数据之道,追求卓越价值回到上一级页面: PostgreSQL集群方案相关索引页     回到顶级页面:PostgreSQL索引页[作者 高健@博客园  luckyjackgao@gm ...

  6. mysql主从同步(4)-Slave延迟状态监控

    mysql主从同步(4)-Slave延迟状态监控  转自:http://www.cnblogs.com/kevingrace/p/5685511.html 之前部署了mysql主从同步环境(Mysql ...

  7. redis状态监控与性能调优

    本文主要介绍及演示一些Redis相关的状态监控和性能调优的命令及使用方法: 1.redis-benchmark redis基准信息,redis服务器性能检测 例如: 检测redis服务器性能,本机63 ...

  8. 6. MGR状态监控 | 深入浅出MGR

    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. 目录 1. 节点状态监控 2. MGR事务状态监控 3. 其他监控 4. 小结 参考资料.文档 免责声明 文章推荐: 关于 ...

  9. 【VC++技术杂谈003】打印技术之打印机状态监控

    在上一篇博文中我主要介绍了如何获取以及设置系统的默认打印机,本文将介绍如何对打印机状态进行实时监控,记录下所打印的文档.打印的份数以及打印时间等打印信息. 1.打印机虚脱机技术 在正式介绍如何对打印机 ...

随机推荐

  1. 每天努力一点之SQL(二) count sum case when then group by

    1. select sum(CASE WHEN A.[STATUS]=0 THEN 1 ELSE 0 end) as a1,  sum(CASE A.[STATUS] WHEN 1 THEN 1 EL ...

  2. Hosting WCF Service

    之前在博客几个实例DemoWCF服务寄宿到控制到应用程序中,这篇来总结一下,经常使用的几种宿主的方式. 1.Self-Hosting 一个WCF服务可以寄宿在控制台应用程序或者WinForms app ...

  3. JTextArea Demo

    在往JTextArea中填充数据时,JTextArea上的滚动条也可以拖动.解决办法:主线程放在EDT中,fill JTextArea的操作放在另外一个线程中,这样fill操作与GUI上的操作就分离了 ...

  4. 基本shell编程【3】- 常用的工具awk\sed\sort\uniq\od(转)

    awk awk是个很好用的东西,大量使用在linux系统分析的结果展示处理上.并且可以使用管道, input | awk ''  | output 1.首先要知道形式 awk 'command' fi ...

  5. c语言实现hashtable,相似C++的map和iOS的NSDictionary

    跟线性数组和链表不同.HashTable是高速查找的数据结构.本文中的HashTable使用链表处理数组. 该HashTable能够指定table的长度.提供了遍历的方法. 包含table的长度的选择 ...

  6. D3D 扎带 小样本

    D3D 符合基本程序 #pragma once #pragma comment(lib,"d3d9.lib") #pragma comment(lib,"d3dx9.li ...

  7. Windows 事件查看器(收集)

    原文:Windows 事件查看器(收集) 事件查看器相当于一本厚厚的系统日志,可以查看关于硬件.软件和系统问题的信息,也可以监视 Windows 的安全事件 提示:除了可以在"控制面板→管理 ...

  8. Hsql中In没有1000的限制

    SELECT * FROM user , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...

  9. cocos2d-x3.0 解释具体的新的物理引擎setCategoryBitmask()、setContactTestBitmask()、setCollisionBitmask()

    转载请注明出处:游戏开发实验室http://blog.csdn.net/u010019717/article/details/32942641 我在编写游戏的时候遇到了这个问题.  物理引擎其它的内容 ...

  10. SQL开发中容易忽视的一些小地方( 三)

    原文:SQL开发中容易忽视的一些小地方( 三) 目的:这篇文章我想说说我在工作中关于in和union all 的用法. 索引定义 : 微软的SQL SERVER提供了两种索引:聚集索引(cluster ...