Skip to content

Conversation

@andy-kimball
Copy link
Contributor

sql: add index_bytes_written metric

Add new sql.statements.index_bytes_written.count metric that counts the number
of primary and secondary index bytes modified by SQL statements.

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.

sql: add bytes_read metric

Add new sql.statements.bytes_read.count metric that counts the number of
bytes scanned by SQL statements. This is the same value that's collected by
SQL stats for each statement and transaction, except in aggregated metric
form.

@andy-kimball andy-kimball requested review from a team as code owners October 28, 2025 00:23
@andy-kimball andy-kimball requested review from dhartunian and removed request for a team October 28, 2025 00:23
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I only have some nits.

Thanks for cleaning up the stale comments! Let's also adjust the stale comment in ValuesLegalForInsertFastPath.

Currently there are some failures about generating cockroachdb_metrics.go - I hope we can avoid that via #156335.

@yuzefovich reviewed 5 of 5 files at r1, 19 of 19 files at r2, 19 of 19 files at r3, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @dhartunian)


pkg/sql/tablewriter.go line 149 at r2 (raw file):

	// The mutation operators add one request to the KV batch for each index
	// entry that's written.
	tb.indexRowsWritten += int64(len(tb.b.Requests()))

nit: for symmetry with other spots let's modify rowsWritten before indexRowsWritten here and below.


pkg/sql/metric_test.go line 313 at r2 (raw file):

						s, sql.MetaStatementIndexRowsWritten, lastRowsWritten, tc.indexRowsWritten)
					require.NoError(t, err)
					lastRowsWritten = actual

Shouldn't we compare lastRowsWritten to actual here?


pkg/sql/execinfrapb/data.proto line 347 at r2 (raw file):

    // IndexRowsWritten is the number of primary and secondary index rows
    // modified while executing a statement. It is always >= RowsWritten.
    optional int64 index_rows_written = 13 [(gogoproto.nullable) = false];

nit: this can use ID 5 since it's internal to Metrics protobuf - "outer" IDs are only applicable to oneof value part. Ditto for index_bytes_written.


pkg/sql/exec_util.go line 1539 at r3 (raw file):

		Help:        "Number of primary and secondary index bytes modified by SQL statements",
		Measurement: "SQL Statements",
		Unit:        metric.Unit_COUNT,

nit: we usually use metric.Unit_BYTES for metrics that are in bytes.


pkg/sql/conn_executor_exec.go line 3291 at r2 (raw file):

	// indexRowsWritten is the number of rows written to primary and secondary
	// indexes. It is always >= rowsWritten.
	indexRowsWritten int64

nit: consider extending recently added TestTopLevelQueryStats a bit to cover indexRowsWritten as well.


pkg/sql/conn_executor_exec.go line 3291 at r3 (raw file):

	// indexBytesWritten is the number of bytes written to primary and secondary
	// indexes.
	indexBytesWritten int64

nit: maybe place indexBytesWritten after indexRowsWritten for consistency.

@andy-kimball
Copy link
Contributor Author

I was going to update the ValuesLegalForInsertFastPath comment in a different PR, since it's actually an orthogonal issue that I just happened to discover while working on this PR.

@andy-kimball andy-kimball requested a review from a team as a code owner October 28, 2025 17:31
@andy-kimball andy-kimball requested review from nameisbhaskar and williamchoe3 and removed request for a team October 28, 2025 17:31
Copy link
Contributor Author

@andy-kimball andy-kimball left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @dhartunian, @nameisbhaskar, @williamchoe3, and @yuzefovich)


pkg/sql/conn_executor_exec.go line 3291 at r2 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: consider extending recently added TestTopLevelQueryStats a bit to cover indexRowsWritten as well.

Done


pkg/sql/conn_executor_exec.go line 3291 at r3 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: maybe place indexBytesWritten after indexRowsWritten for consistency.

heh, I placed it before to be consistent with how bytesRead and rowsRead are ordered. I can change those as well, though.


pkg/sql/exec_util.go line 1539 at r3 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: we usually use metric.Unit_BYTES for metrics that are in bytes.

Hm, I'd fixed that, but must have neglected to push. Fixed now.


pkg/sql/tablewriter.go line 149 at r2 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: for symmetry with other spots let's modify rowsWritten before indexRowsWritten here and below.

Done


pkg/sql/execinfrapb/data.proto line 347 at r2 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: this can use ID 5 since it's internal to Metrics protobuf - "outer" IDs are only applicable to oneof value part. Ditto for index_bytes_written.

true, fixed


pkg/sql/metric_test.go line 313 at r2 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

Shouldn't we compare lastRowsWritten to actual here?

The comparisons are actually done inside checkCounterDelta. actual is actually no longer needed, so I removed.

Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:, just one comment on the test adjustment.

Updating the ValuesLegalForInsertFastPath comment separately SGTM.

@yuzefovich reviewed 19 of 19 files at r4, 18 of 19 files at r5, 21 of 21 files at r6, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @dhartunian, @nameisbhaskar, and @williamchoe3)


pkg/sql/distsql_running_test.go line 1229 at r6 (raw file):

	// The callback will send number of rows read and rows written (for each
	// ProducerMetadata.Metrics object) on these channels, respectively.
	rowsReadCh, rowsWrittenCh := make(chan int64), make(chan int64)

We'll need a bit more adjustments here for indexRowsWritten - creating a channel, send'ing and recv'ing from the channel, and then asserting the sum equals the expected count.

Add new `sql.statements.bytes_read.count` metric that counts the number of
bytes scanned by SQL statements. This is the same value that's collected by
SQL stats for each statement and transaction, except in aggregated metric
form.

Epic: AOP-30

Release note (sql change): Added sql.statements.bytes_read.count metric that
counts the number of bytes scanned by SQL statements.
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.
Add new `sql.statements.index_bytes_written.count` metric that counts the number
of primary and secondary index bytes modified by SQL statements.

Epic: AOP-30

Release note (sql change): Added sql.statements.index_bytes_written.count metric
that counts the number of primary and secondary index bytes modified by SQL
statements.
Copy link
Contributor Author

@andy-kimball andy-kimball left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @dhartunian, @nameisbhaskar, @williamchoe3, and @yuzefovich)


pkg/sql/distsql_running_test.go line 1229 at r6 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

We'll need a bit more adjustments here for indexRowsWritten - creating a channel, send'ing and recv'ing from the channel, and then asserting the sum equals the expected count.

whoops, i got distracted and didn't finish that, fixed now

@andy-kimball
Copy link
Contributor Author

bors r=yuzefovich

@craig
Copy link
Contributor

craig bot commented Oct 29, 2025

@craig craig bot merged commit 792292c into cockroachdb:master Oct 29, 2025
24 checks passed
@andy-kimball andy-kimball deleted the metrics branch October 29, 2025 03:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants