簡單說明
postgreSql邏輯復制的原理這里不再贅述,度娘一下即可。這里只是對常用的語句做一些匯總和說明,以便日后查找時方便。
邏輯復制的概念
邏輯復制整體上采用的是一個發布訂閱的模型,訂閱者可以訂閱一個或者多個發布者, 發布者也可以被一個或者多個訂閱者訂閱,通常我們在做邏輯復制的通用步驟是:訂閱者首先獲取發布者數據庫上的數據快照并拷貝這部分存量數據,當這個快照復制執行完成之后,發布者數據庫上的變更數據就可以實時的發送給訂閱者,訂閱者也會順序處理這些增量數據,基于此邏輯復制可以保證事物的一致性,所以有時候也稱邏輯復制為事物復制。
前置條件
想要使用postgreSql的邏輯復制,需要PostgreSQL 10以上版本,并且需要將數據庫屬性wal_level配置為logical。
wal_level 包含三個選項:minimal、replica、logical,默認是replica。
wal_level = logical
常用語句匯總
//創建發布 定義發布別名 表可以設置多個 使用逗號分割
create publication test_sync_p for table table_sync;//刪除發布
drop publication test_sync_p;
//創建訂閱 定義訂閱別名 publication與發布的別名保持一致 配置發布數據庫鏈接信息
create subscription test_sync_s connection 'host=192.168.0.1 dbname=system user=postgres password=12345 port=5432' publication test_sync_p;//刪除訂閱
drop subscription test_sync_s;
//使用唯一索引設置復制標識 默認為主鍵
alter table table_sync replica identity using index index_name;
//設置訂閱槽為空 方便刪除訂閱
alter subscription test_sync_s set (slot_name = none)
//查詢publication發布的表
select * from pg_publication_tables;//查詢publication
select * from pg_publication;
//查詢槽
select * from pg_replication_slots;//刪除槽
select pg_drop_replication_slot('slot_name');