Skip to content

Commit 3c396f9

Browse files
authored
Merge pull request cockroachdb#156595 from andy-kimball/backport25.2-156592
release-25.2: sql: add read/write metrics
2 parents 8b6db0d + fd9ef8e commit 3c396f9

21 files changed

+368
-14
lines changed

docs/generated/metrics/metrics.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,6 +1834,14 @@
18341834
<tr><td>APPLICATION</td><td>sql.statements.active.internal</td><td>Number of currently active user SQL statements (internal queries)</td><td>SQL Internal Statements</td><td>GAUGE</td><td>COUNT</td><td>AVG</td><td>NONE</td></tr>
18351835
<tr><td>APPLICATION</td><td>sql.statements.auto_retry.count</td><td>Number of SQL statement automatic retries</td><td>SQL Statements</td><td>COUNTER</td><td>COUNT</td><td>AVG</td><td>NON_NEGATIVE_DERIVATIVE</td></tr>
18361836
<tr><td>APPLICATION</td><td>sql.statements.auto_retry.count.internal</td><td>Number of SQL statement automatic retries (internal queries)</td><td>SQL Internal Statements</td><td>COUNTER</td><td>COUNT</td><td>AVG</td><td>NON_NEGATIVE_DERIVATIVE</td></tr>
1837+
<tr><td>APPLICATION</td><td>sql.statements.bytes_read.count</td><td>Number of bytes read by SQL statements</td><td>Bytes</td><td>COUNTER</td><td>BYTES</td><td>AVG</td><td>NON_NEGATIVE_DERIVATIVE</td></tr>
1838+
<tr><td>APPLICATION</td><td>sql.statements.bytes_read.count.internal</td><td>Number of bytes read by SQL statements (internal queries)</td><td>SQL Internal Statements</td><td>COUNTER</td><td>BYTES</td><td>AVG</td><td>NON_NEGATIVE_DERIVATIVE</td></tr>
1839+
<tr><td>APPLICATION</td><td>sql.statements.index_bytes_written.count</td><td>Number of primary and secondary index bytes modified by SQL statements</td><td>Bytes</td><td>COUNTER</td><td>BYTES</td><td>AVG</td><td>NON_NEGATIVE_DERIVATIVE</td></tr>
1840+
<tr><td>APPLICATION</td><td>sql.statements.index_bytes_written.count.internal</td><td>Number of primary and secondary index bytes modified by SQL statements (internal queries)</td><td>SQL Internal Statements</td><td>COUNTER</td><td>BYTES</td><td>AVG</td><td>NON_NEGATIVE_DERIVATIVE</td></tr>
1841+
<tr><td>APPLICATION</td><td>sql.statements.index_rows_written.count</td><td>Number of primary and secondary index rows modified by SQL statements</td><td>Rows</td><td>COUNTER</td><td>COUNT</td><td>AVG</td><td>NON_NEGATIVE_DERIVATIVE</td></tr>
1842+
<tr><td>APPLICATION</td><td>sql.statements.index_rows_written.count.internal</td><td>Number of primary and secondary index rows modified by SQL statements (internal queries)</td><td>SQL Internal Statements</td><td>COUNTER</td><td>COUNT</td><td>AVG</td><td>NON_NEGATIVE_DERIVATIVE</td></tr>
1843+
<tr><td>APPLICATION</td><td>sql.statements.rows_read.count</td><td>Number of rows read by SQL statements</td><td>Rows</td><td>COUNTER</td><td>COUNT</td><td>AVG</td><td>NON_NEGATIVE_DERIVATIVE</td></tr>
1844+
<tr><td>APPLICATION</td><td>sql.statements.rows_read.count.internal</td><td>Number of rows read by SQL statements (internal queries)</td><td>SQL Internal Statements</td><td>COUNTER</td><td>COUNT</td><td>AVG</td><td>NON_NEGATIVE_DERIVATIVE</td></tr>
18371845
<tr><td>APPLICATION</td><td>sql.stats.activity.update.latency</td><td>The latency of updates made by the SQL activity updater job. Includes failed update attempts</td><td>Nanoseconds</td><td>HISTOGRAM</td><td>NANOSECONDS</td><td>AVG</td><td>NONE</td></tr>
18381846
<tr><td>APPLICATION</td><td>sql.stats.activity.updates.failed</td><td>Number of update attempts made by the SQL activity updater job that failed with errors</td><td>failed updates</td><td>COUNTER</td><td>COUNT</td><td>AVG</td><td>NON_NEGATIVE_DERIVATIVE</td></tr>
18391847
<tr><td>APPLICATION</td><td>sql.stats.activity.updates.successful</td><td>Number of successful updates made by the SQL activity updater job</td><td>successful updates</td><td>COUNTER</td><td>COUNT</td><td>AVG</td><td>NON_NEGATIVE_DERIVATIVE</td></tr>

pkg/roachprod/opentelemetry/cockroachdb_metrics.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,6 +1844,14 @@ var cockroachdbMetrics = map[string]string{
18441844
"sql_service_latency_sum": "sql.service.latency.sum",
18451845
"sql_statements_active": "sql.statements.active",
18461846
"sql_statements_active_internal": "sql.statements.active.internal",
1847+
"sql_statements_rows_read_count": "sql.statements.rows_read.count",
1848+
"sql_statements_rows_read_count_internal": "sql.statements.rows_read.count.internal",
1849+
"sql_statements_bytes_read_count": "sql.statements.bytes_read.count",
1850+
"sql_statements_bytes_read_count_internal": "sql.statements.bytes_read.count.internal",
1851+
"sql_statements_index_rows_written_count": "sql.statements.index_rows_written.count",
1852+
"sql_statements_index_rows_written_count_internal": "sql.statements.index_rows_written.count.internal",
1853+
"sql_statements_index_bytes_written_count": "sql.statements.index_bytes_written.count",
1854+
"sql_statements_index_bytes_written_count_internal": "sql.statements.index_bytes_written.count.internal",
18471855
"sql_stats_activity_update_latency": "sql.stats.activity.update.latency",
18481856
"sql_stats_activity_update_latency_bucket": "sql.stats.activity.update.latency.bucket",
18491857
"sql_stats_activity_update_latency_count": "sql.stats.activity.update.latency.count",

pkg/sql/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,7 @@ go_test(
944944
"//pkg/util/log/logconfig",
945945
"//pkg/util/log/logpb",
946946
"//pkg/util/log/logtestutils",
947+
"//pkg/util/metamorphic",
947948
"//pkg/util/metric",
948949
"//pkg/util/mon",
949950
"//pkg/util/protoutil",

pkg/sql/conn_executor.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,10 @@ func makeMetrics(internal bool, sv *settings.Values) Metrics {
596596
FullTableOrIndexScanRejectedCount: metric.NewCounter(getMetricMeta(MetaFullTableOrIndexScanRejected, internal)),
597597
TxnRetryCount: metric.NewCounter(getMetricMeta(MetaTxnRetry, internal)),
598598
StatementRetryCount: metric.NewCounter(getMetricMeta(MetaStatementRetry, internal)),
599+
StatementRowsRead: metric.NewCounter(getMetricMeta(MetaStatementRowsRead, internal)),
600+
StatementBytesRead: metric.NewCounter(getMetricMeta(MetaStatementBytesRead, internal)),
601+
StatementIndexRowsWritten: metric.NewCounter(getMetricMeta(MetaStatementIndexRowsWritten, internal)),
602+
StatementIndexBytesWritten: metric.NewCounter(getMetricMeta(MetaStatementIndexBytesWritten, internal)),
599603
},
600604
StartedStatementCounters: makeStartedStatementCounters(internal),
601605
ExecutedStatementCounters: makeExecutedStatementCounters(internal),

pkg/sql/conn_executor_exec.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3438,12 +3438,23 @@ func (ex *connExecutor) makeExecPlan(
34383438

34393439
// topLevelQueryStats returns some basic statistics about the run of the query.
34403440
type topLevelQueryStats struct {
3441-
// bytesRead is the number of bytes read from disk.
3442-
bytesRead int64
3443-
// rowsRead is the number of rows read from disk.
3441+
// rowsRead is the number of rows read from primary and secondary indexes.
34443442
rowsRead int64
3445-
// rowsWritten is the number of rows written.
3443+
// bytesRead is the number of bytes read from primary and secondary indexes.
3444+
bytesRead int64
3445+
// rowsWritten is the number of rows written to the primary index. It does not
3446+
// include rows written to secondary indexes.
3447+
// NB: There is an asymmetry between rowsRead and rowsWritten - rowsRead
3448+
// includes rows read from secondary indexes, while rowsWritten does not
3449+
// include rows written to secondary indexes. This matches the behavior of
3450+
// EXPLAIN ANALYZE and SQL "rows affected".
34463451
rowsWritten int64
3452+
// indexRowsWritten is the number of rows written to primary and secondary
3453+
// indexes. It is always >= rowsWritten.
3454+
indexRowsWritten int64
3455+
// indexBytesWritten is the number of bytes written to primary and secondary
3456+
// indexes.
3457+
indexBytesWritten int64
34473458
// networkEgressEstimate is an estimate for the number of bytes sent to the
34483459
// client. It is used for estimating the number of RUs consumed by a query.
34493460
networkEgressEstimate int64
@@ -3457,6 +3468,8 @@ func (s *topLevelQueryStats) add(other *topLevelQueryStats) {
34573468
s.bytesRead += other.bytesRead
34583469
s.rowsRead += other.rowsRead
34593470
s.rowsWritten += other.rowsWritten
3471+
s.indexBytesWritten += other.indexBytesWritten
3472+
s.indexRowsWritten += other.indexRowsWritten
34603473
s.networkEgressEstimate += other.networkEgressEstimate
34613474
s.clientTime += other.clientTime
34623475
}

pkg/sql/delete.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,15 @@ func (d *deleteNode) rowsWritten() int64 {
251251
return d.run.td.rowsWritten
252252
}
253253

254+
func (d *deleteNode) indexRowsWritten() int64 {
255+
return d.run.td.indexRowsWritten
256+
}
257+
258+
func (d *deleteNode) indexBytesWritten() int64 {
259+
// No bytes counted as written for a deletion.
260+
return 0
261+
}
262+
254263
func (d *deleteNode) enableAutoCommit() {
255264
d.run.td.enableAutoCommit()
256265
}

pkg/sql/delete_range.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ func (d *deleteRangeNode) rowsWritten() int64 {
7878
return int64(d.rowCount)
7979
}
8080

81+
func (d *deleteRangeNode) indexRowsWritten() int64 {
82+
// Same as rowsWritten, because deleteRangeNode only applies to primary index
83+
// rows (it is not used if there's a secondary index on the table).
84+
return int64(d.rowCount)
85+
}
86+
87+
func (d *deleteRangeNode) indexBytesWritten() int64 {
88+
// No bytes counted as written for a deletion.
89+
return 0
90+
}
91+
8192
// startExec implements the planNode interface.
8293
func (d *deleteRangeNode) startExec(params runParams) error {
8394
if err := params.p.cancelChecker.Check(); err != nil {

pkg/sql/distsql_running.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,8 @@ func (r *DistSQLReceiver) pushMeta(meta *execinfrapb.ProducerMetadata) execinfra
15011501
r.stats.bytesRead += meta.Metrics.BytesRead
15021502
r.stats.rowsRead += meta.Metrics.RowsRead
15031503
r.stats.rowsWritten += meta.Metrics.RowsWritten
1504+
r.stats.indexRowsWritten += meta.Metrics.IndexRowsWritten
1505+
r.stats.indexBytesWritten += meta.Metrics.IndexBytesWritten
15041506
if r.progressAtomic != nil && r.expectedRowsRead != 0 {
15051507
progress := float64(r.stats.rowsRead) / float64(r.expectedRowsRead)
15061508
atomic.StoreUint64(r.progressAtomic, math.Float64bits(progress))

pkg/sql/exec_util.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,30 @@ var (
12871287
Measurement: "SQL Statements",
12881288
Unit: metric.Unit_COUNT,
12891289
}
1290+
MetaStatementRowsRead = metric.Metadata{
1291+
Name: "sql.statements.rows_read.count",
1292+
Help: "Number of rows read by SQL statements",
1293+
Measurement: "Rows",
1294+
Unit: metric.Unit_COUNT,
1295+
}
1296+
MetaStatementBytesRead = metric.Metadata{
1297+
Name: "sql.statements.bytes_read.count",
1298+
Help: "Number of bytes read by SQL statements",
1299+
Measurement: "Bytes",
1300+
Unit: metric.Unit_BYTES,
1301+
}
1302+
MetaStatementIndexRowsWritten = metric.Metadata{
1303+
Name: "sql.statements.index_rows_written.count",
1304+
Help: "Number of primary and secondary index rows modified by SQL statements",
1305+
Measurement: "Rows",
1306+
Unit: metric.Unit_COUNT,
1307+
}
1308+
MetaStatementIndexBytesWritten = metric.Metadata{
1309+
Name: "sql.statements.index_bytes_written.count",
1310+
Help: "Number of primary and secondary index bytes modified by SQL statements",
1311+
Measurement: "Bytes",
1312+
Unit: metric.Unit_BYTES,
1313+
}
12901314
)
12911315

12921316
func getMetricMeta(meta metric.Metadata, internal bool) metric.Metadata {

pkg/sql/execinfrapb/data.proto

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,23 @@ message RemoteProducerMetadata {
334334
}
335335
// Metrics are unconditionally emitted by table readers.
336336
message Metrics {
337-
// Total number of bytes read while executing a statement.
337+
// BytesRead is the number of primary and secondary index bytes read while
338+
// executing a statement.
338339
optional int64 bytes_read = 1 [(gogoproto.nullable) = false];
339-
// Total number of rows read while executing a statement.
340+
// RowsRead is the number of primary and secondary index rows read while
341+
// executing a statement.
340342
optional int64 rows_read = 2 [(gogoproto.nullable) = false];
341-
// Total number of rows modified while executing a statement.
343+
// RowsWritten is the number of primary index rows modified while executing
344+
// a statement. It does not include secondary index rows.
342345
optional int64 rows_written = 3 [(gogoproto.nullable) = false];
346+
// IndexRowsWritten is the number of primary and secondary index rows
347+
// modified while executing a statement. It is always >= RowsWritten.
348+
optional int64 index_rows_written = 5 [(gogoproto.nullable) = false];
349+
// IndexBytesWritten is the number of primary and secondary index bytes
350+
// modified while executing a statement.
351+
optional int64 index_bytes_written = 6 [(gogoproto.nullable) = false];
352+
reserved 4;
353+
// NEXT ID: 7
343354
}
344355
oneof value {
345356
RangeInfos range_info = 1;

0 commit comments

Comments
 (0)