Skip to content

[Bug]: ALTER TABLE during in-flight LOAD DATA fails with txn write conflict instead of waiting #31401

Open

Description

@iamlinjunhong

Environment

  • matrixorigin/matrixone, commit 26e3289 (detached HEAD of main)
  • macOS 14, Apple Silicon (arm64)
  • Single-node mo-service built in this workspace, launched with .loom-test/launch/launch.toml (copy of etc/launch with all ports offset by LOOM_TEST_PORT_OFFSET=20000; MySQL port 26201), data dir .loom-test/mo-data
  • Client: mysql CLI, default account dump/111
  • MySQL comparison: local MySQL 8.0.46 (mysqld --initialize-insecure, port 23506, same machine)

Actual Behavior

While a bulk LOAD DATA import is in flight against a table, ALTER TABLE ... ADD COLUMN on the same table fails immediately with an internal transaction write conflict and the DDL is not applied:

ERROR 20601 (HY000): txn write conflict ALTER COPY source changed after physical preparation: source data changed

SHOW COLUMNS afterwards still shows the original schema. The concurrent LOAD DATA itself commits normally (all rows landed) and the server stays healthy. Re-issuing the same ALTER after the import finishes succeeds, so manual retry is a workaround.

Reproduced 4/4 independent runs: three runs with a 356MB 3-file glob import, one with a single 612MB / 20M-row CSV (self-contained repro below).

Expected Behavior

MySQL 8.0 semantics, verified by running the identical scenario on a local MySQL 8.0.46 instance (same machine, same data, same timing): ALTER TABLE issued 2s into the import blocks on the metadata lock, returns exactly when the 20M-row LOAD DATA commits (T=36s), and applies successfully: the new column d int default 7 appears in SHOW COLUMNS and all 20,000,000 existing rows get the default value 7.

MatrixOne itself also serializes other DDL/DML against the same import correctly - DROP TABLE and TRUNCATE TABLE issued 2s into the same load both block until the load commits and then take effect (verified), and a concurrent INSERT succeeds immediately with no lost rows. Only ALTER TABLE takes the fail-fast conflict path, which breaks bulk-import + DDL interleaving that works against MySQL.

Steps to Reproduce

Prerequisite: a running mo-service on 127.0.0.1:26201 (single-node launch config, default account dump/111) and a mysql client in PATH.

#!/bin/bash
MYSQL="mysql -h 127.0.0.1 -P 26201 -u dump -p111"
CSV=/tmp/alterload_20m.csv   # ~612MB, 20M rows; import runs ~5s, enough window
awk 'BEGIN{for(i=1;i<=20000000;i++)print i",line-"i","i*2}' > "$CSV"

$MYSQL -e "drop database if exists loom_alterload;
create database loom_alterload;
create table loom_alterload.t(a int,b varchar(32),c int);"

$MYSQL -e "use loom_alterload; load data infile '$CSV' into table t;" &
LOAD=$!
sleep 2
$MYSQL -e "alter table loom_alterload.t add column d int default 7;"   # <-- fails with ERROR 20601

wait $LOAD
echo "-- schema after import:"
$MYSQL -e "show columns from loom_alterload.t;"        # only a,b,c, no d
echo "-- retry the ALTER now that the import is done:"
$MYSQL -e "alter table loom_alterload.t add column d int default 7;"   # succeeds
$MYSQL -e "select count(*), count(d) from loom_alterload.t;"           # 20000000, 20000000
$MYSQL -e "drop database loom_alterload;"
rm -f "$CSV"

Expected output (MySQL 8.0 parity): the ALTER blocks until the import commits, then the first SHOW COLUMNS already contains d int default 7 and all rows have the default. Actual output on MatrixOne: the ALTER prints ERROR 20601 (HY000) ... txn write conflict ALTER COPY source changed after physical preparation: source data changed; the first SHOW COLUMNS lists only a,b,c.

Additional information

  • Root cause pointer: the ALTER COPY commit path re-validates the source table's commit timestamp after physical preparation; when the concurrent LOAD commits and advances it, the check at pkg/sql/compile/alter.go:1680 (sourceChanged -> alterCopyPostPublicationConflict("source data changed")) converts the race into a hard 20601 failure (moerr.NewTxnWriteConflict, pkg/sql/compile/alter.go:1299) instead of waiting on the table lock or retrying internally.
  • Existing coverage: pkg/tests/issues/issue_277xx_ddl_consistency_test.go ("27725 concurrent alter conflict supports external retry") asserts the retryable-conflict behavior for concurrent ALTER-vs-ALTER; no test covers DDL-vs-LOAD interleave, where both MySQL and MO's own DROP/TRUNCATE path queue instead of failing.
  • Recommendation: either take the same table-lock wait behind in-flight bulk imports as DROP/TRUNCATE do, or internally re-run the ALTER COPY when only the source commit timestamp moved (keep the client-visible conflict for real definition changes). Both keep existing data and behavior for non-conflicting cases unchanged.
Full reproduction, exactly as Loom ran it
#!/bin/bash
# Prereq: mo-service on 127.0.0.1:26201 (single-node launch config, account dump/111), mysql client in PATH
MYSQL="mysql -h 127.0.0.1 -P 26201 -u dump -p111"
CSV=/tmp/alterload_20m.csv   # ~612MB, 20M rows; import runs ~5s so the ALTER lands inside the import window
awk 'BEGIN{for(i=1;i<=20000000;i++)print i",line-"i","i*2}' > "$CSV"

$MYSQL -e "drop database if exists loom_alterload;
create database loom_alterload;
create table loom_alterload.t(a int,b varchar(32),c int);"

$MYSQL -e "use loom_alterload; load data infile '$CSV' into table t;" &
LOAD=$!
sleep 2
$MYSQL -e "alter table loom_alterload.t add column d int default 7;"   # Actual: ERROR 20601 txn write conflict ALTER COPY source changed after physical preparation: source data changed

wait $LOAD
echo "-- schema right after the import:"
$MYSQL -e "show columns from loom_alterload.t;"   # Actual: only a,b,c (ALTER not applied)
echo "-- retry the ALTER after the import finished:"
$MYSQL -e "alter table loom_alterload.t add column d int default 7;"   # succeeds (workaround)
$MYSQL -e "select count(*), count(d) from loom_alterload.t;"           # 20000000, 20000000
$MYSQL -e "drop database loom_alterload;"
rm -f "$CSV"
echo done
# Expected (MySQL 8.0 parity, verified on local 8.0.46): the ALTER blocks until the import commits and then applies; the first SHOW COLUMNS already contains d int default 7 with all 20M rows defaulted to 7.

Found by Loom bug hunt (module: custom-82082add71; tests: white-box, black-box, concurrency; severity: S1)

Activity

  1. added
    kind/bug
    severity/s1
    on Oct 27, 2026
  2. iamlinjunhong commented on Oct 27, 2026

    @iamlinjunhong
    ContributorAuthor

    Human-confirmed change risks after #31435

    Reviewed merge commit: 043fc9d.

    The following code-review concerns were confirmed for follow-up by the user. They are not a claim that the original issue still reproduces, or that a local benchmark was run. Evidence is quoted in its original language.

    performance

    alter.go:1833在已经持有源表排他锁及全局 SNAPSHOT 生命周期排他门时调用1841重新准备整表、allocator和索引;该门在 lifecycle_admission.go:117、139、144以系统账户全局键取得。触发条件为首次准备后源数据提交变化:增加一轮数据/索引准备的 IO 和临时存储,旧对象可能保留至事务结束或 GC,大表会延长其他数据库或租户需要该门的生命周期操作等待;正常无变化路径仍只准备一次,影响幅度未实测。

    This issue is being returned to Loom's candidate list for follow-up. The PR remains merged.

  3. MatrixAdventurer commented on Oct 28, 2026

    @MatrixAdventurer
    Contributor

    【每日核实】结论:部分修复,暂不建议关闭。
    证据:原诉“in-flight LOAD 期间 ALTER 报 txn write conflict 而非等待”已由 #31435 修复(首次 ALTER 于 LOAD 提交后成功);PR #31484(2026-10-27 22:19 合入 main,14 文件 +1523/-80)将纯标量 ADD COLUMN 改为共享 admission,不再全量重准备占用全局 SNAPSHOT 门,收窄已确认的 alter.go:1833/lifecycle_admission.go:117/139/144 风险。回归测试(PR 报告):4 个 Issue31401 集成测试(含 NOT NULL 刷新、保留锁、事务清理、LOAD),新回归修复前必失败;ddl/alter_copy_add_index 95/95 两轮;10 个 BVT 脚本共 2808 语句两轮通过;./pkg/sql/compile mo-cgo-test 全包通过。
    还缺:索引/分区/FK/受保护表仍走原宽路径、额外拷贝未消除;完整 Linux CI 与报告人复验未回填(PR 亦注明保持 open 待复验)。
    本操作由LW授权的MOI智能体提交

  4. iamlinjunhong commented on Oct 28, 2026

    @iamlinjunhong
    ContributorAuthor

    Human-confirmed change risks after #31484

    Reviewed merge commit: 7e0c9d9.

    The following code-review concerns were confirmed for follow-up by the user. They are not a claim that the original issue still reproduces, or that a local benchmark was run. Evidence is quoted in its original language.

    performance

    源数据变化仍触发第二次完整数据、allocator 和索引准备(pkg/sql/compile/alter.go:1672、1713、1726、1954),大表增加 IO、临时空间及源表 X 锁持有时间,旧代对象可能留至事务结束或 GC;正常无变化只准备一次。此为前次修复遗留且本次未消除的风险,未测量幅度。

    performance

    索引、分区、FK 和历史保护表仍在系统账户全局 SNAPSHOT G-X 下刷新(pkg/sql/compile/alter_copy_scalar.go:33、167;alter.go:1763、1954;lifecycle_admission.go:120、142、147),大表可延长其他数据库或租户的生命周期操作等待。标量 G-S 改善共享拥有者并发,但仍阻塞需要 G-X 的操作,锁可保留至显式事务结束。

    This issue is being returned to Loom's candidate list for follow-up. The PR remains merged.

  5. XuPeng-SH commented on Oct 28, 2026

    @XuPeng-SH
    Contributor

    QA handoff for merged #31435 (043fc9d3b9327bb7e542bc46ec3b73526c637ddc) and follow-up #31484 (7e0c9d94fce444a2930c335135a3fc5860e1fe6a): this environment did not run current-head concurrent LOAD/ALTER, rollback or SQL; resource headroom blocked large links. The author reports separate passing tests. Previously noted broad-path gate and extra-copy performance concerns are static, not a measured regression; please verify the original concurrency contract and those limits.

    Follow-up remains open: The developer's post-#31484 follow-up retains second-copy cost and wider global admission for indexed/partitioned/FK cases. #31401 (comment) The issue is currently assigned to the developer; this is not a measured regression or a claim that the original bug still reproduces.

  6. Ariznawlll commented on Oct 29, 2026

    @Ariznawlll
    Collaborator

    Scope extension from a different workload class — plain sinusol INSERT traffic (no LOAD involved) triggers the identical failure, so this is not specific to bulk import paths:

    # table with 2000 rows; one writer thread inserting single-row INSERTs at ~100/s
    # main thread: ALTER TABLE t ADD COLUMN extra INT DEFAULT 7
    # ERROR 20601: txn write conflict ALTER COPY source changed after physical preparation: source data change...
    • Period of contention: 1 second of continuous single-row INSERTs (50 committed) — trivial concurrency by production standards, so any DDL on a hot table hits this, not just during loads.
    • Verified the failure is clean in data terms: the writer's committed rows all survive (2050 total), the ADD COLUMN is fully rolled back (SELECT extra → 1054 column does not exist), and subsequent writes continue normally.
    • The DML side never errors; only the ALTER fails — so an application's DDL step (config-management tool, Liquibase/Flyway migration) dies against any actively-written table, which for schema migrations is nearly always the case unless the table is drained first.

    This makes the issue's blast radius "any DDL on any live table" rather than "DDL during bulk load". MySQL's behavior for reference is online DDL (ALGORITHM=INPLACE/COPY with concurrent DML permitted).

    Main c6683bd2f5 (2026-10-29), dual-CN launch, pymysql.

  7. Ariznawlll commented on Oct 29, 2026

    @Ariznawlll
    Collaborator

    Second data point on the LOCK clause semantics discovered while extending the earlier comment — the LOCK=... modifier in MO does not implement wait semantics; it only changes the failure surface:

    -- with a concurrent single-row UPDATE stream (~80/s) against a 3501-row table:
    
    ALTER TABLE t ADD COLUMN cl INT, ALGORITHM=COPY, LOCK=SHARED;
    -- OK in 0.06s — concurrent writes proceed (40 succeeded during the ALTER window);
    -- real data copy verified: 3501/3501 rows carry the new column's DEFAULT after
    
    ALTER TABLE t ADD COLUMN ce INT, ALGORITHM=COPY, LOCK=EXCLUSIVE;
    -- ERROR 20601: txn write conflict ALTER COPY source changed after physical preparation

    So under identical write pressure, LOCK=EXCLUSIVE — the level whose MySQL meaning is "block writers until the DDL holds the table" — is itself aborted by the writers instead of waiting. Every path collapses to "DDL fails on conflict":

    request MySQL 8 semantic MO current behavior
    default (no LOCK) DDL waits behind row locks (or INPLACE coexists) DDL fails 20601
    LOCK=NONE DML allowed during DDL rejected with message recommending…
    LOCK=SHARED reads proceed, writes blocked writes proceed, DDL succeeds (inverted)
    LOCK=EXCLUSIVE writers blocked until DDL done DDL aborts on any concurrent write (inverted)

    The recommended-LOCK error message chain is also self-inconsistent: LOCK=NONE is rejected saying "COPY algorithm requires an exclusive lock. Try LOCK=SHARED." — but SHARED then happily allows the concurrent writes that NONE's message said must be excluded.

    Net effect for schema-migration tooling: there is no spelling of ALTER TABLE on MO that both succeeds and coordinates with concurrent DML; any tool that follows MySQL's documented lock-level URLs ends in a fail-and-retry loop. This matches the same root as the original report (no DDL/DML coordination mechanism) and suggests the fix for this issue would ideally include the LOCK-clause wait semantics, not just special-case LOAD.

  8. XuPeng-SH commented on Nov 30, 2026

    @XuPeng-SH
    Contributor

    Consolidated duplicate #32120 here as the sustained-DML form of the same ALTER COPY conflict.

    Additional independent evidence retained:

    • Continuous INSERT writers make both ADD COLUMN and MODIFY COLUMN fail with 20601: ALTER COPY source changed after physical preparation; quiet/light-write controls succeed.
    • This generalizes the original in-flight LOAD DATA trigger to ordinary sustained DML and confirms that the DDL fails instead of waiting or recovering.
    • The 2026-11-19 main TKE Weekly concurrent run again produced widespread 20601 failures; this is a nightly observation, not a new minimized reproduction.
    • PR fix(sql): let ALTER COPY recover from concurrent writes #33304 is still unmerged and is not a fix or validation claim.

    Keep LOAD DATA, sustained INSERT, ADD/MODIFY COLUMN, quiet-table controls, and LOCK-clause behavior as independent acceptance cases.

    Administrative consolidation only; the concurrent ALTER COPY defect remains open and unverified.

  9. XuPeng-SH commented on Nov 30, 2026

    @XuPeng-SH
    Contributor

    更新 #32120 合并到本工单后的状态:#33304 已合入 main(a1818496c3f7f9871f06deeeac8652252f1f15ec),覆盖悲观 RC 下 ALTER COPY 遇到数据并发变化后的有界重建,以及发布时的 lineage cutoff 检查。PR 报告三 CN、三并发写入方的 ADD/MODIFY 测试 18/18;较早评审头 d242d89b 另有两 CN 与 LOAD-wait 矩阵通过的记录。最终 head 87e7817d 的树与合入树一致,但其 Required CI 未通过,不能用旧评审代替最终验证。

    本轮未重跑合入版。仍保持本工单与现有负责人,待核对原始大批量 LOAD DATA 工况和 LOCK 修饰行为;此次合入不代表这两项或生产夜跑已验收。

  10. XuPeng-SH commented on Nov 30, 2026

    @XuPeng-SH
    Contributor

    补充最终合并版本实测:在 main@a1818496c3f7f9871f06deeeac8652252f1f15ec 的隔离集群上经 MySQL TCP 跑了已入库的 3 组回归,全部通过(Go 1.27.1,6.507 秒)。TestIssue31401AlterCopyWaitsForLoad 的 5 项覆盖无主键、带索引、分区、LOAD 回滚及取消等待;TestIssue32120AlterCopyWaitsForInsert 的 ADD 和带索引 MODIFY 两项均通过;TestIssue32120AlterCopyPreservesLateBranchDiff 通过。

    LOAD 用的是可控的两行 CSV,不等同于原大批量工况。显式 LOCK 修饰符和生产夜跑未验证,仍保留当前研发负责人。

  11. XuPeng-SH commented on Nov 30, 2026

    @XuPeng-SH
    Contributor

    补充原规模实测:在 main@a1818496c3f7f9871f06deeeac8652252f1f15ec 的单 CN 上,通过两个独立 MySQL TCP 会话,用 20,000,000 行(612,222,246 字节)CSV 重跑两轮。每轮在 LOAD 启动约 2 秒后发起 ALTER:默认语句和 ALGORITHM=COPY, LOCK=SHARED 均在 LOAD 完成后返回成功,最终 20,000,000 行全部保留且新列均为预期默认值。默认轮 LOAD/ALTER 各为 11.72/26.72 秒,SHARED 轮为 12.16/27.41 秒;静态表上的 COPY/LOCK=NONE 按预期拒绝。

    这补齐了原大文件 LOAD 工况,但没有验证 该评论 的持续小 UPDATE 下 SHARED 写入约束和 EXCLUSIVE 等待语义,也没有验证生产夜跑,所以保留研发负责人。

  12. XuPeng-SH commented on Nov 30, 2026

    @XuPeng-SH
    Contributor

    补充并发小 UPDATE 的实测(main@a1818496c3f7f9871f06deeeac8652252f1f15ec,隔离单 CN,真实 MySQL TCP)。

    • 已持锁事务:20,000 行表,先 UPDATE 并保持事务不提交;发起 ALTER 后约 150 ms 再提交该事务。COPY, LOCK=SHARED 与 COPY, LOCK=EXCLUSIVE 各跑 3 次,ALTER 都在持锁事务提交后成功,下一条 UPDATE 也成功,未见 20601 或数据丢失。
    • 持续短事务:4 个独立写会话先启动,各约每 48 ms 发一次单行 UPDATE;3,501 行与 200,000 行表共 6 轮。两种模式的 ALTER 全部成功,均未再出现 20601;SHARED 在 ALTER 请求未返回期间分别有 3/20/20 条 UPDATE 成功,EXCLUSIVE 有 4/18/17 条。逐轮核对了成功更新数和新列默认值。

    第二组证明持续更新下 ALTER 可以完成,并且两种模式都没有阻止写入覆盖整个请求窗口。客户端时间戳不能判定写入是否发生在 DDL 已获取最终锁的阶段,也没验证 SHARED/EXCLUSIVE 对读者的差异、生产夜跑或多 CN。显式锁语义仍未闭环,保留研发负责人,不转测。

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

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s1

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions