If an estimate is good enough, then statistical sampling is your friend. I'd probably use a sample size calculator to determine how many rows I need, then write some code to randomly insert that many keys into a table. A join, a function, and you're done.
If you've never done anything like this before, you'll probably want to do some background reading. When I had to do that stuff, I used a handbook from nist.gov. (And you'll probably be surprised at how small a sample you need.)
I try to answer your question - to be correct, I try to answer the question I think you've asked (see the comments). Until then I'll assume that a virtual table is simply a table and you want to count the tables a user has SELECT
privilege on.
First, you have to collect all roles which the given user belongs to. This will include the user itself, all roles that are granted to it, all roles that are granted to the roles granted to your user etc., and finally, but usually most importantly, the PUBLIC role.
You can collect these from the information_schema.applicable_roles
view with a query like this:
WITH RECURSIVE privs(grantee, role_name) AS (
SELECT grantee, role_name
FROM information_schema.applicable_roles
WHERE grantee = 'my_user'
UNION ALL
SELECT ar.grantee, ar.role_name
FROM
information_schema.applicable_roles ar
JOIN privs ON ar.grantee = privs.role_name
)
SELECT role_name FROM privs
UNION
SELECT 'my_user'
UNION
SELECT 'PUBLIC'
Then you have to consult the information_schema.table_privileges
view. In order to see all the rows belonging to all grantees, you have to be logged in as a superuser. With an ordinary user you will see only the roles granted to your user (including indirectly granted roles, ie. if the grants are user -> user1 -> user2
, you will see user -> user1
and user1 -> user2
if logged in as user
, and only user1 -> user2
if logged in as user1
.)
Here you can look for tables with SELECT
privilege like
SELECT table_name
FROM information_schema.table_privileges
WHERE
privilege_type = 'SELECT'
AND grantee IN (... the recursive query above goes here ...)
;
Turning this into a count is left as an exercise for the reader :)
Best Answer
This SQL will help you
Result:
You can put this in shell:
or you can record the results into a table, then execute
to get result csv file.