1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-27 12:41:57 +03:00

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.moond412@gmail.com>
Author: Zhijie Hou <houzj.fnst@fujitsu.com>
Reviewed-by: Amit Kapila <amit.kapila16@gmail.com>
Reviewed-by: Peter Smith <smithpb2250@gmail.com>
Reviewed-by: Dilip Kumar <dilipbalaut@gmail.com>
Discussion: https://postgr.es/m/CABdArM7FW-_dnthGkg2s0fy1HhUB8C3ELA0gZX1kkbs1ZZoV3Q@mail.gmail.com
This commit is contained in:
Amit Kapila
2025-03-24 12:30:44 +05:30
parent 35a92b7c25
commit 73eba5004a
14 changed files with 275 additions and 87 deletions

View File

@ -1877,6 +1877,19 @@ test_sub=# SELECT * from tab_gen_to_gen;
</para>
</listitem>
</varlistentry>
<varlistentry id="conflict-multiple-unique-conflicts" xreflabel="multiple_unique_conflicts">
<term><literal>multiple_unique_conflicts</literal></term>
<listitem>
<para>
Inserting or updating a row violates multiple
<literal>NOT DEFERRABLE</literal> unique constraints. Note that to log
the origin and commit timestamp details of conflicting keys, ensure
that <link linkend="guc-track-commit-timestamp"><varname>track_commit_timestamp</varname></link>
is enabled on the subscriber. In this case, an error will be raised until
the conflict is resolved manually.
</para>
</listitem>
</varlistentry>
</variablelist>
Note that there are other conflict scenarios, such as exclusion constraint
violations. Currently, we do not provide additional details for them in the
@ -1935,8 +1948,8 @@ DETAIL: <replaceable class="parameter">detailed_explanation</replaceable>.
<para>
The <literal>Key</literal> section includes the key values of the local
tuple that violated a unique constraint for
<literal>insert_exists</literal> or <literal>update_exists</literal>
conflicts.
<literal>insert_exists</literal>, <literal>update_exists</literal> or
<literal>multiple_unique_conflicts</literal> conflicts.
</para>
</listitem>
<listitem>
@ -1945,8 +1958,8 @@ DETAIL: <replaceable class="parameter">detailed_explanation</replaceable>.
tuple if its origin differs from the remote tuple for
<literal>update_origin_differs</literal> or <literal>delete_origin_differs</literal>
conflicts, or if the key value conflicts with the remote tuple for
<literal>insert_exists</literal> or <literal>update_exists</literal>
conflicts.
<literal>insert_exists</literal>, <literal>update_exists</literal> or
<literal>multiple_unique_conflicts</literal> conflicts.
</para>
</listitem>
<listitem>
@ -1982,6 +1995,16 @@ DETAIL: <replaceable class="parameter">detailed_explanation</replaceable>.
The large column values are truncated to 64 bytes.
</para>
</listitem>
<listitem>
<para>
Note that in case of <literal>multiple_unique_conflicts</literal> conflict,
multiple <replaceable class="parameter">detailed_explanation</replaceable>
and <replaceable class="parameter">detail_values</replaceable> lines
will be generated, each detailing the conflict information associated
with distinct unique
constraints.
</para>
</listitem>
</itemizedlist>
</listitem>
</varlistentry>

View File

@ -2250,6 +2250,18 @@ description | Waiting for a newly initialized WAL file to reach durable storage
</para></entry>
</row>
<row>
<entry role="catalog_table_entry"><para role="column_definition">
<structfield>confl_multiple_unique_conflicts</structfield> <type>bigint</type>
</para>
<para>
Number of times a row insertion or an updated row values violated multiple
<literal>NOT DEFERRABLE</literal> unique constraints during the
application of changes. See <xref linkend="conflict-multiple-unique-conflicts"/>
for details about this conflict.
</para></entry>
</row>
<row>
<entry role="catalog_table_entry"><para role="column_definition">
<structfield>stats_reset</structfield> <type>timestamp with time zone</type>