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 Decode Change

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

Overview

Logical decoding change callback that filters WAL row changes by replication set membership, row filter, and column list, then emits relation metadata and protocol-encoded change messages.

Description

The pg_decode_change() function is the core per-change callback registered by the pglogical output plugin. For each row change (INSERT/UPDATE/DELETE) decoded from the WAL, it:

  1. Calls pglogical_change_filter() to check replication set membership and row/column filters
  2. If the change passes the filter, checks whether relation metadata has been sent to the subscriber (via the relation metadata cache)
  3. If not cached, emits a REL message with relation ID, namespace, name, and column metadata
  4. Encodes and emits the change (INSERT/UPDATE/DELETE) using the protocol API's write callbacks
  5. Writes the message to the replication stream via OutputPluginWrite

The pglogical_change_filter() function checks whether the table is in any of the subscriber's replication sets, whether the operation (INSERT/UPDATE/DELETE/TRUNCATE) is enabled for that set, and whether the row passes any configured row filter. It also computes the column list projection.

Usage

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

  • Debugging why changes are not being replicated (filtering issues)
  • Understanding the replication set filtering pipeline
  • Troubleshooting missing relation metadata messages

Code Reference

Source Location

  • Repository: pglogical
  • File: pglogical_output_plugin.c
  • Lines: L679-730+ (pg_decode_change); pglogical_change_filter within same file

Signature

static void pg_decode_change(LogicalDecodingContext *ctx,
                             ReorderBufferTXN *txn,
                             Relation relation,
                             ReorderBufferChange *change);

bool pglogical_change_filter(PGLogicalOutputData *data,
                              Relation rel,
                              ReorderBufferChange *change,
                              Bitmapset **att_list);

Import

/* Internal callback, registered by _PG_output_plugin_init().
   Called by PostgreSQL logical decoding framework for each row change. */

I/O Contract

Inputs

Name Type Required Description
ctx LogicalDecodingContext* Yes Decoding context with PGLogicalOutputData private data
txn ReorderBufferTXN* Yes Transaction being decoded
relation Relation Yes Table being changed
change ReorderBufferChange* Yes INSERT/UPDATE/DELETE action with tuple data

Outputs

Name Type Description
protocol messages stream REL (if uncached) + INSERT/UPDATE/DELETE written to output
relation metadata cache cache entry Marked as cached after REL message sent
filtered out void If change does not pass filter, no output emitted

Usage Examples

Filtering Pipeline (Internal)

// Abstract filtering logic (NOT real implementation)
void pg_decode_change(ctx, txn, relation, change) {
    Bitmapset *att_list = NULL;

    // 1. Check replication set membership + row filter + column list
    if (!pglogical_change_filter(data, relation, change, &att_list))
        return;  // Change filtered out

    // 2. Send relation metadata if not yet cached
    if (!relmetacache_is_cached(data, relation)) {
        OutputPluginPrepareWrite(ctx, false);
        data->api->write_rel(ctx->out, data, relation, att_list);
        OutputPluginWrite(ctx, false);
        relmetacache_mark_cached(data, relation);
    }

    // 3. Encode and emit the change
    OutputPluginPrepareWrite(ctx, true);
    switch (change->action) {
        case INSERT: data->api->write_insert(ctx->out, data, relation, newtup, att_list); break;
        case UPDATE: data->api->write_update(ctx->out, data, relation, oldtup, newtup, att_list); break;
        case DELETE: data->api->write_delete(ctx->out, data, relation, oldtup, att_list); break;
    }
    OutputPluginWrite(ctx, true);
}

Related Pages

Implements Principle

Requires Environment

Page Connections

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