Skip to content

[Refactoring]: Separate transaction-scoped relation state from reusable compiled operators #25627

Closed

Description

@XuPeng-SH

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.Prepare
  • LockOp.Prepare
  • LockMeta.initLockExe

The current design combines three incompatible properties:

  1. compiled operators are long-lived and reusable;
  2. relation handles contain mutable transaction-specific state;
  3. 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:

  1. Treat relation handles stored in execution operators as execution/transaction scoped, not compile scoped.
  2. Keep reusable compiled operators limited to immutable plan metadata and object references.
  3. Reacquire or create per-execution relation handles when an execution begins.
  4. Make shared txnTable state immutable/stateless where practical, or split immutable table metadata from a transaction-bound relation wrapper.
  5. Audit all engine.Relation.Reset call sites for concurrent reuse and stale-transaction risks.
  6. Add race-enabled lifecycle tests covering two operators targeting the same table across repeated prepared executions and retry paths.
  7. Document whether Relation.Reset requires exclusive ownership and whether it may overlap with Write, Delete, locking, or reader construction.

Acceptance criteria:

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 into Write or Delete while 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:

Activity

  1. added a commit that references this issue on Jul 12, 2026
    7b34bfe
  2. self-assigned this
    on Jul 12, 2026
  3. added
    severity/s0
    on Jul 12, 2026
  4. added theissue type on Jul 12, 2026
  5. added this to the MO-202607 milestone on Jul 12, 2026
  6. added 3 commits that reference this issue on Sep 17, 2026
    b5f1987
    3003e44
    1c962b6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/refactoringseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions