From 7ab8f8ca203374130af0c09e938745924edd69d8 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Tue, 17 Oct 2023 17:17:00 +0800 Subject: [PATCH] planner: output a warning if plan rebuilding fails when reusing a cached plan (#46278) (#46281) close pingcap/tidb#46159 --- planner/core/plan_cache.go | 2 +- planner/core/plan_cache_test.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/planner/core/plan_cache.go b/planner/core/plan_cache.go index d3490e74907bb..abab100018b4e 100644 --- a/planner/core/plan_cache.go +++ b/planner/core/plan_cache.go @@ -353,7 +353,7 @@ func RebuildPlan4CachedPlan(p Plan) (ok bool) { sc.InPreparedPlanBuilding = true defer func() { sc.InPreparedPlanBuilding = false }() if err := rebuildRange(p); err != nil { - // TODO: log or warn this error. + sc.AppendWarning(errors.Errorf("skip plan-cache: plan rebuild failed, %s", err.Error())) return false // fail to rebuild ranges } if !sc.UseCache { diff --git a/planner/core/plan_cache_test.go b/planner/core/plan_cache_test.go index 52572b99b3d25..ab85e9b32cb4c 100644 --- a/planner/core/plan_cache_test.go +++ b/planner/core/plan_cache_test.go @@ -2380,6 +2380,18 @@ func TestIssue45378(t *testing.T) { tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1")) } +func TestIssue46159(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec(`create table t (a varchar(10), key(a(5)))`) + tk.MustExec(`prepare st from 'select a from t use index(a) where a=?'`) + tk.MustExec(`set @a='a'`) + tk.MustQuery(`execute st using @a`).Check(testkit.Rows()) + tk.MustQuery(`execute st using @a`).Check(testkit.Rows()) + tk.MustQuery(`show warnings`).Check(testkit.Rows("Warning 1105 skip plan-cache: plan rebuild failed, rebuild to get an unsafe range")) +} + func TestNonPreparedPlanCacheBuiltinFuncs(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store)