Skip to content

Instantly share code, notes, and snippets.

@ardiesaeidi
Created August 27, 2020 16:54
Show Gist options
  • Save ardiesaeidi/e097386f8c3f2a9cccbed2d51b330334 to your computer and use it in GitHub Desktop.
Save ardiesaeidi/e097386f8c3f2a9cccbed2d51b330334 to your computer and use it in GitHub Desktop.
postgres get table size
-- https://wiki.postgresql.org/wiki/Disk_Usage
-- get size of table
SELECT *, pg_size_pretty(total_bytes) AS total
, pg_size_pretty(index_bytes) AS index
, pg_size_pretty(toast_bytes) AS toast
, pg_size_pretty(table_bytes) AS table
FROM (
SELECT *, total_bytes-index_bytes-coalesce(toast_bytes,0) AS table_bytes FROM (
SELECT c.oid,nspname AS table_schema, relname AS table_name
, c.reltuples AS row_estimate
, pg_total_relation_size(c.oid) AS total_bytes
, pg_indexes_size(c.oid) AS index_bytes
, pg_total_relation_size(reltoastrelid) AS toast_bytes
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE relkind = 'r'
) a
) a
where a.table_schema = 'public'
and a.table_name like 'client_issued%'
order by a.table_name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment