Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: not use the latest schema when plan-cache is off (#30959) #40046

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion planner/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/parser"
"github.com/pingcap/parser/ast"
"github.com/pingcap/tidb/bindinfo"
Expand Down Expand Up @@ -238,12 +239,16 @@ func optimize(ctx context.Context, sctx sessionctx.Context, node ast.Node, is in
sctx.GetSessionVars().RewritePhaseInfo.DurationRewrite = time.Since(beginRewrite)

if execPlan, ok := p.(*plannercore.Execute); ok {
failpoint.Inject("optimizeExecuteStmt", nil)
execID := execPlan.ExecID
if execPlan.Name != "" {
execID = sctx.GetSessionVars().PreparedStmtNameToID[execPlan.Name]
}
if preparedPointer, ok := sctx.GetSessionVars().PreparedStmts[execID]; ok {
if preparedObj, ok := preparedPointer.(*core.CachedPrepareStmt); ok && preparedObj.ForUpdateRead {
// When plan cache is enabled, and it's a for-update read, use the latest schema to detect plan expired.
// Otherwise, do not update schema to avoid plan and execution use different schema versions.
if preparedObj, ok := preparedPointer.(*core.CachedPrepareStmt); ok && preparedObj.ForUpdateRead &&
preparedObj.PreparedAst.UseCache {
is = domain.GetDomain(sctx).InfoSchema()
}
}
Expand Down
28 changes: 28 additions & 0 deletions session/pessimistic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2648,3 +2648,31 @@ func (s *testPessimisticSuite) TestChangeLockToPut(c *C) {

tk.MustExec("admin check table t1")
}

func (s *testPessimisticSuite) TestIssue30940(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk2 := testkit.NewTestKitWithInit(c, s.store)

tk.MustExec("use test")
tk2.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(id int primary key, v int)")
tk.MustExec("insert into t values(1, 1)")

stmts := []string{
"update t set v = v + 1 where id = 1",
"delete from t where id = 1",
}
errCh := make(chan error, 1)
for _, stmt := range stmts {
tk.MustExec(fmt.Sprintf("prepare t from '%s'", stmt))
tk2.MustExec("alter table t add column a int")
c.Assert(failpoint.Enable("github.com/pingcap/tidb/planner/optimizeExecuteStmt", "pause"), IsNil)
go func() {
errCh <- tk.ExecToErr("execute t")
}()
tk2.MustExec("alter table t drop column a")
c.Assert(failpoint.Disable("github.com/pingcap/tidb/planner/optimizeExecuteStmt"), IsNil)
c.Assert(<-errCh, IsNil)
}
}