decoderbufs is a logical decoding output plugin for PolarDB for PostgreSQL that serializes write-ahead logging (WAL) change events into Protocol Buffers (ProtoBuf) format. Use it when your downstream consumers—such as stream processors or data pipelines—expect binary ProtoBuf-encoded change events.
Prerequisites
Before you begin, ensure that you have:
A PolarDB for PostgreSQL cluster running a supported version (see Applicability)
The
wal_levelparameter set tologicalin the cluster parameters
Set wal_level in the console. For instructions, see Set cluster parameters. Changing this parameter restarts the cluster—plan your maintenance window accordingly.Applicability
decoderbufs is supported on the following PolarDB for PostgreSQL versions:
| PostgreSQL version | Minimum minor engine version |
|---|---|
| PostgreSQL 17 | 2.0.17.6.4.0 |
| PostgreSQL 16 | 2.0.16.9.9.0 |
| PostgreSQL 15 | 2.0.15.14.6.0 |
| PostgreSQL 14 | 2.0.14.5.1.0 |
| PostgreSQL 11 | 2.0.11.9.28.0 |
To check your minor engine version, run SHOW polardb_version; or view it in the console. If your version doesn't meet the requirement, upgrade the minor engine version.
The decoderbufs extension requires the following libraries:
| Library | Minimum version | Purpose |
|---|---|---|
| PostgreSQL | 9.6 | Database engine |
| Protobuf-c | 1.2 | Data serialization |
| PostGIS | 2.1 | Spatial geographic type support |
How it works
decoderbufs is loaded automatically—no manual setup is required. When wal_level is set to logical, PostgreSQL writes additional metadata to the WAL that enables logical decoding. decoderbufs reads this metadata and emits each row change as a ProtoBuf-encoded message.
A logical replication slot holds the WAL stream for a given consumer. Two functions let you interact with the slot:
pg_logical_slot_peek_changes— reads change events without advancing the WAL position. Use this to inspect events during debugging without consuming them.pg_logical_slot_get_changes— reads change events and advances the WAL position. Use this for normal consumption, where processed events should not be replayed.
Set up logical replication
Create a logical replication slot with decoderbufs as the output plugin.
SELECT * FROM pg_create_logical_replication_slot('decoderbufs_demo', 'decoderbufs');On the publisher instance, modify the table you want to replicate.
Inspect the WAL log without advancing the position (useful for debugging).
SELECT data FROM pg_logical_slot_peek_changes('decoderbufs_demo', NULL, NULL, 'debug-mode', '1');Consume the WAL changes and advance the position.
SELECT data FROM pg_logical_slot_get_changes('decoderbufs_demo', NULL, NULL, 'debug-mode', '1');Verify the current WAL position of the replication slot.
SELECT * FROM pg_replication_slots WHERE slot_type = 'logical';
Type mapping
decoderbufs maps PostgreSQL types to the following ProtoBuf fields:
| PostgreSQL type | decoderbufs field |
|---|---|
| BOOLOID | datum_boolean |
| INT2OID | datum_int32 |
| INT4OID | datum_int32 |
| INT8OID | datum_int64 |
| OIDOID | datum_int64 |
| FLOAT4OID | datum_float |
| FLOAT8OID | datum_double |
| NUMERICOID | datum_double |
| CHAROID | datum_string |
| VARCHAROID | datum_string |
| BPCHAROID | datum_string |
| TEXTOID | datum_string |
| JSONOID | datum_string |
| XMLOID | datum_string |
| UUIDOID | datum_string |
| TIMESTAMPOID | datum_string |
| TIMESTAMPTZOID | datum_string |
| BYTEAOID | datum_bytes |
| POINTOID | datum_point |
| PostGIS geometry | datum_point |
| PostGIS geography | datum_point |