Skip to content

Commit cb78919

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 55247e7 commit cb78919

19 files changed

+168
-34
lines changed

docs/generated/metrics/metrics.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8763,6 +8763,22 @@ layers:
87638763
unit: BYTES
87648764
aggregation: AVG
87658765
derivative: NON_NEGATIVE_DERIVATIVE
8766+
- name: sql.statements.index_rows_written.count
8767+
exported_name: sql_statements_index_rows_written_count
8768+
description: Number of primary and secondary index rows modified by SQL statements
8769+
y_axis_label: SQL Statements
8770+
type: COUNTER
8771+
unit: COUNT
8772+
aggregation: AVG
8773+
derivative: NON_NEGATIVE_DERIVATIVE
8774+
- name: sql.statements.index_rows_written.count.internal
8775+
exported_name: sql_statements_index_rows_written_count_internal
8776+
description: Number of primary and secondary index rows modified by SQL statements (internal queries)
8777+
y_axis_label: SQL Internal Statements
8778+
type: COUNTER
8779+
unit: COUNT
8780+
aggregation: AVG
8781+
derivative: NON_NEGATIVE_DERIVATIVE
87668782
- name: sql.statements.rows_read.count
87678783
exported_name: sql_statements_rows_read_count
87688784
description: Number of rows read by SQL statements

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
@@ -3275,12 +3275,20 @@ func (ex *connExecutor) makeExecPlan(
32753275

32763276
// topLevelQueryStats returns some basic statistics about the run of the query.
32773277
type topLevelQueryStats struct {
3278-
// bytesRead is the number of bytes read from disk.
3278+
// bytesRead is the number of bytes read from primary and secondary indexes.
32793279
bytesRead int64
3280-
// rowsRead is the number of rows read from disk.
3280+
// rowsRead is the number of rows read from primary and secondary indexes.
32813281
rowsRead int64
3282-
// rowsWritten is the number of rows written.
3282+
// rowsWritten is the number of rows written to the primary index. It does not
3283+
// include rows written to secondary indexes.
3284+
// NB: There is an asymmetry between rowsRead and rowsWritten - rowsRead
3285+
// includes rows read from secondary indexes, while rowsWritten does not
3286+
// include rows written to secondary indexes. This matches the behavior of
3287+
// EXPLAIN ANALYZE and SQL "rows affected".
32833288
rowsWritten int64
3289+
// indexRowsWritten is the number of rows written to primary and secondary
3290+
// indexes. It is always >= rowsWritten.
3291+
indexRowsWritten int64
32843292
// networkEgressEstimate is an estimate for the number of bytes sent to the
32853293
// client. It is used for estimating the number of RUs consumed by a query.
32863294
networkEgressEstimate int64
@@ -3296,6 +3304,7 @@ func (s *topLevelQueryStats) add(other *topLevelQueryStats) {
32963304
s.bytesRead += other.bytesRead
32973305
s.rowsRead += other.rowsRead
32983306
s.rowsWritten += other.rowsWritten
3307+
s.indexRowsWritten += other.indexRowsWritten
32993308
s.networkEgressEstimate += other.networkEgressEstimate
33003309
s.clientTime += other.clientTime
33013310
}

pkg/sql/delete.go

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

254+
func (d *deleteNode) indexRowsWritten() int64 {
255+
return d.run.td.indexRowsWritten
256+
}
257+
254258
func (d *deleteNode) returnsRowsAffected() bool {
255259
return !d.run.rowsNeeded
256260
}

pkg/sql/delete_range.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ func (d *deleteRangeNode) rowsWritten() int64 {
5959
return d.rowsAffected()
6060
}
6161

62+
func (d *deleteRangeNode) indexRowsWritten() int64 {
63+
// Same as rowsWritten, because deleteRangeNode only applies to primary index
64+
// rows (it is not used if there's a secondary index on the table).
65+
return d.rowsAffected()
66+
}
67+
6268
func (d *deleteRangeNode) returnsRowsAffected() bool {
6369
// DeleteRange always returns the number of rows deleted.
6470
return true

pkg/sql/delete_swap.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ func (d *deleteSwapNode) rowsWritten() int64 {
130130
return d.run.rowsAffected()
131131
}
132132

133+
func (d *deleteSwapNode) indexRowsWritten() int64 {
134+
return d.run.td.indexRowsWritten
135+
}
136+
133137
func (d *deleteSwapNode) returnsRowsAffected() bool {
134138
return !d.run.rowsNeeded
135139
}

pkg/sql/distsql_running.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,7 @@ func forwardInnerQueryStats(f metadataForwarder, stats topLevelQueryStats) {
16231623
meta.Metrics.BytesRead = stats.bytesRead
16241624
meta.Metrics.RowsRead = stats.rowsRead
16251625
meta.Metrics.RowsWritten = stats.rowsWritten
1626+
meta.Metrics.IndexRowsWritten = stats.indexRowsWritten
16261627
// stats.networkEgressEstimate and stats.clientTime are ignored since they
16271628
// only matter at the "true" top-level query (and actually should be zero
16281629
// here anyway).
@@ -1671,6 +1672,7 @@ func (r *DistSQLReceiver) pushMeta(meta *execinfrapb.ProducerMetadata) execinfra
16711672
r.stats.bytesRead += meta.Metrics.BytesRead
16721673
r.stats.rowsRead += meta.Metrics.RowsRead
16731674
r.stats.rowsWritten += meta.Metrics.RowsWritten
1675+
r.stats.indexRowsWritten += meta.Metrics.IndexRowsWritten
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: 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

pkg/sql/executor_statement_metrics.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ type EngineMetrics struct {
9292
// StatementBytesRead counts the number of bytes scanned by SQL statements
9393
// from primary and secondary indexes.
9494
StatementBytesRead *metric.Counter
95+
96+
// StatementIndexRowsWritten counts the number of primary and secondary index
97+
// rows modified by SQL statements.
98+
StatementIndexRowsWritten *metric.Counter
9599
}
96100

97101
// EngineMetrics implements the metric.Struct interface.
@@ -209,6 +213,7 @@ func (ex *connExecutor) recordStatementSummary(
209213
// Update SQL statement metrics.
210214
ex.metrics.EngineMetrics.StatementRowsRead.Inc(stats.rowsRead)
211215
ex.metrics.EngineMetrics.StatementBytesRead.Inc(stats.bytesRead)
216+
ex.metrics.EngineMetrics.StatementIndexRowsWritten.Inc(stats.indexRowsWritten)
212217

213218
if ex.statsCollector.EnabledForTransaction() {
214219
recordedStmtStats := &sqlstats.RecordedStmtStats{

0 commit comments

Comments
 (0)