diff --git a/docs/generated/http/full.md b/docs/generated/http/full.md index 95c2a0076530..d895dbde47a5 100644 --- a/docs/generated/http/full.md +++ b/docs/generated/http/full.md @@ -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) | diff --git a/pkg/server/admin.go b/pkg/server/admin.go index 5af4dfe14cb3..d2f0763bfb33 100644 --- a/pkg/server/admin.go +++ b/pkg/server/admin.go @@ -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 */ diff --git a/pkg/server/serverpb/admin.proto b/pkg/server/serverpb/admin.proto index f3eda8801cfd..e9bfc24b081d 100644 --- a/pkg/server/serverpb/admin.proto +++ b/pkg/server/serverpb/admin.proto @@ -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