Skip to content

Instantly share code, notes, and snippets.

@drob
drob / oustanding.sh
Created November 16, 2013 01:07
A truly disgusting script to compute the total amount of space used in all tables in a postgres database.
# How much space, in bytes, is used by the tables in this database?
psql -c "SELECT tablename FROM pg_tables WHERE schemaname = 'public'" | sed -r "s/(.*)/\"SELECT * FROM pg_total_relation_size('\1')\"/g" | xargs -n 1 psql -c | egrep "\s+[0-9]+" | tr -d ' ' | awk '{total = total + $1}END{print total}'
# How much space, in bytes, is used by the indexes in this database?
psql -c "SELECT tablename FROM pg_tables WHERE schemaname = 'public'" | sed -r "s/(.*)/\"SELECT * FROM pg_indexes_size('\1')\"/g" | xargs -n 1 psql -c | egrep "\s+[0-9]+" | tr -d ' ' | awk '{total = total + $1}END{print total}'
# This is not a proud gist.