Skip to content

Commit

Permalink
*: add wait lock time in tidb_trx (pingcap#43105)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackysp committed Jun 2, 2023
1 parent a7c0d95 commit 6605403
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,7 @@ var tableTiDBTrxCols = []columnInfo{
{name: txninfo.DBStr, tp: mysql.TypeVarchar, size: 64, comment: "The schema this transaction works on"},
{name: txninfo.AllSQLDigestsStr, tp: mysql.TypeBlob, size: types.UnspecifiedLength, comment: "A list of the digests of SQL statements that the transaction has executed"},
{name: txninfo.RelatedTableIDsStr, tp: mysql.TypeBlob, size: types.UnspecifiedLength, comment: "A list of the table IDs that the transaction has accessed"},
{name: txninfo.WaitingTimeStr, tp: mysql.TypeDouble, size: 22, comment: "Current lock waiting time"},
}

var tableDeadlocksCols = []columnInfo{
Expand Down
18 changes: 17 additions & 1 deletion infoschema/test/clustertablestest/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1522,10 +1522,26 @@ func TestTiDBTrx(t *testing.T) {
sm.TxnInfo[1].BlockStartTime.Time = blockTime2
tk.Session().SetSessionManager(sm)

tk.MustQuery("select * from information_schema.TIDB_TRX;").Check(testkit.Rows(
tk.MustQuery(`select ID,
START_TIME,
CURRENT_SQL_DIGEST,
CURRENT_SQL_DIGEST_TEXT,
STATE,
WAITING_START_TIME,
MEM_BUFFER_KEYS,
MEM_BUFFER_BYTES,
SESSION_ID,
USER,
DB,
ALL_SQL_DIGESTS,
RELATED_TABLE_IDS
from information_schema.TIDB_TRX`).Check(testkit.Rows(
"424768545227014155 2021-05-07 12:56:48.001000 "+digest.String()+" update `test_tidb_trx` set `i` = `i` + ? Idle <nil> 1 19 2 root test [] ",
"425070846483628033 2021-05-20 21:16:35.778000 <nil> <nil> LockWaiting 2021-05-20 13:18:30.123456 0 19 10 user1 db1 [\"sql1\",\"sql2\",\""+digest.String()+"\"] "))

rows := tk.MustQuery(`select WAITING_TIME from information_schema.TIDB_TRX where WAITING_TIME is not null`)
require.Len(t, rows.Rows(), 1)

// Test the all_sql_digests column can be directly passed to the tidb_decode_sql_digests function.
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/expression/sqlDigestRetrieverSkipRetrieveGlobal", "return"))
defer func() {
Expand Down
8 changes: 8 additions & 0 deletions session/txninfo/txn_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ const (
AllSQLDigestsStr = "ALL_SQL_DIGESTS"
// RelatedTableIDsStr is the table id of the TIDB_TRX table's RelatedTableIDs column.
RelatedTableIDsStr = "RELATED_TABLE_IDS"
// WaitingTimeStr is the column name of the TIDB_TRX table's WaitingTime column.
WaitingTimeStr = "WAITING_TIME"
)

// TxnRunningStateStrs is the names of the TxnRunningStates
Expand Down Expand Up @@ -252,6 +254,12 @@ var columnValueGetterMap = map[string]func(*TxnInfo) types.Datum{
}
return types.NewDatum(str.String())
},
WaitingTimeStr: func(info *TxnInfo) types.Datum {
if !info.BlockStartTime.Valid {
return types.NewDatum(nil)
}
return types.NewFloat64Datum(time.Since(info.BlockStartTime.Time).Seconds())
},
}

// ToDatum Converts the `TxnInfo`'s specified column to `Datum` to show in the `TIDB_TRX` table.
Expand Down

0 comments on commit 6605403

Please sign in to comment.