diff --git a/executor/test/writetest/write_test.go b/executor/test/writetest/write_test.go index 92c271de0a1dd..7561c3e054f57 100644 --- a/executor/test/writetest/write_test.go +++ b/executor/test/writetest/write_test.go @@ -3551,3 +3551,14 @@ func TestMutipleReplaceAndInsertInOneSession(t *testing.T) { tk2.MustQuery("select * from t_securities").Sort().Check(testkit.Rows("1 1 2 7", "2 7 1 7", "3 8 1 7", "8 9 1 7")) } + +func TestHandleColumnWithOnUpdateCurrentTimestamp(t *testing.T) { + // Test https://github.com/pingcap/tidb/issues/44565 + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("create table t (a timestamp on update current_timestamp(0), b int, primary key (a) clustered, key idx (a))") + tk.MustExec("insert into t values ('2023-06-11 10:00:00', 1)") + tk.MustExec("update t force index(primary) set b = 10 where a = '2023-06-11 10:00:00'") + tk.MustExec("admin check table t") +} diff --git a/executor/write.go b/executor/write.go index 2ff925194c96f..82d5cd25f7a4d 100644 --- a/executor/write.go +++ b/executor/write.go @@ -147,6 +147,14 @@ func updateRecord(ctx context.Context, sctx sessionctx.Context, h kv.Handle, old if v, err := expression.GetTimeValue(sctx, strings.ToUpper(ast.CurrentTimestamp), col.GetType(), col.GetDecimal(), nil); err == nil { newData[i] = v modified[i] = true + // Only TIMESTAMP and DATETIME columns can be automatically updated, so it cannot be PKIsHandle. + // Ref: https://dev.mysql.com/doc/refman/8.0/en/timestamp-initialization.html + if col.IsPKHandleColumn(t.Meta()) { + return false, errors.Errorf("on-update-now column should never be pk-is-handle") + } + if col.IsCommonHandleColumn(t.Meta()) { + handleChanged = true + } } else { return false, err }