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 Replication Origin Tracking

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

Overview

Mechanism for tagging each applied transaction with the identity of its source node and the remote commit LSN, enabling loop prevention, conflict detection, and restart-position tracking in cascading replication topologies.

Description

Replication origin tracking solves three problems in distributed replication:

  1. Loop prevention: In cascading or bi-directional topologies, changes forwarded from one node to another could circle back indefinitely. By tagging each transaction with a replication origin ID, the output plugin can filter out changes that originated from the same node (or from specified forward origins).
  1. Conflict detection: When applying a remote change, the system can inspect the local tuple's origin (via commit timestamp and origin ID) to determine whether it was locally modified or came from a different source, triggering conflict resolution if origins differ.
  1. Restart position: After each committed transaction, the replication origin session records the remote LSN. On restart, the apply worker resumes from this position, avoiding re-processing of already-applied changes.

Each subscription has a unique replication origin (identified by the slot name). The apply worker sets replorigin_session_origin, replorigin_session_origin_lsn, and replorigin_session_origin_timestamp before applying each transaction.

Usage

Replication origin tracking is relevant when:

  • Setting up cascading replication (A → B → C)
  • Configuring bi-directional (multi-master) replication
  • Filtering forwarded changes (forward_origins parameter in subscriptions)
  • Detecting conflicts by comparing tuple origins
  • Ensuring crash-safe restart positions for apply workers

Theoretical Basis

Origin tagging during apply:

function apply_transaction(remote_txn):
    replorigin_session_origin = subscription.origin_id
    replorigin_session_origin_lsn = remote_txn.commit_lsn
    replorigin_session_origin_timestamp = remote_txn.commit_time
    apply_all_changes()
    commit_transaction()
    replorigin_advance(origin_id, remote_txn.end_lsn, ...)

Origin filtering during decode:

The output plugin's filter_by_origin_cb callback is called for each change. If the change's origin matches one of the subscription's forward_origins, the change is forwarded. Otherwise, it is filtered out to prevent loops.

forward(change)=change.origin∈subscription.forward_origins

Restart position:

Failed to parse (syntax error): {\displaystyle \text{restart\_lsn} = \text{replorigin\_session\_origin\_lsn} = \text{remote\_commit\_lsn} }

On worker restart, the apply worker requests streaming from restart_lsn, ensuring no committed transactions are lost.

Practical Guide

Configuring Forward Origins

-- Forward changes from specific origins (cascading replication)
SELECT pglogical.create_subscription(
    subscription_name := 'cascading_sub',
    provider_dsn := 'host=provider.example.com dbname=mydb',
    replication_sets := ARRAY['default'],
    synchronize_structure := true,
    synchronize_data := true,
    forward_origins := ARRAY['upstream_node_name']  -- Forward changes from this origin
);

-- To NOT forward any changes (only local changes from provider):
-- forward_origins := ARRAY['']

Key Considerations

  • Each subscription creates a replication origin named after its slot
  • replorigin_advance is called after each transaction commit
  • Origin filtering happens on the provider side during logical decoding
  • The pglogical_origin extension provides replication origin support for PG 9.4
  • LSN mapping entries are cleaned up periodically to prevent unbounded growth

Related Pages

Implemented By

Page Connections

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