diff --git a/executor/adapter.go b/executor/adapter.go index bcad6e4a9101d..e734e3d9ba142 100644 --- a/executor/adapter.go +++ b/executor/adapter.go @@ -283,16 +283,6 @@ func (a *ExecStmt) handleNoDelayExecutor(ctx context.Context, sctx sessionctx.Co pi.SetProcessInfo("") } terror.Log(errors.Trace(e.Close())) - txnTS := uint64(0) - // Don't active pending txn here. - if txn, err1 := sctx.Txn(false); err1 != nil { - logutil.Logger(ctx).Error("get current transaction failed", zap.Error(err)) - } else { - if txn.Valid() { - txnTS = txn.StartTS() - } - } - a.LogSlowQuery(txnTS, err == nil) a.logAudit() }() diff --git a/session/session.go b/session/session.go index 5376501698d4d..5ef2d4d56efa2 100644 --- a/session/session.go +++ b/session/session.go @@ -393,18 +393,12 @@ func (s *session) doCommitWithRetry(ctx context.Context) error { } func (s *session) CommitTxn(ctx context.Context) error { - stmt := executor.ExecStmt{ - Text: "commitTxn", - Ctx: s, - StartTime: time.Now(), - } var commitDetail *execdetails.CommitDetails ctx = context.WithValue(ctx, execdetails.CommitDetailCtxKey, &commitDetail) err := s.doCommitWithRetry(ctx) if commitDetail != nil { s.sessionVars.StmtCtx.MergeExecDetails(nil, commitDetail) } - stmt.LogSlowQuery(s.sessionVars.TxnCtx.StartTS, err == nil) label := metrics.LblOK if err != nil { label = metrics.LblError diff --git a/session/tidb.go b/session/tidb.go index 347eccaa314f8..7c519a37a1163 100644 --- a/session/tidb.go +++ b/session/tidb.go @@ -181,10 +181,16 @@ func checkStmtLimit(ctx context.Context, sctx sessionctx.Context, se *session, s } // runStmt executes the sqlexec.Statement and commit or rollback the current transaction. -func runStmt(ctx context.Context, sctx sessionctx.Context, s sqlexec.Statement) (sqlexec.RecordSet, error) { - var err error - var rs sqlexec.RecordSet +func runStmt(ctx context.Context, sctx sessionctx.Context, s sqlexec.Statement) (rs sqlexec.RecordSet, err error) { se := sctx.(*session) + defer func() { + // If it is not a select statement, we record its slow log here, + // then it could include the transaction commit time. + if rs == nil { + s.(*executor.ExecStmt).LogSlowQuery(se.GetSessionVars().TxnCtx.StartTS, err != nil) + } + }() + err = se.checkTxnAborted(s) if err != nil { return nil, err