diff --git a/pkg/sql/conn_executor_exec.go b/pkg/sql/conn_executor_exec.go index 02ad908e957b..38ba52f84949 100644 --- a/pkg/sql/conn_executor_exec.go +++ b/pkg/sql/conn_executor_exec.go @@ -508,7 +508,7 @@ func (ex *connExecutor) execStmtInOpenState( ctx = ih.Setup( ctx, ex.server.cfg, ex.statsCollector, p, ex.stmtDiagnosticsRecorder, - stmt.StmtNoConstants, os.ImplicitTxn.Get(), + &stmt, os.ImplicitTxn.Get(), // This goroutine is the only one that can modify // txnState.mu.priority, so we don't need to get a mutex here. ex.state.mu.priority, @@ -1462,7 +1462,7 @@ func (ex *connExecutor) execStmtInOpenStateWithPausablePortal( if !portal.isPausable() || portal.pauseInfo.execStmtInOpenState.ihWrapper == nil { ctx = ih.Setup( ctx, ex.server.cfg, ex.statsCollector, p, ex.stmtDiagnosticsRecorder, - vars.stmt.StmtNoConstants, os.ImplicitTxn.Get(), + &vars.stmt, os.ImplicitTxn.Get(), // This goroutine is the only one that can modify // txnState.mu.priority, so we don't need to get a mutex here. ex.state.mu.priority, diff --git a/pkg/sql/instrumentation.go b/pkg/sql/instrumentation.go index e194f6a42ceb..336cfb5fff76 100644 --- a/pkg/sql/instrumentation.go +++ b/pkg/sql/instrumentation.go @@ -418,12 +418,12 @@ func (ih *instrumentationHelper) Setup( statsCollector *sslocal.StatsCollector, p *planner, stmtDiagnosticsRecorder *stmtdiagnostics.Registry, - fingerprint string, + stmt *Statement, implicitTxn bool, txnPriority roachpb.UserPriority, collectTxnExecStats bool, ) (newCtx context.Context) { - ih.fingerprint = fingerprint + ih.fingerprint = stmt.StmtNoConstants ih.implicitTxn = implicitTxn ih.txnPriority = txnPriority ih.codec = cfg.Codec @@ -447,7 +447,7 @@ func (ih *instrumentationHelper) Setup( default: ih.collectBundle, ih.diagRequestID, ih.diagRequest = - stmtDiagnosticsRecorder.ShouldCollectDiagnostics(ctx, fingerprint, "" /* planGist */) + stmtDiagnosticsRecorder.ShouldCollectDiagnostics(ctx, stmt.StmtNoConstants, "" /* planGist */) // IsRedacted will be false when ih.collectBundle is false. ih.explainFlags.RedactValues = ih.explainFlags.RedactValues || ih.diagRequest.IsRedacted() } @@ -456,7 +456,7 @@ func (ih *instrumentationHelper) Setup( ih.withStatementTrace = cfg.TestingKnobs.WithStatementTrace var previouslySampled bool - previouslySampled, ih.savePlanForStats = statsCollector.ShouldSample(fingerprint, implicitTxn, p.SessionData().Database) + previouslySampled, ih.savePlanForStats = statsCollector.ShouldSample(stmt.StmtNoConstants, implicitTxn, p.SessionData().Database) defer func() { ih.finalizeSetup(newCtx, cfg) }() @@ -481,6 +481,12 @@ func (ih *instrumentationHelper) Setup( } shouldSampleFirstEncounter := func() bool { + if stmt.AST.StatementType() == tree.TypeTCL { + // We don't collect stats for TCL statements so + // there's no need to trace them. + return false + } + // If this is the first time we see this statement in the current stats // container, we'll collect its execution stats anyway (unless the user // disabled txn or stmt stats collection entirely). diff --git a/pkg/sql/instrumentation_test.go b/pkg/sql/instrumentation_test.go index 7fd656fac022..d5997278e631 100644 --- a/pkg/sql/instrumentation_test.go +++ b/pkg/sql/instrumentation_test.go @@ -241,7 +241,8 @@ func TestSampledStatsCollectionOnNewFingerprint(t *testing.T) { "SELECT 1, 2, 3", "CREATE TABLE IF NOT EXISTS foo (x INT)", "SELECT * FROM foo", - // An explicit txn results in the queries inside being recorded as 'new' statements. + // Since the sampling key does not include the txn fingerprint, no + // statements in this txn should be sampled. "BEGIN; SELECT 1; COMMIT;", } @@ -251,10 +252,11 @@ func TestSampledStatsCollectionOnNewFingerprint(t *testing.T) { require.Equal(t, len(queries), len(collectedTxnStats)) - // We should have collected stats for each of the queries. - for i := range collectedTxnStats { + // We should have collected stats for each of the queries except the last. + for i := range collectedTxnStats[:len(queries)-1] { require.True(t, collectedTxnStats[i].CollectedExecStats) } + require.False(t, collectedTxnStats[len(queries)-1].CollectedExecStats) }) collectedTxnStats = nil