Skip to content

Commit

Permalink
sql: record transaction idle time during rollback
Browse files Browse the repository at this point in the history
Transaction idle time is recorded when we execute the next statement,
including COMMIT. The ROLLBACK path was not doing so, this fixes that.

Epic: none
Release note: none
  • Loading branch information
mw5h committed Feb 28, 2025
1 parent 5bb2161 commit 67648f8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/sql/conn_executor_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2621,6 +2621,9 @@ func (ex *connExecutor) createJobs(ctx context.Context) error {
func (ex *connExecutor) rollbackSQLTransaction(
ctx context.Context, stmt tree.Statement,
) (fsm.Event, fsm.EventPayload) {
ex.extraTxnState.idleLatency += ex.statsCollector.PhaseTimes().
GetIdleLatency(ex.statsCollector.PreviousPhaseTimes())

if err := ex.extraTxnState.sqlCursors.closeAll(&ex.planner, cursorCloseForTxnRollback); err != nil {
return ex.makeErrEvent(err, stmt)
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/sql/sqlstats/sslocal/sql_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,21 @@ func TestSQLStatsIdleLatencies(t *testing.T) {
require.NoError(t, err)
},
},
{
name: "simple statement with rollback",
stmtLats: map[string]float64{"SELECT _": 0.1},
txnLat: 0.2,
ops: func(t *testing.T, db *gosql.DB) {
tx, err := db.Begin()
require.NoError(t, err)
time.Sleep(100 * time.Millisecond)
_, err = tx.Exec("SELECT 1")
require.NoError(t, err)
time.Sleep(100 * time.Millisecond)
err = tx.Rollback()
require.NoError(t, err)
},
},
{
name: "compound statement",
stmtLats: map[string]float64{"SELECT _": 0.1, "SELECT count(*) FROM crdb_internal.statement_statistics": 0},
Expand Down

0 comments on commit 67648f8

Please sign in to comment.