DEV Community

Dmitry Romanoff
Dmitry Romanoff

Posted on

How to find PostgreSQL DB growth rate?

Approach #1

with
a as (select table_name, pg_total_relation_size(table_name) table_size_b from (SELECT ('"' || table_schema || '"."' || table_name || '"') AS table_name FROM information_schema.tables where table_schema='public') M),
b as (select table_name, pg_total_relation_size(table_name) table_size_b from (SELECT ('"' || table_schema || '"."' || table_name || '"') AS table_name FROM information_schema.tables where table_schema='public') M, pg_sleep(10))
select
a.table_name, sum(b.table_size_b-a.table_size_b) as growth_rate_bytes
from a,b
where a.table_name= b.table_name 
group by 1
order by 2 desc;
Enter fullscreen mode Exit fullscreen mode

Approach #2

drop table if exists temp_storage_growth_trend_a;

drop table if exists temp_storage_growth_trend_b;

create table temp_storage_growth_trend_a
as
 select
    datname,
    round(sum(pg_database_size(pg_database.datname))/1024/1024, 2) AS size_in_MB,
    round(sum(pg_database_size(pg_database.datname))/1024/1024/1024, 2) AS size_in_GB
from pg_database
group by datname;

-- wait some time

create table temp_storage_growth_trend_b
as
 select
    datname,
    round(sum(pg_database_size(pg_database.datname))/1024/1024, 2) AS size_in_MB,
    round(sum(pg_database_size(pg_database.datname))/1024/1024/1024, 2) AS size_in_GB
from pg_database
group by datname;

select a.datname, b.size_in_MB - a.size_in_MB the_growth_rate_in_MB
from temp_storage_growth_trend_a a, temp_storage_growth_trend_b b
where a.datname = b.datname
order by 2 desc;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)