Find the sum of tablespaces in PostgreSQL

47 Views Asked by At

What I am trying to do is to calculate the sum of the 2 tableSpaces I have in my PostgreSQL DB.

SELECT pg_size_pretty (pg_tablespace_size ('pg_default') );

with this query I am able to find the used space but when I modified it to find the details of both the tablespaces by using below query

SELECT pg_size_pretty ( pg_tablespace_size ('poolData')
                       ,pg_tablespace_size ('pool')    );

but it didn't gave any output. Is there any way to find both in a single query, so that I can calculate the sum of tablespaces?

1

There are 1 best solutions below

0
Zegarek On

Function pg_tablespace_size() returns type bigint, so you can add them with a regular +.

SELECT pg_size_pretty( pg_tablespace_size('poolData')
                      +pg_tablespace_size ('pool')    );

You can also sum() sizes of tablespaces you're interested in, fetching them from pg_tablespace.

select pg_size_pretty(sum(spcname)) from pg_tablespace
where spcname in ('poolData','pool');