专注,勤学,慎思。戒骄戒躁,谦虚谨慎

just do it

导航

PostgreSQL 逻辑复制中的同步和异步模式以及其表现

PostgreSQL中,逻辑复制和物理复制一样,有同步和异常两种模式可选,不同的同步模式会对主节点上的写入有不同的影响以及表现形式,尤其是订阅库(从库)不可用的情况下,对主库的影响也完全不一样。

 

发布端创建发布

--创建复制用户以及表
create user logical_repl_user with password '123456';
alter user logical_repl_user with replication;
grant usage on schema public to logical_repl_user;
grant all on t1 to logical_repl_user;

select  pg_drop_replication_slot('mydb01_logic_repl_slot01');

create table t1
(
	c1 int generated always as identity primary key,
	c2 varchar(100),
	c3 timestamptz
);

insert into t1(c2,c3) values('aaa',now());
insert into t1(c2,c3) values('bbb',now());

select * from t1;
/*
c1|c2 |c3                              |
--+---+--------------------------------+
 1|aaa|2026-04-23 12:33:42.431825 +0800|
 2|bbb|2026-04-23 12:33:43.364887 +0800|
*/

--创建发布
create publication mydb01_pulication_01 for table t1 with (publish = 'insert,update');

--注意设置synchronous_standby_names名字,这里为了简单起见设置为*,也就所有副本都为同步模式
ALTER SYSTEM SET synchronous_standby_names = '*';
SELECT pg_reload_conf();

 

订阅端创建订阅

create table t1
(
	c1 int generated always as identity primary key,
	c2 varchar(100),
	c3 timestamptz
);

create subscription mydb01_subscription 
connection 'host=192.168.152.122 port=9400 dbname=mydb01 user=logical_repl_user  password=123456' 
publication mydb01_pulication_01 
with 
(
    create_slot = true,--这里创建slot的话,在订阅删除后,主节点上会自动删除该复制槽
    slot_name = 'mydb01_logic_repl_slot01',
    --并行复制,配合max_parallel_apply_workers_per_subscription参数
    streaming = parallel,
    --默认是异步复制
    --synchronous_commit = 'on'
    synchronous_commit = 'remote_apply',  -- 等待应用到订阅者,主节点synchronous_standby_names设置为*,监听所有客户端(订阅者)
    copy_data = true
);
    

 

同步逻辑复制测试

测试同步模式在正常和异常情况下的表现,可见同步模式下,订阅节点故障会导致主节点(发布端)事务无法提交。如果是异步逻辑复制,则订阅端的异常不会对发布端造成任何影响,该场景测试略。

--case 1,主从都正常,主节点顺利写入数据,经验证数据也同步到订阅节点
insert into t1(c2,c3) values('ccc',now());

--case 2,强制关闭订阅节点模拟订阅节点故障,因为是同步模式,主节点写入时无法得到从节点的反馈,写入被阻塞
insert into t1(c2,c3) values('ddd',now());

image

image

 

subscription的synchronous_commit参数

image

这里不太好理解,笔者也反复思考了很久,这段英文要表达的含义
A different setting might be appropriate when doing synchronous logical replication. The logical replication workers report the positions of writes and flushes to the publisher, and when using synchronous replication, the publisher will wait for the actual flush. This means that setting synchronous_commit for the subscriber to off when the subscription is used for synchronous replication might increase the latency for COMMIT on the publisher. In this scenario, it can be advantageous to set synchronous_commit to local or higher.

1,逻辑复制同步模式下create subscription with(synchronous_commit = 'remote_apply'),
2,由于是同步订阅,所以发布端要等订阅事务的提交
3,订阅自己异步提交(PostgreSQL的postgresql.conf配置文件中的synchronous_commit为off),会增加事务提交的时间,从而会导致发布端“等”的时间更久

 

 

 

截止20260423,chatgpt还是存在比较严重的“幻觉”,一直说逻辑复制没有同步模式。

image

posted on 2026-04-23 14:56  MSSQL123  阅读(7)  评论(0)    收藏  举报