当前位置: 首页 > article >正文

PostgreSQL的查看主从同步状态

PostgreSQL的查看主从同步状态

PostgreSQL 提供了一些系统视图和函数,查看和监控主从同步的状态。

1 在主节点上查看同步状态

pg_stat_replication 视图

在主节点上,可以通过查询 pg_stat_replication 视图来查看复制的详细状态信息,包括每个从节点的同步进度、滞后情况以及连接状态。

postgres=# \d pg_stat_replication
                    View "pg_catalog.pg_stat_replication"
      Column      |           Type           | Collation | Nullable | Default 
------------------+--------------------------+-----------+----------+---------
 pid              | integer                  |           |          | 
 usesysid         | oid                      |           |          | 
 usename          | name                     |           |          | 
 application_name | text                     |           |          | 
 client_addr      | inet                     |           |          | 
 client_hostname  | text                     |           |          | 
 client_port      | integer                  |           |          | 
 backend_start    | timestamp with time zone |           |          | 
 backend_xmin     | xid                      |           |          | 
 state            | text                     |           |          | 
 sent_lsn         | pg_lsn                   |           |          | 
 write_lsn        | pg_lsn                   |           |          | 
 flush_lsn        | pg_lsn                   |           |          | 
 replay_lsn       | pg_lsn                   |           |          | 
 write_lag        | interval                 |           |          | 
 flush_lag        | interval                 |           |          | 
 replay_lag       | interval                 |           |          | 
 sync_priority    | integer                  |           |          | 
 sync_state       | text                     |           |          | 
 reply_time       | timestamp with time zone |           |          | 
  • pid: 进程ID。
  • usesysid: 使用者系统ID。
  • usename: 复制连接的用户名。
  • application_name: 复制连接的应用程序名称,通常每个从节点会设置一个唯一的名字。
  • client_addr: 从节点的IP地址。
  • state: 连接状态,可以是 startup, catchup, streaming 等。
  • sent_lsn: 主节点最后发送给从节点的WAL记录的位置。
  • write_lsn: 从节点接收到的最远的WAL记录的位置。
  • flush_lsn: 从节点写入磁盘的最远的WAL记录的位置。
  • replay_lsn: 从节点应用的最远的WAL记录的位置。
  • sync_priority: 同步复制节点的优先级。
  • sync_state: 同步状态,可以是 async, potential, sync, quorum 等。

示例

postgres=# select * from pg_stat_replication;
  pid  | usesysid | usename | application_name |  client_addr   | client_hostname | client_port |         backend_start         | backend_xmin |   state   | sent_lsn  | wr
ite_lsn | flush_lsn | replay_lsn |    write_lag    |    flush_lag    |   replay_lag    | sync_priority | sync_state |          reply_time           
-------+----------+---------+------------------+----------------+-----------------+-------------+-------------------------------+--------------+-----------+-----------+---
--------+-----------+------------+-----------------+-----------------+-----------------+---------------+------------+-------------------------------
 55877 |    16384 | repmgr  | test2            | 192.168.10.101 |                 |       34636 | 2024-09-15 12:13:48.651404-04 |              | streaming | 0/44B8EF0 | 0/
44B8EF0 | 0/44B8EF0 | 0/44B8EF0  | 00:00:00.000237 | 00:00:00.000476 | 00:00:00.000537 |             0 | async      | 2024-09-15 12:33:24.543761-04
(1 row)

2 在从节点上查看同步状态

pg_stat_wal_receiver 视图

对从节点,可以通过查询 pg_stat_wal_receiver 视图来查看WAL接收器的状态。

postgres=# \d pg_stat_wal_receiver
                      View "pg_catalog.pg_stat_wal_receiver"
        Column         |           Type           | Collation | Nullable | Default 
-----------------------+--------------------------+-----------+----------+---------
 pid                   | integer                  |           |          | 
 status                | text                     |           |          | 
 receive_start_lsn     | pg_lsn                   |           |          | 
 receive_start_tli     | integer                  |           |          | 
 received_lsn          | pg_lsn                   |           |          | 
 received_tli          | integer                  |           |          | 
 last_msg_send_time    | timestamp with time zone |           |          | 
 last_msg_receipt_time | timestamp with time zone |           |          | 
 latest_end_lsn        | pg_lsn                   |           |          | 
 latest_end_time       | timestamp with time zone |           |          | 
 slot_name             | text                     |           |          | 
 sender_host           | text                     |           |          | 
 sender_port           | integer                  |           |          | 
 conninfo              | text                     |           |          | 

  • pid: 接收器进程ID。
  • status: WAL接收状态。
  • receive_start_lsn: 接收起始的LSN。
  • received_lsn: 已接收的最新的LSN。
  • last_msg_send_time: 主节点发送最后一个消息的时间。
  • last_msg_receipt_time: 从节点接收最后一个消息的时间。
  • latest_end_lsn: 最近接收的WAL记录的LSN。
  • latest_end_time: 最近接收到的WAL记录的时间。

示例

postgres=# select * from pg_stat_wal_receiver;
  pid  |  status   | receive_start_lsn | receive_start_tli | received_lsn | received_tli |      last_msg_send_time       |     last_msg_receipt_time     | latest_end_lsn |
        latest_end_time        | slot_name |  sender_host   | sender_port |                                                                                                
                        conninfo                                                                                                                        
-------+-----------+-------------------+-------------------+--------------+--------------+-------------------------------+-------------------------------+----------------+
-------------------------------+-----------+----------------+-------------+------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------
 87404 | streaming | 0/3000000         |                 5 | 0/44C0BF8    |            5 | 2024-09-15 12:35:33.842389-04 | 2024-09-15 12:35:33.842893-04 | 0/44C0BF8      |
 2024-09-15 12:35:33.842389-04 |           | 192.168.10.100 |        5432 | user=repmgr passfile=/home/pg12/.pgpass dbname=replication host=192.168.10.100 port=5432 applic
ation_name=test2 fallback_application_name=walreceiver sslmode=disable sslcompression=0 gssencmode=disable krbsrvname=postgres target_session_attrs=any
(1 row)

跨节点比对状态

为了确保主节点和从节点的状态是一致的,可以比对 pg_stat_replication 中的 sent_lsnpg_stat_wal_receiver 中的 received_lsn。如果两者没有显著滞后,可以认为同步状态正常。


http://www.kler.cn/news/307357.html

相关文章:

  • 凸优化学习(3)——对偶方法、KKT条件、ADMM
  • 「C++系列」文件和流
  • 医学数据分析实训 项目四回归分析--预测帕金森病病情的严重程度
  • Java设计模式—面向对象设计原则(二) --------> 里氏代换原则 LSP (完整详解,附有代码+案列)
  • Linux 系统盘空间不足,想要将 Docker 镜像和容器数据迁移到数据盘
  • sqlgun靶场攻略
  • Mysql系列-索引简介
  • Vert.x HttpClient调用后端服务时使用Idle Timeout和KeepAlive Timeout的行为分析
  • 11.java面向对象
  • macOS上谷歌浏览器的十大隐藏功能
  • c语言中的常量定义(补充)
  • 【兼容性记录】video标签在 IOS 和 安卓中的问题
  • 队列-------
  • 英语学习交流平台|基于java的英语学习交流平台系统小程序(源码+数据库+文档)
  • EP12 分类列表元素点击跳转
  • 【云原生监控】Prometheus之PushGateway
  • 机器学习的入门指南
  • JVM HotSpot 虚拟机: 对象的创建, 内存布局和访问定位
  • Oracle数据库中的归档日志(Archive Log)详解与应用
  • 07_Python数据类型_集合
  • 系统 IO
  • 08_Python数据类型_字典
  • C# 记录一个获取系统空闲时间的函数,可用于判断休眠
  • 性能测试:Locust使用介绍(三)
  • MoCo对比损失
  • LC并联电路在正弦稳态下的传递函数推导(LC并联谐振选频电路)
  • 带你如何使用CICD持续集成与持续交付
  • 2024网络安全、应用软件系统开发决赛技术文件
  • Go语言现代web开发15 泛型和错误
  • C++中string字符串类型介绍及数组模拟