Repository navigation
Description
Why do you want to refactor this code?
Prepared-statement compile reuse exposed an ownership and lifecycle mismatch between reusable execution plans and transaction-scoped engine relations.
A compiled Scope and its operators can be reused across executions, but several operator containers retain engine.Relation handles across executions. At the same time, Transaction.tableCache can return the same underlying *txnTable to multiple operators, and txnTable.Reset mutates shared transaction-specific fields:
tbl.db.op = op
tbl.proc.Store(txn.proc)
tbl.createdInTxn = false
tbl.lastTS = op.SnapshotTS()This caused the Ubuntu UT race detector to report concurrent writes from Insert.Prepare and Deletion.Prepare during the second execution of a prepared UPDATE ... JOIN. The failure was observed in TestIssue25526PreparedUpdateJoinSecondExecute in Actions job 86539533869 for PR #25623.
The immediate race has been mitigated in PR #25623 by clearing Insert.ctr.source and Deletion.ctr.source from operator Reset, so those operators reacquire relations for the next execution. However, the underlying contract remains unclear and similar relation reuse still exists in other paths, including:
MultiUpdate.PrepareLockOp.PrepareLockMeta.initLockExe
The current design combines three incompatible properties:
- compiled operators are long-lived and reusable;
- relation handles contain mutable transaction-specific state;
- the same relation object can be shared by concurrently prepared or running operators.
The existing comment above txnTable.Reset already notes: TODO: txnTable should be stateless.
Describe the solution you would like
Define and enforce explicit relation ownership and lifetime rules:
- Treat relation handles stored in execution operators as execution/transaction scoped, not compile scoped.
- Keep reusable compiled operators limited to immutable plan metadata and object references.
- Reacquire or create per-execution relation handles when an execution begins.
- Make shared
txnTablestate immutable/stateless where practical, or split immutable table metadata from a transaction-bound relation wrapper. - Audit all
engine.Relation.Resetcall sites for concurrent reuse and stale-transaction risks. - Add race-enabled lifecycle tests covering two operators targeting the same table across repeated prepared executions and retry paths.
- Document whether
Relation.Resetrequires exclusive ownership and whether it may overlap withWrite,Delete, locking, or reader construction.
Acceptance criteria:
- No reusable operator retains a relation handle from a previous transaction unless the handle has a documented exclusive-ownership contract.
- Shared table-cache objects do not store unsynchronized caller-specific transaction state.
- All remaining
Relation.Resetcall sites are proven safe or replaced. - Prepared DML reuse tests pass under
-race. - The fix preserves the compile-reuse performance improvement tracked by TPCC 10 warehouse performance regression: 25% tpmC drop after commit 522224799 (prepared statement recompile fix) #25614.
Describe alternatives you have considered
- Clear relation fields in each operator
Reset: appropriate as a short-term mitigation, but easy to miss when new operators or relation fields are added. - Add a mutex only inside
txnTable.Reset: insufficient because one scope can proceed intoWriteorDeletewhile another scope is still resetting the shared object. - Serialize all scope preparation: avoids the race at a broad execution-performance cost.
- Recompile every prepared execution: avoids stale runtime state but reintroduces the 25% TPCC regression tracked by TPCC 10 warehouse performance regression: 25% tpmC drop after commit 522224799 (prepared statement recompile fix) #25614.
Additional information
Related items:
- Original prepared UPDATE JOIN hang: [Bug]: Protocol-level prepared UPDATE JOIN hangs on second execution #25526
- Compile-reuse performance regression: TPCC 10 warehouse performance regression: 25% tpmC drop after commit 522224799 (prepared statement recompile fix) #25614
- Fix under development: fix(frontend/compile): reset PipelineEdge terminal state on compiled scope reuse to fix TPCC regression #25623
- Failing CI job: https://github.com/matrixorigin/matrixone/actions/runs/29150642450/job/86539533869
- Race location:
pkg/vm/engine/disttae/txn_table.go,txnTable.Reset - Immediate mitigation commit in PR fix(frontend/compile): reset PipelineEdge terminal state on compiled scope reuse to fix TPCC regression #25623:
3c99de345ba29fd5cfa2885959ffe06d39f8b565
Activity
fix(disttae): strengthen reusable relation handle reset coverage and …
fix(disttae): strengthen reusable relation handle reset coverage and …
fix(disttae): strengthen reusable relation handle reset coverage and …
fix(disttae): strengthen reusable relation handle reset coverage and …
fix(disttae): backport exclusive relation handles to 4.1.6 hotfix (#3…