Principle:2ndQuadrant Pglogical Conflict Resolution
| Knowledge Sources | |
|---|---|
| Domains | Database, Replication, Distributed_Systems |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Mechanism for detecting and resolving conflicting writes when multiple replication sources (or local writes) modify the same row on a subscriber, using configurable strategies including error, apply-remote, keep-local, last-update-wins, and first-update-wins.
Description
In multi-master or bi-directional replication, the same row may be modified by different sources concurrently. When a remote change arrives at the subscriber and the target row already exists (for INSERT) or has been locally modified (for UPDATE/DELETE), a conflict occurs. Conflict resolution determines which version of the row wins.
pglogical detects conflicts by scanning indexes to find existing tuples that would violate uniqueness constraints. It then resolves them using the strategy configured in the pglogical.conflict_resolution GUC:
- error: Abort the apply operation (transaction fails)
- apply_remote: Always apply the remote tuple, overwriting local
- keep_local: Always keep the local tuple, discard remote
- last_update_wins: Compare commit timestamps; the most recent write wins
- first_update_wins: Compare commit timestamps; the earliest write wins
Timestamp-based strategies require track_commit_timestamp = on on the subscriber. If commit timestamps are not available, the system falls back to assuming the local tuple's origin matches the current session origin (effectively treating it as non-conflicting).
Usage
Use conflict resolution when:
- Running bi-directional or multi-source replication
- The subscriber has local writes that may conflict with replicated changes
- Deterministic conflict outcomes are required (choose a strategy)
- Auditing conflicts is needed (conflict logging at configurable log level)
Theoretical Basis
Conflict types:
| Conflict Type | Scenario |
|---|---|
| INSERT-INSERT | Remote INSERT finds existing row with same unique key |
| UPDATE-UPDATE | Remote UPDATE finds local row modified by different origin |
| UPDATE-DELETE | Remote UPDATE targets a row that was locally deleted |
| DELETE-DELETE | Remote DELETE targets a row already deleted locally |
Resolution decision function:
function try_resolve_conflict(local_tuple, remote_tuple, strategy):
switch strategy:
case ERROR: raise error
case APPLY_REMOTE: return remote_tuple
case KEEP_LOCAL: return local_tuple
case LAST_WINS:
if remote_commit_ts > local_commit_ts: return remote_tuple
else: return local_tuple
case FIRST_WINS:
if remote_commit_ts < local_commit_ts: return remote_tuple
else: return local_tuple
Origin tracking:
Each tuple's origin is determined by get_tuple_origin():
If track_commit_timestamp is off, origin cannot be determined, and timestamp-based strategies fall back to assuming local origin matches session origin (effectively no conflict detected).
Conflict detection via index scan:
The system uses the replica identity index to find the local tuple matching the remote key. If a tuple is found and its origin differs from the current session origin, a conflict is declared. For INSERT conflicts, any unique index may detect a conflicting existing row.
Practical Guide
Configuring Conflict Resolution
-- In postgresql.conf
pglogical.conflict_resolution = 'last_update_wins' -- or: error, apply_remote, keep_local, first_update_wins
pglogical.conflict_log_level = 'log' -- Log level for conflict reports
-- For timestamp-based strategies:
track_commit_timestamp = on -- Required in postgresql.conf, needs restart
Conflict Log Output
When a conflict occurs, pglogical logs:
- Conflict type (INSERT_INSERT, UPDATE_UPDATE, UPDATE_DELETE, DELETE_DELETE)
- Relation name
- Local and remote tuple contents
- Resolution applied (apply_remote / keep_local / skip)
- Origin information of the local tuple
- Name of the index where the conflict was detected
Key Considerations
- Only heap API supports conflict resolution; SPI mode requires error strategy
- track_commit_timestamp is required for timestamp-based strategies
- The replica identity index is preferred but any unique index may detect conflicts
- Order-of-apply between multiple upstreams can lead to non-deterministic results
Related Pages
Implemented By
- Implementation:2ndQuadrant_Pglogical_Conflict_Detection
- Implementation:2ndQuadrant_Pglogical_Try_Resolve_Conflict
- Implementation:2ndQuadrant_Pglogical_Report_Conflict
- Implementation:2ndQuadrant_Pglogical_Apply_Heap