How to estimate the number of rows and size of tables in Postgres DB?

Dmitry Romanoff - Oct 26 '23 - - Dev Community

How to estimate the number of rows and size of tables in Postgres DB?

select
    nspname as schema,
    relname as tablename, 
    to_char(reltuples, '999,999,999,999') as rowcounts,
    round(pg_relation_size(c.oid)/1024/1024/1024) AS tablesize_gb
  from pg_class c JOIN pg_catalog.pg_namespace n
   ON n.oid = c.relnamespace where relkind='r' and nspname = 'public' 
   order by nspname, reltuples desc;
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .