惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园_首页
大猫的无限游戏
大猫的无限游戏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog
B
Blog RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
Engineering at Meta
Engineering at Meta
量子位
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Tailwind CSS Blog
Stack Overflow Blog
Stack Overflow Blog
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
U
Unit 42
aimingoo的专栏
aimingoo的专栏
博客园 - 叶小钗
博客园 - 【当耐特】
云风的 BLOG
云风的 BLOG
博客园 - Franky
博客园 - 聂微东

博客园 - MSSQL123

timescaledb 扩展 PostgreSQL 18+ 版本下源码编译安装 ubuntu24下 ext4和xfs类型磁盘以及PostgreSQL实例性能对比测试 PostgreSQL 18 新特性 skip scan,一个鸡肋的功能 PostgreSQL 17 流复制中开启逻辑复制槽同步,以及逻辑槽的故障转移 记一次PostgreSQL交叉表crosstab行转列导致的OOM TiDB 扩容与缩容 PostgreSQL 高可用集群 patroni 自动故障转移测试 Ubuntu 20 环境下 patroni 自动化安装,一分钟快速搭建 patroni 集群 Ubuntu 20 环境下 pg_auto_failover 自动化安装,一分钟快速搭建pg_auto_failover集群 PostgreSQL的消息队列扩展pgmq 磁盘IO延迟和队列深度的关系 TiDB 最小拓扑架构集群安装 prometheus 的 altermanager Silences静默告警,优雅地抑制告警 “世界上百分之99的博士和他们的论文都是垃圾”,原本以为这个观点偏激 pg_auto_failover 在多种场景下自动故障转移的验证 pgbouncer连接池设置与压力测试的最大连接数测试 pg_auto_failover 自动故障转移参数 博文阅读密码验证 - 博客园 pg_auto_failover 高可用中,PostgreSQL实例配置文件的加载步骤 pg_auto_failover集群monitor节点的高可用 prometheus监控Linux Server node_exporter代理安装和配置 prometheus监控windows window_exporter代理安装和配置 Prometheus 和 Grafana 监控 PostgreSQL 记一次MySQL binlog日志导致磁盘空间占满的问题 SQLServer 2019 标准版在虚拟机上无法充分利用CPU的问题诊断 Windows Failover Cluster集群中的EventId 1196错误日志 pg_auto_failover 环境变量导致的show命令错误 SqlServer 事务复制(transaction replication)的复制位点信息 SqlServer 事务复制的两个参数immediate_sync,allow_anonymous MySQL,SqlServer,PostgreSQL中,如何实现锁定一张表
PostgreSQL 逻辑复制中的同步和异步模式以及其表现
MSSQL123 · 2026-04-23 · via 博客园 - MSSQL123

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