Sub topic 11
Apply Errors and Conflicts on the Subscriber
This chapter assumes that you know basic SQL, primary keys, PostgreSQL roles, and the difference between a publication and a subscription. You should also know how to run psql and read PostgreSQL server logs.
Logical replication applies decoded row changes on the subscriber. The apply worker executes an INSERT, UPDATE, or DELETE against the subscriber's table. If that operation violates a constraint or cannot find the expected row, the worker stops. Later changes wait until you resolve the problem.
Common causes of apply errors
The publisher and subscriber must have compatible data and table definitions. Logical replication does not automatically make independently changed databases consistent.
| Cause | Typical error | Usual reason |
|---|---|---|
| Duplicate key | `duplicate key value violates unique constraint` | The row already exists on the subscriber |
| Missing row | `UPDATE ... affected 0 rows` | A row was deleted or changed independently |
| Schema mismatch | `column does not exist` or type error | DDL was applied on only one database |
| Permission failure | `permission denied` | The subscription owner cannot modify the table |
| Conflicting constraint | `violates foreign key constraint` | Related rows arrived in an unusable order or are absent |
A common example is a duplicate key. Suppose both databases contain an orders row with id = 42, but the rows have different values. When the publisher inserts its version, the subscriber rejects it because its primary key already contains 42.
How to investigate a stopped worker
First, inspect the subscription on the subscriber:
SELECT subname,
pid,
received_lsn,
latest_end_lsn,
latest_end_time
FROM pg_stat_subscription;A missing process or an old latest_end_time suggests that the worker is not making progress. Read the PostgreSQL server log for the detailed error. The view identifies the subscription, but the log normally identifies the table, constraint, and failing statement.
Then compare the affected row and schema on both databases. Do not immediately delete or overwrite data. Determine which database contains the authoritative version and whether the conflict is isolated.
A safe recovery workflow
Use this sequence for a constraint conflict:
- Stop or disable the subscription if repeated retries create noise.
- Record the error and inspect both copies of the affected data.
- Correct the subscriber's data or schema.
- Re-enable the subscription.
- Confirm that the worker advances and that no new error appears in the log.
For example:
-- On the subscriber
ALTER SUBSCRIPTION orders_sub DISABLE;
-- Resolve the known conflict according to your data policy.
-- This example removes a duplicate row that should come from the publisher.
DELETE FROM orders
WHERE id = 42;
ALTER SUBSCRIPTION orders_sub ENABLE;The DELETE is only safe when you have verified that the subscriber's row is the incorrect copy. If the subscriber's data must be preserved, export it first or merge it into another record.
Missing rows and replica identity
An update or delete needs a way to identify the target row. A primary key is the usual replica identity. If the subscriber has already deleted that row, the apply worker cannot apply the change correctly.
You can compare counts and keys, but a complete repair may require a controlled resynchronization. For a small table, copy the table's authoritative contents into a staging table, reconcile differences, and then refresh the table's subscription data according to your PostgreSQL version and operational procedure.
Preventing future conflicts
Keep schema changes coordinated. DDL is not replicated, so add columns, constraints, and indexes deliberately on both databases. Avoid application writes to tables that the subscription manages. If both databases must accept writes, logical replication alone does not resolve concurrent edits. You need an application-level ownership or conflict-resolution design.
Exercise
The subscriber reports a duplicate primary-key error for order 42. What should you do before deleting the subscriber's row?
Show answer
Compare the publisher and subscriber rows, decide which copy is authoritative, preserve any needed subscriber data, and then make a targeted repair before re-enabling the subscription.