本篇关于逻辑复制实现双写的案例,本来准备了3个环境,分别是306、501和505,奈何在5版本向3版本订阅的时候,出现了报错,但也将整个过程都记录下来吧。
环境准备
节点信息
MogDB=# select version(); version------------------------------------------------------------------------------------------------------------------------------------------------------(MogDB 5.0.5 build b77f1a82) compiled at 2023-12-08 03:11:47 commit 0 last mr 1804 on x86_64-unknown-linux-gnu, compiled by g++ (GCC) 7.3.0, 64-bit
(1 row)MogDB=# show wal_level ;wal_level
-----------logical
(1 row)MogDB=# select version();version
------------------------------------------------------------------------------------------------------------------------------------------------------(MogDB 5.0.1 build ae6d2ada) compiled at 2023-08-16 09:07:43 commit 0 last mr 1804 on x86_64-unknown-linux-gnu, compiled by g++ (GCC) 7.3.0, 64-bit
(1 row)MogDB=# show wal_level ;wal_level
-----------logical
(1 row)MogDB=# select version(); version------------------------------------------------------------------------------------------------------------------------------------------------------(MogDB 3.0.6 build 0e82b772) compiled at 2023-09-08 03:05:33 commit 0 last mr 1801 on x86_64-unknown-linux-gnu, compiled by g++ (GCC) 7.3.0, 64-bit
(1 row)MogDB=# show wal_level ;wal_level
-----------logical
(1 row)
用户权限 (所有节点)
--创建用户
create user logicalrep REPLICATION OPRADMIN encrypted password 'logicalrep@123';--配置白名单
host replication logicalrep 172.20.22.1/24 sha256
语句准备
--测试表(所有节点)
create table logicaltest(id serial, col1 text, ctime timestamp);
alter table logicaltest REPLICA IDENTITY full;--初始数据(源)
insert into logicaltest select generate_series(1,5),md5(random()),now();--发布者mypub506(在505实例,发布者名字应该叫505)
create publication mypub506 for table logicaltest;--订阅者mysub506(在501实例)
create subscription mysub506 connection 'dbname=postgres host=172.20.22.218 user=logicalrep password=logicalrep@123 port=25001' publication mypub506;--发布者mypub501(在501实例)
create publication mypub501 for table logicaltest;--订阅者mysub501(在505实例)
create subscription mysub501 connection 'dbname=postgres host=172.20.22.220 user=logicalrep password=logicalrep@123 port=26001' publication mypub501;--订阅者mysub501(在306实例)
create subscription mysub501 connection 'dbname=postgres host=172.20.22.220 user=logicalrep password=logicalrep@123 port=26001' publication mypub501;
复制测试
用户访问测试
订阅发布信息检查
目前三个节点都是干净的,没有订阅发布
建立发布订阅关系
506实例初始化数据,并创建发布者
501创建订阅者,发布者是mypub506
5.0.x版本支持了复制历史数据
501创建发布者
506创建订阅者
双写测试
在501节点上插入数据11-15
数据已经同步到506,且在506上再次插入数据1-5,发现数据已经正常写入且同步到501
双写分析
逻辑复制实现双写,没有造成数据循环的原因在于record_origin这个标记,这与PG16中的WITH (ORIGIN = NONE)异曲同工。
REDO @ 1B/312EC090; LSN 1B/312EC100: prev 1B/312EC050; xid 1233997; term 1; len 72; total 111; crc 4149308913; desc: Transaction - XLOG_XACT_COMMIT commit: 2024-04-30 18:28:45.671021 CST; csn:1049046; RecentXmin:1233996; origin: node 1, lsn C/2EE68978, at 2024-04-30 18:42:35.911074 CSTSYSID 0; record_origin 1; max_block_id 4294967295; readSegNo 6961; readOff 3063808; readPageTLI 0; curReadSegNo 0; curReadOff 0 latestPagePtr 1B/312EC000; latestPageTLI 1; currRecPtr 1B/312EC090PRIVATE @1B/31000000-1B/32000000; TLI 1; endptr_reached 0MAINDATA main_data_len 72; main_data_bufsz 72
mog_xlogdump: FATAL: error in WAL record at 1B/3141D9E0: invalid record length at 1B/3141DA38: wanted 32, got 0
问题汇总
高版本5.0.x无法订阅低版本3.0.x
LOG: received wal replication command: IDENTIFY_MODE
LOG: received wal replication command: SELECT 1 FROM pg_catalog.pg_publication t WHERE t.pubname IN ('mypub3')
FATAL: replication command parser returned 1
本文作者:高云龙