Skip to content

[Bug]: Failed ADD UNIQUE resurrects UPDATE versions and creates duplicate primary keys #26804

Description

@XuPeng-SH

MatrixOne version

bdcada5438 (2026-08-09), local single-node deployment, 6 GiB memory cache.

What happened?

A failed ALTER TABLE ... ADD UNIQUE INDEX after online index DDL and concurrent indexed UPDATEs made previously invisible UPDATE versions visible in the base table. The table grew from 300,000 to 325,000 logical rows and now returns duplicate composite primary keys. Existing hidden indexes remain at 300,000 rows, so the base table and all physical indexes disagree.

This is persistent logical corruption, not only an incorrect duplicate diagnostic.

Sequence

The table has:

PRIMARY KEY(tenant_id,row_id),
UNIQUE KEY uq_ddl_external_tenant(external_key,tenant_id),
KEY idx_ddl_tenant_status_pk(tenant_id,status,row_id),
KEY idx_ddl_row_tenant_reverse(row_id,tenant_id)
  1. Insert 300,000 rows; row_id and external_key are globally unique.
  2. Run four concurrent autocommit UPDATE writers changing status, category, amount, and updated_at, plus readers.
  3. While writers run, add (status,category,amount,row_id,tenant_id). Stop writers after 8 seconds. ADD succeeds in 9.62 seconds.
  4. Repeat bounded writers while dropping idx_ddl_tenant_status_pk. DROP succeeds in 9.16 seconds.
  5. Verify COUNT(*)=300000, COUNT(DISTINCT row_id)=300000, all readers match, and the new physical index is 300000 / 300000 distinct key / 300000 distinct PK mapping.
  6. With no writers remaining, run:
ALTER TABLE idx_ddl_probe
  ADD UNIQUE INDEX uq_ddl_category_row_tenant(category,row_id,tenant_id);

This key is logically unique because row_id is globally unique. The statement incorrectly fails:

ERROR 1062: Duplicate entry '(cat-9,37,37)' for key '__mo_index_idx_col'

A second intentionally failing unique build on status was then issued. Both failed index names were correctly absent from the catalog, but the base table had become corrupted.

Post-failure evidence

SELECT COUNT(*),COUNT(DISTINCT row_id) FROM idx_ddl_probe;
-- 325000, 300000

SELECT COUNT(*) FROM (
  SELECT tenant_id,row_id FROM idx_ddl_probe
  GROUP BY tenant_id,row_id HAVING COUNT(*)>1
) d;
-- 25000

SELECT tenant_id,row_id,category,status,amount
FROM idx_ddl_probe WHERE row_id=37;
-- 37,37,cat-8,3,0.5100
-- 37,37,cat-9,4,0.5200

The duplicate PK groups are exactly all 6,250 rows from tenants 4, 15, 26, and 37, corresponding to one UPDATE generation from each of the four writer streams.

Direct physical audit after corruption:

object rows
base table 325,000
idx_ddl_row_tenant_reverse hidden table 300,000
idx_ddl_status_category_amount_pk hidden table 300,000
uq_ddl_external_tenant hidden table 300,000

Before the failed unique DDL, the same hidden tables and the base table were all 300,000. A fresh 20,000-row control with four sequential UPDATE generations can add the same logical unique key successfully; the online ADD/DROP + concurrent UPDATE history is relevant.

Expected behavior

Failed index DDL must be fully atomic and must never change base-table visibility. A unique-index builder must scan one current committed version per primary key. On failure, cleanup must affect only temporary index data.

Suspected risk area

The unique-index build or rollback path appears to publish/backfill rows from a stale snapshot or reuse an intermediate DDL relation in a way that resurrects one UPDATE generation. Please audit snapshot ownership, sink/rollback targets, and ADD/DROP index transaction cleanup.

Required regression checks

  • bounded concurrent indexed UPDATEs during ADD and DROP;
  • successful ADD/DROP followed by logically valid and intentionally invalid unique builds;
  • base count and duplicate composite-PK groups before/after every failure;
  • existing hidden table count, distinct encoded key, and distinct PK mapping;
  • retry, flush, restart, and rollback durability;
  • multiple writer generations and writer counts.

Activity

  1. added
    kind/bug
    on Aug 8, 2026
  2. self-assigned this
    on Aug 9, 2026
  3. added
    severity/s0
    on Aug 9, 2026
  4. XuPeng-SH commented on Aug 9, 2026

    @XuPeng-SH
    ContributorAuthor

    Fresh minimization/race results narrow the trigger but do not yet reproduce the corruption deterministically.

    Two clean 300,000-row trials repeated the same operation classes, including a higher-pressure variant:

    • 8 concurrent autocommit UPDATE writers + 4 readers;
    • 12-second writer windows around online ADD of (status,category,amount,row_id,tenant_id) and DROP of (tenant_id,status,row_id);
    • valid UNIQUE (category,row_id,tenant_id);
    • intentionally invalid UNIQUE (status).

    The valid build succeeded and the invalid build returned duplicate entry; after every stage:

    base table                         300000 rows / 300000 distinct row_id / 0 duplicate PK groups
    reverse hidden index              300000 / 300000 distinct PK
    new wide hidden index             300000 / 300000 distinct PK
    valid unique hidden index         300000 / 300000 distinct PK
    existing external unique index    300000 / 300000 distinct PK
    failed index catalog entries      0
    

    A 120,000-row 4-writer/4-reader control was also clean. The original corrupted table remains persistently 325000 / 300000 across many service restarts, so the report is still valid; these negative controls show that row count and operation class alone are insufficient and the failure is a low-probability transaction/DDL timing race. A regression/fix must use repeated fresh generations and must not overfit the one captured schedule.

  5. added 4 commits that reference this issue on Aug 10, 2026
    11cb7e1
    d81cb67
    391462f
    c3ba9c7
  6. Ariznawlll commented on Aug 14, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • MatrixOne:官方 main,commit 43923c5e64abdc174f82ef4055ca653e501c3d34。
    • 已确认该版本包含修复 PR #26905(merge commit c3ba9c7df14388956cb38d78681c6c35b75c42b8);测试版本位于修复之后。
    • 测试环境:TKE 分布式部署,2 CN + Proxy + TN/DN + Log Service。
    • 测试日期:2026-08-14。
    • 回归代码:mo-nightly-regression PR #1365,测试脚本 issue_26804_online_index_race.py。

    原问题验证

    本轮按原问题的核心触发条件执行在线索引 DDL 与并发 UPDATE,并在 DDL 完成后立即审计基表主键完整性。

    测试步骤:

    1. 创建 idx_ddl_probe,声明复合主键 (tenant_id, row_id),同时创建一个唯一二级索引和两个普通二级索引。

    2. 写入 300,000 行初始数据:tenant_id = row_id % 48,row_id 为 1~300,000,external_key 全局唯一。

    3. DDL 前检查:基表行数、主键 distinct 数量及重复主键分组数;该阶段通过,重复主键分组数为 0。

    4. 启动 8 个独立 autocommit UPDATE writer,分别持续更新 tenant 4、15、26、37、0、11、22、33。UPDATE 只修改 status/category/amount/version/payload/updated_at,不修改任何主键列。

    5. writer 运行期间在线执行:

      alter table idx_ddl_probe
        add index idx_ddl_wide_status_category_amount
          (status, category, amount, row_id, tenant_id);
    6. ADD INDEX 成功返回且索引 catalog 可见后,立即按真实声明主键审计:

      select count(*)
      from (
        select tenant_id, row_id
        from idx_ddl_probe
        group by tenant_id, row_id
        having count(*) > 1
      ) t;

    实际结果:第 1 个 generation 即发现 50,000 个重复主键分组,触发 fail-fast,原问题仍可稳定命中,验收不通过。

    该数量与测试数据严格对应:300,000 / 48 = 6,250 行/tenant,8 个 writer tenant × 6,250 = 50,000。原 issue 使用 4 个 writer 时出现 25,000 个重复主键分组,本轮扩大到 8 个 writer 后得到 50,000,现象与原缺陷模式一致。

    Happy Path 覆盖

    • TKE 环境构建、MatrixOne 启动和连接检查通过。
    • 300,000 行初始化成功。
    • DDL 前基表完整性检查通过:初始数据不存在重复复合主键。
    • 8 个 writer 均进入有效更新阶段;脚本带有 writer/DDL overlap gate,避免把“DDL 与 UPDATE 未真实重叠”误判为通过。
    • 在线 ADD INDEX 成功完成,新索引在 catalog 中可见。
    • 测试连接采用 autocommit,DDL 后审计不是显式事务中的旧快照。

    Unhappy Path 覆盖

    • 覆盖了本 issue 最关键的并发异常路径:持续 UPDATE 与在线 ADD INDEX 重叠后,检查基表是否出现旧版本复活和重复主键。
    • DDL 前重复主键为 0,DDL 后为 50,000;writer 又从未修改主键列,因此不能归因于初始化数据或业务主动改写主键。
    • 失败发生在必选的基表完整性 gate,脚本按设计立即停止,避免在已经损坏的数据上继续执行并稀释根因。
    • 因第 1 个 generation 已失败,以下后续步骤本轮未执行:在线 DROP INDEX、合法 UNIQUE 创建、故意非法 UNIQUE 的拒绝原子性、隐藏索引完整性审计、generation 2~10,以及 flush/restart durability 检查。这些不能记为通过。

    白盒验证

    本轮未重新执行 MatrixOne 白盒测试;本轮结论来自最新 main 上的 TKE 分布式黑盒并发回归。

    修复 PR #26905 已包含 transferred tombstone sink/事务路径的单元与 race 测试,但当前黑盒测试仍出现数据完整性失败,因此白盒用例不能替代本轮产品行为证据。建议研发结合 transferred_tombstone_sink 的错误传播、同步时序及跨 CN 可见性继续定位。

    回归测试

    • 已将场景加入 mo-nightly-regression PR #1365。
    • 当前参数:--rows 300000 --writers 8 --writer-seconds 8 --generations 10 --ddl-timeout 180。
    • 该缺陷属于低概率并发/数据完整性问题,放在 nightly 大数据并发回归比默认 MOTR 更合适;场景会逐 generation 重建数据,并对基表、索引及失败原子性进行审计。
    • 首次 CI 曾因 runner 缺少 _cffi_backend 在产品测试前失败;PR 已显式安装 cffi/cryptography/pymysql,本次运行环境构建和脚本启动均成功,因此当前 50,000 个重复主键分组是有效产品测试结果,不是依赖安装失败。

    测试证据

    • CI Run:31786586823

    • 并发测试 Job:94725032612

    • 关键日志:

      generation=1 phase=setup status=PASS
      generation=1 phase=add_index status=PASS
      issue=26804 status=FAIL cause=duplicate primary-key groups: 50000
      AssertionError: duplicate primary-key groups: 50000
      
    • 测试结束后的资源清理 Job 成功,测试环境已回收。

    最新 main 已包含 PR #26905,但在 300,000 行、8 个并发 writer、2 CN 的在线 ADD INDEX 场景中,第 1 个 generation 即产生 50,000 个重复复合主键分组。该结果与 writer 覆盖的数据量完全一致,且 DDL 前审计为 0,说明原始数据完整性问题仍然存在。

    测试结论

    测试不通过。

  7. added a commit that references this issue on Aug 14, 2026
    e7e0818
  8. Ariznawlll commented on Aug 16, 2026

    @Ariznawlll
    Collaborator

    验证结果

    针对本 issue,在最新 MatrixOne main 上完成了大数据并发回归复核。

    • MatrixOne commit:1f9e501594509fb5d2e0284833faf8c3426e2285
    • 部署:本地隔离单机实例,使用临时数据目录
    • 参数:300,000 行、8 个 autocommit UPDATE writers、writer window 8s、10 generations、DDL timeout 180s
    • 执行脚本:script/issue_26804_online_index_race.py

    执行结果

    • 回归脚本单测:30/30 通过
    • 10/10 generations 通过
    • 每轮 setup、add_index、drop_index、valid_unique、invalid_unique_atomicity、cleanup、status 均为 PASS
    • 最终结果:issue=26804 generations=10 rows=300000 writers=8 status=PASS

    覆盖内容

    • 并发 UPDATE 与在线 ADD/DROP INDEX 的真实重叠;
    • 成功 UNIQUE 创建;
    • 重复键导致的失败 UNIQUE 创建及原子性校验;
    • 失败 DDL 后基表行数、distinct row_id、重复复合主键组和数据 fingerprint 保持不变;
    • 隐藏索引物理行数/distinct primary key 与 catalog 映射一致;
    • 每轮独立数据库清理和状态审计。

    该回归属于 nightly big-data/concurrent 覆盖,用于验证大数据量下在线索引变更与并发写入的稳定性;本次不涉及 Chaos 故障注入。

    补充:旧的本地 4.0-dev 二进制曾在初始化阶段因隐藏索引尚未完成构建触发 0 行审计失败;切换到上述最新 main 后,1 generation 预检及完整 10 generations 均通过,因此旧结果不作为本 issue 的产品失败结论。

    测试结论

    测试通过。

  9. Ariznawlll commented on Aug 16, 2026

    @Ariznawlll
    Collaborator

    大数据量回归添加位置

    本 issue 的大数据量回归已加入 mo-nightly-regression PR #1365 的 nightly CONCURRENT TEST 流程,具体位置如下:

    因此该 case 的归类是 nightly big-data/concurrent 回归,不是 Chaos 测试;定时 nightly 主调用默认执行,其他调用方可按 selector 选择执行。

  10. added 6 commits that reference this issue on Sep 17, 2026
    8d94506
    d9632aa
    6905abe
    9d944cb
    0a578e2
    cf84d0f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions