From 887be3db50e6ee6cf937e7527b8848f79d86626e Mon Sep 17 00:00:00 2001 From: you06 Date: Thu, 23 Dec 2021 01:49:08 +0800 Subject: [PATCH 1/4] do not use the latest schema for cache miss Signed-off-by: you06 --- planner/optimize.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/planner/optimize.go b/planner/optimize.go index f19622ee23aa1..d3de2143434cc 100644 --- a/planner/optimize.go +++ b/planner/optimize.go @@ -243,7 +243,8 @@ func optimize(ctx context.Context, sctx sessionctx.Context, node ast.Node, is in execID = sctx.GetSessionVars().PreparedStmtNameToID[execPlan.Name] } if preparedPointer, ok := sctx.GetSessionVars().PreparedStmts[execID]; ok { - if preparedObj, ok := preparedPointer.(*core.CachedPrepareStmt); ok && preparedObj.ForUpdateRead { + if preparedObj, ok := preparedPointer.(*core.CachedPrepareStmt); ok && preparedObj.ForUpdateRead && + preparedObj.PreparedAst.UseCache { is = domain.GetDomain(sctx).InfoSchema() } } From 6b00ab19c6fb60ea77672e89e03d374bacf09b73 Mon Sep 17 00:00:00 2001 From: you06 Date: Thu, 23 Dec 2021 14:01:57 +0800 Subject: [PATCH 2/4] add test and comment Signed-off-by: you06 --- go.sum | 1 + planner/optimize.go | 4 ++++ session/pessimistic_test.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/go.sum b/go.sum index 9111eac2df13c..c295bb814cf04 100644 --- a/go.sum +++ b/go.sum @@ -507,6 +507,7 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD github.com/sasha-s/go-deadlock v0.2.0/go.mod h1:StQn567HiB1fF2yJ44N9au7wOhrPS3iZqiDbRupzT10= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/sergi/go-diff v1.0.1-0.20180205163309-da645544ed44/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= +github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/shirou/gopsutil v2.19.10+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/shirou/gopsutil v3.21.2+incompatible h1:U+YvJfjCh6MslYlIAXvPtzhW3YZEtc9uncueUNpD/0A= diff --git a/planner/optimize.go b/planner/optimize.go index d3de2143434cc..7933f4b83779b 100644 --- a/planner/optimize.go +++ b/planner/optimize.go @@ -16,6 +16,7 @@ package planner import ( "context" "fmt" + "github.com/pingcap/failpoint" "math" "runtime/trace" "strings" @@ -238,11 +239,14 @@ 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 { + // 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() diff --git a/session/pessimistic_test.go b/session/pessimistic_test.go index 07ea9e72b6d6b..f711444b6a880 100644 --- a/session/pessimistic_test.go +++ b/session/pessimistic_test.go @@ -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) + } +} From c9c9f3d46f02b24ed2f7307387f78f804567bff0 Mon Sep 17 00:00:00 2001 From: you06 Date: Thu, 23 Dec 2021 14:13:00 +0800 Subject: [PATCH 3/4] format code Signed-off-by: you06 --- planner/optimize.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planner/optimize.go b/planner/optimize.go index 7933f4b83779b..7089423cc8973 100644 --- a/planner/optimize.go +++ b/planner/optimize.go @@ -16,13 +16,13 @@ package planner import ( "context" "fmt" - "github.com/pingcap/failpoint" "math" "runtime/trace" "strings" "time" "github.com/pingcap/errors" + "github.com/pingcap/failpoint" "github.com/pingcap/parser" "github.com/pingcap/parser/ast" "github.com/pingcap/tidb/bindinfo" From d2c711fb0cb6b924b58c231a4fced761a7b79e6f Mon Sep 17 00:00:00 2001 From: AilinKid <314806019@qq.com> Date: Mon, 19 Dec 2022 20:29:18 +0800 Subject: [PATCH 4/4] make tidy Signed-off-by: AilinKid <314806019@qq.com> --- go.sum | 1 - 1 file changed, 1 deletion(-) diff --git a/go.sum b/go.sum index c295bb814cf04..9111eac2df13c 100644 --- a/go.sum +++ b/go.sum @@ -507,7 +507,6 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD github.com/sasha-s/go-deadlock v0.2.0/go.mod h1:StQn567HiB1fF2yJ44N9au7wOhrPS3iZqiDbRupzT10= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/sergi/go-diff v1.0.1-0.20180205163309-da645544ed44/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= -github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/shirou/gopsutil v2.19.10+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/shirou/gopsutil v3.21.2+incompatible h1:U+YvJfjCh6MslYlIAXvPtzhW3YZEtc9uncueUNpD/0A=