Repository navigation
Description
Is there an existing issue for the same bug?
- I searched open and closed issues. This is a follow-up gap in the fix for [Bug]: stale table generation after cross-CN ALTER can surface ExpectedEOB #26756 / fix(txn): prevent stale table generation writes after DDL #26757, specifically for cached prepared plans whose lifetime crosses transactions.
Branch Name
main
Commit ID
a53f5a3b75 (base used by the failing PR run; failure reproduced at PR head 3a429afd6a)
Other Environment Information
- CI: two-CN compose BVT, pessimistic transaction mode, RC isolation
- Workflow job: https://github.com/matrixorigin/matrixone/actions/runs/32955020443/job/98136868773?pr=27648
- Failing case:
test/distributed/cases/pessimistic_transaction/prepare.sql
Actual Behavior
A cached prepared DML plan can remain bound to an old physical table ID after a copy-based ALTER TABLE commits on another CN. The executing transaction is newer than the DDL commit, so lockservice suppresses the retained definition-change fence. TN's authoritative generation check then rejects the stale write during commit and exposes:
txn need retry in rc mode, def changed
The valid EXECUTE fails and the expected row is absent.
Failing SQL:
drop table if exists t1;
create table t1 (a int primary key, b int);
begin;
alter table t1 alter column b drop default;
-- another session/CN
use prepare;
prepare s1 from insert into t1 values (?, 2);
set @a=1;
-- wait for ALTER to commit
execute s1 using @a;
select * from t1;
commit;Expected Behavior
Under RC, a committed table-generation change must cause the stale prepared statement to rebuild its logical and physical plan against the new table ID and retry transparently. The internal retry signal must not escape to the client.
SI and non-retryable failures must retain their existing semantics.
Root Cause
The definition fence introduced by #26757 is filtered at lockservice using LockOptions.SnapShotTs. The CN currently fills that field with txnOp.CreateTS().
That assumes:
a logical plan cannot be older than the transaction executing it.
This is false for cached prepared statements: the plan is built by PREPARE, retained in Compile, and later reused by EXECUTE in a new transaction.
The cached-compile Reset path also overwrites the plan-generation snapshot with the newer EXECUTE transaction snapshot. Consequently both sides agree on the wrong generation boundary:
- PREPARE builds a plan for old table ID
280394. - Another CN commits the copy ALTER, replacing it with ID
280395at1787739600251721220-0. - EXECUTE starts a newer transaction at
1787739600257604781-1, but reuses the old plan. - Lockservice compares the newer transaction creation timestamp with the DDL fence and omits
TableDefChangedAt. - CN therefore does not rebuild the plan and writes using ID
280394. - TN rejects the stale generation during commit with
ErrTxnNeedRetryWithDefChanged.
This is why the compile retry loop does not repair this occurrence: the first actionable error is returned by TN commit, after pipeline execution, rather than by the lock operator inside Compile.Run.
CI Evidence
10:20:00.250244: TN drops old relation ID280394.10:20:00.250576: TN creates new relation ID280395.10:20:00.255137: DDL commits at1787739600251721220-0.10:20:00.257765: the executing CN creates a lock bind for old ID280394.10:20:00.259466: TN rejects commit for the newer transaction (S:1787739600257604781-1) withtxn need retry in rc mode, def changed.10:20:00.261936: the CN subsequently resolves/subscribes to new ID280395.
The failure is unrelated to #27648's JoinMap cancellation change; the failing statement is a single-table INSERT. It is an incomplete prepared-plan coverage case in #26757's stale-generation protection.
Required Invariant
A definition fence must be compared with the immutable snapshot of the logical plan generation, not the creation or current snapshot of the transaction executing that plan.
For a cached prepared plan, this binding must survive EXECUTE reset, ordinary data retry, child-process creation, and remote-fragment serialization. A successful definition-change rebuild must atomically advance the cached plan and its binding together.
Proposed Systematic Fix
- Store the immutable logical-plan snapshot on
Compilerather than only on the mutable sessionProcess. - Preserve it when a prepared physical pipeline is reset for another transaction and when an ordinary retry reuses the logical plan.
- Capture a new binding only when a definition-change retry rebuilds the logical plan; publish the rebuilt plan and binding together after compilation succeeds.
- Populate
LockOptions.SnapShotTsfrom the plan binding when present, with transaction creation time only as the compatibility fallback for lock callers without a compiled plan. - Keep the existing lockservice response optimization: plans at or after the retained fence still receive no marker, allocation, or wire payload.
Do not solve this with sleeps, unconditional prepared-plan rebuilds, broad client retries, or by swallowing the TN error.
Validation / Acceptance Criteria
- Deterministic UT: a plan built at T1 and executed by a transaction created at T3 still sends T1 to the retained-fence filter when DDL committed at T2.
- Deterministic UT: cached
Compile.Resetpreserves the logical-plan binding; ordinary retry preserves it; definition-change rebuild advances it. - Existing controls: callers without a plan use transaction creation time; plans built at/after the fence do not rebuild; SI still returns write-write conflict.
- Existing remote process codec coverage continues to prove propagation to child/remote fragments.
- Multi-CN pessimistic BVT
pessimistic_transaction/prepare.sqlpasses without exposingErrTxnNeedRetryWithDefChangedand inserts(1,2). - Focused race and owning-package tests pass.
Risk, Performance, and Workaround
Risk if unfixed: user-visible statement/transaction failure during concurrent copy-based DDL and prepared DML. TN prevents a stale write from committing, so current evidence indicates availability loss rather than silent corruption.
Performance target: no new RPC, polling, catalog lookup, or plan rebuild on the ordinary path. The change should replace one timestamp source in an existing lock request and retain the marker-free fast path for current plans.
Temporary workaround: after the DDL is visible on the executing CN, deallocate/re-prepare and retry the statement. For an explicit transaction, roll back and retry the whole transaction; do not blindly retry only a partially executed statement.
Activity
fix(txn): preserve cached plan generation across transactions (#27738)
XuPeng-SH commentedon Sep 14, 2026
已由已合并的 PR #27738 完成:prepared/session cached plan 保留 DDL generation fence,相关缓存、重试和版本兼容路径已合入。关闭为 completed。
fix(txn): preserve cached plan generation across transactions (matrix…
fix(txn): preserve cached plan generation across transactions (matrix…
fix(txn): preserve cached plan generation across transactions (matrix…