使用 LSN 監控邏輯複製
如何通過查看 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我確保這些列中的所有值都相同。
我對麼 ?或者有沒有其他方法可以通過檢查一些參數來查看複製工作?
我在 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 伺服器)。