逻辑复制中的冲突解决
学习逻辑复制中冲突产生的原因,以及 PostgreSQL 提供的检测和解决冲突的策略。
逻辑复制中的冲突解决 是 CoddyKit 上的免费 Advanced PostgreSQL: Indexing, Partitioning, Replication 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Advanced PostgreSQL: Indexing, Partitioning, Replication 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Conflicts Happen
Unlike physical replication, logical replication applies changes as SQL-level operations on the subscriber. If local data diverges from the incoming change, a conflict occurs and apply stops.
Common Conflict Types
Typical conflicts include:
- Unique violation — a row with the same key already exists
- Missing row — an UPDATE or DELETE targets a row not present on the subscriber
- Type or constraint mismatch
Apply Worker Stalls
When a conflict hits, the subscription's apply worker errors out and retries the same transaction repeatedly, so replication lag grows until you intervene.
Finding the Conflict
The PostgreSQL log records the failing transaction and its LSN. You can also inspect subscription stats.
SELECT subname, last_error_time, last_error_message
FROM pg_stat_subscription_stats;Resolving by Fixing Data
One option is to make the local data consistent so the change can apply: delete the conflicting row, then let replication retry.
DELETE FROM products WHERE id = 42;
-- the incoming INSERT for id 42 now appliesSkipping a Transaction
If the offending change should be ignored, skip it by LSN. Use this carefully because it abandons that data.
ALTER SUBSCRIPTION my_sub SKIP (lsn = '0/14A0B30');Disabling on Error
Set the subscription to disable itself on error so it stops retrying and waits for an operator.
ALTER SUBSCRIPTION my_sub SET (disable_on_error = true);Preventing Conflicts
Reduce conflicts by:
- Using a single writer per logical dataset
- Avoiding overlapping primary key ranges across nodes
- Ensuring the subscriber schema matches the publisher
Replica Identity Matters
UPDATE and DELETE need a replica identity to locate the target row. A table without a primary key needs REPLICA IDENTITY FULL or replication will fail.
ALTER TABLE logs REPLICA IDENTITY FULL;Monitoring Lag
Watch pg_replication_slots on the publisher; a stalled subscriber holds back WAL and grows the slot, risking disk exhaustion.
SELECT slot_name, active, pg_size_pretty(
pg_wal_lsn_diff(pg_current_wal_lsn(), confirmed_flush_lsn))
FROM pg_replication_slots;Document a Runbook
Because conflicts need manual judgement, teams keep a runbook: how to identify, whether to fix-data or skip, and how to verify consistency afterward.
Quick Check
An incoming INSERT hits a unique violation on the subscriber. What is the safest resolution?
Recap
You learned how logical replication conflicts arise, how to find them via stats and logs, and how to resolve them by fixing data, skipping an LSN, or using disable_on_error. Proper replica identity and single-writer design prevent most conflicts.
常见问题解答
「逻辑复制中的冲突解决」课时是免费的吗?
是的 — 「逻辑复制中的冲突解决」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程的其余内容,请升级到 CoddyKit PRO。 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程共包含 4 节课。
「逻辑复制中的冲突解决」这节课中我会学到什么?
学习逻辑复制中冲突产生的原因,以及 PostgreSQL 提供的检测和解决冲突的策略。 你通过在浏览器中直接运行的动手代码来练习 Advanced PostgreSQL: Indexing, Partitioning, Replication,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Advanced PostgreSQL: Indexing, Partitioning, Replication 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「逻辑复制中的冲突解决」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课中编写并运行代码吗?
能。每节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。