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 3 new parameter to the table
details endpoint:
- dataTotalBytes
- dataNonLiveBytes
- dataNonLivePercentage

Partially addresses #82617

Release note (api change): Add information about total bytes,
non live (MVCC) bytes and non live (MVCC) percentage to
Table Details endpoint.
  • Loading branch information
maryliag committed Jul 7, 2022
1 parent 0848d90 commit 8b83a21
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/generated/http/full.md
Original file line number Diff line number Diff line change
Expand Up @@ -5206,6 +5206,9 @@ 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) |
| data_total_bytes | [int64](#cockroach.server.serverpb.TableDetailsResponse-int64) | | data_total_bytes is the size in bytes of live and non-live data on the table. | [reserved](#support-status) |
| data_non_live_bytes | [int64](#cockroach.server.serverpb.TableDetailsResponse-int64) | | data_non_live_bytes is the size in bytes of non-live (MVCC) data on the table. | [reserved](#support-status) |
| data_non_live_percentage | [float](#cockroach.server.serverpb.TableDetailsResponse-float) | | garbage_percentage is the percentage of non-live (MVCC) data on the table. | [reserved](#support-status) |



Expand Down
18 changes: 18 additions & 0 deletions docs/generated/swagger/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,24 @@
"type": "string",
"x-go-name": "CreateTableStatement"
},
"data_non_live_bytes": {
"description": "data_non_live_bytes is the size in bytes of non-live (MVCC) data on the table.",
"type": "integer",
"format": "int64",
"x-go-name": "DataNonLiveBytes"
},
"data_non_live_percentage": {
"description": "garbage_percentage is the percentage of non-live (MVCC) data on the table.",
"type": "number",
"format": "float",
"x-go-name": "DataNonLivePercentage"
},
"data_total_bytes": {
"description": "data_total_bytes is the size in bytes of live and non-live data on the table.",
"type": "integer",
"format": "int64",
"x-go-name": "DataTotalBytes"
},
"descriptor_id": {
"description": "descriptor_id is an identifier used to uniquely identify this table.\nIt can be used to find events pertaining to this table by filtering on\nthe 'target_id' field of events.",
"type": "integer",
Expand Down
51 changes: 51 additions & 0 deletions pkg/server/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,57 @@ 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
total::INT8,
(total - live)::INT8 as garbage_bytes,
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)
var totalBytes int64
if err := scanner.Scan(row, "total", &totalBytes); err != nil {
return nil, err
}
resp.DataTotalBytes = totalBytes

var garbageBytes int64
if err := scanner.Scan(row, "garbage_bytes", &garbageBytes); err != nil {
return nil, err
}
resp.DataNonLiveBytes = garbageBytes

var garbagePct float32
if err := scanner.Scan(row, "garbage_percentage", &garbagePct); err != nil {
return nil, err
}
resp.DataNonLivePercentage = garbagePct
}

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

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

0 comments on commit 8b83a21

Please sign in to comment.