pgsql: Record logical replication conflicts in the conflict log table.

From: Amit Kapila <akapila(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Record logical replication conflicts in the conflict log table.
Date: 2026-11-24 11:41:51
Message-ID: E1xVouF-000000008Q7-0PyQ@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Record logical replication conflicts in the conflict log table.

Populate the conflict log table whenever a logical replication conflict is
detected. Each row records the remote transaction (xid, final LSN, commit
timestamp), the replica identity key of the conflicting row, and metadata
of the local conflicting tuples.

Only resolved (LOG-level) conflicts are recorded in the table. Conflicts
that raise an ERROR (such as unique constraint violations) halt
replication and abort the transaction, so they are logged exclusively to
the server log. Conflict log tables are also excluded from logical
decoding.

To avoid serializing full tuples that could exceed the 1GB varlena limit
and halt the apply worker, the 'remote_tuple' column is removed from the
table and 'local_conflicts' records only transaction metadata. Tables
using REPLICA IDENTITY FULL set 'replica_identity_full' to true with
'replica_identity' set to NULL. For other tables, 'replica_identity' can
be used to identify the conflicting tuple.

replica_identity: When the table has a replica identity index or primary
key, this column stores a JSON object keyed by column name. A value whose
output exceeds 64kB is replaced by a marker recording its length
({"omitted":true,"length":...}), and 'has_omitted_values' is set to true.
Values are omitted rather than truncated so that join predicates against
the table never silently give wrong answers.

local_conflicts: This column is an array of JSON objects (json[]), so that
conflicts involving several local tuples can be recorded in the future.
Each element has the format:
[ { "xid": "1001", "commit_ts": "2025-12-25 10:00:00+05:30", "origin": "node_1" }, ... ]

Example:

SELECT remote_xid, relname, apply_origin, replica_identity,
local_conflicts[1] ->> 'xid' AS local_xid
FROM pg_conflict.pg_conflict_log_16396;

remote_xid | relname | apply_origin | replica_identity | local_xid
------------+----------+---------------+------------------+-----------
760 | test | pg_16406 | {"a":"1"} | 771
765 | conf_tab | pg_16406 | {"a":"2"} | 775

With two_phase enabled, a prepared transaction that recorded a conflict
holds a lock on the conflict log table until the apply worker finishes it.
Dropping the table would wait for that lock, while DROP SUBSCRIPTION and
ALTER SUBSCRIPTION ... SET (conflict_log_destination = 'log') also keep the
worker from finishing it, so they could hang. Refuse both while prepared
transactions exist.

Preserving the conflict log table across pg_upgrade is handled in a
separate follow-up patch.

Bumped catversion as the schema of conflict history table is changed.

Based on design inputs by Amit Kapila and Sawada Masahiko.

Author: Dilip Kumar <dilipbalaut(at)gmail(dot)com>
Author: Nisha Moond <nisha(dot)moond412(at)gmail(dot)com>
Author: Zhijie Hou <houzhijie22(at)gmail(dot)com>
Reviewed-by: shveta malik <shveta(dot)malik(at)gmail(dot)com>
Reviewed-by: Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>
Reviewed-by: vignesh C <vignesh21(at)gmail(dot)com>
Reviewed-by: shihao zhong <zhong950419(at)gmail(dot)com>
Reviewed-by: Kedar Anavardekar <kedar(dot)anavardekar(at)gmail(dot)com>
Reviewed-by: Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>
Reviewed-by: Narayanan Venkateswaran <narayananvpostgres(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAFiTN-u5D5o_AGNbHRZHaOqAMWkxLf+hSk_r9X3gv6HbLOB5+g@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/6a0a66b3e7ac394c499708b790effea9a564ef0d

Modified Files
--------------
doc/src/sgml/logical-replication.sgml | 824 +++++++++++++++++++----------
doc/src/sgml/ref/alter_subscription.sgml | 12 +-
doc/src/sgml/ref/create_subscription.sgml | 31 +-
doc/src/sgml/ref/drop_subscription.sgml | 16 +-
src/backend/commands/subscriptioncmds.c | 125 ++++-
src/backend/executor/execReplication.c | 36 +-
src/backend/replication/logical/conflict.c | 659 +++++++++++++++++++++--
src/backend/replication/logical/worker.c | 146 +++--
src/include/catalog/catversion.h | 2 +-
src/include/executor/executor.h | 6 +-
src/include/replication/conflict.h | 9 +-
src/include/replication/worker_internal.h | 5 +
src/include/utils/rel.h | 13 +-
src/test/regress/expected/subscription.out | 13 +-
src/test/regress/sql/subscription.sql | 7 +
src/test/subscription/t/035_conflicts.pl | 342 +++++++++++-
16 files changed, 1854 insertions(+), 392 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Fujii Masao 2026-11-24 11:44:51 pgsql: Improve SQLSTATE reporting for several error conditions
Previous Message Michael Paquier 2026-11-24 06:58:16 pgsql: doc: Describe buffer access strategy types in the glossary