How to: list table sizes in PostgreSQL Database using only SQL and built in PG functions.
Table of Contents
Show
About
The below SQL will output details of every table in the current DB you are connected to including all schemas you have access to. The code is easily modifiable to include only a specific table of schema by adding filters to the inner most where statement i.e. where relkind = 'r'
.
Use the nspname
to filter on schemas and relname
to filter on tables.
Code
Below is the full code
Explanation
Using the pg_class
and pg_namespace
tables we join them together to get the table details. In the next level up with calculate the table size in bytes and finally we tidy up the bytes using the built-in function pg_size_pretty
to make it more readable.
Links
- PostgreSQL link