Postgresql发布与订阅

深入探索 PostgreSQL 13.8 的订阅与发布功能 订阅与发布(Publish/Subscribe)是一种数据同步机制,允许一个数据库(发布端)将其更改通知发送给其他数据库(订阅端)。这种机制在实时数据同步、数据备份、负载均衡等场景中发挥着重要作用。 阅读详情

重点

1、当Subscriber创建时会在对应的Publisher上创建一个slot,因为每一个订阅,都要消耗掉一个replication slot,需要消耗一个wal sender。
2、Publisher发生的改变都保存为wal文件,由walsender进程发送,然后Subscriber的apply进程应用后,才会删除对应的wal文件。所以断网恢复后可以自动同步。

配置

On the publisher side, wal_level must be set to logical, and max_replication_slots must be set to at least the number of subscriptions expected to connect, plus some reserve for table synchronization. And max_wal_senders should be set to at least the same as max_replication_slots plus the number of physical replicas that are connected at the same time.

The subscriber also requires the max_replication_slots to be set. In this case it should be set to at least the number of subscriptions that will be added to the subscriber. max_logical_replication_workers must be set to at least the number of subscriptions, again plus some reserve for the table synchronization. Additionally the max_worker_processes may need to be adjusted to accommodate for replication workers, at least (max_logical_replication_workers + 1). Note that some extensions and parallel queries also take worker slots from max_worker_processes.

关于Slot

As mentioned earlier, each (active) subscription receives changes from a replication slot on the remote (publishing) side. Normally, the remote replication slot is created automatically when the subscription is created using CREATE SUBSCRIPTION and it is dropped automatically when the subscription is dropped using DROP SUBSCRIPTION. In some situations, however, it can be useful or necessary to manipulate the subscription and the underlying replication slot separately. Here are some scenarios:

When creating a subscription, the replication slot already exists. In that case, the subscription can be created using the create_slot = false option to associate with the existing slot.

When creating a subscription, the remote host is not reachable or in an unclear state. In that case, the subscription can be created using the connect = false option. The remote host will then not be contacted at all. This is what pg_dump uses. The remote replication slot will then have to be created manually before the subscription can be activated.

When dropping a subscription, the replication slot should be kept. This could be useful when the subscriber database is being moved to a different host and will be activated from there. In that case, disassociate the slot from the subscription using ALTER SUBSCRIPTIONbefore attempting to drop the subscription.

When dropping a subscription, the remote host is not reachable. In that case, disassociate the slot from the subscription using ALTER SUBSCRIPTION before attempting to drop the subscription. If the remote database instance no longer exists, no further action is then necessary. If, however, the remote database instance is just unreachable, the replication slot should then be dropped manually; otherwise it would continue to reserve WAL and might eventually cause the disk to fill up. Such cases should be carefully investigated.

Replication slots provide an automated way to ensure that the master does not remove WAL segments until they have been received by all standbys, and that the master does not remove rows which could cause a recovery conflict even when the standby is disconnected.

复制的详细过程

Logical replication is built with an architecture similar to physical streaming replication (see Section 26.2.5). It is implemented by “walsender” and “apply” processes. The walsender process starts logical decoding (described in Chapter 48) of the WAL and loads the standard logical decoding plugin (pgoutput). The plugin transforms the changes read from WAL to the logical replication protocol (see Section 52.5) and filters the data according to the publication specification. The data is then continuously transferred using the streaming replication protocol to the apply worker, which maps the data to local tables and applies the individual changes as they are received, in correct transactional order.

The apply process on the subscriber database always runs with session_replication_role set to replica, which produces the usual effects on triggers and constraints.The logical replication apply process currently only fires row triggers, not statement triggers. The initial table synchronization, however, is implemented like a COPY command and thus fires both row and statement triggers for INSERT.
The initial data in existing subscribed tables are snapshotted and copied in a parallel instance of a special kind of apply process. This process will create its own temporary replication slot and copy the existing data. Once existing data is copied, the worker enters synchronization mode, which ensures that the table is brought up to a synchronized state with the main apply process by streaming any changes that happened during the initial data copy using standard logical replication. Once the synchronization is done, the control of the replication of the table is given back to the main apply process where the replication continues as normal.

The subscription apply process will run in the local database with the privileges of a superuser.

Privileges are only checked once at the start of a replication connection. They are not re-checked as each change record is read from the publisher, nor are they re-checked for each change when applied.

冲突解决

A conflict will produce an error and will stop the replication; it must be resolved manually by the user. Details about the conflict can be found in the subscriber’s server log.

如何监控

Because logical replication is based on a similar architecture as physical streaming replication, the monitoring on a publication node is similar to monitoring of a physical replication master (see Section 26.2.5.2).

The monitoring information about subscription is visible in pg_stat_subscription. This view contains one row for every subscription worker. A subscription can have zero or more active subscription workers depending on its state.

Normally, there is a single apply process running for an enabled subscription. A disabled subscription or a crashed subscription will have zero rows in this view. If the initial data synchronization of any table is in progress, there will be additional workers for the tables being synchronized.

缺点

Logical replication currently has the following restrictions or missing functionality. These might be addressed in future releases.

The database schema and DDL commands are not replicated. The initial schema can be copied by hand using pg_dump –schema-only. Subsequent schema changes would need to be kept in sync manually. (Note, however, that there is no need for the schemas to be absolutely the same on both sides.) Logical replication is robust when schema definitions change in a live database: When the schema is changed on the publisher and replicated data starts arriving at the subscriber but does not fit into the table schema, replication will error until the schema is updated. In many cases, intermittent errors can be avoided by applying additive schema changes to the subscriber first.

Sequence data is not replicated. The data in serial or identity columns backed by sequences will of course be replicated as part of the table, but the sequence itself would still show the start value on the subscriber. If the subscriber is used as a read-only database, then this should typically not be a problem. If, however, some kind of switchover or failover to the subscriber database is intended, then the sequences would need to be updated to the latest values, either by copying the current data from the publisher (perhaps using pg_dump) or by determining a sufficiently high value from the tables themselves.

TRUNCATE commands are not replicated. This can, of course, be worked around by using DELETE instead. To avoid accidental TRUNCATE invocations, you can revoke the TRUNCATE privilege from tables.

Large objects (see Chapter 34) are not replicated. There is no workaround for that, other than storing data in normal tables.

Replication is only possible from base tables to base tables. That is, the tables on the publication and on the subscription side must be normal tables, not views, materialized views, partition root tables, or foreign tables. In the case of partitions, you can therefore replicate a partition hierarchy one-to-one, but you cannot currently replicate to a differently partitioned setup. Attempts to replicate tables other than base tables will result in an error.

PostgreSQL的发布和订阅功能 从 PostgreSQL 9.0 版本开始,就可以使用发布和订阅功能来实现数据复制和同步。 发布和订阅的流程 阅读详情

相关推荐

PostgreSQL逻辑复制实战:从零配置发布订阅,同步新表也不怕

本文详细介绍了PostgreSQL逻辑复制的实战配置,从零开始搭建发布订阅系统,特别解决了新增表同步的常见需求。通过发布端和订阅端的逐步配置指南,帮助开发者实现关键业务数据的实时同步,提升数据管理效率。

weixin_30319153的博客 436

postgresql发布和订阅

发布和订阅使用了pg的逻辑复制的功能,通过发布端创建publication与表绑定,订阅端创建subscription同时会在发布端创建逻辑复制槽实现逻辑复制功能一个 发布者(Publisher) 上可以有多个发布,一个 订阅者(Subscriber) 上可以有多个 订阅。一个发布可被多个订阅者订阅,一个订阅只能订阅一个发布者,但可订阅同发布者上的多个不同发布。迁移,跨PostgreSQL大版本,跨操作系统平台进行复制。

Du.的博客 3933

MOSFET和IGBT栅极驱动器电路的基本原理学习(2)

1.PWM直接驱动 驱动主开关晶体管栅极的最简单方法是利用 PWM 控制器的栅极驱动输出,如图 (1)如 图 8中所示,PWM 控制器和 MOSFET 之间可能有较大距离。由于栅极驱动和接地环路迹线形成的环路,这个距离形成了寄生电感,从而降低了开关速度,并导致栅极驱动波形中形成振铃。为了降低与栅极驱动连接相关的电感,需要更宽的 PCB 迹线。 (2)直接栅极驱动的另一个问题是PWM 控制器的驱动电流能力有限。极少有集成电路能提供高于 1A 的峰值栅极驱动能力。 2.旁路电容的确定 (1)位置 如果是独立的

weixin_43732932的博客 5837

13.PG14逻辑复制总结(一)

文章目录前言一、pandas是什么?二、使用步骤1.引入库2.读入数据总结 前言 一、pandas是什么? 示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。 二、使用步骤 1.引入库 代码如下(示例): import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import warnings warnings.filterwarnings('

weixin_40068220的博客 3941

360加固DEX解密与ELF修复技术解析

本文深入解析360加固的DEX解密与ELF修复技术,详细介绍了其多层保护机制、Java层入口分析、Native层加载流程及反调试绕过技巧。通过RC4和zlib解密技术,结合Frida工具实现内存Dump和ELF文件修复,为逆向工程师提供实用解决方案。

weixin_29327525的博客 520

手把手教你配置UEFI Secure Boot:从密钥生成到系统验证(附避坑指南)

本文详细介绍了如何配置UEFI Secure Boot,从密钥生成到系统验证的全过程,包括PK、KEK等关键密钥的生成方法,以及常见问题的解决方案。通过实战案例和避坑指南,帮助用户有效提升系统启动安全性,防止恶意软件植入。

weixin_30776273的博客 348

canal同步mysql到PostgreSQL数据库自己编写代码的客户端实战

目录1. 准备工作1.1 active mysql查看1.2 mysql数据情况2. canal.deploy配置修改2.2 删除conf/example,建立新的example实例3. 同步代码的开发3.1 3.3 重复测试的脚本 canal高可用和mysql节点自动切换的部署,可以参考我的这篇文章 1. 准备工作 1.1 active mysql查看 因为我们部署了mysql节点自动切换,我们需要查看现在同步的是哪一台mysql服务器 先查看example_db实例运行的服务器 [zk: can

Bulut0907 2278

BambuStudio学习笔记:GCode 类

关键成员解析 核心配置组件 状态跟踪系统 功能模块集成 核心方法剖析 主输出管道 实现多阶段处理: 分层处理流程: 实现智能路径规划: 关键特性: 实现模板故障检测: 采用三级流水线: 支持扩展类型: 实现多通道输出: 采用: 实现: 支持: 支持模块化编译: 典型流程: 实现功能:

zzzkk2009的专栏 950

MySQL 到 ADB MySQL 实时数据同步实操分享

摘要:很多 DBA 同学经常会遇到要从一个数据库实时同步到另一个数据库的问题,同构数据还相对容易,遇上异构数据、表多、数据量大等情况就难以同步。我自己亲测了一种方式,可以非常方便地完成 MySQL 数据实时同步到ADB MySQL,跟大家分享一下,希望对你有帮助。 本次 MySQL 数据实时同步到 ADB MySQL大概只花了几分钟就完成。使用的工具是 Tapdata Cloud ,这个工具是永久免费的。 第一步:配置MySQL 连接 1.点击 Tapdata Cloud 操作后台左侧菜单栏的【连接管

努力是为了站在万人之中,成为别人的光 1万+

C/C++关于VScode报错“终端将被任务重用,按任意键关闭”的解决方案

这其实不是一种报错,有这个提示的原因是本地端口是被共享的,所以必须关闭一个进程才能执行另一个,只需要对 tasks.json文件做一些修改即可。 报错时候 此时的tasks.json为: { "version": "2.0.0", "command": "g++", "args": ["-g","${file}","-o","${fileDirname}/${fileBasenameNoExtension}.exe"], // 编译命令参数 "problemMatche

Jerry Man的博客 6435

云服务器安装ros教程

要在 CentOS 7 系统上安装 RouterOS,您需要将 RouterOS 安装为虚拟机或直接安装到物理硬盘上。这里提供的是将 RouterOS 安装到物理硬盘上的步骤,这将完全替换现有的 CentOS 7 系统。请确保您已经备份了所有重要数据。

qq_37662563的博客 2148

PostgreSQL内核源码分析 逻辑复制基本流程,发布订阅创建背后的故事

PostgreSQL中如何实现逻辑复制功能呢?我们从几个方面来逐层展开介绍,首先介绍一下逻辑复制的代码结构,再来看一下产生通知的流程,以及如何应用到备份。 本文就来分享一下逻辑复制的代码框架结构,在整体上对逻辑复制有初步的认识。

一个追逐梦想的码农 7655

在WIN10系统中安装TIA博途V18,重启后提示安装介质不可用,请插入DVD 或检查网络连接的解决办法

在WIN10系统中安装TIA博途V18,重启后提示安装介质不可用,请插入DVD 或检查网络连接的解决办法

Robot_PLC_HMI_Automation 1万+

PostgreSQL 数据库实时同步(通过发布与订阅)

现有一套业务库,想再创建一个中间库,保证中间库和业务库数据实时同步。

ayt007的博客 848

OpenCV-Python实战(10)——详解 OpenCV 轮廓检测

在计算机视觉领域,轮廓通常指图像中对象边界的一系列点。因此,轮廓通常描述了对象边界的关键信息,包含了有关对象形状的主要信息,该信息可用于形状分析与对象检测和识别。在本文中,将首次通过简单示例了解轮廓的基本概念,然后通过实际示例来了解如何检测和压缩轮廓,最后利用介绍如何利用图像矩描述检测到的轮廓属性。

盼小辉丶的博客 3万+

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

PostgreSQL逻辑复制:pgAdmin4配置发布与订阅实战

逻辑复制(Logical Replication)是PostgreSQL实现数据同步的高级特性,通过复制特定表的更改(INSERT/UPDATE/DELETE)实现跨数据库数据共享。本文基于pgAdmin4工具,从零开始演示如何配置发布(Publication)与订阅(Subscription),解决分布式系统中的数据一致性问题。 ## 核心概念与架构 逻辑复制基于**发布-订阅模型**,pu...

gitblog_00969的博客 469

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

[译]PostgreSQL-15逻辑复制发布/订阅的两阶段提交

即将推出的 PostgreSQL 15 引入了由富士通 OSS 团队与 PostgreSQL 开源社区合作添加的一项新功能,该功能允许在使用发布/订阅的逻辑复制中支持两阶段提交。让我们来看看如何使用它。 此功能支持创建允许对两阶段事务的复制进行解码的发布/订阅。我们还修改了逻辑解码插件 pgoutput 以支持所有必需的两阶段回调 。 启用两阶段提交时,准备好的事务在 PREPARE TRANSACTION 时发送给订阅者,订阅者也将其作为两阶段事务处理。 一、背景 PostgreSQL 14 已

小怪兽的博客 667

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

【Postgresql】在PGXL上测试逻辑复制发布订阅功能

修改wal_level = logical #wal_level = replica # minimal, replica, or logical wal_level = logical # minimal, replica, or logical 测试开始 CREATE TABLE customers ( login text PRIMARY KEY, full_name text NOT NULL, registration_date timestamptz NOT NULL DEFAU

BIG BOSS的博客 348

PostgreSQL数据库基于发布订阅的数据同步方案实现

要实现从两台源服务器(192.168.31.60和192.168.31.82)到目标服务器(192.168.31.118)的PostgreSQL数据增量备份,我们将使用PostgreSQL的逻辑复制功能。

字节熊猫 1309

PostgreSQL通知机制终极指南:如何实现高效的发布订阅模式

PostgreSQL通知机制是数据库系统中强大的实时通信功能,通过LISTEN和NOTIFY命令实现发布订阅模式。pq驱动作为纯Go语言的PostgreSQL数据库驱动,为开发者提供了完整的事件通知解决方案,让应用程序能够实时响应数据库状态变化。📢 ## 什么是PostgreSQL通知机制? PostgreSQL通知机制基于发布订阅模式,允许不同会话之间进行异步通信。当某个会话执行NOTIF

gitblog_01130的博客 450

探秘PG PubSub:基于PostgreSQL的高效发布/订阅工具

探秘PG PubSub:基于PostgreSQL的高效发布/订阅工具 在快速迭代的技术世界中,实时数据传输和事件驱动架构已经成为构建现代应用不可或缺的一部分。今天,我们要向大家推荐一个名为PG PubSub的开源项目,它巧妙地利用了PostgreSQL数据库的NOTIFY/LISTEN机制,为Node.js开发者提供了一种简洁高效的发布/订阅解决方案。 项目介绍 PG PubSub是一个基于Nod...

gitblog_00083的博客 479

Node-PG-PubSub: 使用PostgreSQL实现发布/订阅模式的Node.js库

Node-PG-PubSub: 使用PostgreSQL实现发布/订阅模式的Node.js库 1. 项目基础介绍 Node-PG-PubSub 是一个基于 PostgreSQL 的发布/订阅模式的 Node.js 库。该项目主要由 JavaScript 编写,利用 PostgreSQL 的 NOTIFY 和 LISTEN 功能来实现消息的发布与订阅。它允许开发者轻松地在 Node.js 应用中集成...

gitblog_00742的博客 299

PostgreSQL逻辑复制实战:从发布/订阅模型到CDC与数据集成

本文深入探讨PostgreSQL逻辑复制的实战应用,从发布/订阅模型到CDC与数据集成。通过详细的配置指南和实战案例,帮助开发者掌握逻辑复制的核心机制,包括发布端与订阅端的参数调优、权限配置、CDC架构搭建以及数据集成方案,提升数据同步效率与系统可靠性。

weixin_30564785的博客 461

探索PG PubSub:基于PostgreSQL的发布/订阅实现

探索PG PubSub:基于PostgreSQL的发布/订阅实现 在现代应用开发中,实时通信和事件驱动架构变得越来越重要。今天,我们将介绍一个强大的开源项目——PG PubSub,它利用PostgreSQL的NOTIFY/LISTEN功能,为开发者提供了一个高效、可靠的发布/订阅系统。 项目介绍 PG PubSub是一个基于PostgreSQL的发布/订阅实现,它通过利用PostgreSQL的NO...

gitblog_00968的博客 469

PostgreSQL 逻辑复制:CloudNativePG 发布与订阅功能

当数据库集群需要跨版本升级、数据分片或实时分析时,传统物理复制的局限性日益凸显。物理复制(Physical Replication)通过字节级复制实现高可用,但无法满足精细化数据同步需求。而逻辑复制(Logical Replication)作为 PostgreSQL 10+ 引入的革命性特性,通过**发布-订阅(Publish-Subscribe)** 模型实现表级数据同步,成为云原生环境下数据流...

gitblog_00717的博客 933
上一篇: Oracle对象批量编译
下一篇: Pycharm取消py脚本中SQL识别
DataCareer
博客等级 码龄15年 30粉丝 45原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值