Principle:2ndQuadrant Pglogical Logical Decoding
| Knowledge Sources | |
|---|---|
| Domains | Database, Replication, Change_Data_Capture |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Mechanism that transforms database write-ahead log (WAL) records into a stream of logical change events by registering callback functions that filter, decode, and encode row-level modifications.
Description
Logical decoding is the PostgreSQL framework that allows output plugins to intercept WAL records and transform them into a consumer-defined format. It bridges the gap between the physical WAL stream (which contains page-level changes in binary format) and logical replication (which requires row-level INSERT/UPDATE/DELETE events).
The output plugin registers callbacks for five events: startup (slot creation/replication begin), begin (transaction start), change (individual row modification), commit (transaction end), and shutdown. Additionally, an origin filter callback determines whether changes originating from specific replication origins should be forwarded.
Logical decoding solves the problem of change data capture (CDC): extracting meaningful data changes from the low-level WAL without requiring triggers on every table. It is the foundation of all logical replication in PostgreSQL, including pglogical's custom output plugin.
Usage
Use logical decoding when:
- Building a custom logical replication system
- Implementing change data capture (CDC) for external consumers
- Streaming changes to message queues, caches, or analytics systems
- Implementing an output plugin with a custom wire protocol
Theoretical Basis
The logical decoding framework processes WAL through a reorder buffer that reconstructs transactions in commit order from the interleaved WAL stream.
Callback registration:
// Abstract plugin initialization (NOT real implementation)
void _PG_output_plugin_init(OutputPluginCallbacks *cb) {
cb->startup_cb = decode_startup; // Slot creation, option parsing
cb->begin_cb = decode_begin; // Transaction start
cb->change_cb = decode_change; // Row-level change
cb->commit_cb = decode_commit; // Transaction commit
cb->filter_by_origin_cb = origin_filter; // Origin filtering
cb->shutdown_cb = decode_shutdown; // Cleanup
}
Change processing pipeline:
Failed to parse (syntax error): {\displaystyle \text{WAL} \xrightarrow{\text{reorder}} \text{ReorderBufferTXN} \xrightarrow{\text{begin_cb}} \text{stream start} \xrightarrow{\text{change_cb} \times N} \text{row changes} \xrightarrow{\text{commit_cb}} \text{stream end} }
The change_cb callback receives the decoded relation and the specific change (INSERT/UPDATE/DELETE with old and new tuple data). The plugin decides whether to encode and emit the change or filter it out.
Protocol negotiation:
At startup, the client (subscriber) sends parameters (max_proto_version, min_proto_version, binary, pg_version). The plugin responds with a startup message containing server capabilities (binary basetypes support, endianness, sizeof Datum/int/long, float4/float8 byval, integer datetimes). The client and plugin negotiate the highest mutually supported protocol version and whether binary transfer is possible.
Practical Guide
How pglogical Uses Logical Decoding
pglogical registers a custom output plugin (pglogical_output) that:
- Parses subscriber startup parameters and negotiates protocol version
- Applies replication set filtering (skips changes to non-subscribed tables)
- Applies row filters and column lists
- Emits REL messages for relation metadata (cached on subscriber)
- Encodes changes using the native binary or JSON wire protocol
- Sends BEGIN/INSERT/UPDATE/DELETE/COMMIT messages via OutputPluginWrite
Key Considerations
- The output plugin runs on the provider (publisher) side
- Changes are only decoded for tables that have a REPLICA IDENTITY
- The reorder buffer holds transactions in memory until commit (large transactions may spill to disk)
- The plugin's private data (PGLogicalOutputData) maintains negotiation state and protocol API
Related Pages
Implemented By
- Implementation:2ndQuadrant_Pglogical_Output_Plugin_Init
- Implementation:2ndQuadrant_Pglogical_Decode_Change
- Implementation:2ndQuadrant_Pglogical_Protocol_API