From b1448427dfc7463d004dcfbc52eca069361e15dc Mon Sep 17 00:00:00 2001 From: Jack Yu Date: Tue, 30 Apr 2019 16:01:56 +0800 Subject: [PATCH] session: add the transaction commit time to slow log (#10294) (#10310) --- executor/adapter.go | 10 ---------- session/session.go | 6 ------ session/tidb.go | 12 +++++++++--- 3 files changed, 9 insertions(+), 19 deletions(-) diff --git a/executor/adapter.go b/executor/adapter.go index 41bac81170b75..d906ed35b1a1c 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