PostgreSQL 解码插件
PostgreSQL 逻辑解码输出插件安装
| 从 Debezium 0.10 起,连接器支持使用 pgoutput 进行 PostgreSQL 10+ 逻辑复制流传输。这意味着不再需要逻辑解码输出插件,连接器可以直接从复制流中输出变更。 |
|---|
逻辑解码插件
逻辑解码是将数据库表中所有持久化变更提取为一种连贯、易于理解的格式的过程,无需深入了解数据库内部状态即可对其进行解释。
从 PostgreSQL 9.4 起,逻辑解码通过将预写日志(WAL)中的内容解码为应用特定的形式(如元组流或 SQL 语句)来实现,预写日志描述的是存储层面的变更。在逻辑复制的上下文中,槽(slot)代表一个变更流,可以按照变更在源服务器上发生的顺序重放到客户端。每个槽从单个数据库流出一系列变更。输出插件将预写日志内部表示形式中的数据转换为复制槽消费者所需的格式。插件使用 C 语言编写、编译,并安装在运行 PostgreSQL 服务器的机器上,它们使用了多个 PostgreSQL 特定的 API,详见 PostgreSQL 文档。
Debezium 的 PostgreSQL 连接器配合 Debezium 支持的逻辑解码插件之一使用:
| 为简化使用,Debezium 还提供了一个基于原版 PostgreSQL 服务器镜像的容器镜像,在其之上编译并安装这些插件。 |
|---|
| Debezium 逻辑解码插件仅在 Linux 机器上安装和测试过。在 Windows 及其他平台上可能需要不同的安装步骤 |
|---|
插件之间的差异
所有最新的差异都记录在一个测试套件的 Java 类中。
有关逻辑解码和输出插件的更多信息,可参阅:
安装
在当前的安装示例中,使用的是用于逻辑解码的 decoderbufs 输出插件。decoderbufs 输出插件会为每个数据库变更生成一条 Protobuf 消息。每条消息包含被更新表行的新/旧元组。该插件的编译和安装通过执行从 Debezium Dockerfile 中提取的相关命令来完成。
在执行这些命令之前,请确保当前用户拥有向 PostgreSQL lib 目录写入 decoderbufs 库的权限(在测试环境中,该目录为:/usr/lib64/pgsql/)。同时请注意,安装过程需要 PostgreSQL 工具 pg_config。请确认 PATH 环境变量已正确设置,以便能够找到该工具。如果没有,请相应地更新 PATH 环境变量。例如在测试环境中:
decoderbufs 安装命令
$ git clone https://github.com/debezium/postgres-decoderbufs -b v{debezium-version} --single-branch \
&& cd postgres-decoderbufs \
&& make && make install \
&& cd .. \
&& rm -rf postgres-decoderbufsdecoderbufs 安装输出
Cloning into 'postgres-decoderbufs'...
remote: Enumerating objects: 288, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 288 (delta 0), reused 1 (delta 0), pack-reused 284
Receiving objects: 100% (288/288), 91.62 KiB | 3.66 MiB/s, done.
Resolving deltas: 100% (131/131), done.
Note: switching to 'c9b00aa8c093fa77e08b256bb09d33069a30db86'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC -std=c11 -I/usr/local/include -I. -I./ -I/usr/include/pgsql/server -I/usr/include/pgsql/internal -D_GNU_SOURCE -I/usr/include/libxml2 -c -o src/decoderbufs.o src/decoderbufs.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC -std=c11 -I/usr/local/include -I. -I./ -I/usr/include/pgsql/server -I/usr/include/pgsql/internal -D_GNU_SOURCE -I/usr/include/libxml2 -c -o src/proto/pg_logicaldec.pb-c.o src/proto/pg_logicaldec.pb-c.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC -shared -o decoderbufs.so src/decoderbufs.o src/proto/pg_logicaldec.pb-c.o -L/usr/lib64 -Wl,-z,relro -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,--as-needed -lprotobuf-c
/usr/bin/mkdir -p '/usr/lib64/pgsql'
/usr/bin/mkdir -p '/usr/share/pgsql/extension'
/usr/bin/install -c -m 755 decoderbufs.so '/usr/lib64/pgsql/decoderbufs.so'
/usr/bin/install -c -m 644 .//decoderbufs.control '/usr/share/pgsql/extension/'在 Fedora 30+ 上安装
Debezium 同样为 Fedora 操作系统提供了 RPM 软件包。该软件包总是在 Debezium 正式发布之后进行更新。要使用该 RPM 包,只需执行标准的 Fedora 安装命令:
sudo dnf install postgres-decoderbufs$ sudo dnf -y install postgres-decoderbufs其余配置与下文所述相同。
PostgreSQL 服务器配置
decoderbufs 插件安装完成后,应对数据库服务器进行配置。
设置库、WAL 和复制参数
在 PostgreSQL 配置文件 postgresql.conf 的末尾添加以下各行,以便将该插件纳入共享库,并调整部分 WAL 和流复制设置。该配置摘自 postgresql.conf.sample。如果你另外安装了 shared_preload_libraries,可能需要对其进行修改。
postgresql.conf ,配置文件参数设置
############ REPLICATION ##############
# MODULES
shared_preload_libraries = 'decoderbufs' (1)
# REPLICATION
wal_level = logical (2)
max_wal_senders = 4 (3)
max_replication_slots = 4 (4)| 1 | 告诉服务器应在启动时加载 decoderbufs(插件名称在 decoderbufs 的 Makefile 中设置) |
|---|---|
| 2 | 告诉服务器应使用基于预写日志(WAL)的逻辑解码 |
| 3 | 告诉服务器处理 WAL 变更时最多使用 4 个独立进程 |
| 4 | 告诉服务器最多允许创建 4 个用于流式传输 WAL 变更的复制槽 |
Debezium 使用 PostgreSQL 的逻辑解码,而逻辑解码依赖复制槽。即使在 Debezium 停机期间,复制槽也能保证保留 Debezium 所需的全部 WAL。因此,密切监控复制槽非常重要,以避免占用过多磁盘空间,以及出现其他问题——例如当某个 Debezium 复制槽长期未被使用时,可能导致系统目录膨胀。更多详细信息,请参阅官方 Postgres 文档中关于这一主题的说明。
| 我们强烈建议阅读并理解官方文档中关于 PostgreSQL 预写日志的机制与配置的说明。 |
|---|
设置复制权限
只有拥有相应权限的数据库用户才能执行复制操作,并且只能针对已配置的主机数量进行复制。要为用户授予复制权限,请定义一个 PostgreSQL 角色,该角色至少具有 REPLICATION 和 LOGIN 权限。例如:
CREATE ROLE name REPLICATION LOGIN;| 超级用户默认同时拥有上述两个角色。 |
|---|
在 PostgreSQL 配置文件 pg_hba.conf 末尾添加以下内容,以配置数据库复制的客户端认证。PostgreSQL 服务器应允许在服务器主机和运行 Debezium PostgreSQL 连接器的主机之间进行复制。
请注意,此处的认证指的是数据库超级用户 postgres。如果您已创建了具有 REPLICATION 和 LOGIN 权限的其他用户,可以相应地进行修改。
pg_hba.conf,配置文件参数设置
############ REPLICATION ##############
local replication postgres trust (1)
host replication postgres 127.0.0.1/32 trust (2)
host replication postgres ::1/128 trust (3)| 1 | 告诉服务器允许 postgres 在本地(即服务器机器上)进行复制 |
|---|---|
| 2 | 告诉服务器允许 localhost 上的 postgres 使用 IPV4 接收复制变更 |
| 3 | 告诉服务器允许 localhost 上的 postgres 使用 IPV6 接收复制变更 |
| 有关网络掩码的更多信息,请参阅 PostgreSQL 文档。 |
|---|
评论
登录后参与评论
KnowForge