// SQL snippet

Database size summary

The disk filled up, and the question is with what. This recipe totals the database size and breaks it down per schema so you know where the space went.

Main statement
PostgreSQL
Também cobre
SQL Server MySQL SQLite

The task

Monitoring reports fast growth in the database files and the cleanup cannot start until someone names the schema responsible.

The snippet

Written for PostgreSQL. Versions for SQL Server, MySQL and SQLite are under Variations.

PostgreSQL
SELECT pg_size_pretty(pg_database_size(current_database())) AS database_size;

-- breakdown per schema, largest first; indexes and TOAST are
-- already inside each table's total, so only tables and matviews
SELECT n.nspname AS schema_name,
       pg_size_pretty(SUM(pg_total_relation_size(c.oid))) AS total_size
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r', 'm')
  AND n.nspname NOT IN ('pg_catalog', 'information_schema')
GROUP BY n.nspname
ORDER BY SUM(pg_total_relation_size(c.oid)) DESC;

Como funciona

pg_database_size adds up every file in the database's directory: tables, indexes, TOAST, free-space and visibility maps. WAL is shared by the whole cluster and is not included. The per-schema query sums pg_total_relation_size over tables and materialized views; that function already counts each table's indexes and TOAST, so listing indexes separately (relkind 'i') would count them twice. The schemas add up to less than the total because the system catalogs are filtered out.

Variations

SQL Server

SQL Server
-- size is in 8 KB pages; FILEPROPERTY gives the used part
SELECT name AS file_name, type_desc,
       size * 8 / 1024 AS size_mb,
       FILEPROPERTY(name, 'SpaceUsed') * 8 / 1024 AS used_mb
FROM sys.database_files
ORDER BY size DESC;

MySQL

MySQL
SELECT table_schema,
       ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) AS size_mb
FROM information_schema.tables
WHERE table_schema NOT IN ('mysql', 'information_schema',
                           'performance_schema', 'sys')
GROUP BY table_schema
ORDER BY size_mb DESC;

SQLite

SQLite
-- the database is one file; the size IS the file size
PRAGMA page_count;
PRAGMA page_size;   -- bytes = page_count * page_size

Questions that come up

Why is the sum of my tables much smaller than the database?

Look at what the per-table query counts. pg_relation_size and MySQL's data_length alone leave out indexes and TOAST, so use pg_total_relation_size or data_length + index_length. System catalogs are filtered out of most per-table lists. On SQL Server the transaction log is a separate LOG file in sys.database_files and often the largest one; tempdb is a separate database altogether.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.