-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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: improve SHOW TABLES to show row count #52203
sql: improve SHOW TABLES to show row count #52203
Conversation
Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR. My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI. I have added a few people who may be able to assist in reviewing: 🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan. |
78c3875
to
36c0f80
Compare
Thanks for the contribution @ekalinin! I gave this a try earlier and ran into a problem that I think your implementation here also hits. The problem is that only the admin user is allowed to access the |
A potential solution is to implement a new crdb internal virtual table that exposes the latest statistics on tables, and then restricts its own output to only the tables that the current user has privileges to access. Finally, the delegate command can join against it. If this sounds like something you want to take on, I can link a commit that adds a new virtual table. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: complete! 0 of 0 LGTMs obtained (waiting on @awoods187, @ekalinin, and @rohany)
pkg/sql/delegate/show_tables.go, line 54 at r1 (raw file):
ELSE 'table' END AS "type" , (select max("rowCount")
The maximum may not be the most accurate. You'll probably want to look at the "createdAt" column to get the most recent value for the given table.
Thanks. Fixed. |
36c0f80
to
0bb6dd6
Compare
Thank you for updating your pull request. My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI. 🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan. |
@rohany sounds like a plan :).
Looks good to me. Could you link that commit, please? Thanks. |
0bb6dd6
to
206dd21
Compare
@rohany Are there any contraindications for allowing all users to read I've updated the tests with this logic (moved a table into a separate db and added
And it works good. (see full test here: https://github.com/cockroachdb/cockroach/pull/52203/files#diff-cdf15f008dd1eaef8d7900a2736e8a97 ) |
Yes -- we can't allow users to get information about tables that they don't have permissions for. |
7b25483
to
45d614d
Compare
Thanks. Just added a
Got it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, this is definitely on the right track!
b34aadb
to
ae78004
Compare
Fixed some tests. PTAL. |
decf9a7
to
28bac8f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if background stats collection could result in these test's row counts flaking. Could that happen @rytaft?
Also, what are the thoughts on displaying this row count only if requested, like SHOW TABLES ... WITH ROW COUNT
or something @awoods187?
Also, this row count is irrelevant for virtual tables, so I would prefer if we reported null rather than 0 for them.
pkg/sql/delegate/show_tables.go
Outdated
, ( | ||
SELECT estimated_row_count | ||
FROM crdb_internal.table_row_statistics | ||
WHERE table_id = pc.oid::int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this handle when the requested table ID doesn't exist in the stats table?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you elaborate, please.
Currently, If there's no requested table ID in the stats table then we return NULL.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if background stats collection could result in these test's row counts flaking.
Yes, it could. I think we currently have automatic stats collection disabled for logic tests, but I think the hope was that we'd be able to re-enable it eventually. You should probably explicitly disable automatic stats collection with SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false
in any file where an automatic stats run could cause flakiness.
Reviewable status: complete! 0 of 0 LGTMs obtained (waiting on @awoods187, @ekalinin, @knz, and @rohany)
28bac8f
to
80876f5
Compare
Yeah, good point. Will update
Done. |
Hold off on updating until we decide whether we should do this or not -- it's a good amount of work to be going back and forth. |
Ok :) |
80876f5
to
45f12e2
Compare
Rebased. Fixed some tests. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would also improve the release note here to say the new column name, and how it is calculated (for the docs team to document).
I'd also like you to add a test where you select from this new table and ensure that only tables the current user has permission to see show up
tableID := tree.DInt(table.ID) | ||
rowCount := tree.DNull | ||
// For Virtual Tables report NULL row count. | ||
if !table.IsVirtualTable() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do virtual tables show up in the stats? I think we also want to just default row count to NULL if we cannot find the table in statMap
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, virtual tables will show up in crdb_internal.table_row_statistics
(even if there's no any rows for it in system.table_statistics
).
All virtual tables will have NULL number of rows as you asked here:
Also, this row count is irrelevant for virtual tables, so I would prefer if we reported null rather than 0 for them.
45f12e2
to
36f161c
Compare
Done.
Done: Test user can see only virtual tables ( |
9e8b49d
to
2118a44
Compare
This PR still has one failed test:
It failed here: And looks like it related to this issue: |
Oh, it's started to fail because you've added a stats collection run. Just add a |
This change adds column "estimated_row_count" into result of the "SHOW TABLES" to show estimated (not real) number of rows. Besides that the body of the `delegator.delegateShowTables` was a bit simplified (we do not need two almost similar versions of the query) and new virtual table `crdb_internal.table_row_statistics` was added (to show stats for non-root users). Release note (sql change): This change modifies SHOW TABLES to return estimates number of rows. New column's name is `estimated_row_count`. Number of rows is taken from `system.table_statistics` table (via `crdb_internal.table_row_statistics` which shows only tables accessible for current user).
2118a44
to
a9fbed6
Compare
Thanks! Added. |
This looks good to me! Thanks for your contribution. bors r=rohany |
Build succeeded: |
@ekalinin Thank you for contributing to CockroachDB this year. As a token of our appreciation, we would like to send you a gift. Please DM me on our community Slack @amruta so I can send you a link. (If you don’t want a gift, we also have a charitable contribution choice.) All orders need to be in by December 13, so please contact me as soon as possible :) |
Fixes #48755
This change adds column
estimated_row_count
into result of the "SHOW TABLES"to show estimated (not real) number of rows.
Besides that the body of the
delegator.delegateShowTables
was a bitsimplified (we do not need two almost similar versions of the query) and
new virtual table
crdb_internal.table_row_statistics
was added (toshow stats for non-root users).
Release note (sql change): This change modifies
SHOW TABLES
to returnestimates number of rows. New column's name is
estimated_row_count
.Number of rows is taken from
system.table_statistics
table (viacrdb_internal.table_row_statistics
which shows only tables accessiblefor current user).