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

PostgreSQL的查看主从同步状态

PostgreSQL的查看主从同步状态

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

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

pg_stat_replication 视图

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

postgres=# \d pg_stat_replicationView "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_receiverView "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.lryc.cn/news/439875.html

相关文章:

  • Java多态性的理解
  • 安全工具 | 使用Burp Suite的10个小tips
  • 企业项目中字符串工具类
  • 下载github patch到本地
  • C++基础部分代码
  • neo4j(spring) 使用示例
  • 链接升级:Element UI <el-link> 的应用
  • 简单题26 - 删除有序数组中的重复项(Java)20240917
  • DFS:深搜+回溯+剪枝实战解决OJ问题
  • 命令语境中的“可以”的字词含义分析
  • 直播相关03-录制麦克风声音, ffmpeg 命名,使用命令行完成录音
  • VUE3中ref与reactive
  • 高职院校人工智能技术和无人机技术实训室建设方案
  • x-cmd pkg | shtris: 在终端体验经典的俄罗斯方块游戏
  • Linux基础---07文件传输及解决yum安装失效的方法
  • [项目][WebServer][Makefile Shell]详细讲解
  • ElementUI大坑Notification修改样式
  • vivado中的diagram
  • 项目实现:云备份②(文件操作、Json等工具类的实现)
  • ‌内网穿透技术‌总结
  • Git使用—把当前仓库的一个分支push到另一个仓库的指定分支、基于当前仓库创建另一个仓库的分支并推送到对应仓库(mit6828)
  • windows11+ubuntu20.04.6双系统安装
  • 如何通过 PhantomJS 模拟用户行为抓取动态网页内容
  • ARM驱动学习之8 动态申请字符类设备号
  • TCP.IP四层模型
  • 极狐GitLab DevSecOps 功能合集(七大安全功能)
  • 进阶SpringBoot之异步任务、邮件任务和定时执行任务
  • 【设计模式-桥接】
  • JVM JMM 专题篇 ( 12000 字详解 )
  • 【C++】—— list 模拟实现