使用LSN监控逻辑复制

pad*_*jee 1 postgresql replication data-synchronization postgresql-12

如何通过查看 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

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

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

小智 5

我在 Postgres 12 上使用了复制。

在发布商方面,您可以检查以下几项内容:

pg_catalog.pg_publication;
pg_catalog.pg_publication_tables;
pg_current_wal_lsn();
Run Code Online (Sandbox Code Playgroud)

我将创建一个包含两个表的出版物“test_publication”:t_1t_2。我不会介绍先决条件(用户、角色等)。

pg_catalog.pg_publication;
pg_catalog.pg_publication_tables;
pg_current_wal_lsn();
Run Code Online (Sandbox Code Playgroud)

在订户端:

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 
Run Code Online (Sandbox Code Playgroud)

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

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

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

发布方:

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)  
Run Code Online (Sandbox Code Playgroud)

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

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

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 
Run Code Online (Sandbox Code Playgroud)

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

test_logical_replication=> select pg_current_wal_lsn();  
 pg_current_wal_lsn  
--------------------  
 0/8EB83768     
Run Code Online (Sandbox Code Playgroud)

让我们看看pg_catalog.pg_stat_subscription订阅者上的复制过程中值如何变化:

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        
Run Code Online (Sandbox Code Playgroud)

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

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