Implementation:2ndQuadrant Pglogical Try Resolve Conflict
| Knowledge Sources | |
|---|---|
| Domains | Database, Replication, Distributed_Systems |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Function that applies the configured conflict resolution strategy (error, apply-remote, keep-local, last-update-wins, first-update-wins) to determine whether the remote or local tuple wins, returning the winning tuple and resolution decision.
Description
The try_resolve_conflict() function is the decision point for conflict resolution. It reads the pglogical.conflict_resolution GUC and applies the corresponding strategy:
- PGLOGICAL_RESOLVE_ERROR: Raises an error, aborting the apply operation.
- PGLOGICAL_RESOLVE_APPLY_REMOTE: Sets the result tuple to the remote tuple and resolution to ApplyRemote.
- PGLOGICAL_RESOLVE_KEEP_LOCAL: Sets the result tuple to the local tuple and resolution to KeepLocal.
- PGLOGICAL_RESOLVE_LAST_UPDATE_WINS: Calls get_tuple_origin() to get commit timestamps of both local and remote tuples, then calls conflict_resolve_by_timestamp() with last=true to select the most recent write.
- PGLOGICAL_RESOLVE_FIRST_UPDATE_WINS: Same as above but with last=false to select the earliest write.
The function returns true if the remote tuple should be applied (i.e., the remote tuple won), false otherwise.
Usage
This is an internal function called by the heap apply backend. It implements a strategy pattern dispatched via the GUC enum. Users configure the strategy via pglogical.conflict_resolution.
Code Reference
Source Location
- Repository: pglogical
- File: pglogical_conflict.c
- Lines: L537-587
Signature
bool try_resolve_conflict(Relation rel,
HeapTuple localtuple,
HeapTuple remotetuple,
HeapTuple *resulttuple,
PGLogicalConflictResolution *resolution);
Import
#include "pglogical_conflict.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| rel | Relation | Yes Target table | |
| localtuple | HeapTuple | Yes Existing local tuple | |
| remotetuple | HeapTuple | Yes Incoming remote tuple | |
| pglogical.conflict_resolution | GUC | Yes Conflict resolution strategy enum | |
| track_commit_timestamp | GUC | No Required for timestamp-based strategies |
Outputs
| Name | Type | Description |
|---|---|---|
| return value | bool | True if remote tuple should be applied |
| *resulttuple | HeapTuple | Winning tuple (local or remote) |
| *resolution | PGLogicalConflictResolution | ApplyRemote, KeepLocal, or Skip |
Usage Examples
Strategy Dispatch (Internal)
// Abstract resolution logic (NOT real implementation)
bool try_resolve_conflict(rel, localtuple, remotetuple, &resulttuple, &resolution) {
switch (pglogical_conflict_resolver) {
case PGLOGICAL_RESOLVE_ERROR:
elog(ERROR, "cannot apply conflicting row");
break;
case PGLOGICAL_RESOLVE_APPLY_REMOTE:
apply = true;
*resolution = PGLogicalResolution_ApplyRemote;
break;
case PGLOGICAL_RESOLVE_KEEP_LOCAL:
apply = false;
*resolution = PGLogicalResolution_KeepLocal;
break;
case PGLOGICAL_RESOLVE_LAST_UPDATE_WINS:
get_tuple_origin(localtuple, &xmin, &local_origin, &local_ts);
apply = conflict_resolve_by_timestamp(
local_origin, replorigin_session_origin,
local_ts, replorigin_session_origin_timestamp,
true, resolution); // last=true
break;
case PGLOGICAL_RESOLVE_FIRST_UPDATE_WINS:
get_tuple_origin(localtuple, &xmin, &local_origin, &local_ts);
apply = conflict_resolve_by_timestamp(
local_origin, replorigin_session_origin,
local_ts, replorigin_session_origin_timestamp,
false, resolution); // last=false
break;
}
*resulttuple = apply ? remotetuple : localtuple;
return apply;
}
Configuring Conflict Resolution
-- In postgresql.conf:
pglogical.conflict_resolution = 'last_update_wins'
track_commit_timestamp = on -- Required for timestamp-based strategies