The list of all the queries

The size of base tables and their indexes

Query 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.
Query type: General (Overview of some aspect of the database.)
Query license: MIT License
Data source: system catalog only
SQL query: Click on query to copy it

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 where the query belongs to

Collection nameCollection description
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 where the query belongs to

Category nameCategory description
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.

The list of all the queries