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 Wire Protocol Negotiation

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

Overview

Process of establishing a common communication format between provider and subscriber by negotiating protocol version, transfer mode (binary vs. text), and capability flags at replication startup.

Description

Wire protocol negotiation solves the interoperability problem between pglogical provider and subscriber nodes that may run different PostgreSQL versions, have different binary layouts, or prefer different encoding formats. At replication startup, the subscriber sends parameters and the provider responds with its capabilities, allowing both sides to agree on the highest mutually supported protocol version and transfer mode.

pglogical supports two wire formats:

  • Native binary protocol: Compact binary encoding using PostgreSQL's internal type representation. Requires binary compatibility (same major version, same endianness, same sizeof(Datum), etc.). Used when both sides agree on binary transfer.
  • JSON protocol: Human-readable JSON encoding. Used for debugging, external consumers, or when binary compatibility cannot be established. Output-only (no JSON decoder on the subscriber side for apply).

The protocol API is abstracted through a dispatch table (PGLogicalProtoAPI) that selects native or JSON write callbacks. This allows the output plugin to switch formats without changing the encoding logic.

Usage

Wire protocol negotiation is relevant when:

  • Provider and subscriber run different PostgreSQL major versions (use text mode)
  • Debugging the replication stream (use JSON format)
  • Building external consumers that read the pglogical protocol
  • Ensuring binary compatibility for efficient native transfer

Theoretical Basis

Negotiation handshake:

Subscriber → START_REPLICATION with parameters:
  max_proto_version, min_proto_version, binary, pg_version

Provider → startup message with capabilities:
  protocol_version, binary_basetypes_major_version,
  server_endian, sizeof(Datum), sizeof(int), sizeof(long),
  float4_byval, float8_byval, integer_datetimes

Both sides agree on:
  - Highest protocol version in [min, max] range
  - Binary transfer if capabilities match and both sides support it
  - Otherwise: text/JSON transfer

Protocol API dispatch:

// Abstract dispatch table (NOT real implementation)
PGLogicalProtoAPI *pglogical_init_api(type) {
    if (type == PGLogicalProtoJson)
        return json_dispatch_table;  // JSON write callbacks
    else
        return native_dispatch_table; // Native binary write callbacks
}

Message types in native protocol:

  • B: BEGIN (transaction start: LSN, commit time, XID)
  • C: COMMIT (commit LSN, end LSN, commit time)
  • O: ORIGIN (origin name, origin LSN)
  • R: REL (relation metadata: ID, namespace, name, columns)
  • I: INSERT (relation ID, tuple data)
  • U: UPDATE (relation ID, old key tuple, new tuple)
  • D: DELETE (relation ID, old key tuple)
  • S: STARTUP MESSAGE (capabilities)

Practical Guide

Forcing Text Transfer

When provider and subscriber run different PostgreSQL major versions, binary transfer may fail due to incompatible type layouts. Use force_text_transfer:

SELECT pglogical.create_subscription(
    subscription_name := 'cross_version_sub',
    provider_dsn := 'host=provider.example.com dbname=mydb',
    replication_sets := ARRAY['default'],
    synchronize_structure := true,
    synchronize_data := true,
    force_text_transfer := true  -- Force text mode for cross-version compat
);

Key Considerations

  • Binary transfer requires matching major version, endianness, and type sizes
  • JSON protocol is output-only (no apply-side JSON decoder)
  • Protocol version negotiation prevents version skew issues
  • The startup message is always sent in a format both sides understand

Related Pages

Implemented By

Page Connections

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