Skip to content

Commit 99dd3cd

Browse files
authored
Merge pull request #156459 from andy-kimball/backport25.4-155820
release-25.4: sql: add read/write metrics
2 parents 398ae39 + 3d860c6 commit 99dd3cd

23 files changed

+442
-14
lines changed

docs/generated/metrics/metrics.yaml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8953,6 +8953,70 @@ layers:
89538953
unit: COUNT
89548954
aggregation: AVG
89558955
derivative: NON_NEGATIVE_DERIVATIVE
8956+
- name: sql.statements.bytes_read.count
8957+
exported_name: sql_statements_bytes_read_count
8958+
description: Number of bytes read by SQL statements
8959+
y_axis_label: Bytes
8960+
type: COUNTER
8961+
unit: BYTES
8962+
aggregation: AVG
8963+
derivative: NON_NEGATIVE_DERIVATIVE
8964+
- name: sql.statements.bytes_read.count.internal
8965+
exported_name: sql_statements_bytes_read_count_internal
8966+
description: Number of bytes read by SQL statements (internal queries)
8967+
y_axis_label: SQL Internal Statements
8968+
type: COUNTER
8969+
unit: BYTES
8970+
aggregation: AVG
8971+
derivative: NON_NEGATIVE_DERIVATIVE
8972+
- name: sql.statements.index_bytes_written.count
8973+
exported_name: sql_statements_index_bytes_written_count
8974+
description: Number of primary and secondary index bytes modified by SQL statements
8975+
y_axis_label: Bytes
8976+
type: COUNTER
8977+
unit: BYTES
8978+
aggregation: AVG
8979+
derivative: NON_NEGATIVE_DERIVATIVE
8980+
- name: sql.statements.index_bytes_written.count.internal
8981+
exported_name: sql_statements_index_bytes_written_count_internal
8982+
description: Number of primary and secondary index bytes modified by SQL statements (internal queries)
8983+
y_axis_label: SQL Internal Statements
8984+
type: COUNTER
8985+
unit: BYTES
8986+
aggregation: AVG
8987+
derivative: NON_NEGATIVE_DERIVATIVE
8988+
- name: sql.statements.index_rows_written.count
8989+
exported_name: sql_statements_index_rows_written_count
8990+
description: Number of primary and secondary index rows modified by SQL statements
8991+
y_axis_label: Rows
8992+
type: COUNTER
8993+
unit: COUNT
8994+
aggregation: AVG
8995+
derivative: NON_NEGATIVE_DERIVATIVE
8996+
- name: sql.statements.index_rows_written.count.internal
8997+
exported_name: sql_statements_index_rows_written_count_internal
8998+
description: Number of primary and secondary index rows modified by SQL statements (internal queries)
8999+
y_axis_label: SQL Internal Statements
9000+
type: COUNTER
9001+
unit: COUNT
9002+
aggregation: AVG
9003+
derivative: NON_NEGATIVE_DERIVATIVE
9004+
- name: sql.statements.rows_read.count
9005+
exported_name: sql_statements_rows_read_count
9006+
description: Number of rows read by SQL statements
9007+
y_axis_label: Rows
9008+
type: COUNTER
9009+
unit: COUNT
9010+
aggregation: AVG
9011+
derivative: NON_NEGATIVE_DERIVATIVE
9012+
- name: sql.statements.rows_read.count.internal
9013+
exported_name: sql_statements_rows_read_count_internal
9014+
description: Number of rows read by SQL statements (internal queries)
9015+
y_axis_label: SQL Internal Statements
9016+
type: COUNTER
9017+
unit: COUNT
9018+
aggregation: AVG
9019+
derivative: NON_NEGATIVE_DERIVATIVE
89569020
- name: sql.stats.activity.update.latency
89579021
exported_name: sql_stats_activity_update_latency
89589022
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
@@ -1872,6 +1872,14 @@ var cockroachdbMetrics = map[string]string{
18721872
"sql_service_latency_sum": "sql.service.latency.sum",
18731873
"sql_statements_active": "sql.statements.active",
18741874
"sql_statements_active_internal": "sql.statements.active.internal",
1875+
"sql_statements_rows_read_count": "sql.statements.rows_read.count",
1876+
"sql_statements_rows_read_count_internal": "sql.statements.rows_read.count.internal",
1877+
"sql_statements_bytes_read_count": "sql.statements.bytes_read.count",
1878+
"sql_statements_bytes_read_count_internal": "sql.statements.bytes_read.count.internal",
1879+
"sql_statements_index_rows_written_count": "sql.statements.index_rows_written.count",
1880+
"sql_statements_index_rows_written_count_internal": "sql.statements.index_rows_written.count.internal",
1881+
"sql_statements_index_bytes_written_count": "sql.statements.index_bytes_written.count",
1882+
"sql_statements_index_bytes_written_count_internal": "sql.statements.index_bytes_written.count.internal",
18751883
"sql_stats_activity_update_latency": "sql.stats.activity.update.latency",
18761884
"sql_stats_activity_update_latency_bucket": "sql.stats.activity.update.latency.bucket",
18771885
"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
@@ -970,6 +970,7 @@ go_test(
970970
"//pkg/util/log/logconfig",
971971
"//pkg/util/log/logpb",
972972
"//pkg/util/log/logtestutils",
973+
"//pkg/util/metamorphic",
973974
"//pkg/util/metric",
974975
"//pkg/util/mon",
975976
"//pkg/util/protoutil",

pkg/sql/conn_executor.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,10 @@ func makeMetrics(internal bool, sv *settings.Values) Metrics {
625625
FullTableOrIndexScanRejectedCount: metric.NewCounter(getMetricMeta(MetaFullTableOrIndexScanRejected, internal)),
626626
TxnRetryCount: metric.NewCounter(getMetricMeta(MetaTxnRetry, internal)),
627627
StatementRetryCount: metric.NewCounter(getMetricMeta(MetaStatementRetry, internal)),
628+
StatementRowsRead: metric.NewCounter(getMetricMeta(MetaStatementRowsRead, internal)),
629+
StatementBytesRead: metric.NewCounter(getMetricMeta(MetaStatementBytesRead, internal)),
630+
StatementIndexRowsWritten: metric.NewCounter(getMetricMeta(MetaStatementIndexRowsWritten, internal)),
631+
StatementIndexBytesWritten: metric.NewCounter(getMetricMeta(MetaStatementIndexBytesWritten, internal)),
628632
},
629633
StartedStatementCounters: makeStartedStatementCounters(internal),
630634
ExecutedStatementCounters: makeExecutedStatementCounters(internal),

pkg/sql/conn_executor_exec.go

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

32803280
// topLevelQueryStats returns some basic statistics about the run of the query.
32813281
type topLevelQueryStats struct {
3282-
// bytesRead is the number of bytes read from disk.
3283-
bytesRead int64
3284-
// rowsRead is the number of rows read from disk.
3282+
// rowsRead is the number of rows read from primary and secondary indexes.
32853283
rowsRead int64
3286-
// rowsWritten is the number of rows written.
3284+
// bytesRead is the number of bytes read from primary and secondary indexes.
3285+
bytesRead int64
3286+
// rowsWritten is the number of rows written to the primary index. It does not
3287+
// include rows written to secondary indexes.
3288+
// NB: There is an asymmetry between rowsRead and rowsWritten - rowsRead
3289+
// includes rows read from secondary indexes, while rowsWritten does not
3290+
// include rows written to secondary indexes. This matches the behavior of
3291+
// EXPLAIN ANALYZE and SQL "rows affected".
32873292
rowsWritten int64
3293+
// indexRowsWritten is the number of rows written to primary and secondary
3294+
// indexes. It is always >= rowsWritten.
3295+
indexRowsWritten int64
3296+
// indexBytesWritten is the number of bytes written to primary and secondary
3297+
// indexes.
3298+
indexBytesWritten int64
32883299
// networkEgressEstimate is an estimate for the number of bytes sent to the
32893300
// client. It is used for estimating the number of RUs consumed by a query.
32903301
networkEgressEstimate int64
@@ -3300,6 +3311,8 @@ func (s *topLevelQueryStats) add(other *topLevelQueryStats) {
33003311
s.bytesRead += other.bytesRead
33013312
s.rowsRead += other.rowsRead
33023313
s.rowsWritten += other.rowsWritten
3314+
s.indexBytesWritten += other.indexBytesWritten
3315+
s.indexRowsWritten += other.indexRowsWritten
33033316
s.networkEgressEstimate += other.networkEgressEstimate
33043317
s.clientTime += other.clientTime
33053318
}

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
@@ -83,6 +83,17 @@ func (d *deleteRangeNode) rowsWritten() int64 {
8383
return int64(d.rowCount)
8484
}
8585

86+
func (d *deleteRangeNode) indexRowsWritten() int64 {
87+
// Same as rowsWritten, because deleteRangeNode only applies to primary index
88+
// rows (it is not used if there's a secondary index on the table).
89+
return int64(d.rowCount)
90+
}
91+
92+
func (d *deleteRangeNode) indexBytesWritten() int64 {
93+
// No bytes counted as written for a deletion.
94+
return 0
95+
}
96+
8697
// startExec implements the planNode interface.
8798
func (d *deleteRangeNode) startExec(params runParams) error {
8899
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
@@ -1671,6 +1671,8 @@ func (r *DistSQLReceiver) pushMeta(meta *execinfrapb.ProducerMetadata) execinfra
16711671
r.stats.bytesRead += meta.Metrics.BytesRead
16721672
r.stats.rowsRead += meta.Metrics.RowsRead
16731673
r.stats.rowsWritten += meta.Metrics.RowsWritten
1674+
r.stats.indexRowsWritten += meta.Metrics.IndexRowsWritten
1675+
r.stats.indexBytesWritten += meta.Metrics.IndexBytesWritten
16741676

16751677
if sm, ok := r.scanStageEstimateMap[meta.Metrics.StageID]; ok {
16761678
sm.rowsRead += uint64(meta.Metrics.RowsRead)

pkg/sql/exec_util.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,30 @@ var (
15141514
Measurement: "SQL Statements",
15151515
Unit: metric.Unit_COUNT,
15161516
}
1517+
MetaStatementRowsRead = metric.Metadata{
1518+
Name: "sql.statements.rows_read.count",
1519+
Help: "Number of rows read by SQL statements",
1520+
Measurement: "Rows",
1521+
Unit: metric.Unit_COUNT,
1522+
}
1523+
MetaStatementBytesRead = metric.Metadata{
1524+
Name: "sql.statements.bytes_read.count",
1525+
Help: "Number of bytes read by SQL statements",
1526+
Measurement: "Bytes",
1527+
Unit: metric.Unit_BYTES,
1528+
}
1529+
MetaStatementIndexRowsWritten = metric.Metadata{
1530+
Name: "sql.statements.index_rows_written.count",
1531+
Help: "Number of primary and secondary index rows modified by SQL statements",
1532+
Measurement: "Rows",
1533+
Unit: metric.Unit_COUNT,
1534+
}
1535+
MetaStatementIndexBytesWritten = metric.Metadata{
1536+
Name: "sql.statements.index_bytes_written.count",
1537+
Help: "Number of primary and secondary index bytes modified by SQL statements",
1538+
Measurement: "Bytes",
1539+
Unit: metric.Unit_BYTES,
1540+
}
15171541
)
15181542

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

0 commit comments

Comments
 (0)