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

Implementation:2ndQuadrant Pglogical Output Plugin Init

From Leeroopedia
Knowledge Sources
Domains Database, Replication, Change_Data_Capture
Last Updated 2026-10-13 00:00 GMT

Overview

PostgreSQL logical decoding output plugin entry point that registers callbacks for startup, begin, change, commit, origin filtering, and shutdown, implementing pglogical's custom replication protocol on the provider side.

Description

The _PG_output_plugin_init() function is the PostgreSQL logical decoding plugin entry point. It registers pglogical's callback functions with the logical decoding framework. When the subscriber connects and starts replication, pg_decode_startup() is called to parse client parameters, negotiate protocol version, and exchange capability information via a startup message.

The pg_decode_startup() function:

  1. Allocates and initializes the PGLogicalOutputData private context
  2. Parses client parameters (max_proto_version, min_proto_version, binary, pg_version)
  3. Negotiates the highest mutually supported protocol version
  4. Checks binary compatibility (endianness, sizeof(Datum), etc.)
  5. Selects the protocol API (native binary or JSON) via pglogical_init_api()
  6. Sends a startup message with server capabilities

The pg_decode_change() function (documented separately) is the per-change callback.

Usage

This is an internal plugin loaded by PostgreSQL's logical decoding framework. Users do not call it directly, but should understand it when:

  • Debugging protocol negotiation failures
  • Understanding which protocol version and transfer mode is in use
  • Developing custom output plugins
  • Troubleshooting binary vs. text transfer issues

Code Reference

Source Location

  • Repository: pglogical
  • File: pglogical_output_plugin.c, pglogical_output_config.c
  • Lines: pglogical_output_plugin.c:L93-106 (_PG_output_plugin_init), L168-260+ (pg_decode_startup), L808+ (send_startup_message); pglogical_output_config.c:L124+ (process_parameters_v1)

Signature

void _PG_output_plugin_init(OutputPluginCallbacks *cb);

static void pg_decode_startup(LogicalDecodingContext *ctx,
                              OutputPluginOptions *opt,
                              bool is_init);

static void send_startup_message(LogicalDecodingContext *ctx,
                                  PGLogicalOutputData *data,
                                  bool last_message);

void process_parameters_v1(List *options, PGLogicalOutputData *data);

/* Registered callbacks: */
/* cb->startup_cb  = pg_decode_startup */
/* cb->begin_cb    = pg_decode_begin_txn */
/* cb->change_cb   = pg_decode_change */
/* cb->commit_cb   = pg_decode_commit_txn */
/* cb->filter_by_origin_cb = pg_decode_origin_filter (if HAVE_REPLICATION_ORIGINS) */
/* cb->shutdown_cb = pg_decode_shutdown */

Import

/* Loaded by PostgreSQL logical decoding framework when creating a slot:
   SELECT pg_create_logical_replication_slot('slot_name', 'pglogical_output');
*/

I/O Contract

Inputs

Name Type Required Description
cb OutputPluginCallbacks* Yes Output plugin callback structure to fill
ctx LogicalDecodingContext* Yes Decoding context (provided by PostgreSQL)
opt OutputPluginOptions* Yes Output format options
is_init bool Yes True for slot init, false for replication start
options List* Yes process_parameters_v1: client startup parameters

Outputs

Name Type Description
callbacks OutputPluginCallbacks Filled with pglogical callback functions
PGLogicalOutputData struct Private context with negotiated protocol, API, capabilities
startup message protocol msg Sent to subscriber with server capabilities

Usage Examples

Creating a Replication Slot (User API)

-- The plugin is loaded when creating a replication slot
-- (pglogical does this internally during create_subscription)
SELECT pg_create_logical_replication_slot(
    slot_name := 'pgl_mydb_provider1_sub1',
    plugin := 'pglogical_output'
);

Protocol Parameters (Sent by Subscriber)

-- Subscriber sends these during START_REPLICATION:
max_proto_version=2
min_proto_version=1
binary=true          -- Request binary transfer
pg_version=150000    -- Subscriber's PostgreSQL version

Startup Message (Sent by Provider)

-- Provider responds with capabilities:
protocol_version=2
binary_basetypes_major_version=150
server_bigendian=false
sizeof_datum=8
sizeof_int=4
sizeof_long=8
float4_byval=true
float8_byval=true
integer_datetimes=true

Related Pages

Implements Principle

Requires Environment

Page Connections

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