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 <[email protected]>
Author: Nisha Moond <[email protected]>
Author: Zhijie Hou <[email protected]>
Reviewed-by: shveta malik <[email protected]>
Reviewed-by: Amit Kapila <[email protected]>
Reviewed-by: vignesh C <[email protected]>
Reviewed-by: shihao zhong <[email protected]>
Reviewed-by: Kedar Anavardekar <[email protected]>
Reviewed-by: Masahiko Sawada <[email protected]>
Reviewed-by: Narayanan Venkateswaran <[email protected]>
Discussion:
https://postgr.es/m/cafitn-u5d5o_agnbhrzhaoqamwkxlf+hsk_r9x3gv6hblob...@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(-)