From 6fedaac3d4718536db27f059a247510aedeafe21 Mon Sep 17 00:00:00 2001 From: zyguan Date: Fri, 16 Dec 2022 08:57:30 +0000 Subject: [PATCH 1/2] planner: fix pessimistic-auto-commit not working for point plans Signed-off-by: zyguan --- planner/core/point_get_plan.go | 3 ++- tests/realtikvtest/pessimistictest/pessimistic_test.go | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/planner/core/point_get_plan.go b/planner/core/point_get_plan.go index afb34814f7196..56fbc52b499bb 100644 --- a/planner/core/point_get_plan.go +++ b/planner/core/point_get_plan.go @@ -21,6 +21,7 @@ import ( "strings" "github.com/pingcap/errors" + "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/expression" "github.com/pingcap/tidb/infoschema" "github.com/pingcap/tidb/kv" @@ -530,7 +531,7 @@ func getLockWaitTime(ctx sessionctx.Context, lockInfo *ast.SelectLockInfo) (lock // autocommit to 0. If autocommit is enabled, the rows matching the specification are not locked. // See https://dev.mysql.com/doc/refman/5.7/en/innodb-locking-reads.html sessVars := ctx.GetSessionVars() - if !sessVars.IsAutocommit() || sessVars.InTxn() { + if !sessVars.IsAutocommit() || sessVars.InTxn() || config.GetGlobalConfig().PessimisticTxn.PessimisticAutoCommit.Load() { lock = true waitTime = sessVars.LockWaitTimeout if lockInfo.LockType == ast.SelectLockForUpdateWaitN { diff --git a/tests/realtikvtest/pessimistictest/pessimistic_test.go b/tests/realtikvtest/pessimistictest/pessimistic_test.go index fdadded9613c7..3ee95f226325b 100644 --- a/tests/realtikvtest/pessimistictest/pessimistic_test.go +++ b/tests/realtikvtest/pessimistictest/pessimistic_test.go @@ -3108,13 +3108,17 @@ func TestPessimisticAutoCommitTxn(t *testing.T) { tk.MustExec("set tidb_txn_mode = 'pessimistic'") tk.MustExec("drop table if exists t") - tk.MustExec("create table t (i int)") + tk.MustExec("create table t (i int primary key)") tk.MustExec("insert into t values (1)") tk.MustExec("set autocommit = on") rows := tk.MustQuery("explain update t set i = -i").Rows() explain := fmt.Sprintf("%v", rows[1]) require.NotRegexp(t, ".*SelectLock.*", explain) + rows = tk.MustQuery("explain update t set i = -i where i = -1").Rows() + explain = fmt.Sprintf("%v", rows[1]) + require.Regexp(t, ".*handle:-1.*", explain) + require.NotRegexp(t, ".*handle:-1, lock.*", explain) originCfg := config.GetGlobalConfig() defer config.StoreGlobalConfig(originCfg) @@ -3125,6 +3129,9 @@ func TestPessimisticAutoCommitTxn(t *testing.T) { rows = tk.MustQuery("explain update t set i = -i").Rows() explain = fmt.Sprintf("%v", rows[1]) require.Regexp(t, ".*SelectLock.*", explain) + rows = tk.MustQuery("explain update t set i = -i where i = -1").Rows() + explain = fmt.Sprintf("%v", rows[1]) + require.Regexp(t, ".*handle:-1, lock.*", explain) } func TestPessimisticLockOnPartition(t *testing.T) { From bb5187481e76a923c80525d89c5ad2cb577de7fb Mon Sep 17 00:00:00 2001 From: zyguan Date: Fri, 16 Dec 2022 10:56:19 +0000 Subject: [PATCH 2/2] address https://github.com/pingcap/tidb/pull/39993#discussion_r1050541556 Signed-off-by: zyguan --- tests/realtikvtest/pessimistictest/pessimistic_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/realtikvtest/pessimistictest/pessimistic_test.go b/tests/realtikvtest/pessimistictest/pessimistic_test.go index 3ee95f226325b..d4e05d3329ad0 100644 --- a/tests/realtikvtest/pessimistictest/pessimistic_test.go +++ b/tests/realtikvtest/pessimistictest/pessimistic_test.go @@ -3119,6 +3119,10 @@ func TestPessimisticAutoCommitTxn(t *testing.T) { explain = fmt.Sprintf("%v", rows[1]) require.Regexp(t, ".*handle:-1.*", explain) require.NotRegexp(t, ".*handle:-1, lock.*", explain) + rows = tk.MustQuery("explain update t set i = -i where i in (-1, 1)").Rows() + explain = fmt.Sprintf("%v", rows[1]) + require.Regexp(t, ".*handle:\\[-1 1\\].*", explain) + require.NotRegexp(t, ".*handle:\\[-1 1\\].*, lock.*", explain) originCfg := config.GetGlobalConfig() defer config.StoreGlobalConfig(originCfg) @@ -3132,6 +3136,9 @@ func TestPessimisticAutoCommitTxn(t *testing.T) { rows = tk.MustQuery("explain update t set i = -i where i = -1").Rows() explain = fmt.Sprintf("%v", rows[1]) require.Regexp(t, ".*handle:-1, lock.*", explain) + rows = tk.MustQuery("explain update t set i = -i where i in (-1, 1)").Rows() + explain = fmt.Sprintf("%v", rows[1]) + require.Regexp(t, ".*handle:\\[-1 1\\].*, lock.*", explain) } func TestPessimisticLockOnPartition(t *testing.T) {