Resolución de conflictos en la replicación lógica
Aprenda cómo surgen los conflictos en la replicación lógica y qué estrategias ofrece PostgreSQL para detectarlos y resolverlos.
Resolución de conflictos en la replicación lógica es una lección gratuita de Advanced PostgreSQL: Indexing, Partitioning, Replication en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Advanced PostgreSQL: Indexing, Partitioning, Replication, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Advanced PostgreSQL: Indexing, Partitioning, Replication incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
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.
Preguntas frecuentes
¿La lección «Resolución de conflictos en la replicación lógica» es gratis?
Sí — el texto completo de «Resolución de conflictos en la replicación lógica» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Advanced PostgreSQL: Indexing, Partitioning, Replication, actualiza a CoddyKit PRO. El curso de Advanced PostgreSQL: Indexing, Partitioning, Replication incluye 4 lecciones en total.
¿Qué aprenderé en «Resolución de conflictos en la replicación lógica»?
Aprenda cómo surgen los conflictos en la replicación lógica y qué estrategias ofrece PostgreSQL para detectarlos y resolverlos. Practicas Advanced PostgreSQL: Indexing, Partitioning, Replication con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Advanced PostgreSQL: Indexing, Partitioning, Replication?
No se requiere experiencia previa. Advanced PostgreSQL: Indexing, Partitioning, Replication en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.
¿Cuánto tiempo toma la lección «Resolución de conflictos en la replicación lógica»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Advanced PostgreSQL: Indexing, Partitioning, Replication?
Sí. Cada lección de Advanced PostgreSQL: Indexing, Partitioning, Replication incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Fundamentos de la replicación lógica
- Configuración de publicaciones
- Gestión de suscripciones
- Resolución de conflictos en la replicación lógica