Goal Find the size of base tables without indexes, size of the indexes of the table, total size of the table (including its indexes) and percentage of the index size from the total size. If the size of indexes of a table is relatively high, then check as to whether all the indexes are needed.
Type General (Overview of some aspect of the database.)
License MIT License
Data Source system catalog only
SQL Query
WITH sizes AS (SELECT 
nspname AS table_schema,
relname AS table_name, 
Round(pg_table_size(c.oid)::numeric/1024/1024,3) AS table_size_MB, 
Round(pg_indexes_size(c.oid)::numeric/1024/1024,3) AS index_size_MB,
Round(pg_table_size(c.oid)::numeric/1024/1024,3)+Round(pg_indexes_size(c.oid)::numeric/1024/1024,3) AS total_size_MB
FROM pg_namespace AS n INNER JOIN pg_authid AS a ON n.nspowner=a.oid
INNER JOIN pg_class AS c ON n.oid=c.relnamespace
WHERE (n.nspname='public' OR a.rolname<>'postgres')
AND relkind='r')
SELECT table_schema, 
table_name, 
table_size_MB, 
index_size_MB, 
total_size_MB,
Round(index_size_MB*100/CASE WHEN total_size_MB=0 THEN NULL ELSE total_size_MB END,1) AS index_size_percentage
FROM sizes
ORDER BY index_size_percentage DESC, table_schema, table_name;

Collections

This query belongs to the following collections:

NameDescription
Find problems by overviewQueries that results point to different aspects of database that might have problems. A human reviewer has to decide based on the results as to whether there are problems or not .
Categories

This query is classified under the following categories:

NameDescription
Data at the database physical levelQueries of this category provide information about the disk usage.
PerformanceQueries of this category provide information about indexes in a database.