首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用LSN监视逻辑复制

使用LSN监视逻辑复制
EN

Database Administration用户
提问于 2022-07-12 14:37:31
回答 1查看 1.9K关注 0票数 0

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

我确保所有的值都是相同的。

我说得对吗?或者是否有其他方法通过检查某些参数来确保复制工作?

EN

回答 1

Database Administration用户

回答已采纳

发布于 2022-07-12 15:45:37

我在Postgres 12上使用了复制。

在发布方,您可以查看以下几点内容:

代码语言:javascript
复制
pg_catalog.pg_publication;
pg_catalog.pg_publication_tables;
pg_current_wal_lsn();

我将创建一个出版物"test_publication“,其中包含两个表:t_1t_2。我将不讨论先决条件(用户、角色等)。

代码语言:javascript
复制
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)  

在订户方面:

代码语言:javascript
复制
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:向发行者报告上次预写日志位置的时间。

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

出版者一方:

代码语言:javascript
复制
test_logical_replication=> select pg_current_wal_lsn();  
 pg_current_wal_lsn  
--------------------  
 0/8EB83768     

这显示了WAL文件中我们现在所在的位置,然后再开始一个新的插入。

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

代码语言:javascript
复制
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        

我将向表t_1中添加4000行,并查看发布服务器上发生了什么:

代码语言:javascript
复制
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值是如何变化的:

代码语言:javascript
复制
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_timelast_msg_receipt_time列中的时间差可以提供发布服务器和订阅服务器之间的延迟信息。在这种情况下,这两个服务器位于同一个数据中心的不同子网上。

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

票数 3
EN
页面原文内容由Database Administration提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://dba.stackexchange.com/questions/314324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档