-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
combined_statement_stats.go
804 lines (685 loc) · 25.1 KB
/
combined_statement_stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package server
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/security/username"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats"
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats/persistedsqlstats/sqlstatsutil"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
)
func getTimeFromSeconds(seconds int64) *time.Time {
if seconds != 0 {
t := timeutil.Unix(seconds, 0)
return &t
}
return nil
}
func (s *statusServer) CombinedStatementStats(
ctx context.Context, req *serverpb.CombinedStatementsStatsRequest,
) (*serverpb.StatementsResponse, error) {
ctx = propagateGatewayMetadata(ctx)
ctx = s.AnnotateCtx(ctx)
if err := s.privilegeChecker.requireViewActivityOrViewActivityRedactedPermission(ctx); err != nil {
return nil, err
}
return getCombinedStatementStats(
ctx,
req,
s.sqlServer.pgServer.SQLServer.GetSQLStatsProvider(),
s.internalExecutor,
s.st,
s.sqlServer.execCfg.SQLStatsTestingKnobs)
}
func getCombinedStatementStats(
ctx context.Context,
req *serverpb.CombinedStatementsStatsRequest,
statsProvider sqlstats.Provider,
ie *sql.InternalExecutor,
settings *cluster.Settings,
testingKnobs *sqlstats.TestingKnobs,
) (*serverpb.StatementsResponse, error) {
startTime := getTimeFromSeconds(req.Start)
endTime := getTimeFromSeconds(req.End)
limit := SQLStatsResponseMax.Get(&settings.SV)
whereClause, orderAndLimit, args := getCombinedStatementsQueryClausesAndArgs(startTime, endTime, limit, testingKnobs)
statements, err := collectCombinedStatements(ctx, ie, whereClause, args, orderAndLimit)
if err != nil {
return nil, serverError(ctx, err)
}
transactions, err := collectCombinedTransactions(ctx, ie, whereClause, args, orderAndLimit)
if err != nil {
return nil, serverError(ctx, err)
}
response := &serverpb.StatementsResponse{
Statements: statements,
Transactions: transactions,
LastReset: statsProvider.GetLastReset(),
InternalAppNamePrefix: catconstants.InternalAppNamePrefix,
}
return response, nil
}
// getCombinedStatementsQueryClausesAndArgs returns:
// - where clause (filtering by name and aggregates_ts when defined)
// - order and limit clause
// - args that will replace the clauses above
// The whereClause will be in the format `WHERE A = $1 AND B = $2` and
// args will return the list of arguments in order that will replace the actual values.
func getCombinedStatementsQueryClausesAndArgs(
start, end *time.Time, limit int64, testingKnobs *sqlstats.TestingKnobs,
) (whereClause string, orderAndLimitClause string, args []interface{}) {
var buffer strings.Builder
buffer.WriteString(testingKnobs.GetAOSTClause())
// Filter out internal statements by app name.
buffer.WriteString(fmt.Sprintf(" WHERE app_name NOT LIKE '%s%%'", catconstants.InternalAppNamePrefix))
if start != nil {
buffer.WriteString(" AND aggregated_ts >= $1")
args = append(args, *start)
}
if end != nil {
args = append(args, *end)
buffer.WriteString(fmt.Sprintf(" AND aggregated_ts <= $%d", len(args)))
}
args = append(args, limit)
orderAndLimitClause = fmt.Sprintf(` ORDER BY aggregated_ts DESC LIMIT $%d`, len(args))
return buffer.String(), orderAndLimitClause, args
}
func collectCombinedStatements(
ctx context.Context,
ie *sql.InternalExecutor,
whereClause string,
args []interface{},
orderAndLimit string,
) ([]serverpb.StatementsResponse_CollectedStatementStatistics, error) {
query := fmt.Sprintf(
`SELECT
fingerprint_id,
transaction_fingerprint_id,
app_name,
max(aggregated_ts) as aggregated_ts,
metadata,
crdb_internal.merge_statement_stats(array_agg(statistics)) AS statistics,
max(sampled_plan) AS sampled_plan,
aggregation_interval
FROM crdb_internal.statement_statistics %s
GROUP BY
fingerprint_id,
transaction_fingerprint_id,
app_name,
metadata,
aggregation_interval
%s`, whereClause, orderAndLimit)
const expectedNumDatums = 8
it, err := ie.QueryIteratorEx(ctx, "combined-stmts-by-interval", nil,
sessiondata.InternalExecutorOverride{
User: username.NodeUserName(),
}, query, args...)
if err != nil {
return nil, serverError(ctx, err)
}
defer func() {
closeErr := it.Close()
if closeErr != nil {
err = errors.CombineErrors(err, closeErr)
}
}()
var statements []serverpb.StatementsResponse_CollectedStatementStatistics
var ok bool
for ok, err = it.Next(ctx); ok; ok, err = it.Next(ctx) {
var row tree.Datums
if row = it.Cur(); row == nil {
return nil, errors.New("unexpected null row")
}
if row.Len() != expectedNumDatums {
return nil, errors.Newf("expected %d columns, received %d", expectedNumDatums)
}
var statementFingerprintID uint64
if statementFingerprintID, err = sqlstatsutil.DatumToUint64(row[0]); err != nil {
return nil, serverError(ctx, err)
}
var transactionFingerprintID uint64
if transactionFingerprintID, err = sqlstatsutil.DatumToUint64(row[1]); err != nil {
return nil, serverError(ctx, err)
}
app := string(tree.MustBeDString(row[2]))
aggregatedTs := tree.MustBeDTimestampTZ(row[3]).Time
var metadata roachpb.CollectedStatementStatistics
metadataJSON := tree.MustBeDJSON(row[4]).JSON
if err = sqlstatsutil.DecodeStmtStatsMetadataJSON(metadataJSON, &metadata); err != nil {
return nil, serverError(ctx, err)
}
metadata.Key.App = app
metadata.Key.TransactionFingerprintID =
roachpb.TransactionFingerprintID(transactionFingerprintID)
statsJSON := tree.MustBeDJSON(row[5]).JSON
if err = sqlstatsutil.DecodeStmtStatsStatisticsJSON(statsJSON, &metadata.Stats); err != nil {
return nil, serverError(ctx, err)
}
planJSON := tree.MustBeDJSON(row[6]).JSON
plan, err := sqlstatsutil.JSONToExplainTreePlanNode(planJSON)
if err != nil {
return nil, serverError(ctx, err)
}
metadata.Stats.SensitiveInfo.MostRecentPlanDescription = *plan
aggInterval := tree.MustBeDInterval(row[7]).Duration
stmt := serverpb.StatementsResponse_CollectedStatementStatistics{
Key: serverpb.StatementsResponse_ExtendedStatementStatisticsKey{
KeyData: metadata.Key,
AggregatedTs: aggregatedTs,
AggregationInterval: time.Duration(aggInterval.Nanos()),
},
ID: roachpb.StmtFingerprintID(statementFingerprintID),
Stats: metadata.Stats,
}
statements = append(statements, stmt)
}
if err != nil {
return nil, serverError(ctx, err)
}
return statements, nil
}
func collectCombinedTransactions(
ctx context.Context,
ie *sql.InternalExecutor,
whereClause string,
args []interface{},
orderAndLimit string,
) ([]serverpb.StatementsResponse_ExtendedCollectedTransactionStatistics, error) {
query := fmt.Sprintf(
`SELECT
app_name,
max(aggregated_ts) as aggregated_ts,
fingerprint_id,
metadata,
crdb_internal.merge_transaction_stats(array_agg(statistics)) AS statistics,
aggregation_interval
FROM crdb_internal.transaction_statistics %s
GROUP BY
app_name,
fingerprint_id,
metadata,
aggregation_interval
%s`, whereClause, orderAndLimit)
const expectedNumDatums = 6
it, err := ie.QueryIteratorEx(ctx, "combined-txns-by-interval", nil,
sessiondata.InternalExecutorOverride{
User: username.NodeUserName(),
}, query, args...)
if err != nil {
return nil, serverError(ctx, err)
}
defer func() {
closeErr := it.Close()
if closeErr != nil {
err = errors.CombineErrors(err, closeErr)
}
}()
var transactions []serverpb.StatementsResponse_ExtendedCollectedTransactionStatistics
var ok bool
for ok, err = it.Next(ctx); ok; ok, err = it.Next(ctx) {
var row tree.Datums
if row = it.Cur(); row == nil {
return nil, errors.New("unexpected null row")
}
if row.Len() != expectedNumDatums {
return nil, errors.Newf("expected %d columns, received %d", expectedNumDatums, row.Len())
}
app := string(tree.MustBeDString(row[0]))
aggregatedTs := tree.MustBeDTimestampTZ(row[1]).Time
fingerprintID, err := sqlstatsutil.DatumToUint64(row[2])
if err != nil {
return nil, serverError(ctx, err)
}
var metadata roachpb.CollectedTransactionStatistics
metadataJSON := tree.MustBeDJSON(row[3]).JSON
if err = sqlstatsutil.DecodeTxnStatsMetadataJSON(metadataJSON, &metadata); err != nil {
return nil, serverError(ctx, err)
}
statsJSON := tree.MustBeDJSON(row[4]).JSON
if err = sqlstatsutil.DecodeTxnStatsStatisticsJSON(statsJSON, &metadata.Stats); err != nil {
return nil, serverError(ctx, err)
}
aggInterval := tree.MustBeDInterval(row[5]).Duration
txnStats := serverpb.StatementsResponse_ExtendedCollectedTransactionStatistics{
StatsData: roachpb.CollectedTransactionStatistics{
StatementFingerprintIDs: metadata.StatementFingerprintIDs,
App: app,
Stats: metadata.Stats,
AggregatedTs: aggregatedTs,
AggregationInterval: time.Duration(aggInterval.Nanos()),
TransactionFingerprintID: roachpb.TransactionFingerprintID(fingerprintID),
},
}
transactions = append(transactions, txnStats)
}
if err != nil {
return nil, serverError(ctx, err)
}
return transactions, nil
}
func (s *statusServer) StatementDetails(
ctx context.Context, req *serverpb.StatementDetailsRequest,
) (*serverpb.StatementDetailsResponse, error) {
ctx = propagateGatewayMetadata(ctx)
ctx = s.AnnotateCtx(ctx)
if err := s.privilegeChecker.requireViewActivityOrViewActivityRedactedPermission(ctx); err != nil {
return nil, err
}
return getStatementDetails(
ctx,
req,
s.internalExecutor,
s.st,
s.sqlServer.execCfg.SQLStatsTestingKnobs)
}
func getStatementDetails(
ctx context.Context,
req *serverpb.StatementDetailsRequest,
ie *sql.InternalExecutor,
settings *cluster.Settings,
testingKnobs *sqlstats.TestingKnobs,
) (*serverpb.StatementDetailsResponse, error) {
limit := SQLStatsResponseMax.Get(&settings.SV)
whereClause, args, err := getStatementDetailsQueryClausesAndArgs(req, testingKnobs)
if err != nil {
return nil, serverError(ctx, err)
}
statementTotal, err := getTotalStatementDetails(ctx, ie, whereClause, args)
if err != nil {
return nil, serverError(ctx, err)
}
statementStatisticsPerAggregatedTs, err := getStatementDetailsPerAggregatedTs(ctx, ie, whereClause, args, limit)
if err != nil {
return nil, serverError(ctx, err)
}
statementStatisticsPerPlanHash, err := getStatementDetailsPerPlanHash(ctx, ie, whereClause, args, limit)
if err != nil {
return nil, serverError(ctx, err)
}
// At this point the counts on statementTotal.metadata have the count for how many times we saw that value
// as a row, and not the count of executions for each value.
// The values on statementStatisticsPerPlanHash.Metadata.*Count have the correct count,
// since the metadata is unique per plan hash.
// Update the statementTotal.Metadata.*Count with the counts from statementStatisticsPerPlanHash.keyData.
statementTotal.Metadata.DistSQLCount = 0
statementTotal.Metadata.FailedCount = 0
statementTotal.Metadata.FullScanCount = 0
statementTotal.Metadata.VecCount = 0
statementTotal.Metadata.TotalCount = 0
for _, planStats := range statementStatisticsPerPlanHash {
statementTotal.Metadata.DistSQLCount += planStats.Metadata.DistSQLCount
statementTotal.Metadata.FailedCount += planStats.Metadata.FailedCount
statementTotal.Metadata.FullScanCount += planStats.Metadata.FullScanCount
statementTotal.Metadata.VecCount += planStats.Metadata.VecCount
statementTotal.Metadata.TotalCount += planStats.Metadata.TotalCount
}
response := &serverpb.StatementDetailsResponse{
Statement: statementTotal,
StatementStatisticsPerAggregatedTs: statementStatisticsPerAggregatedTs,
StatementStatisticsPerPlanHash: statementStatisticsPerPlanHash,
InternalAppNamePrefix: catconstants.InternalAppNamePrefix,
}
return response, nil
}
// getStatementDetailsQueryClausesAndArgs returns whereClause and its arguments.
// The whereClause will be in the format `WHERE A = $1 AND B = $2` and
// args will return the list of arguments in order that will replace the actual values.
func getStatementDetailsQueryClausesAndArgs(
req *serverpb.StatementDetailsRequest, testingKnobs *sqlstats.TestingKnobs,
) (whereClause string, args []interface{}, err error) {
var buffer strings.Builder
buffer.WriteString(testingKnobs.GetAOSTClause())
fingerprintID, err := strconv.ParseUint(req.FingerprintId, 10, 64)
if err != nil {
return "", nil, err
}
args = append(args, sqlstatsutil.EncodeUint64ToBytes(fingerprintID))
buffer.WriteString(fmt.Sprintf(" WHERE fingerprint_id = $%d", len(args)))
// Filter out internal statements by app name.
buffer.WriteString(fmt.Sprintf(" AND app_name NOT LIKE '%s%%'", catconstants.InternalAppNamePrefix))
// Statements are grouped ignoring the app name in the Statements/Transactions page, so when
// calling for the Statement Details endpoint, this value can be empty or a list of app names.
if len(req.AppNames) > 0 {
if !(len(req.AppNames) == 1 && req.AppNames[0] == "") {
buffer.WriteString(" AND (")
for i, app := range req.AppNames {
if app == "(unset)" {
app = ""
}
if i != 0 {
args = append(args, app)
buffer.WriteString(fmt.Sprintf(" OR app_name = $%d", len(args)))
} else {
args = append(args, app)
buffer.WriteString(fmt.Sprintf(" app_name = $%d", len(args)))
}
}
buffer.WriteString(" )")
}
}
start := getTimeFromSeconds(req.Start)
if start != nil {
args = append(args, *start)
buffer.WriteString(fmt.Sprintf(" AND aggregated_ts >= $%d", len(args)))
}
end := getTimeFromSeconds(req.End)
if end != nil {
args = append(args, *end)
buffer.WriteString(fmt.Sprintf(" AND aggregated_ts <= $%d", len(args)))
}
whereClause = buffer.String()
return whereClause, args, nil
}
// getTotalStatementDetails return all the statistics for the selectec statement combined.
func getTotalStatementDetails(
ctx context.Context, ie *sql.InternalExecutor, whereClause string, args []interface{},
) (serverpb.StatementDetailsResponse_CollectedStatementSummary, error) {
query := fmt.Sprintf(
`SELECT
crdb_internal.merge_stats_metadata(array_agg(metadata)) AS metadata,
aggregation_interval,
array_agg(app_name) as app_names,
crdb_internal.merge_statement_stats(array_agg(statistics)) AS statistics,
max(sampled_plan) as sampled_plan
FROM crdb_internal.statement_statistics %s
GROUP BY
aggregation_interval
LIMIT 1`, whereClause)
const expectedNumDatums = 5
var statement serverpb.StatementDetailsResponse_CollectedStatementSummary
row, err := ie.QueryRowEx(ctx, "combined-stmts-details-total", nil,
sessiondata.InternalExecutorOverride{
User: username.NodeUserName(),
}, query, args...)
if err != nil {
return statement, serverError(ctx, err)
}
if len(row) == 0 {
return statement, nil
}
if row.Len() != expectedNumDatums {
return statement, serverError(ctx, errors.Newf("expected %d columns, received %d", expectedNumDatums))
}
var statistics roachpb.CollectedStatementStatistics
var aggregatedMetadata roachpb.AggregatedStatementMetadata
metadataJSON := tree.MustBeDJSON(row[0]).JSON
if err = sqlstatsutil.DecodeAggregatedMetadataJSON(metadataJSON, &aggregatedMetadata); err != nil {
return statement, serverError(ctx, err)
}
aggInterval := tree.MustBeDInterval(row[1]).Duration
apps := tree.MustBeDArray(row[2])
var appNames []string
for _, s := range apps.Array {
appNames = util.CombineUniqueString(appNames, []string{string(tree.MustBeDString(s))})
}
aggregatedMetadata.AppNames = appNames
statsJSON := tree.MustBeDJSON(row[3]).JSON
if err = sqlstatsutil.DecodeStmtStatsStatisticsJSON(statsJSON, &statistics.Stats); err != nil {
return statement, serverError(ctx, err)
}
planJSON := tree.MustBeDJSON(row[4]).JSON
plan, err := sqlstatsutil.JSONToExplainTreePlanNode(planJSON)
if err != nil {
return statement, serverError(ctx, err)
}
statistics.Stats.SensitiveInfo.MostRecentPlanDescription = *plan
args = []interface{}{}
args = append(args, aggregatedMetadata.Query)
query = fmt.Sprintf(
`SELECT prettify_statement($1, %d, %d, %d)`,
tree.ConsoleLineWidth, tree.PrettyAlignAndDeindent, tree.UpperCase)
row, err = ie.QueryRowEx(ctx, "combined-stmts-details-format-query", nil,
sessiondata.InternalExecutorOverride{
User: username.NodeUserName(),
}, query, args...)
if err != nil {
return statement, serverError(ctx, err)
}
aggregatedMetadata.FormattedQuery = string(tree.MustBeDString(row[0]))
statement = serverpb.StatementDetailsResponse_CollectedStatementSummary{
Metadata: aggregatedMetadata,
AggregationInterval: time.Duration(aggInterval.Nanos()),
Stats: statistics.Stats,
}
return statement, nil
}
// getStatementDetailsPerAggregatedTs returns the list of statements
// per aggregated timestamp, not using the columns plan hash as
// part of the key on the grouping.
func getStatementDetailsPerAggregatedTs(
ctx context.Context,
ie *sql.InternalExecutor,
whereClause string,
args []interface{},
limit int64,
) ([]serverpb.StatementDetailsResponse_CollectedStatementGroupedByAggregatedTs, error) {
query := fmt.Sprintf(
`SELECT
aggregated_ts,
crdb_internal.merge_stats_metadata(array_agg(metadata)) AS metadata,
crdb_internal.merge_statement_stats(array_agg(statistics)) AS statistics,
max(sampled_plan) as sampled_plan,
aggregation_interval
FROM crdb_internal.statement_statistics %s
GROUP BY
aggregated_ts,
aggregation_interval
ORDER BY aggregated_ts ASC
LIMIT $%d`, whereClause, len(args)+1)
args = append(args, limit)
const expectedNumDatums = 5
it, err := ie.QueryIteratorEx(ctx, "combined-stmts-details-by-aggregated-timestamp", nil,
sessiondata.InternalExecutorOverride{
User: username.NodeUserName(),
}, query, args...)
if err != nil {
return nil, serverError(ctx, err)
}
defer func() {
closeErr := it.Close()
if closeErr != nil {
err = errors.CombineErrors(err, closeErr)
}
}()
var statements []serverpb.StatementDetailsResponse_CollectedStatementGroupedByAggregatedTs
var ok bool
for ok, err = it.Next(ctx); ok; ok, err = it.Next(ctx) {
var row tree.Datums
if row = it.Cur(); row == nil {
return nil, errors.New("unexpected null row")
}
if row.Len() != expectedNumDatums {
return nil, errors.Newf("expected %d columns, received %d", expectedNumDatums)
}
aggregatedTs := tree.MustBeDTimestampTZ(row[0]).Time
var metadata roachpb.CollectedStatementStatistics
var aggregatedMetadata roachpb.AggregatedStatementMetadata
metadataJSON := tree.MustBeDJSON(row[1]).JSON
if err = sqlstatsutil.DecodeAggregatedMetadataJSON(metadataJSON, &aggregatedMetadata); err != nil {
return nil, serverError(ctx, err)
}
statsJSON := tree.MustBeDJSON(row[2]).JSON
if err = sqlstatsutil.DecodeStmtStatsStatisticsJSON(statsJSON, &metadata.Stats); err != nil {
return nil, serverError(ctx, err)
}
planJSON := tree.MustBeDJSON(row[3]).JSON
plan, err := sqlstatsutil.JSONToExplainTreePlanNode(planJSON)
if err != nil {
return nil, serverError(ctx, err)
}
metadata.Stats.SensitiveInfo.MostRecentPlanDescription = *plan
aggInterval := tree.MustBeDInterval(row[4]).Duration
stmt := serverpb.StatementDetailsResponse_CollectedStatementGroupedByAggregatedTs{
AggregatedTs: aggregatedTs,
AggregationInterval: time.Duration(aggInterval.Nanos()),
Stats: metadata.Stats,
Metadata: aggregatedMetadata,
}
statements = append(statements, stmt)
}
if err != nil {
return nil, serverError(ctx, err)
}
return statements, nil
}
// getExplainPlanFromGist decode the Explain Plan from a Plan Gist.
func getExplainPlanFromGist(ctx context.Context, ie *sql.InternalExecutor, planGist string) string {
planError := "Error collecting Explain Plan."
var args []interface{}
query := `SELECT crdb_internal.decode_plan_gist($1)`
args = append(args, planGist)
it, err := ie.QueryIteratorEx(ctx, "combined-stmts-details-get-explain-plan", nil,
sessiondata.InternalExecutorOverride{
User: username.NodeUserName(),
}, query, args...)
if err != nil {
return planError
}
var explainPlan []string
var ok bool
for ok, err = it.Next(ctx); ok; ok, err = it.Next(ctx) {
var row tree.Datums
if row = it.Cur(); row == nil {
return planError
}
explainPlanLine := string(tree.MustBeDString(row[0]))
explainPlan = append(explainPlan, explainPlanLine)
}
if err != nil {
return planError
}
return strings.Join(explainPlan, "\n")
}
// getStatementDetailsPerPlanHash returns the list of statements
// per plan hash, not using the columns aggregated timestamp as
// part of the key on the grouping.
func getStatementDetailsPerPlanHash(
ctx context.Context,
ie *sql.InternalExecutor,
whereClause string,
args []interface{},
limit int64,
) ([]serverpb.StatementDetailsResponse_CollectedStatementGroupedByPlanHash, error) {
query := fmt.Sprintf(
`SELECT
plan_hash,
(statistics -> 'statistics' -> 'planGists'->>0) as plan_gist,
crdb_internal.merge_stats_metadata(array_agg(metadata)) AS metadata,
crdb_internal.merge_statement_stats(array_agg(statistics)) AS statistics,
max(sampled_plan) as sampled_plan,
aggregation_interval,
index_recommendations
FROM crdb_internal.statement_statistics %s
GROUP BY
plan_hash,
plan_gist,
aggregation_interval,
index_recommendations
LIMIT $%d`, whereClause, len(args)+1)
args = append(args, limit)
const expectedNumDatums = 7
it, err := ie.QueryIteratorEx(ctx, "combined-stmts-details-by-plan-hash", nil,
sessiondata.InternalExecutorOverride{
User: username.NodeUserName(),
}, query, args...)
if err != nil {
return nil, serverError(ctx, err)
}
defer func() {
closeErr := it.Close()
if closeErr != nil {
err = errors.CombineErrors(err, closeErr)
}
}()
var statements []serverpb.StatementDetailsResponse_CollectedStatementGroupedByPlanHash
var ok bool
for ok, err = it.Next(ctx); ok; ok, err = it.Next(ctx) {
var row tree.Datums
if row = it.Cur(); row == nil {
return nil, errors.New("unexpected null row")
}
if row.Len() != expectedNumDatums {
return nil, errors.Newf("expected %d columns, received %d", expectedNumDatums)
}
var planHash uint64
if planHash, err = sqlstatsutil.DatumToUint64(row[0]); err != nil {
return nil, serverError(ctx, err)
}
planGist := string(tree.MustBeDStringOrDNull(row[1]))
var explainPlan string
if planGist != "" {
explainPlan = getExplainPlanFromGist(ctx, ie, planGist)
}
var metadata roachpb.CollectedStatementStatistics
var aggregatedMetadata roachpb.AggregatedStatementMetadata
metadataJSON := tree.MustBeDJSON(row[2]).JSON
if err = sqlstatsutil.DecodeAggregatedMetadataJSON(metadataJSON, &aggregatedMetadata); err != nil {
return nil, serverError(ctx, err)
}
statsJSON := tree.MustBeDJSON(row[3]).JSON
if err = sqlstatsutil.DecodeStmtStatsStatisticsJSON(statsJSON, &metadata.Stats); err != nil {
return nil, serverError(ctx, err)
}
planJSON := tree.MustBeDJSON(row[4]).JSON
plan, err := sqlstatsutil.JSONToExplainTreePlanNode(planJSON)
if err != nil {
return nil, serverError(ctx, err)
}
metadata.Stats.SensitiveInfo.MostRecentPlanDescription = *plan
aggInterval := tree.MustBeDInterval(row[5]).Duration
recommendations := tree.MustBeDArray(row[6])
var idxRecommendations []string
for _, s := range recommendations.Array {
idxRecommendations = util.CombineUniqueString(idxRecommendations, []string{string(tree.MustBeDString(s))})
}
// A metadata is unique for each plan, meaning if any of the counts are greater than zero,
// we can update the value of each count with the execution count of this plan hash to
// have the correct count of each metric.
if aggregatedMetadata.DistSQLCount > 0 {
aggregatedMetadata.DistSQLCount = metadata.Stats.Count
}
if aggregatedMetadata.FailedCount > 0 {
aggregatedMetadata.FailedCount = metadata.Stats.Count
}
if aggregatedMetadata.FullScanCount > 0 {
aggregatedMetadata.FullScanCount = metadata.Stats.Count
}
if aggregatedMetadata.VecCount > 0 {
aggregatedMetadata.VecCount = metadata.Stats.Count
}
aggregatedMetadata.TotalCount = metadata.Stats.Count
stmt := serverpb.StatementDetailsResponse_CollectedStatementGroupedByPlanHash{
AggregationInterval: time.Duration(aggInterval.Nanos()),
ExplainPlan: explainPlan,
PlanHash: planHash,
Stats: metadata.Stats,
Metadata: aggregatedMetadata,
IndexRecommendations: idxRecommendations,
}
statements = append(statements, stmt)
}
if err != nil {
return nil, serverError(ctx, err)
}
return statements, nil
}