I'm looking for some practical help to resolve Disk space issue on DBs. I've visited these pages already:
- Postgres: Reclaiming Space
- Current size of a PostgreSQL table (vs disk space used)
- https://stackoverflow.com/questions/41991380/whats-the-difference-between-pg-table-size-pg-relation-size-pg-total-relatio
In my context I still have some questions: I have a DB with 100GB of disk space. One table is reporting using 73GB of space query:
select
quote_ident(table_schema) || '.' || quote_ident(table_name),
pg_size_pretty(pg_total_relation_size(quote_ident(table_schema) || '.' || quote_ident(table_name))) as disk_size
from
information_schema.tables
where
pg_total_relation_size(quote_ident(table_schema) || '.' || quote_ident(table_name)) > 100000
order by
pg_total_relation_size(quote_ident(table_schema) || '.' || quote_ident(table_name)) desc;
Devs are telling me they have deleted many rows in the mentioned table. How can I verify this?
pg_total_relation_size returns information on disk used, VACUUM ANALYZE only free available space but not disk space. Thus pg_total_relation_size will still return the same value, right?
In my understanding, if I run VACUUM ANALYZE it will update some reference in the space management that the DB can use for new rows. Is there any practical way to compare disk space used and actual table size? Is it possible with pgstattuple or pg_freespacemap? If yes could you provide a query example?
I've also found some queries checking pg_catalog.pg_stat_all_tables to return the amount of n_dead_tup and n_live_tup. After a VACUUM ANALYZE would these be cleared thus checking the total of dead tuple after a VACUUM ANALYZE to see if a VACUUM FULL would free space not be a good indicator?
\set ECHO_HIDDEN onand then\dand explore the generated SQL.information_schema"tables"consist of a set of views that contain information about the objects defined in the current database, hence the performance hit - you're looking at views over the same tables that native PostgreSQL accesses directly!VIEWdefinition forinformation_schema.tables. Inpsqlrun\d pg_catalog.pg_viewsand you have 4 fields (schemaname, viewname, viewowner, definition) - issue the command\o somefile.txtand use an editor - thepsqloutput is illegible. Then runSELECT schemaname, viewname, definition FROM pg_catalog.pg_views WHERE schemaname = 'information_schema' AND viewname = 'tables';and reissue\oto toggle output off. Checkout the pastebin link.