29.8. Conflicts #

29.8.1. Conflict logging
29.8.2. Notes

Logical replication behaves similarly to normal DML operations in that the data will be updated even if it was changed locally on the subscriber node. If incoming data violates any constraints the replication will stop. This is referred to as a conflict. When replicating UPDATE or DELETE operations, missing data is also considered as a conflict, but does not result in an error and such operations will simply be skipped.

29.8.1. Conflict logging #

Conflict logging is triggered, and conflict statistics are collected (displayed in the pg_stat_subscription_stats view) for the conflict cases described below. Depending on the value of conflict_log_destination, the conflict information is written to the server log, a conflict log table, or both.

The supported conflict cases are:

insert_exists #

Inserting a row that violates a NOT DEFERRABLE unique constraint. Note that to log the origin and commit timestamp details of the conflicting key, track_commit_timestamp should be enabled on the subscriber. In this case, an error will be raised until the conflict is resolved manually.

update_origin_differs #

Updating a row that was previously modified by another origin. Note that this conflict can only be detected when track_commit_timestamp is enabled on the subscriber. Currently, the update is always applied regardless of the origin of the local row.

update_exists #

The updated value of a row violates a NOT DEFERRABLE unique constraint. Note that to log the origin and commit timestamp details of the conflicting key, track_commit_timestamp should be enabled on the subscriber. In this case, an error will be raised until the conflict is resolved manually. Note that when updating a partitioned table, if the updated row value satisfies another partition constraint resulting in the row being inserted into a new partition, the insert_exists conflict may arise if the new row violates a NOT DEFERRABLE unique constraint.

update_deleted #

The tuple to be updated was concurrently deleted by another origin. The update will simply be skipped in this scenario. Note that this conflict can only be detected when track_commit_timestamp and retain_dead_tuples are enabled. Note that if a tuple cannot be found due to the table being truncated, only an update_missing conflict will arise. Additionally, if the tuple was deleted by the same origin, an update_missing conflict will arise.

update_missing #

The row to be updated was not found. The update will simply be skipped in this scenario.

delete_origin_differs #

Deleting a row that was previously modified by another origin. Note that this conflict can only be detected when track_commit_timestamp is enabled on the subscriber. Currently, the delete is always applied regardless of the origin of the local row.

delete_missing #

The row to be deleted was not found. The delete will simply be skipped in this scenario.

multiple_unique_conflicts #

Inserting or updating a row violates multiple NOT DEFERRABLE unique constraints. Note that to log the origin and commit timestamp details of conflicting keys, ensure that track_commit_timestamp is enabled on the subscriber. In this case, an error will be raised until the conflict is resolved manually.

Note that there are other conflict scenarios, such as exclusion constraint violations. Currently, we do not provide additional details for them in the log.

29.8.1.1. File-based logging #

If the conflict_log_destination parameter is set to log or all, conflicts are logged to the server log using the following format:

LOG:  conflict detected on relation "schemaname.tablename": conflict=conflict_type
DETAIL:  detailed_explanation[: detail_values [, ... ]].

where detail_values is one of:

    key (column_name [, ...])=(column_value [, ...])
    local row [(column_name [, ...])=](column_value [, ...])
    remote row [(column_name [, ...])=](column_value [, ...])
    replica identity {(column_name [, ...])=(column_value [, ...]) | full [(column_name [, ...])=](column_value [, ...])}

The log provides the following information:

LOG
  • schemaname.tablename identifies the local relation involved in the conflict.

  • conflict_type is the type of conflict that occurred (e.g., insert_exists, update_exists).

DETAIL
  • detailed_explanation includes the origin, transaction ID, and commit timestamp of the transaction that modified the local row, if available.

  • The key section includes the key values of the local row that violated a unique constraint for insert_exists, update_exists or multiple_unique_conflicts conflicts.

  • The local row section includes the local row if its origin differs from the remote row for update_origin_differs or delete_origin_differs conflicts, or if the key value conflicts with the remote row for insert_exists, update_exists or multiple_unique_conflicts conflicts.

  • The remote row section includes the new row from the remote insert or update operation that caused the conflict. Note that for an update operation, the column value of the new row will be null if the value is unchanged and toasted.

  • The replica identity section includes the replica identity key values that were used to search for the existing local row to be updated or deleted. This may include the full row value if the local relation is marked with REPLICA IDENTITY FULL.

  • column_name is the column name. For local row, remote row, and replica identity full cases, column names are logged only if the user lacks the privilege to access all columns of the table. If column names are present, they appear in the same order as the corresponding column values.

  • column_value is the column value. The large column values are truncated to 64 bytes.

  • Note that in case of multiple_unique_conflicts conflict, multiple detailed_explanation and detail_values lines will be generated, each detailing the conflict information associated with distinct unique constraints.

29.8.1.2. Table-based logging #

If the conflict_log_destination parameter is set to table or all, a dedicated conflict log table will be automatically created. This table is created in the pg_conflict namespace. The name of the conflict log table is pg_conflict_log_<subid>. The predefined schema of this table is detailed in Table 29.3.

Note

Only resolved conflicts (logged at LOG level where the apply transaction continues) are recorded in the conflict log table. Conflicts that raise an ERROR halt replication and are reported to the server log alone.

The conflict log table serves as an audit trail for resolved conflicts where replication continues and the transaction successfully commits (such as update_origin_differs, update_missing, update_deleted, delete_origin_differs, and delete_missing). In these cases, local data may diverge or be overwritten, making a queryable audit log valuable for database administrators to understand why a row changed or was skipped.

In contrast, conflicts raised at ERROR level (such as insert_exists, update_exists, and multiple_unique_conflicts) halt replication and abort the entire apply transaction. Because the transaction rolls back and no data modifications survive on the subscriber, no data divergence occurs. These errors are always reported to the server log (regardless of the conflict_log_destination setting) so that monitoring and alerting systems can capture the replication failure.

Table 29.3. Conflict Log Table Schema

ColumnTypeDescription
relidoidThe OID of the local table where the conflict occurred.
schemanametextThe schema name of the conflicting table.
relnametextThe name of the conflicting table.
conflict_typetextThe type of conflict that occurred (e.g., update_origin_differs).
remote_xidxidThe remote transaction ID that caused the conflict. Since transaction IDs on the publisher can wrap around, this value may be reused by a later transaction; see Section 67.1 for details.
remote_commit_lsnpg_lsnThe commit LSN of the remote transaction, or the prepare LSN if the conflict occurs in a transaction that is prepared on the subscriber (see two_phase), because the changes are applied at prepare time before COMMIT PREPARED arrives. This is NULL if the conflict is handled by a parallel apply worker, because in-progress transactions are streamed and applied before the remote commit or prepare LSN is known.
remote_commit_tstimestamptzThe remote commit timestamp of the remote transaction. This is NULL if the conflict is handled by a parallel apply worker, because in-progress transactions are streamed and applied before the remote commit timestamp is known. It is also NULL if the conflict occurs in a transaction that is prepared on the subscriber (see two_phase), because the commit timestamp is known only when COMMIT PREPARED arrives.
apply_origintextThe local replication origin under which the change was applied. This is pg_<subid> for the apply worker, or pg_<subid>_<relid> for a tablesync worker.
replica_identity_fullbooleanTrue if the conflicting local row was located using the full tuple, rather than the replica identity key of the local table.
replica_identityjsonThe replica identity key values identifying the conflicting row, as a JSON object keyed by column name; see below for how the values are represented. This is NULL when replica_identity_full is true.
local_conflictsjson[] An array of JSON objects representing the state of the local row(s) that caused the conflict. Each object includes the local transaction ID (xid, of type xid8), commit timestamp (commit_ts), and the name of the replication origin that last modified the row (origin). The origin field is NULL if the transaction had no replication origin set or if that origin has since been dropped. The column itself is NULL if there are no local conflicting rows (e.g., in update_missing and delete_missing conflicts).
has_omitted_valuesbooleanIndicates whether any value in replica_identity was too large to be recorded and was replaced by an omitted marker.

Every value in replica_identity is written as a JSON string. For most columns, this is the same text a SELECT of that column would show. Values are quoted whatever the column's type, so values such as numbers, booleans, and jsonb documents all appear as strings:

CREATE TABLE t (id int, tag text, doc jsonb, PRIMARY KEY (id, tag, doc));

SELECT replica_identity FROM pg_conflict.pg_conflict_log_16392;
                 replica_identity
-------------------------------------------------
 {"id":"1","tag":"abc","doc":"{\"x\": 1}"}

Extracting a value with the ->> operator yields a form that can be cast back to the column's type, so a recorded replica identity can be used to locate the row it identifies:

SELECT t.* FROM pg_conflict.pg_conflict_log_16392 c, t
  WHERE t.id  = (c.replica_identity->>'id')::int
    AND t.tag =  c.replica_identity->>'tag'
    AND t.doc = (c.replica_identity->>'doc')::jsonb;

Because container values such as jsonb, arrays and composite types are recorded as strings rather than as nested JSON, looking inside one requires an explicit cast:

SELECT replica_identity->'doc'->>'x'           FROM ...;   -- NULL
SELECT (replica_identity->>'doc')::jsonb->>'x' FROM ...;   -- 1

Any setting of the apply worker that controls how a value is converted to text can affect how a key value is written. Examples are DateStyle, IntervalStyle, TimeZone, lc_monetary, extra_float_digits, bytea_output and quote_all_identifiers. If such settings differ between conflicts, the same key can be recorded in different forms, and comparing the recorded text may miss some of them. For settings that affect only the representation, such as bytea_output and quote_all_identifiers, cast the value back to the column's type to compare equivalent forms. Here are examples for a bytea key column b and a regclass[] key column r:

SELECT * FROM ... WHERE replica_identity->>'b' = '\x0102';                    -- may miss rows
SELECT * FROM ... WHERE (replica_identity->>'b')::bytea = '\x0102';           -- matches all
SELECT * FROM ... WHERE replica_identity->>'r' = '{pg_class}';                -- may miss rows
SELECT * FROM ... WHERE (replica_identity->>'r')::regclass[] = '{pg_class}';  -- matches all

Some of these settings also affect how a value is interpreted, for example DateStyle, IntervalStyle, TimeZone and lc_monetary. When casting such recorded values, use the same settings that the apply worker used when recording the conflict. Otherwise, the original value may not be restored correctly. For example, 02/01/2025 means January 2, 2025 with DMY, but February 1, 2025 with MDY. For intervals, the hours in -1 02:00:00 are positive with postgres, but negative with sql_standard. For floating-point keys, keep extra_float_digits at 1 or higher when recording; zero or negative values round the output and can prevent exact restoration.

If a key value's text representation is larger than 64kB, it is not recorded. In its place the conflict log stores a small object marking it as omitted and giving its length in bytes, and sets has_omitted_values for that row:

 {"id":"1","tag":"abc","doc":{"omitted":true,"length":70000}}

Large values are omitted rather than truncated, because a truncated value would still look like a real one and would give wrong answers when compared or joined against the table.

Use the flag to find rows with a missing value, without having to search the json column:

SELECT * FROM pg_conflict.pg_conflict_log_16392 WHERE has_omitted_values;

A value omitted this way cannot be recovered from the conflict log table.

Key values are limited to 64kB because replica_identity is a json value, which cannot exceed 1GB, and the text form of a value can be longer than the value itself.

The limit is applied only after a key value's text representation has been produced, so it bounds how much text is stored in the conflict log table but not how large that text becomes while it is being built. Normally this is not a problem, because the publisher had to produce the same text, within the same 1GB limit, in order to send the value. This does not hold in some cases, for example when the key column has a different data type on the subscriber, when the subscription uses binary mode so the value is not sent as text, or when the subscriber's bytea_output or quote_all_identifiers setting differs from the publisher's. Then the text can exceed 1GB, and the apply worker fails with an error when it tries to log the conflict.

29.8.2. Notes #

Logical replication operations are performed with the privileges of the role which owns the subscription. Permissions failures on target tables will cause replication conflicts, as will enabled row-level security on target tables that the subscription owner is subject to, without regard to whether any policy would ordinarily reject the INSERT, UPDATE, DELETE or TRUNCATE which is being replicated. This restriction on row-level security may be lifted in a future version of PostgreSQL.

A conflict that produces an error will stop the replication; it must be resolved manually by the user. Details about the conflict can be found in the subscriber's server log.

The resolution can be done either by changing data or permissions on the subscriber so that it does not conflict with the incoming change or by skipping the transaction that conflicts with the existing data. When a conflict produces an error, the replication won't proceed, and the logical replication worker will emit the following kind of message to the subscriber's server log:

ERROR:  conflict detected on relation "public.test": conflict=insert_exists
DETAIL:  Could not apply remote change: remote row (1, 'remote').
Key already exists in unique index "test_pkey", modified locally in transaction 800 at 2026-01-16 18:15:25.652759+09: key (c)=(1), local row (1, 'local').
CONTEXT:  processing remote data for replication origin "pg_16395" during "INSERT" for replication target relation "public.test" in transaction 725 finished at 0/014C0378

The LSN of the transaction that contains the change violating the constraint and the replication origin name can be found from the server log (LSN 0/014C0378 and replication origin pg_16395 in the above case). The transaction that produced the conflict can be skipped by using ALTER SUBSCRIPTION ... SKIP with the finish LSN (i.e., LSN 0/014C0378). The finish LSN could be an LSN at which the transaction is committed or prepared on the publisher. Alternatively, the transaction can also be skipped by calling the pg_replication_origin_advance() function. Before using this function, the subscription needs to be disabled temporarily either by ALTER SUBSCRIPTION ... DISABLE or, the subscription can be used with the disable_on_error option. Then, you can use pg_replication_origin_advance() function with the node_name (i.e., pg_16395) and the next LSN of the finish LSN (i.e., 0/014C0379). The current position of origins can be seen in the pg_replication_origin_status system view. Please note that skipping the whole transaction includes skipping changes that might not violate any constraint. This can easily make the subscriber inconsistent. The additional details regarding conflicting rows, such as their origin and commit timestamp can be seen in the DETAIL line of the log. But note that this information is only available when track_commit_timestamp is enabled on the subscriber. Users can use this information to decide whether to retain the local change or adopt the remote alteration. For instance, the DETAIL line in the above log indicates that the existing row was modified locally. Users can manually perform a remote-change-win.

When the streaming mode is parallel, the finish LSN of failed transactions may not be logged. In that case, it may be necessary to change the streaming mode to on or off and cause the same conflicts again so the finish LSN of the failed transaction will be written to the server log. For the usage of finish LSN, please refer to ALTER SUBSCRIPTION ... SKIP.