@@ -13,7 +13,6 @@ import (
1313 "github.com/cockroachdb/cockroach/pkg/sql/contentionpb"
1414 "github.com/cockroachdb/cockroach/pkg/sql/idxrecommendations"
1515 "github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
16- "github.com/cockroachdb/cockroach/pkg/sql/sessionphase"
1716 "github.com/cockroachdb/cockroach/pkg/sql/sqlstats"
1817 "github.com/cockroachdb/cockroach/pkg/util/log"
1918 "github.com/cockroachdb/cockroach/pkg/util/metric"
@@ -167,47 +166,16 @@ func (ex *connExecutor) recordStatementSummary(
167166 stmtErr error ,
168167 stats topLevelQueryStats ,
169168) appstatspb.StmtFingerprintID {
170- phaseTimes := ex .statsCollector .PhaseTimes ()
171-
172- // Collect the statistics.
173- idleLatRaw := phaseTimes .GetIdleLatency (ex .statsCollector .PreviousPhaseTimes ())
174- idleLatSec := idleLatRaw .Seconds ()
175- runLatRaw := phaseTimes .GetRunLatency ()
176- runLatSec := runLatRaw .Seconds ()
177- parseLatSec := phaseTimes .GetParsingLatency ().Seconds ()
178- planLatSec := phaseTimes .GetPlanningLatency ().Seconds ()
179- // We want to exclude any overhead to reduce possible confusion.
180- svcLatRaw := phaseTimes .GetServiceLatencyNoOverhead ()
181- svcLatSec := svcLatRaw .Seconds ()
182-
183- // processing latency: contributing towards SQL results.
184- processingLatSec := parseLatSec + planLatSec + runLatSec
185-
186- // overhead latency: txn/retry management, error checking, etc
187- execOverheadSec := svcLatSec - processingLatSec
188169
189170 stmt := & planner .stmt
190171 flags := planner .curPlan .flags
191172 ex .recordStatementLatencyMetrics (
192- stmt , flags , automaticRetryTxnCount + automaticRetryStmtCount , runLatRaw , svcLatRaw ,
173+ stmt , flags , automaticRetryTxnCount + automaticRetryStmtCount , ex . statsCollector . RunLatency (), ex . statsCollector . ServiceLatency () ,
193174 )
194175
195- fullScan := flags .IsSet (planFlagContainsFullIndexScan ) || flags .IsSet (planFlagContainsFullTableScan )
196-
197176 idxRecommendations := idxrecommendations .FormatIdxRecommendations (planner .instrumentation .indexRecs )
198177 queryLevelStats , queryLevelStatsOk := planner .instrumentation .GetQueryLevelStats ()
199178
200- var sqlInstanceIDs []int64
201- var kvNodeIDs []int32
202- if queryLevelStatsOk {
203- sqlInstanceIDs = make ([]int64 , 0 , len (queryLevelStats .SQLInstanceIDs ))
204- for _ , sqlInstanceID := range queryLevelStats .SQLInstanceIDs {
205- sqlInstanceIDs = append (sqlInstanceIDs , int64 (sqlInstanceID ))
206- }
207- kvNodeIDs = queryLevelStats .KVNodeIDs
208- }
209- startTime := phaseTimes .GetSessionPhaseTime (sessionphase .PlannerStartExecStmt ).ToUTC ()
210- implicitTxn := flags .IsSet (planFlagImplicitTxn )
211179 stmtFingerprintID := planner .instrumentation .fingerprintId
212180 autoRetryReason := ex .state .mu .autoRetryReason
213181 if automaticRetryStmtCount > 0 {
@@ -221,51 +189,45 @@ func (ex *connExecutor) recordStatementSummary(
221189 ex .metrics .EngineMetrics .StatementIndexBytesWritten .Inc (stats .indexBytesWritten )
222190
223191 if ex .statsCollector .EnabledForTransaction () {
224- recordedStmtStats := & sqlstats.RecordedStmtStats {
225- FingerprintID : stmtFingerprintID ,
226- QuerySummary : stmt .StmtSummary ,
227- Generic : flags .IsSet (planFlagGeneric ),
228- AppliedStmtHints : len (stmt .Hints ) > 0 ,
229- DistSQL : flags .ShouldBeDistributed (),
230- Vec : flags .IsSet (planFlagVectorized ),
231- ImplicitTxn : implicitTxn ,
232- PlanHash : planner .instrumentation .planGist .Hash (),
233- SessionID : ex .planner .extendedEvalCtx .SessionID ,
234- StatementID : stmt .QueryID ,
235- AutoRetryCount : automaticRetryTxnCount + automaticRetryStmtCount ,
236- Failed : stmtErr != nil ,
237- AutoRetryReason : autoRetryReason ,
238- RowsAffected : rowsAffected ,
239- IdleLatencySec : idleLatSec ,
240- ParseLatencySec : parseLatSec ,
241- PlanLatencySec : planLatSec ,
242- RunLatencySec : runLatSec ,
243- ServiceLatencySec : svcLatSec ,
244- OverheadLatencySec : execOverheadSec ,
245- BytesRead : stats .bytesRead ,
246- RowsRead : stats .rowsRead ,
247- RowsWritten : stats .rowsWritten ,
248- Nodes : sqlInstanceIDs ,
249- KVNodeIDs : kvNodeIDs ,
250- StatementType : stmt .AST .StatementType (),
251- PlanGist : planner .instrumentation .planGist .String (),
252- StatementError : stmtErr ,
253- IndexRecommendations : idxRecommendations ,
254- Query : stmt .StmtNoConstants ,
255- StartTime : startTime ,
256- EndTime : startTime .Add (svcLatRaw ),
257- FullScan : fullScan ,
258- ExecStats : queryLevelStats ,
192+ b := sqlstats .NewRecordedStatementStatsBuilder (
193+ stmtFingerprintID ,
194+ planner .SessionData ().Database ,
195+ stmt .StmtNoConstants ,
196+ stmt .StmtSummary ,
197+ stmt .AST .StatementType (),
198+ ex .statsCollector .CurrentApplicationName (),
199+ ).
200+ QueryID (stmt .QueryID ).
201+ SessionID (ex .planner .extendedEvalCtx .SessionID ).
202+ PlanMetadata (
203+ flags .IsSet (planFlagGeneric ),
204+ flags .ShouldBeDistributed (),
205+ flags .IsSet (planFlagVectorized ),
206+ flags .IsSet (planFlagImplicitTxn ),
207+ flags .IsSet (planFlagContainsFullIndexScan ) || flags .IsSet (planFlagContainsFullTableScan ),
208+ ).
209+ PlanGist (planner .instrumentation .planGist .String (), planner .instrumentation .planGist .Hash ()).
210+ LatencyRecorder (ex .statsCollector ).
211+ QueryLevelStats (stats .bytesRead , stats .rowsRead , stats .rowsWritten ).
212+ ExecStats (queryLevelStats ).
259213 // TODO(mgartner): Use a slice of struct{uint64, uint64} instead of
260214 // converting to strings.
261- Indexes : planner .instrumentation .indexesUsed .Strings (),
262- Database : planner .SessionData ().Database ,
263- QueryTags : stmt .QueryTags ,
264- App : ex .statsCollector .CurrentApplicationName (),
265- UnderOuterTxn : ex .extraTxnState .underOuterTxn ,
215+ Indexes (planner .instrumentation .indexesUsed .Strings ()).
216+ AutoRetry (automaticRetryTxnCount + automaticRetryStmtCount , autoRetryReason ).
217+ RowsAffected (rowsAffected ).
218+ IndexRecommendations (idxRecommendations ).
219+ QueryTags (stmt .QueryTags ).
220+ StatementError (stmtErr )
221+
222+ if ex .extraTxnState .underOuterTxn {
223+ b .UnderOuterTxn ()
224+ }
225+
226+ if len (stmt .Hints ) > 0 {
227+ b .AppliedStatementHints ()
266228 }
267229
268- ex .statsCollector .RecordStatement (ctx , recordedStmtStats )
230+ ex .statsCollector .RecordStatement (ctx , b . Build () )
269231 }
270232
271233 // Record statement execution statistics if span is recorded and no error was
@@ -314,11 +276,16 @@ func (ex *connExecutor) recordStatementSummary(
314276 ex .extraTxnState .transactionStatementsHash .Add (uint64 (stmtFingerprintID ))
315277 }
316278 ex .extraTxnState .numRows += rowsAffected
317- ex .extraTxnState .idleLatency += idleLatRaw
279+ ex .extraTxnState .idleLatency += ex . statsCollector . IdleLatency ()
318280
319281 if log .V (2 ) {
320282 // ages since significant epochs
321- sessionAge := phaseTimes .GetSessionAge ().Seconds ()
283+ sessionAge := ex .statsCollector .PhaseTimes ().GetSessionAge ().Seconds ()
284+ parseLatSec := ex .statsCollector .ParsingLatency ().Seconds ()
285+ planLatSec := ex .statsCollector .PlanningLatency ().Seconds ()
286+ runLatSec := ex .statsCollector .RunLatency ().Seconds ()
287+ svcLatSec := ex .statsCollector .ServiceLatency ().Seconds ()
288+ execOverheadSec := ex .statsCollector .ExecOverheadLatency ().Seconds ()
322289
323290 log .Dev .Infof (ctx ,
324291 "query stats: %d rows, %d retries, " +
0 commit comments