decoderbufs
将WAL逻辑解码为ProtocolBuffer协议的消息
概览
| 扩展包名 | 版本 | 分类 | 许可证 | 语言 |
|---|---|---|---|---|
decoderbufs | 3.4.1 | ETL | MIT | C |
| ID | 扩展名 | Bin | Lib | Load | Create | Trust | Reloc | 模式 |
|---|---|---|---|---|---|---|---|---|
| 9650 | decoderbufs | 否 | 是 | 是 | 否 | 否 | 否 | - |
| 相关扩展 | pglogical wal2json decoder_raw test_decoding kafka_fdw pglogical_origin pglogical_ticker pg_failover_slots |
|---|
版本
| 类型 | 仓库 | 版本 | PG 大版本 | 包名 | 依赖 |
|---|---|---|---|---|---|
| EXT | PGDG | 3.4.1 | 1817161514 | decoderbufs | - |
| RPM | PGDG | 3.4.1 | 1817161514 | postgres-decoderbufs_$v | - |
| DEB | PGDG | 3.4.0 | 1817161514 | postgresql-$v-decoderbufs | - |
安装
您可以直接安装 decoderbufs 扩展包的预置二进制包,首先确保 PGDG 仓库已经添加并启用:
pig repo add pgdg -u # 添加 PGDG 仓库并更新缓存
使用 pig 或者是 apt/yum/dnf 安装扩展:
pig install decoderbufs; # 当前活跃 PG 版本安装
pig ext install -y decoderbufs -v 18 # PG 18
pig ext install -y decoderbufs -v 17 # PG 17
pig ext install -y decoderbufs -v 16 # PG 16
pig ext install -y decoderbufs -v 15 # PG 15
pig ext install -y decoderbufs -v 14 # PG 14
dnf install -y postgres-decoderbufs_18 # PG 18
dnf install -y postgres-decoderbufs_17 # PG 17
dnf install -y postgres-decoderbufs_16 # PG 16
dnf install -y postgres-decoderbufs_15 # PG 15
dnf install -y postgres-decoderbufs_14 # PG 14
apt install -y postgresql-18-decoderbufs # PG 18
apt install -y postgresql-17-decoderbufs # PG 17
apt install -y postgresql-16-decoderbufs # PG 16
apt install -y postgresql-15-decoderbufs # PG 15
apt install -y postgresql-14-decoderbufs # PG 14
预加载配置:
shared_preload_libraries = 'decoderbufs';
用法
一个 PostgreSQL 逻辑解码输出插件,将 WAL 变更序列化为 Protocol Buffers 格式,主要由 Debezium PostgreSQL 连接器用于变更数据捕获。
配置
在 postgresql.conf 中:
shared_preload_libraries = 'decoderbufs'
wal_level = logical
max_wal_senders = 8
max_replication_slots = 4
使用 SQL(调试模式)
-- 创建逻辑复制槽
SELECT * FROM pg_create_logical_replication_slot('decoderbufs_demo', 'decoderbufs');
-- 执行表修改
INSERT INTO my_table VALUES (1, 'test');
UPDATE my_table SET col = 'updated' WHERE id = 1;
-- 以调试文本模式查看变更
SELECT data FROM pg_logical_slot_peek_changes(
'decoderbufs_demo', NULL, NULL, 'debug-mode', '1');
-- 消费变更
SELECT data FROM pg_logical_slot_get_changes(
'decoderbufs_demo', NULL, NULL, 'debug-mode', '1');
-- 检查槽状态
SELECT * FROM pg_replication_slots WHERE slot_type = 'logical';
类型映射
| PostgreSQL 类型 | Protobuf 字段 |
|---|---|
| BOOL | datum_boolean |
| INT2, INT4 | datum_int32 |
| INT8, OID | datum_int64 |
| FLOAT4 | datum_float |
| FLOAT8, NUMERIC | datum_double |
| CHAR, VARCHAR, TEXT | datum_string |
| JSON, XML, UUID | datum_string |
| TIMESTAMP(TZ) | datum_string |
| BYTEA | datum_bytes |
| POINT, PostGIS | datum_point |
注意事项
- 对于 UPDATE/DELETE,需要适当设置 REPLICA IDENTITY
- 二进制 Protocol Buffer 输出由 Debezium Postgres Connector 消费
debug-mode选项提供人类可读的文本输出,用于 SQL 控制台测试- 编译需要
protobuf-c库和 PostGIS 开发包