如何通过查看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
我确保所有的值都是相同的。
我说得对吗?或者是否有其他方法通过检查某些参数来确保复制工作?
发布于 2022-07-12 15:45:37
我在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 我将向表t_1中添加4000行,并查看发布服务器上发生了什么:
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服务器)。
https://dba.stackexchange.com/questions/314324
复制相似问题