PostgreSQL邏輯訂閱


測試環境:PostgreSQL 13.2

1、邏輯訂閱簡介

由於物理復制只能做到這個集群的復制,不能正對某個對象(表)進行復制,且物理復制的備庫只能讀,不能寫。相反,邏輯訂閱同時支持主備庫讀寫,且可以做到針對某個對象進行復制。有更高的靈活性。但是邏輯訂閱對主庫性能影響較大。

2、發布端配置

pg_hba.conf 文件中加入,允許備用庫訪問。

host    mydbname     postgres        192.168.6.180/24        trust

postgresql,conf文件中,將wal_level修改為:

wal_level =  logical

登錄發布端db,發布端角色必須具備replication權限,或超級用戶權限,本文采用postgres用戶。

postgres=#\c mydbname  //連接要操作的數據庫
mydbname=# create table t1(id int primary key, info text, crt_time timestamp); //創建被發布表格
mydbname=# create publication testpub1 for table t1; //創建發布對象
ALTER TABLE t1 OWNER TO myuser; //將跟用戶myuser增加訪問權限

查看當前庫有哪些發布:

mydbname=# select * from pg_publication;  
 pubname  | pubowner | puballtables | pubinsert | pubupdate | pubdelete 
----------+----------+--------------+-----------+-----------+-----------
 testpub1 |       10 | f            | t         | t         | t
(1 row)

3、配置訂閱端

創建subscription用戶,必須是超級用戶,本文使用postgres用戶。

postgres=#\c mydbname  //連接要操作的數據庫
mydbname=# create table t1(id int primary key, info text, crt_time timestamp); //創建被發布表格
ALTER TABLE t1 OWNER TO myuser; //將跟用戶myuser增加訪問權限
mydbname=# create subscription testsub1 connection 'hostaddr=192.168.6.180 port=5433 user=postgres dbname=mydbname' publication testpub1 with (enabled, create_slot, slot_name='sub1_from_pub1');

查詢有哪些訂閱

postgres=# select * from pg_subscription ;  
 subdbid | subname  | subowner | subenabled |                          subconninfo                           |  subslotname   | subsynccommit | subpublications 
---------+----------+----------+------------+----------------------------------------------------------------+----------------+---------------+-----------------
   16401 | testsub1 |       10 | t          | hostaddr=192.168.7.177 port=1921 user=postgres dbname=postgres | sub1_from_pub1 | off           | {testpub1}
(1 row)

postgres=# select * from pg_stat_subscription ;  
 subid | subname  | pid  | relid | received_lsn |      last_msg_send_time       |     last_msg_receipt_time     | latest_end_lsn |        latest_end_time        
-------+----------+------+-------+--------------+-------------------------------+-------------------------------+----------------+-------------------------------
 44943 | testsub1 | 7877 |       | 1/76119308   | 2021-04-10 13:20:07.497634+08 | 2021-04-11 01:23:39.104443+08 | 1/76119308     | 2021-04-10 13:20:07.497634+08
(1 row)

4、發布端數據庫插入測試

mydbname=# insert into t1 select t,md5(random()::text),clock_timestamp() from generate_series(1,1000) t;
INSERT 0 1000

5、訂閱端查看:

mydbname=# select count(*) from t1;
 count 
-------
  100
(1 row)
mydbname=# select * from t1 limit 10;
 id |               info               |          crt_time          
----+----------------------------------+----------------------------
  1 | 31422ff05d990455a4632bf97cbcda45 | 2021-04-10 13:21:57.780586
  2 | 4a3f42ed6421f89cdf8c09e221431520 | 2021-04-10 13:21:57.780713
  3 | 88423917787f7c99340668c324fc35d2 | 2021-04-10 13:21:57.78072
  4 | 11dd182301956e9458460fc3f33cc5d6 | 2021-04-10 13:21:57.780724
  5 | bd0a78455bc400b39a697148a564e9eb | 2021-04-10 13:21:57.780727
  6 | 6b3b36fd0fe55d49932b990809913744 | 2021-04-10 13:21:57.78073
  7 | 3e13b1856a5f5623acfe607de775f0c1 | 2021-04-10 13:21:57.780733
  8 | f7b51908bf88974a7532ce0ec760d585 | 2021-04-10 13:21:57.780736
  9 | f9002e38c2fb8a3ed6c0453d518815aa | 2021-04-10 13:21:57.780739
 10 | ea68f6d87051fb8fef4586c0a2c04f5c | 2021-04-10 13:21:57.780741
(10 rows)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM