Skip to content

Instantly share code, notes, and snippets.

@hannesstruss
Created February 12, 2013 10:22
Show Gist options
  • Save hannesstruss/4761410 to your computer and use it in GitHub Desktop.
Save hannesstruss/4761410 to your computer and use it in GitHub Desktop.
get a list of postgres table and index sizes
DBNAME=
QUERY="select tablename from pg_catalog.pg_tables where tablename not like 'sql_%' and tablename not like 'pg_%';"
for table in `psql $DBNAME -t -c "$QUERY"`; do
TABLE_QUERY="select pg_size_pretty(pg_relation_size('$table'));"
TABLE_SIZE=`psql $DBNAME -t -c "$TABLE_QUERY"`
TOTAL_QUERY="select pg_total_relation_size('$table');"
TOTAL=`psql $DBNAME -t -c "$TOTAL_QUERY"`
INDEX_QUERY="select pg_size_pretty(pg_total_relation_size('$table') - pg_relation_size('$table'));"
INDEX=`psql $DBNAME -t -c "$INDEX_QUERY"`
echo $TOTAL $table index: $INDEX table: $TABLE_SIZE
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment