-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
97534: builtins: add `tenant_span_stats` generator function r=THardy98 a=THardy98 Part of: #94332 Part of: #94330 Added new builtin function `tenant_span_stats` that retrieves span statistics for the current tenant. `tenant_span_stats` can be called as: - `crdb_internal.tenant_span_stats()`: returns table span statistics for all of the tenants tables - `crdb_internal.tenant_span_stats(database_id)`: returns table span statistics for the tenant's tables belonging to the specified database id - `crdb_internal.tenant_span_stats(database_id, table_id)`: returns the tenant's table span statistics for the provided table id Returned rows take the format: ``` [email protected]:26257/movr> select * from crdb_internal.tenant_span_stats(104); database_id | table_id | range_count | approximate_disk_bytes | live_bytes | total_bytes | live_percentage --------------+----------+-------------+------------------------+------------+-------------+------------------ 104 | 106 | 27 | 53866 | 16821 | 16821 | 1 104 | 107 | 27 | 45285 | 10932 | 10932 | 1 104 | 108 | 27 | 225645 | 528219 | 528219 | 1 104 | 109 | 3 | 130978 | 260826 | 260826 | 1 104 | 110 | 3 | 393815 | 708387 | 708387 | 1 104 | 111 | 3 | 30021 | 1413 | 1413 | 1 ``` **Note**: This implementation is naive - it calls `SpanStats` for each table span. Future work will entail expanding the endpoint to be able to fetch span statistics for multiple spans at once. Release note (sql change): new builtin function `tenants_span_stats`, retrieves the span statistics for the current tenant. Co-authored-by: Thomas Hardy <[email protected]>
- Loading branch information
Showing
9 changed files
with
359 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
pkg/sql/logictest/testdata/logic_test/tenant_span_stats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# LogicTest: 3node-tenant | ||
|
||
# Create a second database. | ||
statement ok | ||
CREATE DATABASE a | ||
|
||
# Create a table for database. | ||
statement ok | ||
CREATE TABLE a.b (id INT PRIMARY KEY) | ||
|
||
# Insert data into the table | ||
statement ok | ||
INSERT INTO a.b SELECT generate_series(1, 10) | ||
|
||
# Create a second table for database. | ||
statement ok | ||
CREATE TABLE a.c (id INT PRIMARY KEY) | ||
|
||
# SELECT * FROM crdb_internal.tenant_span_stats: span stats for all of the tenant's tables. | ||
# Assert the schema. | ||
query IIIIIIR colnames | ||
SELECT * FROM crdb_internal.tenant_span_stats() LIMIT 0 | ||
---- | ||
database_id table_id range_count approximate_disk_bytes live_bytes total_bytes live_percentage | ||
|
||
# SELECT DISTINCT(database_id) FROM crdb_internal.tenant_span_stats: | ||
# Assert that we are collecting span stats for tables across multiple databases. | ||
query I colnames | ||
SELECT DISTINCT(database_id) FROM crdb_internal.tenant_span_stats() | ||
---- | ||
database_id | ||
1 | ||
106 | ||
|
||
# SELECT database_id, table_id FROM crdb_internal.tenant_span_stats(106): | ||
# Assert that we are collecting span stats scoped to the provided database id. | ||
query II colnames | ||
SELECT database_id, table_id FROM crdb_internal.tenant_span_stats(106) | ||
---- | ||
database_id table_id | ||
106 108 | ||
106 109 | ||
|
||
# SELECT database_id, table_id FROM crdb_internal.tenant_span_stats(106, 108): | ||
# Assert that we are collecting span stats scoped to the provided database/table id combo. | ||
query II colnames | ||
SELECT database_id, table_id FROM crdb_internal.tenant_span_stats(106, 108) | ||
---- | ||
database_id table_id | ||
106 108 | ||
|
||
# Assert that we cannot provide an invalid database id. | ||
query error pq: provided database id must be greater than or equal to 1 | ||
SELECT database_id, table_id FROM crdb_internal.tenant_span_stats(0) | ||
|
||
# Assert that we cannot provide an invalid table id. | ||
query error pq: provided table id must be greater than or equal to 1 | ||
SELECT database_id, table_id FROM crdb_internal.tenant_span_stats(1, -1) | ||
|
||
# Assert that we cannot provide an invalid database id with a valid table id. | ||
query error pq: provided database id must be greater than or equal to 1 | ||
SELECT database_id, table_id FROM crdb_internal.tenant_span_stats(-1, 1) | ||
|
||
# SELECT * FROM crdb_internal.tenant_span_stats(1000): | ||
# Assert that we get empty rows for a database id that does not correspond to a database. | ||
query IIIIIIR colnames | ||
SELECT * FROM crdb_internal.tenant_span_stats(1000) | ||
---- | ||
database_id table_id range_count approximate_disk_bytes live_bytes total_bytes live_percentage | ||
|
||
# SELECT * FROM crdb_internal.tenant_span_stats(1, 1000): | ||
# Assert that we get empty rows for a table id that does not correspond to a table. | ||
query IIIIIIR colnames | ||
SELECT * FROM crdb_internal.tenant_span_stats(1, 1000) | ||
---- | ||
database_id table_id range_count approximate_disk_bytes live_bytes total_bytes live_percentage | ||
|
||
# Select everything from a table to have 'live bytes'. | ||
statement ok | ||
SELECT * FROM a.b; | ||
|
||
# Assert that we are collecting non-zero span stats scoped to the provided database/table id combo. | ||
query IIBBBB colnames | ||
SELECT database_id, table_id, range_count > 0 as range_count, live_bytes > 0 as live_bytes, total_bytes > 0 as total_bytes, live_percentage > 0 as live_percentage FROM crdb_internal.tenant_span_stats(106, 108) | ||
---- | ||
database_id table_id range_count live_bytes total_bytes live_percentage | ||
106 108 true true true true | ||
|
||
# Create a second user without VIEWACTIVITY permission. | ||
statement ok | ||
CREATE USER testuser2 | ||
|
||
# Switch to user2 | ||
user testuser2 | ||
|
||
# Assert that the user2 doesn't have permission to use this builtin. | ||
query error pq: user needs ADMIN role or the VIEWACTIVITY/VIEWACTIVITYREDACTED permission to view span statistics | ||
SELECT * FROM crdb_internal.tenant_span_stats() LIMIT 0 | ||
|
||
# Switch to root | ||
user root | ||
|
||
# Grant VIEWACTIVITY permission to second user. | ||
statement ok | ||
ALTER ROLE testuser2 WITH VIEWACTIVITY | ||
|
||
user testuser2 | ||
|
||
# Assert that the user2 has permission to use this builtin. | ||
query IIIIIIR colnames | ||
SELECT * FROM crdb_internal.tenant_span_stats() LIMIT 0 | ||
---- | ||
database_id table_id range_count approximate_disk_bytes live_bytes total_bytes live_percentage | ||
|
||
# Switch to root | ||
user root | ||
|
||
# Remove VIEWACTIVITY permission from second user. | ||
statement ok | ||
ALTER ROLE testuser2 WITH NOVIEWACTIVITY | ||
|
||
# Grant VIEWACTIVITYREDACTED permission to second user. | ||
statement ok | ||
ALTER ROLE testuser2 WITH VIEWACTIVITYREDACTED | ||
|
||
# Assert that the user2 has permission to use this builtin. | ||
query IIIIIIR colnames | ||
SELECT * FROM crdb_internal.tenant_span_stats() LIMIT 0 | ||
---- | ||
database_id table_id range_count approximate_disk_bytes live_bytes total_bytes live_percentage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.