pgsql: Detect and Log multiple_unique_conflicts type conflict.

From: Amit Kapila <akapila(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Detect and Log multiple_unique_conflicts type conflict.
Date: 2025-03-24 07:17:11
Message-ID: E1twc3b-000jo9-1j@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Detect and Log multiple_unique_conflicts type conflict.

Introduce a new conflict type, multiple_unique_conflicts, to handle cases
where an incoming row during logical replication violates multiple UNIQUE
constraints.

Previously, the apply worker detected and reported only the first
encountered key conflict (insert_exists/update_exists), causing repeated
failures as each constraint violation needs to be handled one by one
making the process slow and error-prone.

With this patch, the apply worker checks all unique constraints upfront
once the first key conflict is detected and reports
multiple_unique_conflicts if multiple violations exist. This allows users
to resolve all conflicts at once by deleting all conflicting tuples rather
than dealing with them individually or skipping the transaction.

In the future, this will also allow us to specify different resolution
handlers for such a conflict type.

Add the stats for this conflict type in pg_stat_subscription_stats.

Author: Nisha Moond <nisha(dot)moond412(at)gmail(dot)com>
Author: Zhijie Hou <houzj(dot)fnst(at)fujitsu(dot)com>
Reviewed-by: Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>
Reviewed-by: Peter Smith <smithpb2250(at)gmail(dot)com>
Reviewed-by: Dilip Kumar <dilipbalaut(at)gmail(dot)com>
Discussion: https://postgr.es/m/CABdArM7FW-_dnthGkg2s0fy1HhUB8C3ELA0gZX1kkbs1ZZoV3Q@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/73eba5004a06a744b6b8570e42432b9e9f75997b

Modified Files
--------------
doc/src/sgml/logical-replication.sgml | 31 +++++++-
doc/src/sgml/monitoring.sgml | 12 +++
src/backend/catalog/system_views.sql | 1 +
src/backend/executor/execReplication.c | 30 +++++---
src/backend/replication/logical/conflict.c | 64 +++++++++-------
src/backend/replication/logical/worker.c | 68 ++++++++---------
src/backend/utils/adt/pgstatfuncs.c | 6 +-
src/include/catalog/catversion.h | 2 +-
src/include/catalog/pg_proc.dat | 6 +-
src/include/replication/conflict.h | 26 +++++--
src/test/regress/expected/rules.out | 3 +-
src/test/subscription/meson.build | 1 +
src/test/subscription/t/035_conflicts.pl | 113 +++++++++++++++++++++++++++++
src/tools/pgindent/typedefs.list | 1 +
14 files changed, 276 insertions(+), 88 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Heikki Linnakangas 2025-03-24 10:20:09 pgsql: Add test case for when subscriber table is missing a column
Previous Message David Rowley 2025-03-24 06:32:53 pgsql: Add tests for POSITION(bytea, bytea)