Skip to content

Commit

Permalink
sql: add schema_name to index usage stat telemetry
Browse files Browse the repository at this point in the history
This commit adds the schema name to the index usage statistics
telemetry.

Fixes cockroachdb#85932.

Release justification: low risk, high benefit changes to existing functionality
Release note (sql change): Added the schema name to index usage statistics
telemetry.
  • Loading branch information
ericharmeling committed Sep 8, 2022
1 parent bd4baff commit 247fa3e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/generated/eventlog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2380,7 +2380,11 @@ An event of type `captured_index_usage_stats`
| `IsUnique` | IsUnique indicates if the index has a UNIQUE constraint. | no |
| `IsInverted` | IsInverted indicates if the index is an inverted index. | no |
| `CreatedAt` | CreatedAt is the timestamp at which the index was created. | no |
<<<<<<< HEAD
>>>>>>> b224354ea1 (sql: add descriptions to idx usage telemetry fields)
=======
| `SchemaName` | SchemaName is the name of the schema in which the index was created. | no |
>>>>>>> d6dfdede40 (sql: add schema_name to index usage stat telemetry)

#### Common fields
Expand Down
17 changes: 14 additions & 3 deletions pkg/sql/scheduledlogging/captured_index_usage_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func captureIndexUsageStats(

// Capture index usage statistics for each database.
var ok bool
expectedNumDatums := 9
expectedNumDatums := 11
var allCapturedIndexUsageStats []eventpb.EventPayload
for _, databaseName := range allDatabaseNames {
// Omit index usage statistics on the default databases 'system',
Expand All @@ -188,13 +188,17 @@ func captureIndexUsageStats(
ti.is_unique,
ti.is_inverted,
total_reads,
last_read
last_read,
ti.created_at,
t.schema_name
FROM %s.crdb_internal.index_usage_statistics AS us
JOIN %s.crdb_internal.table_indexes ti
ON us.index_id = ti.index_id
AND us.table_id = ti.descriptor_id
JOIN %s.crdb_internal.tables t
ON ti.descriptor_id = t.table_id
ORDER BY total_reads ASC;
`, databaseName, databaseName)
`, databaseName, databaseName, databaseName)

it, err := ie.QueryIteratorEx(
ctx,
Expand Down Expand Up @@ -232,6 +236,11 @@ func captureIndexUsageStats(
if row[8] != tree.DNull {
lastRead = tree.MustBeDTimestampTZ(row[8]).Time
}
createdAt := time.Time{}
if row[9] != tree.DNull {
createdAt = tree.MustBeDTimestamp(row[9]).Time
}
schemaName := tree.MustBeDString(row[10])

capturedIndexStats := &eventpb.CapturedIndexUsageStats{
TableID: uint32(roachpb.TableID(tableID)),
Expand All @@ -244,6 +253,8 @@ func captureIndexUsageStats(
IndexType: string(indexType),
IsUnique: bool(isUnique),
IsInverted: bool(isInverted),
CreatedAt: createdAt.String(),
SchemaName: string(schemaName),
}

allCapturedIndexUsageStats = append(allCapturedIndexUsageStats, capturedIndexStats)
Expand Down
20 changes: 20 additions & 0 deletions pkg/util/log/eventpb/json_encode_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/util/log/eventpb/telemetry.proto
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ message CapturedIndexUsageStats {

// CreatedAt is the timestamp at which the index was created.
string created_at = 12 [(gogoproto.jsontag) = ",omitempty", (gogoproto.moretags) = "redact:\"nonsensitive\""];

// SchemaName is the name of the schema in which the index was created.
string schema_name = 13 [(gogoproto.jsontag) = ",omitempty", (gogoproto.moretags) = "redact:\"nonsensitive\""];
}

// CreateChangefeed is an event for any CREATE CHANGEFEED query that
Expand Down

0 comments on commit 247fa3e

Please sign in to comment.