AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 314324
Accepted
padjee
padjee
Asked: 2022-07-13 06:37:31 +0800 CST2022-07-13 06:37:31 +0800 CST 2022-07-13 06:37:31 +0800 CST

使用 LSN 监控逻辑复制

  • 772

如何通过查看 lsn 来监控 Postgresql 12 中的逻辑复制?

我所做的:检查发布者和订阅者上的一些列。

出版方:

select * from pg_stat_replication; -- 查看 REPLAY_LSN

select * from pg_replication_slots; -- 见 CONFIRMED_FLUSH_LSN

订阅方:

select * from pg_catalog.pg_stat_subscription; -- 查看 RECEIVED_LSN 和 LATEST_END_LSN

我确保这些列中的所有值都相同。

我对么 ?或者有没有其他方法可以通过检查一些参数来查看复制工作?

postgresql replication
  • 1 1 个回答
  • 204 Views

1 个回答

  • Voted
  1. Best Answer
    Saeed Bahmanabadi
    2022-07-13T07:45:37+08:002022-07-13T07:45:37+08:00

    我在 Postgres 12 上使用了复制。

    在发布者方面,您可以检查几件事:

    pg_catalog.pg_publication;
    pg_catalog.pg_publication_tables;
    pg_current_wal_lsn();
    

    我将创建一个包含两个表的发布“test_publication”:t_1和t_2. 我不会介绍先决条件(用户、角色等)。

    test_logical_replication=# create publication test_publication for table t_1, t_2;  
    CREATE PUBLICATION  
    test_logical_replication=# select * from pg_catalog.pg_publication;  
    pubname      | pubowner | puballtables | pubinsert | pubupdate | pubdelete  
    -----------------+----------+--------------+-----------+-----------+-----------  
    test_publication |       10 | f            | t         | t         | t  
    (1 row) 
    
    test_logical_replication=# select * from pg_publication_tables;    
        pubname      | schemaname | tablename  
        ------------------+------------+-----------  
         test_publication | public     | t_1  
         test_publication | public     | t_2  
        (2 rows)  
    

    在订阅者方面:

    test_logical_replication_subscriber=# create subscription test_subscription CONNECTION 'dbname=test_logical_replication host=XXX user=repuser' PUBLICATION test_publication;  
    NOTICE:  created replication slot "test_subscription" on publisher   
    CREATE SUBSCRIPTION 
    

    有趣的信息在表中pg_catalog.pg_stat_subscription。这里重要的列是:

    • received_lsn:收到的最后一个预写日志位置。
    • last_msg_send_time:从发布者收到的最后一条消息的发送时间。
    • last_msg_receipt_time:从发布者收到的最后一条消息的接收时间。
    • latest_end_lsn:报告给发布者的最后一个预写日志位置。
    • latest_end_time:上一次向发布者报告的预写日志位置的时间。

    您必须检查这些列以了解正在发生的事情。首先,检查两个数据库是否同步;

    发行方:

    test_logical_replication=> select pg_current_wal_lsn();  
     pg_current_wal_lsn  
    --------------------  
     0/8EB83768     
    

    这显示了我们现在在 WAL 文件中的位置,在开始新的插入之前。

    我们可以检查订阅者,此时两个数据库是同步的,因为pg_current_wal_lsn()发布者返回的值与列received_lsn和latest_end_lsn订阅者中的值匹配:

    test_logical_replication_subscriber=# select received_lsn, latest_end_lsn from pg_catalog.pg_stat_subscription;  
    
    received_lsn    | latest_end_lsn  
    ----------------+------------------     
     0/8EB83768     | 0/8EB83768        
    

    我将向 table 添加 4000 行t_1,看看发布者会发生什么:

    test_logical_replication=> insert into t_1 select id+1, txt||'--BB' from t_1;  
    INSERT 0 4000  
    
    
    test_logical_replication=> select pg_current_wal_lsn();  
     pg_current_wal_lsn
    --------------------
     0/8EC4B9D0             <<< this value in increasing
    (1 row)
    
    test_logical_replication=> select pg_current_wal_lsn();
     pg_current_wal_lsn
    --------------------
     0/8EC4DE78             <<< this value in increasing
    (1 row)
    
    test_logical_replication=> select pg_current_wal_lsn();
     pg_current_wal_lsn
    --------------------
     0/8EC4DEB0             <<< this value in increasing
    (1 row) 
    
    test_logical_replication=> select pg_current_wal_lsn();
     pg_current_wal_lsn
    --------------------
     0/8EC4DEB0            <<< same value, WAL sending has finished
    (1 row)
    

    让我们看看pg_catalog.pg_stat_subscription在订阅者复制期间值如何变化:

    test_logical_replication_subscriber=# select received_lsn,last_msg_send_time,last_msg_receipt_time,latest_end_lsn,latest_end_time from pg_catalog.pg_stat_subscription;
     received_lsn |      last_msg_send_time       |    last_msg_receipt_time     | latest_end_lsn |        latest_end_time  
    --------------+-------------------------------+------------------------------+----------------+-------------------------------
     0/8EC4B9D0   | 2018-12-17 11:39:56.014564+01 | 2018-12-17 11:39:56.07322+01 | 0/8EC4B9D0     | 2018-12-17 11:39:56.014564+01
    (1 row)  
    
    test_logical_replication_subscriber=# select received_lsn,last_msg_send_time,last_msg_receipt_time,latest_end_lsn,latest_end_time from pg_catalog.pg_stat_subscription;
     received_lsn |      last_msg_send_time       |     last_msg_receipt_time     | latest_end_lsn |        latest_end_time
    --------------+-------------------------------+-------------------------------+----------------+-------------------------------
     0/8EC4BA08   | 2018-12-17 11:39:56.737101+01 | 2018-12-17 11:39:56.736303+01 | 0/8EC4BA08     | 2018-12-17 11:39:56.737101+01  
    (1 row)  
    
    test_logical_replication_subscriber=# select received_lsn,last_msg_send_time,last_msg_receipt_time,latest_end_lsn,latest_end_time from pg_catalog.pg_stat_subscription;
     received_lsn |      last_msg_send_time       |     last_msg_receipt_time     | latest_end_lsn |        latest_end_time
    --------------+-------------------------------+-------------------------------+----------------+-------------------------------
     0/8EC4DE78   | 2018-12-17 11:40:04.184765+01 | 2018-12-17 11:40:04.183937+01 | 0/8EC4DE78     | 2018-12-17 11:40:04.184765+01
    (1 row)  
    
     test_logical_replication_subscriber=# select received_lsn,last_msg_send_time,last_msg_receipt_time,latest_end_lsn,latest_end_time from pg_catalog.pg_stat_subscription;
     received_lsn |      last_msg_send_time       |   last_msg_receipt_time    | latest_end_lsn |        latest_end_time
    --------------+-------------------------------+----------------------------+----------------+-------------------------------
     0/8EC4DEB0   | 2018-12-17 11:40:17.153797+01 | 2018-12-17 11:40:17.153+01 | 0/8EC4DEB0     | 2018-12-17 11:40:17.153797+01
    (1 row)
    

    如您所见,在订阅者上,四列显示了 WAL 如何从发布者到达以及如何应用它。列中的时间差异last_msg_send_time可以last_msg_receipt_time提供有关发布者和订阅者之间滞后的信息。在这种情况下,两台服务器位于同一数据中心的不同子网上。

    考虑到我使用的两台服务器是测试服务器,它们之间的同步并不完美。(订阅服务器根本没有配置 NTP 服务器)。

    • 2

相关问题

  • 运行时间偏移延迟复制的最佳实践

  • 存储过程可以防止 SQL 注入吗?

  • PostgreSQL 中 UniProt 的生物序列

  • PostgreSQL 9.0 Replication 和 Slony-I 有什么区别?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve