Skip to content

Commit e87386e

Browse files
authored
Merge pull request cockroachdb#156592 from andy-kimball/backport25.3-156459
release-25.3: sql: add read/write metrics
2 parents 5066155 + 3604c8f commit e87386e

23 files changed

+441
-14
lines changed

docs/generated/metrics/metrics.yaml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8905,6 +8905,70 @@ layers:
89058905
unit: COUNT
89068906
aggregation: AVG
89078907
derivative: NON_NEGATIVE_DERIVATIVE
8908+
- name: sql.statements.bytes_read.count
8909+
exported_name: sql_statements_bytes_read_count
8910+
description: Number of bytes read by SQL statements
8911+
y_axis_label: Bytes
8912+
type: COUNTER
8913+
unit: BYTES
8914+
aggregation: AVG
8915+
derivative: NON_NEGATIVE_DERIVATIVE
8916+
- name: sql.statements.bytes_read.count.internal
8917+
exported_name: sql_statements_bytes_read_count_internal
8918+
description: Number of bytes read by SQL statements (internal queries)
8919+
y_axis_label: SQL Internal Statements
8920+
type: COUNTER
8921+
unit: BYTES
8922+
aggregation: AVG
8923+
derivative: NON_NEGATIVE_DERIVATIVE
8924+
- name: sql.statements.index_bytes_written.count
8925+
exported_name: sql_statements_index_bytes_written_count
8926+
description: Number of primary and secondary index bytes modified by SQL statements
8927+
y_axis_label: Bytes
8928+
type: COUNTER
8929+
unit: BYTES
8930+
aggregation: AVG
8931+
derivative: NON_NEGATIVE_DERIVATIVE
8932+
- name: sql.statements.index_bytes_written.count.internal
8933+
exported_name: sql_statements_index_bytes_written_count_internal
8934+
description: Number of primary and secondary index bytes modified by SQL statements (internal queries)
8935+
y_axis_label: SQL Internal Statements
8936+
type: COUNTER
8937+
unit: BYTES
8938+
aggregation: AVG
8939+
derivative: NON_NEGATIVE_DERIVATIVE
8940+
- name: sql.statements.index_rows_written.count
8941+
exported_name: sql_statements_index_rows_written_count
8942+
description: Number of primary and secondary index rows modified by SQL statements
8943+
y_axis_label: Rows
8944+
type: COUNTER
8945+
unit: COUNT
8946+
aggregation: AVG
8947+
derivative: NON_NEGATIVE_DERIVATIVE
8948+
- name: sql.statements.index_rows_written.count.internal
8949+
exported_name: sql_statements_index_rows_written_count_internal
8950+
description: Number of primary and secondary index rows modified by SQL statements (internal queries)
8951+
y_axis_label: SQL Internal Statements
8952+
type: COUNTER
8953+
unit: COUNT
8954+
aggregation: AVG
8955+
derivative: NON_NEGATIVE_DERIVATIVE
8956+
- name: sql.statements.rows_read.count
8957+
exported_name: sql_statements_rows_read_count
8958+
description: Number of rows read by SQL statements
8959+
y_axis_label: Rows
8960+
type: COUNTER
8961+
unit: COUNT
8962+
aggregation: AVG
8963+
derivative: NON_NEGATIVE_DERIVATIVE
8964+
- name: sql.statements.rows_read.count.internal
8965+
exported_name: sql_statements_rows_read_count_internal
8966+
description: Number of rows read by SQL statements (internal queries)
8967+
y_axis_label: SQL Internal Statements
8968+
type: COUNTER
8969+
unit: COUNT
8970+
aggregation: AVG
8971+
derivative: NON_NEGATIVE_DERIVATIVE
89088972
- name: sql.stats.activity.update.latency
89098973
exported_name: sql_stats_activity_update_latency
89108974
description: The latency of updates made by the SQL activity updater job. Includes failed update attempts

pkg/roachprod/opentelemetry/cockroachdb_metrics.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,6 +1889,14 @@ var cockroachdbMetrics = map[string]string{
18891889
"sql_service_latency_sum": "sql.service.latency.sum",
18901890
"sql_statements_active": "sql.statements.active",
18911891
"sql_statements_active_internal": "sql.statements.active.internal",
1892+
"sql_statements_rows_read_count": "sql.statements.rows_read.count",
1893+
"sql_statements_rows_read_count_internal": "sql.statements.rows_read.count.internal",
1894+
"sql_statements_bytes_read_count": "sql.statements.bytes_read.count",
1895+
"sql_statements_bytes_read_count_internal": "sql.statements.bytes_read.count.internal",
1896+
"sql_statements_index_rows_written_count": "sql.statements.index_rows_written.count",
1897+
"sql_statements_index_rows_written_count_internal": "sql.statements.index_rows_written.count.internal",
1898+
"sql_statements_index_bytes_written_count": "sql.statements.index_bytes_written.count",
1899+
"sql_statements_index_bytes_written_count_internal": "sql.statements.index_bytes_written.count.internal",
18921900
"sql_stats_activity_update_latency": "sql.stats.activity.update.latency",
18931901
"sql_stats_activity_update_latency_bucket": "sql.stats.activity.update.latency.bucket",
18941902
"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
@@ -960,6 +960,7 @@ go_test(
960960
"//pkg/util/log/logconfig",
961961
"//pkg/util/log/logpb",
962962
"//pkg/util/log/logtestutils",
963+
"//pkg/util/metamorphic",
963964
"//pkg/util/metric",
964965
"//pkg/util/mon",
965966
"//pkg/util/protoutil",

pkg/sql/conn_executor.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,10 @@ func makeMetrics(internal bool, sv *settings.Values) Metrics {
619619
FullTableOrIndexScanRejectedCount: metric.NewCounter(getMetricMeta(MetaFullTableOrIndexScanRejected, internal)),
620620
TxnRetryCount: metric.NewCounter(getMetricMeta(MetaTxnRetry, internal)),
621621
StatementRetryCount: metric.NewCounter(getMetricMeta(MetaStatementRetry, internal)),
622+
StatementRowsRead: metric.NewCounter(getMetricMeta(MetaStatementRowsRead, internal)),
623+
StatementBytesRead: metric.NewCounter(getMetricMeta(MetaStatementBytesRead, internal)),
624+
StatementIndexRowsWritten: metric.NewCounter(getMetricMeta(MetaStatementIndexRowsWritten, internal)),
625+
StatementIndexBytesWritten: metric.NewCounter(getMetricMeta(MetaStatementIndexBytesWritten, internal)),
622626
},
623627
StartedStatementCounters: makeStartedStatementCounters(internal),
624628
ExecutedStatementCounters: makeExecutedStatementCounters(internal),

pkg/sql/conn_executor_exec.go

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

34843484
// topLevelQueryStats returns some basic statistics about the run of the query.
34853485
type topLevelQueryStats struct {
3486-
// bytesRead is the number of bytes read from disk.
3487-
bytesRead int64
3488-
// rowsRead is the number of rows read from disk.
3486+
// rowsRead is the number of rows read from primary and secondary indexes.
34893487
rowsRead int64
3490-
// rowsWritten is the number of rows written.
3488+
// bytesRead is the number of bytes read from primary and secondary indexes.
3489+
bytesRead int64
3490+
// rowsWritten is the number of rows written to the primary index. It does not
3491+
// include rows written to secondary indexes.
3492+
// NB: There is an asymmetry between rowsRead and rowsWritten - rowsRead
3493+
// includes rows read from secondary indexes, while rowsWritten does not
3494+
// include rows written to secondary indexes. This matches the behavior of
3495+
// EXPLAIN ANALYZE and SQL "rows affected".
34913496
rowsWritten int64
3497+
// indexRowsWritten is the number of rows written to primary and secondary
3498+
// indexes. It is always >= rowsWritten.
3499+
indexRowsWritten int64
3500+
// indexBytesWritten is the number of bytes written to primary and secondary
3501+
// indexes.
3502+
indexBytesWritten int64
34923503
// networkEgressEstimate is an estimate for the number of bytes sent to the
34933504
// client. It is used for estimating the number of RUs consumed by a query.
34943505
networkEgressEstimate int64
@@ -3502,6 +3513,8 @@ func (s *topLevelQueryStats) add(other *topLevelQueryStats) {
35023513
s.bytesRead += other.bytesRead
35033514
s.rowsRead += other.rowsRead
35043515
s.rowsWritten += other.rowsWritten
3516+
s.indexBytesWritten += other.indexBytesWritten
3517+
s.indexRowsWritten += other.indexRowsWritten
35053518
s.networkEgressEstimate += other.networkEgressEstimate
35063519
s.clientTime += other.clientTime
35073520
}

pkg/sql/delete.go

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

262+
func (d *deleteNode) indexRowsWritten() int64 {
263+
return d.run.td.indexRowsWritten
264+
}
265+
266+
func (d *deleteNode) indexBytesWritten() int64 {
267+
// No bytes counted as written for a deletion.
268+
return 0
269+
}
270+
262271
func (d *deleteNode) enableAutoCommit() {
263272
d.run.td.enableAutoCommit()
264273
}

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/delete_swap.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ func (d *deleteSwapNode) rowsWritten() int64 {
143143
return d.run.td.rowsWritten
144144
}
145145

146+
func (d *deleteSwapNode) indexRowsWritten() int64 {
147+
return d.run.td.indexRowsWritten
148+
}
149+
150+
func (d *deleteSwapNode) indexBytesWritten() int64 {
151+
// No bytes counted as written for a deletion.
152+
return 0
153+
}
154+
146155
func (d *deleteSwapNode) enableAutoCommit() {
147156
d.run.td.enableAutoCommit()
148157
}

pkg/sql/distsql_running.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,8 @@ func (r *DistSQLReceiver) pushMeta(meta *execinfrapb.ProducerMetadata) execinfra
15391539
r.stats.bytesRead += meta.Metrics.BytesRead
15401540
r.stats.rowsRead += meta.Metrics.RowsRead
15411541
r.stats.rowsWritten += meta.Metrics.RowsWritten
1542+
r.stats.indexRowsWritten += meta.Metrics.IndexRowsWritten
1543+
r.stats.indexBytesWritten += meta.Metrics.IndexBytesWritten
15421544
if r.progressAtomic != nil && r.expectedRowsRead != 0 {
15431545
progress := float64(r.stats.rowsRead) / float64(r.expectedRowsRead)
15441546
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
@@ -1379,6 +1379,30 @@ var (
13791379
Measurement: "SQL Statements",
13801380
Unit: metric.Unit_COUNT,
13811381
}
1382+
MetaStatementRowsRead = metric.Metadata{
1383+
Name: "sql.statements.rows_read.count",
1384+
Help: "Number of rows read by SQL statements",
1385+
Measurement: "Rows",
1386+
Unit: metric.Unit_COUNT,
1387+
}
1388+
MetaStatementBytesRead = metric.Metadata{
1389+
Name: "sql.statements.bytes_read.count",
1390+
Help: "Number of bytes read by SQL statements",
1391+
Measurement: "Bytes",
1392+
Unit: metric.Unit_BYTES,
1393+
}
1394+
MetaStatementIndexRowsWritten = metric.Metadata{
1395+
Name: "sql.statements.index_rows_written.count",
1396+
Help: "Number of primary and secondary index rows modified by SQL statements",
1397+
Measurement: "Rows",
1398+
Unit: metric.Unit_COUNT,
1399+
}
1400+
MetaStatementIndexBytesWritten = metric.Metadata{
1401+
Name: "sql.statements.index_bytes_written.count",
1402+
Help: "Number of primary and secondary index bytes modified by SQL statements",
1403+
Measurement: "Bytes",
1404+
Unit: metric.Unit_BYTES,
1405+
}
13821406
)
13831407

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

0 commit comments

Comments
 (0)