【问题标题】:PostgreSQL database replication failure because of duplicate key violation由于重复键冲突,PostgreSQL 数据库复制失败
【发布时间】:2020-02-14 11:32:18
【问题描述】:

我已经使用以下软件版本配置了两台具有冗余配置(master-master setup)的 RHEL 机器。

  • 数据库:PostgreSQL 9.5
  • Bucardo:Bucardo 5.4

数据库复制一直顺利进行,直到某个时间点,之后复制突然停止。我可以在 bucardo 日志中看到以下语句。

*KID (database_sync) Conflicts for public.interface: 4
KID (database_sync) Conflicts have been resolved
KID (database_sync) Warning! Aborting due to exception for public.interface:? Error was DBD::Pg::db pg_putcopyend failed: ERROR:  duplicate key value violates unique constraint "interface_nodeid_ifindex_unique_idx"\nDETAIL:  Key (nodeid, ifindex)=(81, 1) already exists.\nCONTEXT:  COPY interface, line 2 at /usr/share/perl5/vendor_perl/Bucardo.pm line 9879.
KID (database_sync) Kid has died, error is: DBD::Pg::db pg_putcopyend failed: ERROR:  duplicate key value violates unique constraint "interface_nodeid_ifindex_unique_idx"\nDETAIL:  Key (nodeid, ifindex)=(81, 1) already exists.\nCONTEXT:  COPY interface, line 2 at /usr/share/perl5/vendor_perl/Bucardo.pm line 9879. Line: 5041 Main DB state: ? Error: none DB database_node1 state: ? Error: none DB database_node2 state: 23505 Error: 7
DBI::db=HASH(0x2e1a708)->disconnect invalidates 9 active statement handles (either destroy statement handles or call finish on them before disconnecting) at /usr/share/perl5/vendor_perl/Bucardo.pm line 2756.
DBI::db=HASH(0x2e1a828)->disconnect invalidates 9 active statement handles (either destroy statement handles or call finish on them before disconnecting) at /usr/share/perl5/vendor_perl/Bucardo.pm line 2756.
KID (database_sync) Kid 22825 exiting at cleanup_kid. Sync "database_sync" public.interface Reason: DBD::Pg::db pg_putcopyend failed: ERROR:  duplicate key value violates unique constraint "interface_nodeid_ifindex_unique_idx"\nDETAIL:  Key (nodeid, ifindex)=(81, 1) already exists.\nCONTEXT:  COPY interface, line 2 at /usr/share/perl5/vendor_perl/Bucardo.pm line 9879. Line: 5041 Main DB state:

?错误:无 DB database_node1 状态:?错误:无 DB database_node2 状态:23505 错误:7 MCP 启动 check_sync_health*

以下是我创建同步的步骤。

bucardo add database bucardo add table all --db="$database"_node1    --herd="$database"_herd 
bucardo add sequence all --db="$database"_node1 --herd="$database"_herd 
bucardo add dbgoup "$database"_group 
bucardo add dbgroup "$database"_group    "$database"_node1:source 
bucardo add dbgroup "$database"_group    "$database"_node2:source 
bucardo add sync "$database"_sync    herd="$database_herd"_herd dbs="$database"_group    conflict_strategy=bucardo_latest onetimecopy=2 stayalive=1    kidsalive=1 autokick=1 systemctl enable bucardo.service

能否请您帮助我解决该问题的发现。 我的数据库中的序列可以负责在数据库中创建重复条目吗?

【问题讨论】:

  • 大师体验是否在任何时候崩溃?在数据库因磁盘空间不足而崩溃后,我遇到了类似的问题(主键表中的条目重复)。
  • 那我需要检查崩溃,你是怎么解决的?
  • 我不得不进行转储并从转储中重新创建数据库,因为数据库中存在多个问题。可能值得尝试重新创建有问题的表,看看是否有帮助。

标签: postgresql replication database-replication multi-master-replication bucardo


【解决方案1】:

当用户使用 Bucardo 序列进行多源设置时不得复制,否则会出现冲突,因为您已经进行了实验。

  • 从同步中删除序列。我是凭记忆写的,而不是测试它,所以要小心
bucardo remove sequence all
bucardo validate all
bucardo reload
  • 更改每个源中的序列以避免在两个数据库中创建相同的主键。 Check this answer 有几个选项,比如生成偶数、奇数;在 db1 中生成 1、11、21 等数字,在 db2 中生成 2、12、22,...;或使用 UUID。

【讨论】:

    猜你喜欢
    • 2018-08-30
    • 2012-01-24
    • 2011-02-16
    • 2018-11-17
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多