Method: PgHero::Methods::Tables#table_stats

Defined in:
lib/pghero/methods/tables.rb

#table_stats(schema: nil, table: nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pghero/methods/tables.rb', line 46

def table_stats(schema: nil, table: nil)
  select_all <<~SQL
    SELECT
      nspname AS schema,
      relname AS table,
      reltuples::bigint AS estimated_rows,
      pg_total_relation_size(pg_class.oid) AS size_bytes
    FROM
      pg_class
    INNER JOIN
      pg_namespace ON pg_namespace.oid = pg_class.relnamespace
    WHERE
      relkind = 'r'
      #{schema ? "AND nspname = #{quote(schema)}" : nil}
      #{table ? "AND relname IN (#{Array(table).map { |t| quote(t) }.join(", ")})" : nil}
    ORDER BY
      1, 2
  SQL
end