title: Redshift: Total Row Count for all tables for a particular user
body: <p>We use Periscope which charges(I think) by the total number of rows that are present in the tables that we give it access to.</p>
  
  <p>My question is: Given a user <code>periscope</code>, how do I obtain the total count of rows in the tables that the user <code>periscope</code> has access to?</p>
  
  <p>E.g. if the user <code>periscope</code> can only access two tables: t1 with count 8 and t2 with count 2, the total count should be 10.</p>
  
  <p>I got all the tables with this <a href="https://stackoverflow.com/a/36530337/238012">answer</a>(excluding 'pg_tables' and 'information_schema') but I don't know how to go about next:</p>
  
  <pre><code>SELECT CAST(schemaname as varchar), CAST(  objectname as varchar)
  FROM 
      (
      SELECT 
          schemaname
          ,objectname
          ,usename
          ,HAS_TABLE_PRIVILEGE(usrs.usename, fullobj, 'select') AS sel
          ,HAS_TABLE_PRIVILEGE(usrs.usename, fullobj, 'insert') AS ins
          ,HAS_TABLE_PRIVILEGE(usrs.usename, fullobj, 'update') AS upd
          ,HAS_TABLE_PRIVILEGE(usrs.usename, fullobj, 'delete') AS del
          ,HAS_TABLE_PRIVILEGE(usrs.usename, fullobj, 'references') AS ref
      FROM
          (
          SELECT schemaname, 't' AS obj_type, tablename AS objectname, schemaname + '.' + tablename AS fullobj FROM pg_tables
          WHERE schemaname not in ('pg_internal')
          UNION
          SELECT schemaname, 'v' AS obj_type, viewname AS objectname, schemaname + '.' + viewname AS fullobj FROM pg_views
          WHERE schemaname not in ('pg_internal')
          ) AS objs
          ,(SELECT * FROM pg_user) AS usrs
      ORDER BY fullobj
      )
  WHERE (sel = true or ins = true or upd = true or del = true or ref = true)
  and usename = 'periscope'
  and schemaname not in ('information_schema', 'pg_catalog');
  </code></pre>
  
  <p>If you have an elegant solution, please let me know!</p>