0Pricing
Advanced PostgreSQL: Indexing, Partitioning, Replication · Aula

Resolução de conflitos na replicação lógica

Aprenda como surgem conflitos na replicação lógica e quais estratégias o PostgreSQL oferece para detectá-los e resolvê-los.

Resolução de conflitos na replicação lógica é uma aula grátis de Advanced PostgreSQL: Indexing, Partitioning, Replication no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Advanced PostgreSQL: Indexing, Partitioning, Replication, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Advanced PostgreSQL: Indexing, Partitioning, Replication inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em 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 applies

Skipping 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.

Perguntas Frequentes

A aula “Resolução de conflitos na replicação lógica” é grátis?

Sim — o texto completo de “Resolução de conflitos na replicação lógica” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Advanced PostgreSQL: Indexing, Partitioning, Replication, atualize para CoddyKit PRO. O curso de Advanced PostgreSQL: Indexing, Partitioning, Replication inclui 4 aulas no total.

O que vou aprender em “Resolução de conflitos na replicação lógica”?

Aprenda como surgem conflitos na replicação lógica e quais estratégias o PostgreSQL oferece para detectá-los e resolvê-los. Você pratica Advanced PostgreSQL: Indexing, Partitioning, Replication com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Advanced PostgreSQL: Indexing, Partitioning, Replication?

Nenhuma experiência prévia é necessária. Advanced PostgreSQL: Indexing, Partitioning, Replication no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.

Quanto tempo leva a aula “Resolução de conflitos na replicação lógica”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Advanced PostgreSQL: Indexing, Partitioning, Replication?

Sim. Cada aula de Advanced PostgreSQL: Indexing, Partitioning, Replication inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Fundamentos da replicação lógica
  2. Configurando publicações
  3. Gerenciando assinaturas
  4. Resolução de conflitos na replicação lógica
← Voltar para Advanced PostgreSQL: Indexing, Partitioning, Replication