PostgreSQL 逻辑解码输出插件安装

本文档描述了从 PostgreSQL 流式传输数据更改所需的数据库设置。这包括适用于数据库本身的配置以及 decoderbufs 逻辑解码输出插件的安装。安装和测试在以下环境/配置中执行

从 Debezium 0.10 开始,连接器支持 PostgreSQL 10+ 的逻辑复制流,使用pgoutput。这意味着不再需要逻辑解码输出插件,并且连接器可以直接从复制流中发出更改。

逻辑解码插件

逻辑解码是将数据库表的所有持久性更改提取为一种连贯、易于理解的格式的过程,无需详细了解数据库的内部状态即可进行解释。

从 PostgreSQL 9.4 开始,逻辑解码通过解码写前日志(WAL)的内容来实现,WAL 描述了存储层面的更改,并将其解码为应用程序特定的形式,例如元组流或 SQL 语句。在逻辑复制的上下文中,一个槽代表一个可按原始服务器上发生的顺序重放到客户端的更改流。每个槽都从单个数据库流式传输一系列更改。输出插件将数据从写前日志的内部表示转换为复制槽消费者所需的格式。插件是用 C 编写的,经过编译并在运行 PostgreSQL 服务器的机器上安装,它们使用许多 PostgreSQL 特定的 API,如 PostgreSQL 文档所述。

Debezium 的 PostgreSQL 连接器与 Debezium 支持的逻辑解码插件之一配合使用,

Protobuf 格式或 逻辑复制格式编码更改。

为了简化,Debezium 还提供了一个基于纯 PostgreSQL 服务器镜像的容器镜像,在其之上编译和安装插件。

Debezium 逻辑解码插件仅在Linux机器上安装和测试过。对于 Windows 和其他平台,可能需要不同的安装步骤。

插件之间的差异

所有最新的差异都跟踪在一个测试套件的 Java 类中。

有关逻辑解码和输出插件的更多信息,请参阅

安装

在当前的安装示例中,使用了用于逻辑解码的 decoderbufs 输出插件。decoderbufs 输出插件为每个数据库更改生成一个 Protobuf 消息。每条消息都包含更新的表行的新/旧元组。插件的编译和安装是通过执行从 Debezium Dockerfile 中提取的相关命令来完成的。

在执行命令之前,请确保用户具有将 decoderbufs 库写入 PostgreSQL lib 目录的权限(在测试环境中,目录是:/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-decoderbufs
decoderbufs 安装输出
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 -y install postgres-decoderbufs

其余配置与下面描述的相同。

PostgreSQL 服务器配置

安装完 decoderbufs 插件后,应配置数据库服务器。

设置库、WAL 和复制参数

postgresql.conf PostgreSQL 配置文件末尾添加以下行,以便将插件包含在共享库中,并调整一些 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 告诉服务器使用逻辑解码与写前日志
3 告诉服务器最多使用 4 个独立的进程来处理 WAL 更改
4 告诉服务器允许创建最多 4 个复制槽来流式传输 WAL 更改

Debezium 使用 PostgreSQL 的逻辑解码,它使用复制槽。复制槽保证即使在 Debezium 出现故障期间,也能保留 Debezium 所需的所有 WAL。因此,密切监控复制槽以避免过多的磁盘消耗以及其他可能发生的状况(例如,如果 Debezium 槽长时间未使用而导致目录膨胀)非常重要。有关更多信息,请参阅关于此主题的官方 Postgres 文档。

我们强烈建议阅读并理解关于 PostgreSQL 写前日志机制和配置的官方文档

设置复制权限

复制只能由具有适当权限的数据库用户执行,并且仅限于配置数量的主机。为了授予用户复制权限,请定义一个至少具有 REPLICATIONLOGIN 权限的 PostgreSQL 角色。例如

CREATE ROLE name REPLICATION LOGIN;

超级用户默认拥有上述两个角色。

pg_hba.conf PostgreSQL 配置文件末尾添加以下行,以便配置数据库复制的客户端认证。PostgreSQL 服务器应允许在服务器机器和运行 Debezium PostgreSQL 连接器的宿主机之间进行复制。

请注意,认证指的是数据库超级用户 postgres。如果创建了其他具有 REPLICATIONLOGIN 权限的用户,您可以相应地进行更改。

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 文档