diff --git a/executor/explainfor_test.go b/executor/explainfor_test.go index a97c0d125b869..ff54c14cf9f3f 100644 --- a/executor/explainfor_test.go +++ b/executor/explainfor_test.go @@ -194,6 +194,38 @@ func (s *testPrepareSerialSuite) TestExplainForConnPlanCache(c *C) { wg.Wait() } +func (s *testPrepareSerialSuite) TestSavedPlanPanicPlanCache(c *C) { + orgEnable := core.PreparedPlanCacheEnabled() + defer func() { + core.SetPreparedPlanCache(orgEnable) + }() + core.SetPreparedPlanCache(true) + + var err error + tk := testkit.NewTestKit(c, s.store) + tk.Se, err = session.CreateSession4TestWithOpt(s.store, &session.Opt{ + PreparedPlanCache: kvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64), + }) + c.Assert(err, IsNil) + + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a int, b int, c int generated always as (a+b) stored)") + tk.MustExec("insert into t(a,b) values(1,1)") + tk.MustExec("begin") + tk.MustExec("update t set b = 2 where a = 1") + tk.MustExec("prepare stmt from 'select b from t where a > ?'") + tk.MustExec("set @p = 0") + tk.MustQuery("execute stmt using @p").Check(testkit.Rows( + "2", + )) + tk.MustExec("set @p = 1") + tk.MustQuery("execute stmt using @p").Check(testkit.Rows()) + err = tk.ExecToErr("insert into t(a,b,c) values(3,3,3)") + c.Assert(err, NotNil) + c.Assert(err.Error(), Equals, "[planner:3105]The value specified for generated column 'c' in table 't' is not allowed.") +} + func (s *testPrepareSerialSuite) TestExplainDotForExplainPlan(c *C) { tk := testkit.NewTestKit(c, s.store) diff --git a/session/session.go b/session/session.go index b0d036b37537f..2903e20dbdd0c 100644 --- a/session/session.go +++ b/session/session.go @@ -1033,6 +1033,7 @@ func (s *session) SetProcessInfo(sql string, t time.Time, command byte, maxExecu MaxExecutionTime: maxExecutionTime, } _, pi.Digest = s.sessionVars.StmtCtx.SQLDigest() + s.currentPlan = nil if s.sessionVars.User != nil { pi.User = s.sessionVars.User.Username pi.Host = s.sessionVars.User.Hostname