Skip to content

Instantly share code, notes, and snippets.

@drob
Created November 16, 2013 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drob/7494488 to your computer and use it in GitHub Desktop.
Save drob/7494488 to your computer and use it in GitHub Desktop.
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment