Skip to content

Commit

Permalink
server: return garbage percentage per table
Browse files Browse the repository at this point in the history
This commit adds a new parameter of garbage percentage
per table to the table details endpoint.

Partially addresses #82617

Release note (api change): Add garbage percentage info to Table
Details endpoint.
  • Loading branch information
maryliag committed Jun 30, 2022
1 parent 0917fdc commit 1f66aa0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/generated/http/full.md
Original file line number Diff line number Diff line change
Expand Up @@ -5206,6 +5206,7 @@ a table.
| configure_zone_statement | [string](#cockroach.server.serverpb.TableDetailsResponse-string) | | configure_zone_statement is the output of "SHOW ZONE CONFIGURATION FOR TABLE" for this table. It is a SQL statement that would re-configure the table's current zone if executed. | [reserved](#support-status) |
| stats_last_created_at | [google.protobuf.Timestamp](#cockroach.server.serverpb.TableDetailsResponse-google.protobuf.Timestamp) | | stats_last_created_at is the time at which statistics were last created. | [reserved](#support-status) |
| has_index_recommendations | [bool](#cockroach.server.serverpb.TableDetailsResponse-bool) | | has_index_recommendations notifies if the there are index recommendations on this table. | [reserved](#support-status) |
| garbage_percentage | [float](#cockroach.server.serverpb.TableDetailsResponse-float) | | garbage_percentage is the percentage of existing MVCC garbage on the table. | [reserved](#support-status) |



Expand Down
38 changes: 38 additions & 0 deletions pkg/server/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,44 @@ func (s *adminServer) tableDetailsHelper(
resp.CreateTableStatement = createStmt
}

// MVCC Garbage result.
row, cols, err = s.server.sqlServer.internalExecutor.QueryRowExWithCols(
ctx, "admin-show-mvcc-garbage-pct", nil,
sessiondata.InternalExecutorOverride{User: userName},
`WITH
range_stats AS (
SELECT
crdb_internal.range_stats(start_key) AS d
FROM
crdb_internal.ranges_no_leases
WHERE
table_id = $1::REGCLASS
),
aggregated AS (
SELECT
sum((d->>'live_bytes')::INT8) AS live,
sum((d->>'key_bytes')::INT8 + (d->>'val_bytes')::INT8) AS total
FROM
range_stats
)
SELECT
COALESCE((total - live) / NULLIF(total,0), 0)::FLOAT as garbage_percentage
FROM aggregated`,
escQualTable,
)
if err != nil {
return nil, err
}
if row != nil {
scanner := makeResultScanner(cols)
const garbagePctCol = "garbage_percentage"
var garbagePct float32
if err := scanner.Scan(row, garbagePctCol, &garbagePct); err != nil {
return nil, err
}
resp.GarbagePercentage = garbagePct
}

// Marshal SHOW STATISTICS result.
row, cols, err = s.server.sqlServer.internalExecutor.QueryRowExWithCols(
ctx, "admin-show-statistics", nil, /* txn */
Expand Down
2 changes: 2 additions & 0 deletions pkg/server/serverpb/admin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ message TableDetailsResponse {
// has_index_recommendations notifies if the there are index recommendations
// on this table.
bool has_index_recommendations = 11;
// garbage_percentage is the percentage of existing MVCC garbage on the table.
float garbage_percentage = 12;
}

// TableStatsRequest is a request for detailed, computationally expensive
Expand Down

0 comments on commit 1f66aa0

Please sign in to comment.