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 Protocol API

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

Overview

Protocol API factory and native binary wire protocol encoder/decoder that provides the dispatch table for encoding and decoding BEGIN, COMMIT, INSERT, UPDATE, DELETE, and REL messages between pglogical provider and subscriber.

Description

The pglogical_init_api() function creates a PGLogicalProtoAPI dispatch table that selects between the native binary protocol and the JSON protocol. The dispatch table contains function pointers for each message type (write_begin, write_commit, write_insert, write_update, write_delete, write_rel, write_origin, write_startup_message).

The native binary protocol functions (pglogical_write_begin, pglogical_write_commit, pglogical_write_insert, pglogical_write_update, pglogical_write_delete) encode messages using PostgreSQL's pq_send* functions into a StringInfo buffer. Each message starts with a single-byte type code ('B', 'C', 'I', 'U', 'D', 'R', 'O', 'S').

The corresponding read functions (pglogical_read_begin, pglogical_read_commit, pglogical_read_insert, pglogical_read_update, pglogical_read_delete) decode messages on the subscriber side using pq_getmsg* functions.

Usage

This is an internal API used by the output plugin (provider side) and apply worker (subscriber side). Users do not call it directly, but should understand it when:

  • Debugging protocol-level issues
  • Understanding message format for external consumers
  • Developing custom protocol extensions
  • Troubleshooting binary vs. JSON format selection

Code Reference

Source Location

  • Repository: pglogical
  • File: pglogical_output_proto.c (factory), pglogical_proto_native.c (native encode/decode)
  • Lines: pglogical_output_proto.c:L21-50 (pglogical_init_api); pglogical_proto_native.c:L165-309 (write functions), L559-703 (read functions)

Signature

PGLogicalProtoAPI *pglogical_init_api(PGLogicalProtoType typ);

/* Native write functions: */
void pglogical_write_begin(StringInfo out, PGLogicalOutputData *data, ReorderBufferTXN *txn);
void pglogical_write_commit(StringInfo out, PGLogicalOutputData *data, ReorderBufferTXN *txn, XLogRecPtr commit_lsn);
void pglogical_write_insert(StringInfo out, PGLogicalOutputData *data, Relation rel, HeapTuple newtup, Bitmapset *att_list);
void pglogical_write_update(StringInfo out, PGLogicalOutputData *data, Relation rel, HeapTuple oldtup, HeapTuple newtup, Bitmapset *att_list);
void pglogical_write_delete(StringInfo out, PGLogicalOutputData *data, Relation rel, HeapTuple oldtup, Bitmapset *att_list);
void pglogical_write_rel(StringInfo out, PGLogicalOutputData *data, Relation rel, Bitmapset *att_list);

/* Native read functions: */
void pglogical_read_begin(StringInfo in, XLogRecPtr *remote_lsn, TimestampTz *committime, TransactionId *remote_xid);
void pglogical_read_commit(StringInfo in, XLogRecPtr *commit_lsn, XLogRecPtr *end_lsn, TimestampTz *committime);
PGLogicalRelation *pglogical_read_insert(StringInfo in, LOCKMODE lockmode, PGLogicalTupleData *newtup);
PGLogicalRelation *pglogical_read_update(StringInfo in, LOCKMODE lockmode, bool *hasoldtup, PGLogicalTupleData *oldtup, PGLogicalTupleData *newtup);
PGLogicalRelation *pglogical_read_delete(StringInfo in, LOCKMODE lockmode, PGLogicalTupleData *oldtup);

Import

#include "pglogical_output_proto.h"
#include "pglogical_proto_native.h"

I/O Contract

Inputs

Name Type Required Description
typ PGLogicalProtoType Yes pglogical_init_api: PGLogicalProtoNative or PGLogicalProtoJson
out StringInfo Yes write_*: output buffer to encode into
data PGLogicalOutputData* Yes write_*: plugin context with protocol state
txn ReorderBufferTXN* Yes write_begin/commit: transaction metadata
in StringInfo Yes read_*: input buffer to decode from

Outputs

Name Type Description
PGLogicalProtoAPI struct Dispatch table with write callback function pointers
encoded messages StringInfo write_*: messages written to output buffer
decoded values various read_*: output parameters filled (LSN, xid, tuple data)

Usage Examples

Protocol API Factory (Internal)

// Abstract factory logic (NOT real implementation)
PGLogicalProtoAPI *pglogical_init_api(PGLogicalProtoType typ) {
    PGLogicalProtoAPI *res = palloc0(sizeof(PGLogicalProtoAPI));
    if (typ == PGLogicalProtoJson) {
        res->write_begin   = pglogical_json_write_begin;
        res->write_commit  = pglogical_json_write_commit;
        res->write_insert  = pglogical_json_write_insert;
        // ...
    } else {
        res->write_rel     = pglogical_write_rel;
        res->write_begin   = pglogical_write_begin;
        res->write_commit  = pglogical_write_commit;
        res->write_insert  = pglogical_write_insert;
        res->write_update  = pglogical_write_update;
        res->write_delete  = pglogical_write_delete;
        // ...
    }
    return res;
}

Native Message Format (Internal)

BEGIN message ('B'):
  byte: 'B' (message type)
  byte: flags (currently 0)
  int64: transaction final LSN
  int64: commit time
  int32: transaction XID

INSERT message ('I'):
  byte: 'I' (message type)
  byte: flags
  int32: relation ID
  ... tuple data (key/value pairs) ...

Related Pages

Implements Principle

Requires Environment

Page Connections

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