Schema   |                Name                 | Result data type |                            Argument data types                            |  Type
------------+-------------------------------------+------------------+---------------------------------------------------------------------------+--------
pg_catalog | pg_create_logical_replication_slot | record | slot_name name, plugin name, OUT slot_name text, OUT xlog_position pg_lsn | normal
pg_catalog | pg_create_physical_replication_slot | record | slot_name name, OUT slot_name name, OUT xlog_position pg_lsn | normal

1.需求

遇到一种需要将一部分表通过logical_tool用逻辑复制槽的方式同步到kaffka,之前没有使用过,因此对逻辑复制槽进行了了解。

2.资料

pg_create_logical_replication_slot方法配置两个参数:slot_name、plugin_name,例如:

pg_create_logical_replication_slot('test_logical_slot', 'wal2json');

这样就可以通过wal2json这个工具来解析wal日志,也可以用test_decoding自带的解析工具来做,有时间进行详细了解····

--201906027有时间了:

test_decoding自带的,直接在psql中安装插件:create extension test_decoding;

wal2json需要下载编译安装:

安装:
$ git clone https://github.com/eulerto/wal2json.git
$ cd wal2json
# Make sure your path includes the bin directory that contains the correct `pg_config`
$ PATH=/path/to/pg/bin:$PATH
$ USE_PGXS=1 make
$ USE_PGXS=1 make install 修改wal日志级别:
wal_level = logical
max_replication_slots = 5
max_wal_senders = 5 参数:
include-xids: add xid to each changeset. Default is false.
include-timestamp: add timestamp to each changeset. Default is false.
include-schemas: add schema to each change. Default is true.
include-types: add type to each change. Default is true.
include-typmod: add modifier to types that have it (eg. varchar(20) instead of varchar). Default is true.
include-type-oids: add type oids. Default is false.
include-not-null: add not null information as columnoptionals. Default is false.
pretty-print: add spaces and indentation to JSON structures. Default is false.
write-in-chunks: write after every change instead of every changeset. Default is false.
include-lsn: add nextlsn to each changeset. Default is false.
include-unchanged-toast (deprecated): add TOAST value even if it was not modified. Since TOAST values are usually large, this option could save IO and bandwidth if it is disabled. Default is true.
filter-tables: exclude rows from the specified tables. Default is empty which means that no table will be filtered. It is a comma separated value. The tables should be schema-qualified. *.foo means table foo in all schemas and bar.* means all tables in schema bar. Special characters (space, single quote, comma, period, asterisk) must be escaped with backslash. Schema and table are case-sensitive. Table "public"."Foo bar" should be specified as public.Foo\ bar.
add-tables: include only rows from the specified tables. Default is all tables from all schemas. It has the same rules from filter-tables.
format-version: defines which format to use. Default is 1. 常用参数是,pretty-print 1 修改pg访问权限,可选:
local replication myuser trust 启动:
$ pg_recvlogical -d postgres --slot test_slot --create-slot -P wal2json
$ pg_recvlogical -d postgres --slot test_slot --start -o pretty-print=1 -f - 停止直接ctrl+c 删除:
pg_recvlogical -d postgres --slot test_slot --drop-slot 测试脚本:
$ cat /tmp/example2.sql
CREATE TABLE table2_with_pk (a SERIAL, b VARCHAR(30), c TIMESTAMP NOT NULL, PRIMARY KEY(a, c));
CREATE TABLE table2_without_pk (a SERIAL, b NUMERIC(5,2), c TEXT); SELECT 'init' FROM pg_create_logical_replication_slot('test_slot', 'wal2json'); BEGIN;
INSERT INTO table2_with_pk (b, c) VALUES('Backup and Restore', now());
INSERT INTO table2_with_pk (b, c) VALUES('Tuning', now());
INSERT INTO table2_with_pk (b, c) VALUES('Replication', now());
DELETE FROM table2_with_pk WHERE a < 3; INSERT INTO table2_without_pk (b, c) VALUES(2.34, 'Tapir');
-- it is not added to stream because there isn't a pk or a replica identity
UPDATE table2_without_pk SET c = 'Anta' WHERE c = 'Tapir';
COMMIT; SELECT data FROM pg_logical_slot_get_changes('test_slot', NULL, NULL, 'pretty-print', '1');
SELECT 'stop' FROM pg_drop_replication_slot('test_slot');

3.类似工具

另外关于逻辑槽的工具,在阿里云中看到他们用逻辑复制槽的方式进行数据库迁移:
1.安装他们的ali_decoding工具;
2.创建逻辑复制槽:SELECT * FROM pg_create_logical_replication_slot('replication_slot_test', 'ali_decoding');
然后配置连接参数,开始迁移。

4.应对集群切换

而客户这边有一些不一样的地方,为了适应集群的切换,他们将逻辑槽所在的目录,pglogical设置为共享目录,当主节点被切换走了,在新的主节点重新启动逻辑复制槽(通过守护进程实现,同时,他们还需要重启数据库才能让共享目录被识别,不能简单的promote方式提示从节点为主节点)。

PostgreSQL逻辑复制槽的更多相关文章

  1. PostgreSQL逻辑复制使用记录

    之前逻辑复制刚刚出来的时候就使用过,但是没有进行整理,这次一个项目需要逻辑复制的自动迁移,再次拾起逻辑复制. 在此之前有两个疑问: 1)同一个表,既有流复制,又有逻辑复制,这样数据会有两份吗? --不 ...

  2. PostgreSQL逻辑复制之pglogical篇

    PostgreSQL逻辑复制之slony篇 一.pglogical介绍 pglogical 是 PostgreSQL 的拓展模块, 为 PostgreSQL 数据库提供了逻辑流复制发布和订阅的功能. ...

  3. Windows 环境搭建 PostgreSQL 逻辑复制高可用架构数据库服务

    本文主要介绍 Windows 环境下搭建 PostgreSQL 的主从逻辑复制,关于 PostgreSQl 的相关运维文章,网络上大多都是 Linux 环境下的操作,鲜有在 Windows 环境下配置 ...

  4. PostgreSQL逻辑复制解密

    在数字化时代的今天,我们都认同数据会创造价值.为了最大化数据的价值,我们不停的建立着数据迁移的管道,从同构到异构,从关系型到非关系型,从云下到云上,从数仓到数据湖,试图在各种场景挖掘数据的价值.而在这 ...

  5. PostgreSQL逻辑复制之slony篇

    Slony是PostgreSQL领域中最广泛的复制解决方案之一.它不仅是最古老的复制实现之一,它也是一个拥有最广泛的外部工具支持的工具,比如pgAdmin3.多年来,Slony是在PostgreSQL ...

  6. PostgreSQL逻辑复制到kafka-实践

    kafka 安装 wget http://mirror.bit.edu.cn/apache/kafka/2.3.0/kafka_2.12-2.3.0.tgz cp kafka_2.12-2.0.1.t ...

  7. Windows 环境搭建 PostgreSQL 物理复制高可用架构数据库服务

    PostgreSQL 高可用数据库的常见搭建方式主要有两种,逻辑复制和物理复制,上周已经写过了关于在Windows环境搭建PostgreSQL逻辑复制的教程,这周来记录一下 物理复制的搭建方法. 首先 ...

  8. postgresql从库搭建--逻辑复制

    1 物理复制及逻辑复制对比 前文做了PostgreSQL物理复制的部署,其有如下主要优点 物理层面完全一致,是主要的复制方式,其类似于Oracle的DG 延迟低,事务执行过程中产生REDO recor ...

  9. 跨 PostgreSQL 大版本复制怎么做?| 逻辑复制

    当需要升级PostgreSQL时,可以使用多种方法.为了避免应用程序停机,不是所有升级postgres的方法都适合,如果避免停机是必须的,那么可以考虑使用复制作为升级方法,并且根据方案,可以选择使用逻 ...

随机推荐

  1. JAVA 对象内存结构

    JAVA对象内存结构 HotSpot虚拟机中,对象在内存中存储的布局可以分为三块区域:对象头(Header).实例数据(Instance Data)和对齐填充(Padding). 对象头 markWo ...

  2. HTML基础之DOM常用操作

    DOM(Document Object Model ),文档对象模型,主要用于对HTML和XML文档的内容进行操作. 一.查找节点 直接获取标签 document.getElementById('i1 ...

  3. Spark Core (一) 什么是RDD的Transformation和Action以及Dependency(转载)

    1. Spark的RDD RDD(Resilient Distributed Datasets),弹性分布式数据集,是对分布式数据集的一种抽象. RDD所具备5个主要特性: 一组分区列表 计算每一个数 ...

  4. Android 压力测试工具Monkey

    原文地址http://www.syhm52.com/tools/17.html 一.Monkey定义探索软件测试工具有哪些,本文主要介绍Monkey工具.Monkey测试是Android平台自动化测试 ...

  5. hdu5012 圆环相交面积

    题中给了 两个同心圆, 一个大圆一个小圆,然后再给了一个大圆一个小圆也是同心圆,求这两个圆环相交的面积,用两个大圆面积减去两倍大小圆面积交加上两个小圆面积交,就ok了 这里算是坑明白了 使用acos的 ...

  6. hdu5073 贪心

    这题说的是给了 n个值每个值 然后 他们的品均值 作为中点 然后每个点到中点的均值的平方 和最小值是多少 有 k 个点可以重新 放过位置 , 这样我们 应该 会选 最近的那个 n-k个点 然后 取他们 ...

  7. git使用多个SSH公钥信息

    常常在开发环境存在多个git库,比如官方的github.公司搭建的gitlab.自己的私人库等等多个git库,为了方便使用,git需要配置多个SSH公钥信息. 在centos7.5下,进入用户目录,以 ...

  8. 同样的输入,为什么Objects.hash()方法返回的hash值每次不一样?

    背景 开发过程中发现一个问题,项目中用Set保存AopMethod对象用于去重,但是发现即使往set中添加相同内容的对象,每次也能够添加成功. AopMethod类的部分代码如下: public cl ...

  9. git commit的规范

    https://www.yuque.com/fe9/basic/nruxq8#6c228def 制定一个 git commit 信息的提交规范是开发团队工作流必不可少的环节.试想一下,如果查看主分支上 ...

  10. MyBatis小案例完善增强

    https://blog.csdn.net/techbirds_bao/article/details/9233599 上链接为一个不错的Mybatis进阶博客 当你把握时间,时间与你为伍. 将上一个 ...