Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:2ndQuadrant Pglogical Logical Decoding

From Leeroopedia
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:

  1. Parses subscriber startup parameters and negotiates protocol version
  2. Applies replication set filtering (skips changes to non-subscribed tables)
  3. Applies row filters and column lists
  4. Emits REL messages for relation metadata (cached on subscriber)
  5. Encodes changes using the native binary or JSON wire protocol
  6. 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

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment