diff --git a/executor/adapter.go b/executor/adapter.go index 64c39fe839122..819b749c49caf 100644 --- a/executor/adapter.go +++ b/executor/adapter.go @@ -525,6 +525,7 @@ func (a *ExecStmt) handlePessimisticDML(ctx context.Context, e Executor) error { } txnCtx := sctx.GetSessionVars().TxnCtx for { + startPointGetLocking := time.Now() _, err = a.handleNoDelayExecutor(ctx, e) if !txn.Valid() { return err @@ -533,6 +534,9 @@ func (a *ExecStmt) handlePessimisticDML(ctx context.Context, e Executor) error { // It is possible the DML has point get plan that locks the key. e, err = a.handlePessimisticLockError(ctx, err) if err != nil { + if ErrDeadlock.Equal(err) { + metrics.StatementDeadlockDetectDuration.Observe(time.Since(startPointGetLocking).Seconds()) + } return err } continue @@ -547,12 +551,16 @@ func (a *ExecStmt) handlePessimisticDML(ctx context.Context, e Executor) error { } seVars := sctx.GetSessionVars() lockCtx := newLockCtx(seVars, seVars.LockWaitTimeout) + startLocking := time.Now() err = txn.LockKeys(ctx, lockCtx, keys...) if err == nil { return nil } e, err = a.handlePessimisticLockError(ctx, err) if err != nil { + if ErrDeadlock.Equal(err) { + metrics.StatementDeadlockDetectDuration.Observe(time.Since(startLocking).Seconds()) + } return err } } diff --git a/metrics/metrics.go b/metrics/metrics.go index 38fe0396938b0..20438a00701df 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -130,6 +130,7 @@ func RegisterMetrics() { prometheus.MustRegister(TiKVLocalLatchWaitTimeHistogram) prometheus.MustRegister(TimeJumpBackCounter) prometheus.MustRegister(TransactionDuration) + prometheus.MustRegister(StatementDeadlockDetectDuration) prometheus.MustRegister(UpdateSelfVersionHistogram) prometheus.MustRegister(UpdateStatsCounter) prometheus.MustRegister(WatchOwnerCounter) diff --git a/metrics/session.go b/metrics/session.go index 0598270fd7810..5753e56054d15 100644 --- a/metrics/session.go +++ b/metrics/session.go @@ -90,6 +90,16 @@ var ( Help: "Bucketed histogram of a transaction execution duration, including retry.", Buckets: prometheus.ExponentialBuckets(0.001, 2, 20), // 1ms ~ 1049s }, []string{LblSQLType, LblType}) + + StatementDeadlockDetectDuration = prometheus.NewHistogram( + prometheus.HistogramOpts{ + Namespace: "tidb", + Subsystem: "session", + Name: "statement_deadlock_detect_duration_seconds", + Help: "Bucketed histogram of a statement deadlock detect duration.", + Buckets: prometheus.ExponentialBuckets(0.001, 2, 20), // 1ms ~ 1049s + }, + ) ) // Label constants.