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 Conflict Detection

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

Overview

Internal functions that detect replication conflicts by scanning indexes for matching local tuples and determining the origin of existing tuples via commit timestamps and replication origins.

Description

Conflict detection in pglogical consists of three functions:

  • pglogical_tuple_find_replidx(): Finds a local tuple using the replica identity index (usually the primary key). Builds a scan key from the incoming tuple's indexed columns and performs an index scan. Returns the found tuple in oldslot and the index OID.
  • pglogical_tuple_find_conflict(): Finds a conflicting tuple by scanning all unique indexes on the target relation. This is broader than the replica identity lookup — it detects INSERT-INSERT conflicts via any unique constraint. Returns the OID of the conflicting index.
  • get_tuple_origin(): Determines the origin of a local tuple by looking up its xmin transaction ID in the commit timestamp system. Returns the replication origin ID and commit timestamp. If track_commit_timestamp is off, returns false and assumes the local tuple's origin matches the session origin.
  • build_index_scan_key(): Builds a ScanKey array from a tuple's indexed columns for index scanning. Looks up equality operators from the index's opfamily/opclass.

Usage

These are internal functions called by the heap apply backend during INSERT/UPDATE/DELETE processing. Users do not call them directly, but should understand them when:

  • Debugging conflict detection failures
  • Understanding why conflicts are or are not detected
  • Configuring track_commit_timestamp for timestamp-based conflict resolution

Code Reference

Source Location

  • Repository: pglogical
  • File: pglogical_conflict.c
  • Lines: L62-128 (build_index_scan_key), L270-303 (pglogical_tuple_find_replidx), L321-408 (pglogical_tuple_find_conflict), L499-529 (get_tuple_origin)

Signature

bool pglogical_tuple_find_replidx(ResultRelInfo *relinfo,
                                   PGLogicalTupleData *tuple,
                                   TupleTableSlot *oldslot,
                                   Oid *idxrelid);

Oid pglogical_tuple_find_conflict(ResultRelInfo *relinfo,
                                   PGLogicalTupleData *tuple,
                                   TupleTableSlot *outslot);

bool get_tuple_origin(HeapTuple local_tuple,
                       TransactionId *xmin,
                       RepOriginId *local_origin,
                       TimestampTz *local_ts);

static bool build_index_scan_key(ScanKey skey, Relation rel,
                                  Relation idxrel, PGLogicalTupleData *tup);

Import

#include "pglogical_conflict.h"

I/O Contract

Inputs

Name Type Required Description
relinfo ResultRelInfo* Yes Target relation info with indexes
tuple PGLogicalTupleData* Yes Incoming remote tuple
local_tuple HeapTuple Yes get_tuple_origin: existing local tuple
track_commit_timestamp GUC No Required for origin detection (get_tuple_origin)

Outputs

Name Type Description
found (replidx) bool True if matching tuple found via replica identity index
oldslot TupleTableSlot Filled with found local tuple
idxrelid Oid* OID of the index used
conflict_idx Oid pglogical_tuple_found_conflict: conflicting index OID
local_origin RepOriginId* get_tuple_origin: origin of local tuple
local_ts TimestampTz* get_tuple_origin: commit timestamp of local tuple

Usage Examples

Conflict Detection Flow (Internal)

// Abstract conflict detection (NOT real implementation)

// For INSERT: check all unique indexes for conflicts
conflict_idx = pglogical_tuple_find_conflict(relinfo, newtup, localslot);
if (conflict_idx != InvalidOid) {
    // Conflict detected - get local tuple origin
    get_tuple_origin(local_tuple, &xmin, &local_origin, &local_ts);
    // If origins differ, call try_resolve_conflict
    try_resolve_conflict(rel, localtuple, remotetuple, &resulttuple, &resolution);
}

// For UPDATE/DELETE: use replica identity index
found = pglogical_tuple_find_replidx(relinfo, oldtup, oldslot, &idxrelid);
if (!found) {
    // Row doesn't exist locally - UPDATE became INSERT or skip
}

Related Pages

Implements Principle

Uses Heuristic

Requires Environment

Page Connections

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