Skip to content

Commit 30e5c2a

Browse files
committed
sql: add index_rows_written metric
Add new `sql.statements.index_rows_written.count` metric that counts the number of primary and secondary index rows modified by SQL statements. Epic: AOP-30 Release note (sql change): Added sql.statements.index_rows_written.count metric that counts the number of primary and secondary index rows modified by SQL statements.
1 parent b76fd94 commit 30e5c2a

22 files changed

+193
-33
lines changed

docs/generated/metrics/metrics.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8969,6 +8969,22 @@ layers:
89698969
unit: BYTES
89708970
aggregation: AVG
89718971
derivative: NON_NEGATIVE_DERIVATIVE
8972+
- name: sql.statements.index_rows_written.count
8973+
exported_name: sql_statements_index_rows_written_count
8974+
description: Number of primary and secondary index rows modified by SQL statements
8975+
y_axis_label: SQL Statements
8976+
type: COUNTER
8977+
unit: COUNT
8978+
aggregation: AVG
8979+
derivative: NON_NEGATIVE_DERIVATIVE
8980+
- name: sql.statements.index_rows_written.count.internal
8981+
exported_name: sql_statements_index_rows_written_count_internal
8982+
description: Number of primary and secondary index rows modified by SQL statements (internal queries)
8983+
y_axis_label: SQL Internal Statements
8984+
type: COUNTER
8985+
unit: COUNT
8986+
aggregation: AVG
8987+
derivative: NON_NEGATIVE_DERIVATIVE
89728988
- name: sql.statements.rows_read.count
89738989
exported_name: sql_statements_rows_read_count
89748990
description: Number of rows read by SQL statements

pkg/roachprod/opentelemetry/cockroachdb_metrics.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,8 @@ var cockroachdbMetrics = map[string]string{
18761876
"sql_statements_rows_read_count_internal": "sql.statements.rows_read.count.internal",
18771877
"sql_statements_bytes_read_count": "sql.statements.bytes_read.count",
18781878
"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",
18791881
"sql_stats_activity_update_latency": "sql.stats.activity.update.latency",
18801882
"sql_stats_activity_update_latency_bucket": "sql.stats.activity.update.latency.bucket",
18811883
"sql_stats_activity_update_latency_count": "sql.stats.activity.update.latency.count",

pkg/sql/conn_executor.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ func makeMetrics(internal bool, sv *settings.Values) Metrics {
627627
StatementRetryCount: metric.NewCounter(getMetricMeta(MetaStatementRetry, internal)),
628628
StatementRowsRead: metric.NewCounter(getMetricMeta(MetaStatementRowsRead, internal)),
629629
StatementBytesRead: metric.NewCounter(getMetricMeta(MetaStatementBytesRead, internal)),
630+
StatementIndexRowsWritten: metric.NewCounter(getMetricMeta(MetaStatementIndexRowsWritten, internal)),
630631
},
631632
StartedStatementCounters: makeStartedStatementCounters(internal),
632633
ExecutedStatementCounters: makeExecutedStatementCounters(internal),

pkg/sql/conn_executor_exec.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3279,12 +3279,20 @@ 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.
3282+
// bytesRead is the number of bytes read from primary and secondary indexes.
32833283
bytesRead int64
3284-
// rowsRead is the number of rows read from disk.
3284+
// rowsRead is the number of rows read from primary and secondary indexes.
32853285
rowsRead int64
3286-
// rowsWritten is the number of rows written.
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
32883296
// networkEgressEstimate is an estimate for the number of bytes sent to the
32893297
// client. It is used for estimating the number of RUs consumed by a query.
32903298
networkEgressEstimate int64
@@ -3298,6 +3306,7 @@ func (s *topLevelQueryStats) add(other *topLevelQueryStats) {
32983306
s.bytesRead += other.bytesRead
32993307
s.rowsRead += other.rowsRead
33003308
s.rowsWritten += other.rowsWritten
3309+
s.indexRowsWritten += other.indexRowsWritten
33013310
s.networkEgressEstimate += other.networkEgressEstimate
33023311
s.clientTime += other.clientTime
33033312
}

pkg/sql/delete.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ 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+
262266
func (d *deleteNode) enableAutoCommit() {
263267
d.run.td.enableAutoCommit()
264268
}

pkg/sql/delete_range.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ 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+
8692
// startExec implements the planNode interface.
8793
func (d *deleteRangeNode) startExec(params runParams) error {
8894
if err := params.p.cancelChecker.Check(); err != nil {

pkg/sql/delete_swap.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ 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+
146150
func (d *deleteSwapNode) enableAutoCommit() {
147151
d.run.td.enableAutoCommit()
148152
}

pkg/sql/distsql_running.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,7 @@ func (r *DistSQLReceiver) pushMeta(meta *execinfrapb.ProducerMetadata) execinfra
16061606
r.stats.bytesRead += meta.Metrics.BytesRead
16071607
r.stats.rowsRead += meta.Metrics.RowsRead
16081608
r.stats.rowsWritten += meta.Metrics.RowsWritten
1609+
r.stats.indexRowsWritten += meta.Metrics.IndexRowsWritten
16091610

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

pkg/sql/exec_util.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,12 @@ var (
15261526
Measurement: "SQL Statements",
15271527
Unit: metric.Unit_BYTES,
15281528
}
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: "SQL Statements",
1533+
Unit: metric.Unit_COUNT,
1534+
}
15291535
)
15301536

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

pkg/sql/execinfrapb/data.proto

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,19 +333,26 @@ message RemoteProducerMetadata {
333333
}
334334
// Metrics are unconditionally emitted by table readers.
335335
message Metrics {
336-
// Total number of bytes read while executing a statement.
336+
// BytesRead is the number of primary and secondary index bytes read while
337+
// executing a statement.
337338
optional int64 bytes_read = 1 [(gogoproto.nullable) = false];
338-
// Total number of rows read while executing a statement.
339+
// RowsRead is the number of primary and secondary index rows read while
340+
// executing a statement.
339341
optional int64 rows_read = 2 [(gogoproto.nullable) = false];
340-
// Total number of rows modified while executing a statement.
342+
// RowsWritten is the number of primary index rows modified while executing
343+
// a statement. It does not include secondary index rows.
341344
optional int64 rows_written = 3 [(gogoproto.nullable) = false];
345+
// IndexRowsWritten is the number of primary and secondary index rows
346+
// modified while executing a statement. It is always >= RowsWritten.
347+
optional int64 index_rows_written = 5 [(gogoproto.nullable) = false];
342348
// Stage identifier that produced these metrics. This is used to aggregate
343349
// row counts across distributed table reader processors for misestimate
344350
// logging. Note that other disk-reading processors (index and lookup joins,
345351
// inverted joins, zigzag joins) do not set this field. This is safe because
346352
// we enumerate physical plan stages starting at 1, so the default value of
347353
// 0 isn't conflated with any actual plan stage.
348354
optional int32 stage_id = 4 [(gogoproto.nullable) = false, (gogoproto.customname) = "StageID"];
355+
// NEXT ID: 6
349356
}
350357
oneof value {
351358
RangeInfos range_info = 1;
@@ -360,7 +367,7 @@ message RemoteProducerMetadata {
360367
TracingAggregatorEvents tracing_aggregator_events= 12;
361368
}
362369
reserved 6, 10;
363-
// NEXT ID: 13
370+
// NEXT ID: 14
364371
}
365372

366373
// DistSQLDrainingInfo represents the DistSQL draining state that gets gossiped

0 commit comments

Comments
 (0)