
瀚高數(shù)據(jù)庫
目錄
(相關資料圖)
環(huán)境
文檔用途
詳細信息
環(huán)境
系統(tǒng)平臺:Linux x86-64 Red Hat Enterprise Linux 7
版本:14
文檔用途
本文主要用于介紹pglogical的安裝配置
詳細信息
pglogical 2插件(后邊簡稱pglogical)使用發(fā)布/訂閱的模式為PostgreSQL提供了邏輯流復制的實現(xiàn)方式。pglogicla是基于BDR項目的一部分技術發(fā)展而來。
我們一般使用以下名稱來描述節(jié)點間的數(shù)據(jù)流,復用了之前的Slony技術解析:
Nodes(節(jié)點) - Postgresql數(shù)據(jù)庫實例Providers and Subscribers(發(fā)布者和訂閱者) - Nodes使用的角色Replication Set(復制集) -表的集合pglogical使用pg最新的內(nèi)核特性,因為使用上有一下版本限制:
發(fā)布者和訂閱者節(jié)點必要運行在PostgreSQL 9.4版本及以上復制源的篩選和沖突檢測只在PostgreSQL9.5版本及以上訂閱者端可以是Postgres-XL 9.5版本及以上支持的使用場景包括
可用于大版本升級。完整數(shù)據(jù)庫復制。使用復制集選擇性地復制表集。在發(fā)布者服務器端或訂閱者服務器端選擇性復制表行(row_filter)。在發(fā)布者端選擇性復制表列從多個上游服務器收集/合并數(shù)據(jù)架構(gòu)細節(jié):
pglogical工作在單庫級別而不是像流復制一樣的實例級別一個訂閱者端可以在不引起額外的磁盤寫負載的情況下同時為多個訂閱端傳輸數(shù)據(jù)一個發(fā)布者端可以合并來自多個源的更改并自動檢測與處理沖突。(多主機需求的某些方面)為了使用pglogical插件,發(fā)布者端和訂閱者端必須跑在Postgresql9.4及以上的版本中。
pglogical插件必須在發(fā)布者端跟訂閱者端同時安裝,必須在兩端執(zhí)行CREATE EXTENSIONpglogical
需要同步的表在發(fā)布者端和訂閱者端必須有相同的模式名稱及表名稱
需要同步的表在發(fā)布者和訂閱者端必須有相同的列,每列的數(shù)據(jù)類型必須相同。檢查約束、非空約束等也要相同,或者訂閱者端的限制比發(fā)布者端要弱一些。
表必須有相同的主鍵。不建議額外添加除主鍵之外的其他唯一約束。
pglogical提供rpm包安裝及源碼安裝兩種方式,rpm包安裝較為簡答,本文主要介紹源碼包的安裝方法。
pglogical源碼包的安裝同其他任何PostgreSQL的擴展安裝一樣使用PGXS。
確保包含PostgreSQL發(fā)行版中pg_config的目錄配置在PATH的環(huán)境變量中。如果你的環(huán)境中沒有安裝pg_config這個命令,你需要用你的包管理器來安裝對應版本的開發(fā)包。
下面以pg14.6為數(shù)據(jù)庫版本為大家演示pglogical插件安裝及單向數(shù)據(jù)同步搭建過程。
OS:redhat7.7CPU: X86_64
database:pg14.6
extension version:2.4.2
Providers:192.168.164.51
Subscribers:192.168.164.521、安裝擴展
發(fā)布端、訂閱端解壓縮安裝pglogical,下面為發(fā)布端的日志
[root@host1 opt]# tar -zxvf pglogical-REL2_4_2.tar.gz[root@host1 opt]# chown -R postgres:postgres pglogical-REL2_4_2[root@host1 opt]# su - postgres[postgres@host1 ~]$ cd /opt/pglogical-REL2_4_2[postgres@host1 pglogical-REL2_4_2]$ make clean[postgres@host1 pglogical-REL2_4_2]$ make[postgres@host1 pglogical-REL2_4_2]$ make install
2、修改數(shù)據(jù)庫參數(shù)及pg_hba文件,發(fā)布端訂閱端同時修改postgresql.conf文件修改內(nèi)容
wal_level = "logical"max_worker_processes = 10 # one per database needed on provider node # one per node needed on subscriber nodemax_logical_replication_workers = 10max_replication_slots = 10 # one per node needed on provider nodemax_wal_senders = 10 # one per node needed on provider nodeshared_preload_libraries = "pglogical"pg_hba.conf文件修改內(nèi)容,增加本文段免密登錄。# IPv4 local connections:host all all 127.0.0.1/32 trusthost all all 192.168.164.0/24 trust修改完成后重啟發(fā)布端及訂閱端數(shù)據(jù)庫生效 [postgres@host1 data]$ pg_ctl restart[postgres@host2 data]$ pg_ctl restart
3、創(chuàng)建擴展
發(fā)布端[postgres@host1 data]$ psql test1psql (14.6)Type "help" for help.test1=# create extension pglogical;CREATE EXTENSION訂閱端: [postgres@host2 data]$ psql test2 postgrespsql (14.6)Type "help" for help.test2=# create extension pglogical;CREATE EXTENSION
4、創(chuàng)建測試表及測試數(shù)據(jù)發(fā)布端創(chuàng)建測試表,模擬測試數(shù)據(jù)
test1=# \c test1 test1You are now connected to database "test1" as user "test1".test1=>test1=> create table t01(id int primary key,name text);CREATE TABLEtest1=> insert into t01 select n,"aaaaaa" from generate_series(1,1000) n;INSERT 0 1000test1=> create table t02(id int primary key,name text);CREATE TABLEtest1=> insert into t02 select n,"aaaaaa" from generate_series(1,1000) n;INSERT 0 1000test1=> create table t03(id int primary key,name text);CREATE TABLEtest1=> insert into t03 select n,"aaaaaa" from generate_series(1,1000) n;INSERT 0 1000訂閱端只創(chuàng)建測試表 [postgres@host2 ~]$ psql test2 test2test2=> create table t01(id int primary key,name text);CREATE TABLEtest2=> create table t02(id int primary key,name text);CREATE TABLEtest2=> create table t03(id int primary key,name text);CREATE TABLE
5、發(fā)布端創(chuàng)建節(jié)點,創(chuàng)建復制集,將要同步的表t01加入到復制集中
##使用超級用戶創(chuàng)建發(fā)布節(jié)點test1=> \c test1 postgresYou are now connected to database "test1" as user "postgres".test1=# select pglogical.create_node(node_name :="provider1",dsn :="host=192.168.164.51 port=5432 dbname=test1");create_node------------- 2976894835(1 row)##創(chuàng)建復制集test1=# select pglogical.create_replication_set("defalut");create_replication_set------------------------ 692406752(1 row)##增加表test1=# select pglogical.replication_set_add_table("default","public.t01");replication_set_add_table---------------------------t(1 row)##確定復制集表信息test1=# select * from pglogical.replication_set_table; set_id | set_reloid | set_att_list | set_row_filter-----------+------------+--------------+----------------290045701 | t01 | |(1 row)
6、訂閱端創(chuàng)建節(jié)點##使用超級用戶創(chuàng)建訂閱節(jié)點
test2=# \c test2 postgresYou are now connected to database "test2" as user "postgres".test2=#test2=# select pglogical.create_node(node_name :="subscriber1",dsn :="host=192.168.164.52 port=5432 dbname=test2");create_node------------- 330520249(1 row)test2=# 2022-12-09 16:53:06.188 CST [12715] LOG: manager worker [12715] at slot 0 generation 4 detaching cleanly2022-12-09 16:53:06.191 CST [12716] LOG: starting pglogical database manager for database test22022-12-09 16:53:07.195 CST [12717] LOG: manager worker [12717] at slot 1 generation 1 detaching cleanly##創(chuàng)建訂閱test2=# SELECT pglogical.create_subscription(subscription_name := "subscription1",provider_dsn := "host=192.168.164.51 port=5432 dbname=test1");create_subscription--------------------- 1763399739(1 row)test2=# 2022-12-09 16:56:18.755 CST [12744] LOG: manager worker [12744] at slot 2 generation 1 detaching cleanly2022-12-09 16:56:18.756 CST [12745] LOG: starting apply for subscription subscription12022-12-09 16:56:18.759 CST [12746] LOG: manager worker [12746] at slot 2 generation 2 detaching cleanly
7、核對訂閱端數(shù)據(jù)##查詢?nèi)龔埍硇畔ⅲ梢钥吹街挥屑尤氲綇椭萍谋淼臄?shù)據(jù)同步了過來
test2=# select count(*) from t01;count------- 1000(1 row)test2=# select count(*) from t02;count------- 0(1 row)test2=# select count(*) from t03;count------- 0(1 row)##發(fā)布端再插入1000條數(shù)據(jù),查閱訂閱端數(shù)據(jù)test1=# \c test1 test1You are now connected to database "test1" as user "test1".test1=> insert into t01 select n,"aaaaaa" from generate_series(1001,2000) n;INSERT 0 1000test1=> select count(*) from t01;count------- 2000(1 row)test2=# select count(*) from t01;count------- 2000(1 row)單向邏輯復制同步完成,增量數(shù)據(jù)及基量數(shù)據(jù)均成功。