Résolution des conflits en réplication logique
Apprenez comment les conflits apparaissent en réplication logique et quelles stratégies PostgreSQL propose pour les détecter et les résoudre.
Résolution des conflits en réplication logique est une leçon Advanced PostgreSQL: Indexing, Partitioning, Replication gratuite sur CoddyKit. Ceci est la leçon 4 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Advanced PostgreSQL: Indexing, Partitioning, Replication, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Advanced PostgreSQL: Indexing, Partitioning, Replication comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
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.
Questions Fréquemment Posées
La leçon « Résolution des conflits en réplication logique » est-elle gratuite ?
Oui — le texte complet de « Résolution des conflits en réplication logique » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Advanced PostgreSQL: Indexing, Partitioning, Replication, passe à CoddyKit PRO. Le cours Advanced PostgreSQL: Indexing, Partitioning, Replication comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Résolution des conflits en réplication logique » ?
Apprenez comment les conflits apparaissent en réplication logique et quelles stratégies PostgreSQL propose pour les détecter et les résoudre. Tu pratiques Advanced PostgreSQL: Indexing, Partitioning, Replication avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer Advanced PostgreSQL: Indexing, Partitioning, Replication ?
Aucune expérience préalable n'est requise. Advanced PostgreSQL: Indexing, Partitioning, Replication sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 4 sur 4.
Combien de temps prend la leçon « Résolution des conflits en réplication logique » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon Advanced PostgreSQL: Indexing, Partitioning, Replication ?
Oui. Chaque leçon Advanced PostgreSQL: Indexing, Partitioning, Replication inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Principes de la réplication logique
- Configuration des publications
- Gestion des abonnements
- Résolution des conflits en réplication logique