Repository navigation
Description
Summary
data branch merge fails intermittently in multi-CN PESSIMISTIC mode with:
txn need retry in rc mode, def changed
Error code: ErrTxnNeedRetryWithDefChanged (20631)
Reproduction
Observed in CI: multi cn e2e bvt test docker compose(PESSIMISTIC)
Test file: test/distributed/cases/git4data/branch/merge/merge_1.sql line 85
data branch merge t2 into t1Root Cause
data branch merge executes internal SQL (CREATE TABLE, INSERT, DELETE) via runSql() in data_branch_helpers.go:168:
opts := executor.Options{}.
WithDisableIncrStatement(). // disables compile retry
WithTxn(backSes.GetTxnHandler().GetTxn()).
WithKeepTxnAlive()WithDisableIncrStatement() sets disableRetry = true in the compile path (sql_executor.go:418), which prevents compile2.go from retrying on ErrTxnNeedRetryWithDefChanged.
When any internal INSERT/DELETE hits a lock conflict on mo_tables catalog rows (common in multi-CN PESSIMISTIC mode), lock_meta.go:198-203 converts it to ErrTxnNeedRetryWithDefChanged. Since retry is disabled, the error propagates directly to the client.
Additionally, handleBranchMerge in self_handle.go has no retry wrapper of its own.
Call chain
handleBranchMerge (self_handle.go)
→ diffMergeAgency (data_branch.go)
→ mergeDiffs / diffOnBase (parallel)
→ flushSqlValues → execSQLStatements → runSql (data_branch_helpers.go)
→ SQL executor with disableRetry=true
→ compile2 (retry disabled)
→ lock_meta.go:198 → ErrTxnNeedRetryWithDefChanged
→ error surfaces to client
Why it is flaky
In multi-CN PESSIMISTIC mode, distributed lock contention on catalog metadata rows is non-deterministic. The merge creates temp tables via CTAS and then applies INSERT/DELETE to the base table, creating multiple lock points where conflicts can occur. Single-CN tests rarely trigger this.
Suggested Fix Directions
- Add retry logic at
handleBranchMergelevel — wrap the merge operation in a retry loop forErrTxnNeedRetryWithDefChanged - Remove
WithDisableIncrStatement()for these internal SQL executions (evaluate if safe) - Add a dedicated retry wrapper in
diffMergeAgency
Related Files
pkg/frontend/data_branch_helpers.go:168—WithDisableIncrStatement()usagepkg/frontend/data_branch.go—diffMergeAgencypkg/frontend/self_handle.go—handleBranchMergeentry pointpkg/sql/compile/compile2.go:245-303— retry loop that is bypassedpkg/sql/compile/lock_meta.go:195-210— error generationpkg/sql/compile/sql_executor.go:418—disableRetrymapping
Activity
gouhongshen commentedon Sep 4, 2026
Automated AI triage
This issue requires human involvement and cannot currently be completed independently by the
automated fix loop.
Reason
Requires the transaction owner to define the safe retry boundary and atomicity contract, including whether the enclosing merge transaction is replayable.
Evidence
The path disables compile retry, reproduction depends on nondeterministic multi-CN conflicts, and retries at handler, agency, or internal-executor level have materially different semantics.
Please use this analysis to provide the missing decision, environment, evidence, or ownership
needed before the issue is returned to the AI candidate pool.
gouhongshen commentedon Sep 5, 2026
Investigation on current
mainfound a narrower remaining gap than the original report:__mo_diff_del_and__mo_diff_ins_staging SQL through frontend statement execution, preserving compile retry.__mo_diff_upd_, but the routing predicate still recognized only delete/insert staging names. Generated update-stage INSERTs and UPDATE JOINs therefore returned to theWithDisableIncrStatementpath, whereErrTxnNeedRetryWithDefChangedcan escape.__mo_diff_prefix for all apply staging names and routes every generated statement referencing it through the existing statement retry owner. No whole-merge retry was added.Executable proof:
pkg/frontendtests andmake buildpass on the rebased commit.merge_1,merge_schema_evolve, and substring-matchedmerge_10: 244/244 assertions per run, with checked-in result semantics verified.Draft PR: #28271
The issue remains open for manual validation.
gouhongshen commentedon Sep 5, 2026
PR #28271 is trying to fix this issue.
fix(frontend): keep data branch update staging retryable (#28271)
gouhongshen commentedon Sep 6, 2026
PR #28271 has merged and is awaiting manual validation. The issue remains open.
Ariznawlll commentedon Sep 16, 2026
测试版本
main9572bd3311abb16d1df939956408a727bcf83eba原问题验证
建立两组互不共享的 source/base Data Branch 表,每轮通过两个不同 CN 同时执行
DATA BRANCH MERGE branch INTO base。连续 10 轮、共 20 次 MERGE 均成功,每侧目标表均从 1 行变为 2 行,未出现ErrTxnNeedRetryWithDefChanged、20631、死锁或超时。Happy Path 覆盖
Unhappy Path 覆盖
白盒验证
PR #28271 将 Data Branch apply staging DDL/DML 路由回可重试的 frontend statement 路径,并包含
pkg/frontend、pkg/sql/compile及 routing 单测;本次没有以此替代多 CN SQL 验收。回归测试
PR #28271 保留
merge_1.sql的原 MERGE 路径覆盖,并新增生成 staging 名称与用户__mo_diff_标识符的回归;该问题是多 CN 并发专项,现有 BVT/UT 作为常规回归入口。测试证据
PR #28271;环境 10 轮汇总均为
setup=0, A=0[2], B=0[2]。测试结论
测试通过。
fix(frontend): keep data branch update staging retryable (matrixorigi…
fix(frontend): keep data branch update staging retryable (matrixorigi…
fix(frontend): keep data branch update staging retryable (matrixorigi…