Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres database size much bigger than tablespaces on filesystem

I have a Postgres 8.3 instance with tablespaces totalling on about 74G. This is fine.

But if I ask postgres how big my database is, I get a (unexpected) large answer: 595 GB.

This seems very strange. Disk I/O tests on the system are in the 'normal' range, but queries are slower than they used to be.

Is this corruption of the database? Or are there ways to 'fix' this oddity?

[Edit] I check the tablespace size by the os:

/usr/local/pgsql/data/tblspaces/du -c -h
74G total 

All tablespaces are there

I check te database size like this:

select pg_size_pretty(pg_database_size('database')) 

[Edit2]

I also checked the entire /usr/local/pgsql/ dir. It is 76 G

And I ran this query:

select
  tablename
, pg_relation_size(tablename)
, pg_size_pretty(pg_relation_size(tablename) ) as relsize
, pg_size_pretty(pg_total_relation_size(tablename) ) as disksize
, pg_total_relation_size(tablename)
from pg_tables where schemaname <> 'information_schema'
order by 2 desc

Which returned 'normal' relationsizes, none of which exceeded the disksize.

like image 420
Rob Audenaerde Avatar asked Jun 17 '26 08:06

Rob Audenaerde


2 Answers

Turned out to be a corruption issue. Full restore of a backup fixed the problem

like image 105
Rob Audenaerde Avatar answered Jun 19 '26 15:06

Rob Audenaerde


The size of the tablespace can be checked by using:

SELECT pg_size_pretty(pg_tablespace_size('name of tablespace'));

It looks like your database didn't store all it's objects (tables, indexes, etc.) in your specific tablespace but also another (default?) tablespace. Check pg_class and pg_tablespace, something like this:

SELECT 
    *
FROM pg_class 
    JOIN pg_tablespace ON reltablespace = pg_tablespace.oid
WHERE
    spcname <> 'name of tablespace';
like image 30
Frank Heikens Avatar answered Jun 19 '26 13:06

Frank Heikens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!