decoder_raw

简介

decoder_raw 是逻辑解码插件之一,作为 output plugin,用于将数据库的 WAL(Write-Ahead Log)日志解析为原始数据结构。该插件主要用于开发调试场景,输出为原始的 tuple 数据,方便二次开发或定制数据同步逻辑。


配置说明

unvdbsvr.confpostgresql.conf 中设置以下参数以启用逻辑复制:

wal_level = logical
max_replication_slots = 1

重启数据库以生效

使用方法

创建逻辑复制槽

SELECT * FROM pg_create_logical_replication_slot('custom_slot', 'decoder_raw');

查看数据变更

SELECT * FROM pg_logical_slot_get_changes('custom_slot', NULL, NULL);

输出内容通常为包含 tuple 原始信息的文本或结构化原始内容,适用于开发者进行分析或调试。

删除复制槽

SELECT pg_drop_replication_slot('custom_slot');

示例

-- Create a replication slot
SELECT slot_name FROM pg_create_logical_replication_slot('custom_slot', 'decoder_raw');

-- DEFAULT case with PRIMARY KEY
CREATE TABLE aa (a int primary key, b text NOT NULL);
INSERT INTO aa VALUES (1, 'aa'), (2, 'bb');
-- Update of Non-selective column
UPDATE aa SET b = 'cc' WHERE a = 1;
-- Update of only selective column
UPDATE aa SET a = 3 WHERE a = 1;
-- Update of both columns
UPDATE aa SET a = 4, b = 'dd' WHERE a = 2;
DELETE FROM aa WHERE a = 4;
-- Have a look at changes with different modes.
-- In the second call changes are consumed to not impact the next cases.
SELECT data FROM pg_logical_slot_peek_changes('custom_slot', NULL, NULL, 'include_transaction', 'off');
SELECT data FROM pg_logical_slot_get_changes('custom_slot', NULL, NULL, 'include_transaction', 'on');
DROP TABLE aa;
SELECT pg_drop_replication_slot('custom_slot');