Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: add crdb_internal views for system statistics tables #98261

Merged

Conversation

ericharmeling
Copy link
Contributor

This commit adds two new crdb_internal views:

  • crdb_internal.statement_statistics_persisted, which surfaces the system.statement_statistics table
  • crdb_internal.transaction_statistics_persisted, which surfaces the system.transaction_statistics table

Example output from after flush:

[email protected]:26257/insights> select * from crdb_internal.statement_statistics_persisted limit 3;
-[ RECORD 1 ]
aggregated_ts              | 2023-03-08 23:00:00+00
fingerprint_id             | \x3ab7869b0bc4aa5a
transaction_fingerprint_id | \x95d43bd78dc51d85
plan_hash                  | \x9aec25074eb1e3a0
app_name                   | $ cockroach sql
node_id                    | 1
agg_interval               | 01:00:00
metadata                   | {"db": "insights", "distsql": true, "failed": false, "fullScan": true, "implicitTxn": true, "query": "SELECT * FROM crdb_internal.statement_statistics_persisted", "querySummary": "SELECT * FROM crdb_internal.statement_statis...", "stmtTyp": "TypeDML", "vec": true}
statistics                 | {"execution_statistics": {"cnt": 1, "contentionTime": {"mean": 0, "sqDiff": 0}, "cpuSQLNanos": {"mean": 24667, "sqDiff": 0}, "maxDiskUsage": {"mean": 0, "sqDiff": 0}, "maxMemUsage": {"mean": 2.048E+4, "sqDiff": 0}, "mvccIteratorStats": {"blockBytes": {"mean": 0, "sqDiff": 0}, "blockBytesInCache": {"mean": 0, "sqDiff": 0}, "keyBytes": {"mean": 0, "sqDiff": 0}, "pointCount": {"mean": 0, "sqDiff": 0}, "pointsCoveredByRangeTombstones": {"mean": 0, "sqDiff": 0}, "rangeKeyContainedPoints": {"mean": 0, "sqDiff": 0}, "rangeKeyCount": {"mean": 0, "sqDiff": 0}, "rangeKeySkippedPoints": {"mean": 0, "sqDiff": 0}, "seekCount": {"mean": 1, "sqDiff": 0}, "seekCountInternal": {"mean": 1, "sqDiff": 0}, "stepCount": {"mean": 0, "sqDiff": 0}, "stepCountInternal": {"mean": 0, "sqDiff": 0}, "valueBytes": {"mean": 0, "sqDiff": 0}}, "networkBytes": {"mean": 0, "sqDiff": 0}, "networkMsgs": {"mean": 0, "sqDiff": 0}}, "index_recommendations": [], "statistics": {"bytesRead": {"mean": 0, "sqDiff": 0}, "cnt": 1, "firstAttemptCnt": 1, "idleLat": {"mean": 0, "sqDiff": 0}, "indexes": ["42@1"], "lastErrorCode": "", "lastExecAt": "2023-03-08T23:14:04.614242Z", "latencyInfo": {"max": 0.001212208, "min": 0.001212208, "p50": 0, "p90": 0, "p99": 0}, "maxRetries": 0, "nodes": [1], "numRows": {"mean": 0, "sqDiff": 0}, "ovhLat": {"mean": 0.000007791999999999955, "sqDiff": 0}, "parseLat": {"mean": 0.000016666, "sqDiff": 0}, "planGists": ["AgFUAgD/FwAAAAcYBhg="], "planLat": {"mean": 0.000691666, "sqDiff": 0}, "regions": ["us-east1"], "rowsRead": {"mean": 0, "sqDiff": 0}, "rowsWritten": {"mean": 0, "sqDiff": 0}, "runLat": {"mean": 0.000496084, "sqDiff": 0}, "svcLat": {"mean": 0.001212208, "sqDiff": 0}}}
plan                       | {"Children": [], "Name": ""}
index_recommendations      | {}
indexes_usage              | ["42@1"]
-[ RECORD 2 ]
aggregated_ts              | 2023-03-08 23:00:00+00
fingerprint_id             | \x44c9fdb49be676cf
transaction_fingerprint_id | \xc1efcc0bba0909f8
plan_hash                  | \x780a1ba35976b15d
app_name                   | insights
node_id                    | 1
agg_interval               | 01:00:00
metadata                   | {"db": "insights", "distsql": false, "failed": false, "fullScan": false, "implicitTxn": false, "query": "UPDATE insights_workload_table_0 SET balance = balance + $1 WHERE id = $1", "querySummary": "UPDATE insights_workload_table_0 SET balance = balan... WHERE id = $1", "stmtTyp": "TypeDML", "vec": true}
statistics                 | {"execution_statistics": {"cnt": 28, "contentionTime": {"mean": 0, "sqDiff": 0}, "cpuSQLNanos": {"mean": 402538.75, "sqDiff": 1160598792985.25}, "maxDiskUsage": {"mean": 0, "sqDiff": 0}, "maxMemUsage": {"mean": 4.096E+4, "sqDiff": 0}, "mvccIteratorStats": {"blockBytes": {"mean": 31570.321428571428, "sqDiff": 20932497128.107143}, "blockBytesInCache": {"mean": 0, "sqDiff": 0}, "keyBytes": {"mean": 0, "sqDiff": 0}, "pointCount": {"mean": 6.857142857142857, "sqDiff": 435.42857142857133}, "pointsCoveredByRangeTombstones": {"mean": 0, "sqDiff": 0}, "rangeKeyContainedPoints": {"mean": 0, "sqDiff": 0}, "rangeKeyCount": {"mean": 0, "sqDiff": 0}, "rangeKeySkippedPoints": {"mean": 0, "sqDiff": 0}, "seekCount": {"mean": 2, "sqDiff": 0}, "seekCountInternal": {"mean": 2, "sqDiff": 0}, "stepCount": {"mean": 0, "sqDiff": 0}, "stepCountInternal": {"mean": 4.857142857142857, "sqDiff": 435.42857142857133}, "valueBytes": {"mean": 360.32142857142856, "sqDiff": 756476.107142857}}, "networkBytes": {"mean": 0, "sqDiff": 0}, "networkMsgs": {"mean": 0, "sqDiff": 0}}, "index_recommendations": [], "statistics": {"bytesRead": {"mean": 159.04887361588396, "sqDiff": 3909.7441771668127}, "cnt": 2619, "firstAttemptCnt": 2619, "idleLat": {"mean": 0.021495726165330273, "sqDiff": 36.39774900003032}, "indexes": ["106@1"], "lastErrorCode": "", "lastExecAt": "2023-03-08T23:31:03.079093Z", "latencyInfo": {"max": 1.724660916, "min": 0.0001765, "p50": 0.000757916, "p90": 0.00191375, "p99": 0.004730417}, "maxRetries": 0, "nodes": [1], "numRows": {"mean": 1, "sqDiff": 0}, "ovhLat": {"mean": 0.0000018584035891561339, "sqDiff": 3.132932109484058E-7}, "parseLat": {"mean": 0, "sqDiff": 0}, "planGists": ["AgHUAQIADwIAAAcKBQoh1AEAAA=="], "planLat": {"mean": 0.0002562748900343638, "sqDiff": 0.0002118085353898781}, "regions": ["us-east1"], "rowsRead": {"mean": 1, "sqDiff": 0}, "rowsWritten": {"mean": 1, "sqDiff": 0}, "runLat": {"mean": 0.0024048477613592997, "sqDiff": 4.850230671161608}, "svcLat": {"mean": 0.0026629810549828195, "sqDiff": 4.852464499918359}}}
plan                       | {"Children": [], "Name": ""}
index_recommendations      | {}
indexes_usage              | ["106@1"]
-[ RECORD 3 ]
aggregated_ts              | 2023-03-08 23:00:00+00
fingerprint_id             | \x54202c7b75a5ba87
transaction_fingerprint_id | \x0000000000000000
plan_hash                  | \xbee0e52ec8c08bdd
app_name                   | $$ $ cockroach demo
node_id                    | 1
agg_interval               | 01:00:00
metadata                   | {"db": "insights", "distsql": false, "failed": false, "fullScan": false, "implicitTxn": false, "query": "INSERT INTO system.jobs(id, created, status, payload, progress, claim_session_id, claim_instance_id, job_type) VALUES ($1, $1, __more1_10__)", "querySummary": "INSERT INTO system.jobs(id, created, st...)", "stmtTyp": "TypeDML", "vec": true}
statistics                 | {"execution_statistics": {"cnt": 1, "contentionTime": {"mean": 0, "sqDiff": 0}, "cpuSQLNanos": {"mean": 300625, "sqDiff": 0}, "maxDiskUsage": {"mean": 0, "sqDiff": 0}, "maxMemUsage": {"mean": 1.024E+4, "sqDiff": 0}, "mvccIteratorStats": {"blockBytes": {"mean": 0, "sqDiff": 0}, "blockBytesInCache": {"mean": 0, "sqDiff": 0}, "keyBytes": {"mean": 0, "sqDiff": 0}, "pointCount": {"mean": 0, "sqDiff": 0}, "pointsCoveredByRangeTombstones": {"mean": 0, "sqDiff": 0}, "rangeKeyContainedPoints": {"mean": 0, "sqDiff": 0}, "rangeKeyCount": {"mean": 0, "sqDiff": 0}, "rangeKeySkippedPoints": {"mean": 0, "sqDiff": 0}, "seekCount": {"mean": 0, "sqDiff": 0}, "seekCountInternal": {"mean": 0, "sqDiff": 0}, "stepCount": {"mean": 0, "sqDiff": 0}, "stepCountInternal": {"mean": 0, "sqDiff": 0}, "valueBytes": {"mean": 0, "sqDiff": 0}}, "networkBytes": {"mean": 0, "sqDiff": 0}, "networkMsgs": {"mean": 0, "sqDiff": 0}}, "index_recommendations": [], "statistics": {"bytesRead": {"mean": 0, "sqDiff": 0}, "cnt": 1, "firstAttemptCnt": 1, "idleLat": {"mean": 9223372036.854776, "sqDiff": 0}, "indexes": [], "lastErrorCode": "", "lastExecAt": "2023-03-08T23:13:25.132671Z", "latencyInfo": {"max": 0.000589375, "min": 0.000589375, "p50": 0, "p90": 0, "p99": 0}, "maxRetries": 0, "nodes": [1], "numRows": {"mean": 1, "sqDiff": 0}, "ovhLat": {"mean": 0.0000016249999999999988, "sqDiff": 0}, "parseLat": {"mean": 0, "sqDiff": 0}, "planGists": ["AiACHgA="], "planLat": {"mean": 0.000203792, "sqDiff": 0}, "regions": ["us-east1"], "rowsRead": {"mean": 0, "sqDiff": 0}, "rowsWritten": {"mean": 1, "sqDiff": 0}, "runLat": {"mean": 0.000383958, "sqDiff": 0}, "svcLat": {"mean": 0.000589375, "sqDiff": 0}}}
plan                       | {"Children": [], "Name": ""}
index_recommendations      | {}
indexes_usage              | []

Time: 4ms total (execution 3ms / network 1ms)

[email protected]:26257/insights> select * from crdb_internal.transaction_statistics_persisted limit 3;
-[ RECORD 1 ]
aggregated_ts  | 2023-03-08 23:00:00+00
fingerprint_id | \x17d80cf128571d63
app_name       | $ internal-migration-job-mark-job-succeeded
node_id        | 1
agg_interval   | 01:00:00
metadata       | {"stmtFingerprintIDs": ["b8bbb1bdae56aabc"]}
statistics     | {"execution_statistics": {"cnt": 1, "contentionTime": {"mean": 0, "sqDiff": 0}, "cpuSQLNanos": {"mean": 64709, "sqDiff": 0}, "maxDiskUsage": {"mean": 0, "sqDiff": 0}, "maxMemUsage": {"mean": 1.024E+4, "sqDiff": 0}, "mvccIteratorStats": {"blockBytes": {"mean": 0, "sqDiff": 0}, "blockBytesInCache": {"mean": 0, "sqDiff": 0}, "keyBytes": {"mean": 0, "sqDiff": 0}, "pointCount": {"mean": 0, "sqDiff": 0}, "pointsCoveredByRangeTombstones": {"mean": 0, "sqDiff": 0}, "rangeKeyContainedPoints": {"mean": 0, "sqDiff": 0}, "rangeKeyCount": {"mean": 0, "sqDiff": 0}, "rangeKeySkippedPoints": {"mean": 0, "sqDiff": 0}, "seekCount": {"mean": 0, "sqDiff": 0}, "seekCountInternal": {"mean": 0, "sqDiff": 0}, "stepCount": {"mean": 0, "sqDiff": 0}, "stepCountInternal": {"mean": 0, "sqDiff": 0}, "valueBytes": {"mean": 0, "sqDiff": 0}}, "networkBytes": {"mean": 0, "sqDiff": 0}, "networkMsgs": {"mean": 0, "sqDiff": 0}}, "statistics": {"bytesRead": {"mean": 0, "sqDiff": 0}, "cnt": 6, "commitLat": {"mean": 0, "sqDiff": 0}, "idleLat": {"mean": 0, "sqDiff": 0}, "maxRetries": 0, "numRows": {"mean": 1, "sqDiff": 0}, "retryLat": {"mean": 0, "sqDiff": 0}, "rowsRead": {"mean": 0, "sqDiff": 0}, "rowsWritten": {"mean": 1, "sqDiff": 0}, "svcLat": {"mean": 0.00026919450000000006, "sqDiff": 1.7615729685500012E-8}}}
-[ RECORD 2 ]
aggregated_ts  | 2023-03-08 23:00:00+00
fingerprint_id | \x2b024f7e2567a238
app_name       | $ internal-get-job-row
node_id        | 1
agg_interval   | 01:00:00
metadata       | {"stmtFingerprintIDs": ["8461f232a36615e7"]}
statistics     | {"execution_statistics": {"cnt": 1, "contentionTime": {"mean": 0, "sqDiff": 0}, "cpuSQLNanos": {"mean": 50835, "sqDiff": 0}, "maxDiskUsage": {"mean": 0, "sqDiff": 0}, "maxMemUsage": {"mean": 3.072E+4, "sqDiff": 0}, "mvccIteratorStats": {"blockBytes": {"mean": 0, "sqDiff": 0}, "blockBytesInCache": {"mean": 0, "sqDiff": 0}, "keyBytes": {"mean": 0, "sqDiff": 0}, "pointCount": {"mean": 3, "sqDiff": 0}, "pointsCoveredByRangeTombstones": {"mean": 0, "sqDiff": 0}, "rangeKeyContainedPoints": {"mean": 0, "sqDiff": 0}, "rangeKeyCount": {"mean": 0, "sqDiff": 0}, "rangeKeySkippedPoints": {"mean": 0, "sqDiff": 0}, "seekCount": {"mean": 1, "sqDiff": 0}, "seekCountInternal": {"mean": 1, "sqDiff": 0}, "stepCount": {"mean": 3, "sqDiff": 0}, "stepCountInternal": {"mean": 3, "sqDiff": 0}, "valueBytes": {"mean": 186, "sqDiff": 0}}, "networkBytes": {"mean": 0, "sqDiff": 0}, "networkMsgs": {"mean": 0, "sqDiff": 0}}, "statistics": {"bytesRead": {"mean": 284.81818181818176, "sqDiff": 3465.636363636355}, "cnt": 11, "commitLat": {"mean": 0.000003469727272727273, "sqDiff": 4.946789218181818E-11}, "idleLat": {"mean": 0, "sqDiff": 0}, "maxRetries": 0, "numRows": {"mean": 1, "sqDiff": 0}, "retryLat": {"mean": 0, "sqDiff": 0}, "rowsRead": {"mean": 1, "sqDiff": 0}, "rowsWritten": {"mean": 0, "sqDiff": 0}, "svcLat": {"mean": 0.0006771060909090909, "sqDiff": 8.91510436082909E-7}}}
-[ RECORD 3 ]
aggregated_ts  | 2023-03-08 23:00:00+00
fingerprint_id | \x37e130a1df20d299
app_name       | $ internal-create-stats
node_id        | 1
agg_interval   | 01:00:00
metadata       | {"stmtFingerprintIDs": ["98828ded59216546"]}
statistics     | {"execution_statistics": {"cnt": 1, "contentionTime": {"mean": 0, "sqDiff": 0}, "cpuSQLNanos": {"mean": 11875, "sqDiff": 0}, "maxDiskUsage": {"mean": 0, "sqDiff": 0}, "maxMemUsage": {"mean": 1.024E+4, "sqDiff": 0}, "mvccIteratorStats": {"blockBytes": {"mean": 0, "sqDiff": 0}, "blockBytesInCache": {"mean": 0, "sqDiff": 0}, "keyBytes": {"mean": 0, "sqDiff": 0}, "pointCount": {"mean": 0, "sqDiff": 0}, "pointsCoveredByRangeTombstones": {"mean": 0, "sqDiff": 0}, "rangeKeyContainedPoints": {"mean": 0, "sqDiff": 0}, "rangeKeyCount": {"mean": 0, "sqDiff": 0}, "rangeKeySkippedPoints": {"mean": 0, "sqDiff": 0}, "seekCount": {"mean": 0, "sqDiff": 0}, "seekCountInternal": {"mean": 0, "sqDiff": 0}, "stepCount": {"mean": 0, "sqDiff": 0}, "stepCountInternal": {"mean": 0, "sqDiff": 0}, "valueBytes": {"mean": 0, "sqDiff": 0}}, "networkBytes": {"mean": 0, "sqDiff": 0}, "networkMsgs": {"mean": 0, "sqDiff": 0}}, "statistics": {"bytesRead": {"mean": 0, "sqDiff": 0}, "cnt": 1, "commitLat": {"mean": 0.000002291, "sqDiff": 0}, "idleLat": {"mean": 0, "sqDiff": 0}, "maxRetries": 0, "numRows": {"mean": 0, "sqDiff": 0}, "retryLat": {"mean": 0, "sqDiff": 0}, "rowsRead": {"mean": 0, "sqDiff": 0}, "rowsWritten": {"mean": 0, "sqDiff": 0}, "svcLat": {"mean": 0.008680208, "sqDiff": 0}}}

Time: 3ms total (execution 2ms / network 1ms)

Epic: none

Release note: Added two views to the crdb_internal catalog: crdb_internal.statement_statistics_persisted, which surfaces data in the persisted system.statement_statistics table, and crdb_internal.transaction_statistics_persisted, which surfaces the system.transaction_statistics table.

@ericharmeling ericharmeling requested a review from a team March 8, 2023 23:36
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Contributor

@j82w j82w left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ericharmeling)


pkg/ccl/logictestccl/testdata/logic_test/crdb_internal line 212 at r1 (raw file):

1          can_view_node_info     false
1          can_view_tsdb_metrics  false
5          can_admin_split        false

Are these changes expected? They don't seem to be related to this PR.


pkg/ccl/logictestccl/testdata/logic_test/crdb_internal_tenant line 25 at r1 (raw file):

SHOW TABLES FROM crdb_internal
----
crdb_internal  active_range_feeds                table  admin  NULL  NULL

Can we add a logic test to verify the view can be read with view activity permissions, and that the view matches the system table?

@ericharmeling ericharmeling force-pushed the system-statistics-table-views branch from af56642 to 8db1e93 Compare March 9, 2023 15:22
Copy link
Contributor Author

@ericharmeling ericharmeling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @j82w)


pkg/ccl/logictestccl/testdata/logic_test/crdb_internal_tenant line 25 at r1 (raw file):

Can we add a logic test to verify the view can be read with view activity permissions

I don't think the user needs any permissions to view the crdb_internal views... I saw this locally, and the logic test I just added verifies this.

that the view matches the system table?

Done.


pkg/ccl/logictestccl/testdata/logic_test/crdb_internal line 212 at r1 (raw file):

Previously, j82w (Jake) wrote…

Are these changes expected? They don't seem to be related to this PR.

I agree - this seems a bit off.

Copy link
Contributor Author

@ericharmeling ericharmeling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @j82w)


pkg/sql/logictest/testdata/logic_test/crdb_internal line 1262 at r2 (raw file):


query TTTITTT colnames
SELECT * FROM crdb_internal.transaction_statistics_persisted WHERE NOT EXISTS (SELECT * FROM system.transaction_statistics);

I'm actually going to update these queries a bit and push with some test rewrites...

@ericharmeling ericharmeling force-pushed the system-statistics-table-views branch from 8db1e93 to 752f587 Compare March 9, 2023 16:29
@ericharmeling ericharmeling marked this pull request as ready for review March 9, 2023 16:30
@ericharmeling ericharmeling requested a review from a team March 9, 2023 16:30
@ericharmeling ericharmeling force-pushed the system-statistics-table-views branch from 752f587 to 96cce4d Compare March 9, 2023 17:22
@ericharmeling ericharmeling requested review from a team as code owners March 9, 2023 17:22
@ericharmeling ericharmeling requested a review from cucaroach March 9, 2023 17:22
@rytaft
Copy link
Collaborator

rytaft commented Mar 9, 2023

Can you please create a new file in sql/opt/exec/execbuilder/testdata and include these views there? Going forward SQL Queries would like to be able to review the plans of all new views you create (and performance-sensitive queries you're running) to see if we can suggest optimization improvements.

@ericharmeling ericharmeling force-pushed the system-statistics-table-views branch from 96cce4d to 03f8947 Compare March 9, 2023 18:28
@ericharmeling
Copy link
Contributor Author

@rytaft

Can you please create a new file in sql/opt/exec/execbuilder/testdata and include these views there?

Sure thing! These should just check EXPLAIN (VERBOSE) output, correct?

@ericharmeling ericharmeling force-pushed the system-statistics-table-views branch from 03f8947 to 96d5f50 Compare March 9, 2023 19:04
@rytaft
Copy link
Collaborator

rytaft commented Mar 9, 2023

Yes please!

@ericharmeling
Copy link
Contributor Author

Yes please!

Okay! I've added the SELECT queries that define these views to pkg/sql/opt/exec/execbuilder/testdata/statistics_persisted.

We are planning to add some indexes to the underlying system tables in service of common queries we plan to execute against these new views. When we add those indexes, we'll add those queries as well.

@ericharmeling ericharmeling requested a review from j82w March 9, 2023 23:03
@rytaft
Copy link
Collaborator

rytaft commented Mar 10, 2023

Yes please!

Okay! I've added the SELECT queries that define these views to pkg/sql/opt/exec/execbuilder/testdata/statistics_persisted.

We are planning to add some indexes to the underlying system tables in service of common queries we plan to execute against these new views. When we add those indexes, we'll add those queries as well.

Thanks!

nit: can you please rename to sql_statistics_persisted or something like that to distinguish from table stats?

@j82w
Copy link
Contributor

j82w commented Mar 10, 2023

pkg/ccl/logictestccl/testdata/logic_test/crdb_internal_tenant line 25 at r1 (raw file):

permissions to view the crdb_internal views... I saw this locally, and the logic test I just added verifies this.

Isn't this a bug? Shouldn't it at least require ViewActivity Or ViewActivityRedacted Role like most of the virtual tables?

@j82w
Copy link
Contributor

j82w commented Mar 10, 2023

pkg/ccl/logictestccl/testdata/logic_test/crdb_internal_tenant line 25 at r1 (raw file):

Previously, j82w (Jake) wrote…

permissions to view the crdb_internal views... I saw this locally, and the logic test I just added verifies this.
Isn't this a bug? Shouldn't it at least require ViewActivity Or ViewActivityRedacted Role like most of the virtual tables?

Isn't this a bug? Shouldn't it at least require ViewActivity Or ViewActivityRedacted Role like most of the virtual tables?

Copy link
Contributor Author

@ericharmeling ericharmeling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @cucaroach, @j82w, @maryliag, and @rafiss)


pkg/ccl/logictestccl/testdata/logic_test/crdb_internal_tenant line 25 at r1 (raw file):

Previously, j82w (Jake) wrote…

Isn't this a bug? Shouldn't it at least require ViewActivity Or ViewActivityRedacted Role like most of the virtual tables?

It looks like the default grant/grantee for all crdb_internal virtual tables and views is SELECT/public, so unless we check the role on the user when generating the vtables/views, anyone can select any crdb_internal vtable/view.

For example:

(as root)

[email protected]:26257/movr> create user eric;
CREATE ROLE

[email protected]:26257/movr> show roles;
  username | options | member_of
-----------+---------+------------
  admin    |         | {}
  eric     |         | {}
  root     |         | {admin}
(3 rows)


[email protected]:26257/movr> show grants on crdb_internal.transaction_contention_events;
  database_name |  schema_name  |          table_name           | grantee | privilege_type | is_grantable
----------------+---------------+-------------------------------+---------+----------------+---------------
  movr          | crdb_internal | transaction_contention_events | public  | SELECT         |      f
(1 row)


[email protected]:26257/movr> show grants on crdb_internal.statement_statistics;
  database_name |  schema_name  |      table_name      | grantee | privilege_type | is_grantable
----------------+---------------+----------------------+---------+----------------+---------------
  movr          | crdb_internal | statement_statistics | public  | SELECT         |      f
(1 row)

(as non-admin with no permissions)

[email protected]:26257/defaultdb> select * from crdb_internal.transaction_contention_events;
ERROR: user eric does not have VIEWACTIVITY or VIEWACTIVITYREDACTED privilege


[email protected]:26257/defaultdb> select fingerprint_id from crdb_internal.statement_statistics limit 3;
    fingerprint_id
----------------------
  \xef388c6ce139b396
  \x6906bca0125df305
  \x120ef13c86e9acc9
(3 rows)

We check for VIEWACTIVITY/VIEWACTIVITYREDACTED for crdb_internal.transaction_contention_events here:

hasPermission, err := p.HasViewActivityOrViewActivityRedactedRole(ctx)

But we never do for crdb_internal.statement_statistics. And we don't check any permissions for any crdb_internal views. Perhaps we should? @maryliag

We should also consult Sessions on this. @rafiss

@ericharmeling ericharmeling force-pushed the system-statistics-table-views branch from 96d5f50 to c16d1b3 Compare March 10, 2023 20:40
@ericharmeling
Copy link
Contributor Author

ericharmeling commented Mar 10, 2023

nit: can you please rename to sql_statistics_persisted or something like that to distinguish from table stats?

Done!

Copy link
Contributor

@maryliag maryliag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!
Just one question on the tests, otherwise :lgtm:

Reviewed 5 of 6 files at r1, 6 of 11 files at r3, 1 of 3 files at r4, 6 of 6 files at r6, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @cucaroach, @ericharmeling, @j82w, and @rafiss)


pkg/sql/opt/exec/execbuilder/testdata/sql_statistics_persisted line 19 at r6 (raw file):

    indexes_usage
FROM
    system.statement_statistics

don't you wanna test a select on the new views too?

This commit adds two new crdb_internal views:

- crdb_internal.statement_statistics_persisted, which surfaces the
system.statement_statistics table
- crdb_internal.transaction_statistics_persisted, which surfaces the
system.transaction_statistics table

Epic: none

Release note: Added two views to the crdb_internal catalog:
crdb_internal.statement_statistics_persisted, which surfaces data in
the persisted system.statement_statistics table, and
crdb_internal.transaction_statistics_persisted, which surfaces the
system.transaction_statistics table.
@ericharmeling ericharmeling force-pushed the system-statistics-table-views branch from c16d1b3 to 4557dd0 Compare March 13, 2023 16:42
Copy link
Contributor Author

@ericharmeling ericharmeling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TFTR!

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @cucaroach, @j82w, @maryliag, and @rafiss)


pkg/sql/opt/exec/execbuilder/testdata/sql_statistics_persisted line 19 at r6 (raw file):

Previously, maryliag (Marylia Gutierrez) wrote…

don't you wanna test a select on the new views too?

Done.

@ericharmeling
Copy link
Contributor Author

blathers backport 22.2

@blathers-crl
Copy link

blathers-crl bot commented Mar 15, 2023

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


error creating merge commit from 4557dd0 to blathers/backport-release-22.2-98261: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 22.2 failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

ericharmeling added a commit to ericharmeling/cockroach that referenced this pull request Mar 15, 2023
Backport of commit 4557dd0 (cockroachdb#98261).

This commit adds two new crdb_internal views:

- crdb_internal.statement_statistics_persisted, which surfaces the
system.statement_statistics table
- crdb_internal.transaction_statistics_persisted, which surfaces the
system.transaction_statistics table

Epic: none

Release note (sql change): Added two views to the crdb_internal catalog:
crdb_internal.statement_statistics_persisted, which surfaces data in
the persisted system.statement_statistics table, and
crdb_internal.transaction_statistics_persisted, which surfaces the
system.transaction_statistics table.
ericharmeling added a commit to ericharmeling/cockroach that referenced this pull request Mar 22, 2023
Backport of commit 4557dd0 (cockroachdb#98261).

This commit adds two new crdb_internal views:

- crdb_internal.statement_statistics_persisted, which surfaces the
system.statement_statistics table
- crdb_internal.transaction_statistics_persisted, which surfaces the
system.transaction_statistics table

Epic: none

Release note: Added two views to the crdb_internal catalog:
crdb_internal.statement_statistics_persisted, which surfaces data in
the persisted system.statement_statistics table, and
crdb_internal.transaction_statistics_persisted, which surfaces the
system.transaction_statistics table.
ericharmeling added a commit to ericharmeling/cockroach that referenced this pull request Mar 22, 2023
Backport of commit 4557dd0 (cockroachdb#98261).

This commit adds two new crdb_internal views:

- crdb_internal.statement_statistics_persisted, which surfaces the
system.statement_statistics table
- crdb_internal.transaction_statistics_persisted, which surfaces the
system.transaction_statistics table

Epic: none

Release note: Added two views to the crdb_internal catalog:
crdb_internal.statement_statistics_persisted, which surfaces data in
the persisted system.statement_statistics table, and
crdb_internal.transaction_statistics_persisted, which surfaces the
system.transaction_statistics table.
ericharmeling added a commit to ericharmeling/cockroach that referenced this pull request Apr 25, 2023
Backport of commit 4557dd0 (cockroachdb#98261).

This commit adds two new crdb_internal views:

- crdb_internal.statement_statistics_persisted, which surfaces the
system.statement_statistics table
- crdb_internal.transaction_statistics_persisted, which surfaces the
system.transaction_statistics table

Epic: none

Release note: Added two views to the crdb_internal catalog:
crdb_internal.statement_statistics_persisted, which surfaces data in
the persisted system.statement_statistics table, and
crdb_internal.transaction_statistics_persisted, which surfaces the
system.transaction_statistics table.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants