Skip to content

[Bug]: CREATE INDEX concurrent with committed INSERTs leaves index results incomplete #27487

Closed

Description

@Ariznawlll

Description

When CREATE INDEX overlaps committed INSERTs from another connection, the DDL returns successfully but the new index can permanently miss committed rows. Fulltext indexes reproduce consistently; regular secondary indexes reproduce intermittently. Equivalent scans of the base table return the complete result set, and the mismatch survives a service restart.

Environment

  • Branch: main
  • Commit: 596b2f21131b9391f9fbef41f474feb57ca070d5
  • Deployment: local Docker, 1 Log + 1 TN + 2 CN, querying a direct CN SQL endpoint.
  • Client: MySQL Connector/J 8.0.33 with server prepared statements.
  • Date: 2026-08-22

The same behavior was also observed on the then-current official matrixorigin/matrixone:latest image (v4.2.0, commit 01ae8ec).

Steps to reproduce

  1. Create one of the following tables and insert 1,000 rows:

    CREATE TABLE t(id BIGINT PRIMARY KEY, k INT);
    -- or
    CREATE TABLE t(id BIGINT PRIMARY KEY, body TEXT);
  2. Use two independent connections. The writer commits row 1001, then both connections are released together. The writer commits rows 1002 through 1200 one at a time while the DDL connection runs:

    CREATE INDEX idx ON t(k);
    -- or
    CREATE FULLTEXT INDEX idx ON t(body);
  3. Wait for both connections to return successfully, then compare each index query with a semantically equivalent base-table scan:

    -- regular index
    SELECT COUNT(*) FROM t WHERE k = 0;
    SELECT SUM(CASE WHEN k = 0 THEN 1 ELSE 0 END) FROM t;
    
    -- fulltext index
    SELECT COUNT(*) FROM t WHERE MATCH(body) AGAINST('officialracezebra');
    SELECT COUNT(*) FROM t WHERE body LIKE '%officialracezebra%';

The JDBC reproducer uses a latch after the writer's first committed row; it is not based on sleeps or an unbounded background load.

Actual behavior

On the exact main commit above:

  • Regular index: 13 of 20 runs returned rows=1200, indexed=119, scanned=120.
  • Fulltext index: 13 of 13 runs returned rows=1200, indexed=1189..1193, scanned=1200.
  • Keeping one reproduced database and restarting the complete local service set preserved the mismatch: regular index 119/120; fulltext index 1187/1200.

EXPLAIN confirms that the regular query uses Index Table Scan on t.idx, while the fulltext query uses Table Function on fulltext_index_scan; these are not fallback base-table scans.

Expected behavior

If CREATE INDEX and every INSERT complete successfully, index results must contain exactly the committed base-table rows. A restart must not change that invariant.

Stability and controls

  • Reproducer: regular index 13/20; fulltext index 13/13.
  • Serial control: open the writer connection before DDL but write the 200 rows only after CREATE INDEX returns. Both variants were correct 3/3 (120/120 for regular and 1200/1200 for fulltext).
  • Rejected-DDL atomicity: creating an already existing idx returned 42000/1061 for both variants, 3/3. Base-table and existing-index counts remained correct, and a later valid insert was indexed correctly.
  • No panic, fatal, or OOM marker was present in the service logs during the validation runs.

Evidence

Representative exact-main output:

RACE NORMAL r2 rows=1200 indexed=119 scanned=120
RACE NORMAL r3 rows=1200 indexed=119 scanned=120
RACE FULLTEXT r1 rows=1200 indexed=1190 scanned=1200
RACE FULLTEXT r2 rows=1200 indexed=1191 scanned=1200
RACE FULLTEXT r3 rows=1200 indexed=1191 scanned=1200
CONTROL NORMAL r1..r3 rows=1200 indexed=120 scanned=120
CONTROL FULLTEXT r1..r3 rows=1200 indexed=1200 scanned=1200

Code analysis

This is a code-path hypothesis, not a confirmed root cause. doCreateIndex first creates and backfills hidden index tables (regular indexes through handleRegularSecondaryIndexTable, fulltext through plugin HandleCreateIndex) and calls UpdateConstraint afterwards. A committed-write interval between the backfill read and the index-maintenance publication would match the observed small, durable gap.

Relevant locations:

  • pkg/sql/compile/ddl.go:2745-2815
  • pkg/sql/compile/ddl_index_algo.go:55-72
  • pkg/fulltext/plugin/compile/compile.go:51-102

Regression coverage

Do not baseline the failing behavior. After a fix, add a two-connection scenario under motr/suites/scenarios that covers regular and fulltext index creation, concurrent commits, and post-restart result equivalence.

Duplicate check

I searched open and closed Issues and PRs using CREATE INDEX + concurrent, fulltext + concurrent, index + missing rows, and fulltext_index_scan. I found no matching report for permanently incomplete index results after successful concurrent DDL/DML. #27142 concerns an irregular-index UPDATE lock-order issue and is not the same behavior.

Activity

  1. added this to the MO-202608 milestone on Aug 22, 2026
  2. added
    kind/bug
    on Aug 22, 2026
  3. changed the title [-][Bug]: CREATE INDEX 与并发已提交 INSERT 交错时,全文索引稳定漏项、普通索引间歇漏项[/-] [+][Bug]: CREATE INDEX concurrent with committed INSERTs leaves index results incomplete[/+] on Aug 22, 2026
  4. added theissue type on Aug 22, 2026
  5. cpegeric commented on Aug 27, 2026

    @cpegeric
    Contributor

    both regular index and fulltext index have the same problem. Better assign engine developer to take a look first.

  6. Ariznawlll commented on Aug 31, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:aa7b969f55e655f3f201fd3337589d070f994494
    • 修复提交:52336df80596de93a3f279dfcd6c993d7227bcb9(PR fix: preserve insert metadata lock during index creation #27792)
    • 部署方式:本地隔离部署,1 LogService + 1 TN + 2 CN + Proxy;writer/DDL 分别直连两个不同 CN,锁状态及结果校验通过 Proxy
    • 测试日期:2026-08-31

    原问题验证

    使用 1,000 行原始触发规模,通过两个独立连接验证 INSERT 与 CREATE INDEX 的提交边界。测试不使用固定 sleep:先从 mo_locks() 识别 writer 在 mo_tables 上新增的 shared metadata lock,再启动另一 CN 的 CREATE INDEX,确认同一个 lock 上已经出现 waiter 后才提交或回滚 writer。

    • 修复前 2e1be0927882349e3b1082c1847b60cbb0b0769b:无法观察到 writer 的目标表 metadata lock,按预期 RED,复现了修复前的锁缺口。
    • 当前 main:10 轮全部完成,普通二级索引与 FULLTEXT 索引均未出现缺行、重复行或持久化索引不一致。
    • 最终标记:ISSUE_27487_PASS runs=10 seed_rows=1000。

    Happy Path 覆盖

    1. 普通二级索引 commit:CREATE INDEX 确认等待未提交 INSERT;writer 提交后 DDL 成功,FORCE INDEX(idx_k) 与 IGNORE INDEX(idx_k) 结果完全一致,EXPLAIN 确认真正使用 Index Table Scan。
    2. FULLTEXT commit:CREATE FULLTEXT INDEX 确认等待未提交 INSERT;writer 提交后,MATCH ... AGAINST 与基表 LIKE 行数完全一致。
    3. DDL 完成后继续合法 INSERT:普通索引与 FULLTEXT 新写入均立即可查询,索引结果继续与基表一致。
    4. 普通索引/FULLTEXT × commit/rollback 共 4 个组合,每个组合连续执行 10 轮,共 40/40 通过。

    Unhappy Path 覆盖

    1. Writer rollback:DDL 已进入 metadata lock 等待后回滚 INSERT;DDL 随后成功,回滚行未出现在基础表或索引中,无幽灵索引项;后续合法 INSERT 可正常索引。
    2. 重复创建同名普通索引和 FULLTEXT 索引:均返回预期 duplicate-index 1061;错误后基础表和已有索引结果保持一致。
    3. 重复 DDL 错误后继续执行 SELECT 1 和合法写入,连接可继续使用,未残留阻塞事务。
    4. 运行日志未发现 transfer rowids failed、panic、fatal、OOM。

    白盒验证

    • TestMarkInsertTableScansNotLockMetaDoesNotMutateWriteTarget:连续 10/10 通过,验证 scan-local NotLockMeta 不再污染 INSERT write target。
    • TestIssue27487ConcurrentInsertIsIncludedInNewIndex:真实 2CN 集成测试连续 10/10 通过;每轮同时覆盖普通索引和 FULLTEXT 索引,并确认 DDL waiter 后再提交 writer。
    • -race 不适用:本问题验收目标是跨 CN SQL metadata-lock 与索引可见性,不涉及测试专用的 Go 对象生命周期或资源所有权;并发路径已由真实 2CN 黑盒和集成测试覆盖。

    回归测试

    • MOTR 并发场景 PR:mo-nightly-regression#1559
    • 驱动:script/issue_27487_concurrent_index.py
    • 接入位置:CONCURRENT SCENARIO TEST,默认执行 1,000 行 × 10 轮,writer/DDL 使用不同 CN。
    • PR 校验:Actions run 33380315275,validate 通过(6m46s)。
    • 不需要 big-data:缺陷由 DDL/DML 提交时序触发,1,000 行即可稳定覆盖正确锁路径,不依赖规模、spill 或内存阈值。
    • 不需要 chaos/PITR/snapshot:原问题不涉及节点/网络故障、历史恢复或备份恢复。

    测试证据

    ISSUE_27487_CASE_PASS run=10 index=regular outcome=commit
    ISSUE_27487_CASE_PASS run=10 index=regular outcome=rollback
    ISSUE_27487_CASE_PASS run=10 index=fulltext outcome=commit
    ISSUE_27487_CASE_PASS run=10 index=fulltext outcome=rollback
    ISSUE_27487_PASS runs=10 seed_rows=1000
    

    测试结论

    测试通过。

  7. Ariznawlll commented on Aug 31, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:3f05ced24a6e3b84e5ddf37e40d256a8fac02368
    • 部署方式:TKE 3 CN;writer 与 DDL 分别直连不同 CN,结果通过集群 SQL Service 校验
    • 测试日期:2026-08-31

    原问题验证

    使用 1,000 行初始数据,通过两个独立连接执行 INSERT 与 CREATE INDEX 并发验证。测试通过 mo_locks() 确认 writer 持有目标表 metadata shared lock,并确认 CREATE INDEX 已进入该锁的 waiter 队列后,再提交或回滚 writer;不依赖固定 sleep。

    普通二级索引与 FULLTEXT 索引分别执行 commit、rollback 两种路径,连续运行 10 轮,共 40 个组合全部通过。最终输出:

    ISSUE_27487_PASS runs=10 seed_rows=1000
    

    Happy Path 覆盖

    1. 普通二级索引 commit:DDL 等待 writer metadata lock;commit 后 DDL 成功,FORCE INDEX(idx_k) 与 IGNORE INDEX(idx_k) 结果一致,EXPLAIN 确认真正使用 Index Table Scan。
    2. FULLTEXT commit:DDL 等待 writer metadata lock;commit 后 MATCH ... AGAINST 与基表 LIKE 结果一致。
    3. DDL 完成后继续执行合法 INSERT,普通索引和 FULLTEXT 均可查询到新行,索引结果继续与基表一致。
    4. 普通索引/FULLTEXT × commit/rollback 连续 10 轮,共 40/40 通过。

    Unhappy Path 覆盖

    1. Writer rollback:DDL 等待后回滚 INSERT;DDL 随后成功,回滚行未出现在基表或索引中,无幽灵索引项。
    2. 普通索引与 FULLTEXT 重复创建同名索引,均返回预期错误 1061;错误后已有索引与基表结果仍一致。
    3. 重复 DDL 错误后继续执行 SELECT 1 和合法写入,连接可继续使用,未残留阻塞事务。
    4. 本次 job 未出现 ISSUE_27487_FAIL、Traceback 或断言失败。

    白盒验证

    修复 PR 已在 main 中包含 TestMarkInsertTableScansNotLockMetaDoesNotMutateWriteTarget 与 TestIssue27487ConcurrentInsertIsIncludedInNewIndex,分别覆盖 INSERT write target metadata lock 保留及真实 2 CN 锁交接路径。本轮验收重点为 TKE 黑盒回归,未重复执行白盒测试。

    回归测试

    • MOTR 并发场景 PR:mo-nightly-regression#1559
    • 驱动:script/issue_27487_concurrent_index.py
    • 接入位置:CONCURRENT SCENARIO TEST
    • PR 当前状态:OPEN,检查 validate 与 Linux contracts (offline TKE Family harness) 均通过,等待 review/merge。
    • 非目标 SSB、TPCH、TPCC、load-data、sysbench、向量及 MOTR Suite jobs 在本次 focused run 中均正确跳过。
    • 不需要 big-data:触发条件是 DDL/DML metadata lock 交接,1,000 行确定性同步可以覆盖,不依赖规模、spill 或内存阈值。
    • 不需要 chaos/PITR/snapshot:原问题不涉及节点或网络故障、历史恢复及备份恢复。

    测试证据

    ISSUE_27487_CASE_PASS run=10 index=regular outcome=commit
    ISSUE_27487_CASE_PASS run=10 index=regular outcome=rollback
    ISSUE_27487_CASE_PASS run=10 index=fulltext outcome=commit
    ISSUE_27487_CASE_PASS run=10 index=fulltext outcome=rollback
    ISSUE_27487_PASS runs=10 seed_rows=1000
    

    测试结论

    测试通过。

  8. Ariznawlll commented on Aug 31, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:3f05ced24a6e3b84e5ddf37e40d256a8fac02368
    • 部署方式:TKE 3 CN;writer 与 DDL 分别直连不同 CN,结果通过集群 SQL Service 校验
    • 测试日期:2026-08-31

    原问题验证

    使用 1,000 行初始数据,通过两个独立连接执行 INSERT 与 CREATE INDEX 并发验证。测试通过 mo_locks() 确认 writer 持有目标表 metadata shared lock,并确认 CREATE INDEX 已进入该锁的 waiter 队列后,再提交或回滚 writer;不依赖固定 sleep。

    普通二级索引与 FULLTEXT 索引分别执行 commit、rollback 两种路径,连续运行 10 轮,共 40 个组合全部通过。最终输出:

    ISSUE_27487_PASS runs=10 seed_rows=1000
    

    Happy Path 覆盖

    1. 普通二级索引 commit:DDL 等待 writer metadata lock;commit 后 DDL 成功,FORCE INDEX(idx_k) 与 IGNORE INDEX(idx_k) 结果一致,EXPLAIN 确认真正使用 Index Table Scan。
    2. FULLTEXT commit:DDL 等待 writer metadata lock;commit 后 MATCH ... AGAINST 与基表 LIKE 结果一致。
    3. DDL 完成后继续执行合法 INSERT,普通索引和 FULLTEXT 均可查询到新行,索引结果继续与基表一致。
    4. 普通索引/FULLTEXT × commit/rollback 连续 10 轮,共 40/40 通过。

    Unhappy Path 覆盖

    1. Writer rollback:DDL 等待后回滚 INSERT;DDL 随后成功,回滚行未出现在基表或索引中,无幽灵索引项。
    2. 普通索引与 FULLTEXT 重复创建同名索引,均返回预期错误 1061;错误后已有索引与基表结果仍一致。
    3. 重复 DDL 错误后继续执行 SELECT 1 和合法写入,连接可继续使用,未残留阻塞事务。
    4. 本次 job 未出现 ISSUE_27487_FAIL、Traceback 或断言失败。

    白盒验证

    修复 PR 已在 main 中包含 TestMarkInsertTableScansNotLockMetaDoesNotMutateWriteTarget 与 TestIssue27487ConcurrentInsertIsIncludedInNewIndex,分别覆盖 INSERT write target metadata lock 保留及真实 2 CN 锁交接路径。本轮验收重点为 TKE 黑盒回归,未重复执行白盒测试。

    回归测试

    • MOTR 并发场景 PR:mo-nightly-regression#1559
    • 驱动:script/issue_27487_concurrent_index.py
    • 接入位置:CONCURRENT SCENARIO TEST
    • PR 当前状态:已合入,merge commit:d0e966d3410ed21f7a193f0fa4b0127fef10d32f;检查 validate 与 Linux contracts (offline TKE Family harness) 均通过。
    • 非目标 SSB、TPCH、TPCC、load-data、sysbench、向量及 MOTR Suite jobs 在本次 focused run 中均正确跳过。
    • 不需要 big-data:触发条件是 DDL/DML metadata lock 交接,1,000 行确定性同步可以覆盖,不依赖规模、spill 或内存阈值。
    • 不需要 chaos/PITR/snapshot:原问题不涉及节点或网络故障、历史恢复及备份恢复。

    测试证据

    ISSUE_27487_CASE_PASS run=10 index=regular outcome=commit
    ISSUE_27487_CASE_PASS run=10 index=regular outcome=rollback
    ISSUE_27487_CASE_PASS run=10 index=fulltext outcome=commit
    ISSUE_27487_CASE_PASS run=10 index=fulltext outcome=rollback
    ISSUE_27487_PASS runs=10 seed_rows=1000
    

    修复、最新 main 黑盒验证及 MOTR 回归合入均已完成,关闭本 issue。

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s-1

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions