Skip to content

Instantly share code, notes, and snippets.

@vparihar01
Last active August 29, 2015 14:06
Show Gist options
  • Save vparihar01/a32864831d248251aafd to your computer and use it in GitHub Desktop.
Save vparihar01/a32864831d248251aafd to your computer and use it in GitHub Desktop.
Ruby script to get the postgres database tables sizes and store them into the hash.
sql = "SELECT tablename FROM pg_tables WHERE schemaname = 'public' ORDER BY tablename;"
tables = ActiveRecord::Base.connection.execute(sql)
tables_size={}
tables.each do |table|
name = table['tablename']
sql = "SELECT pg_size_pretty(pg_total_relation_size('#{name}'));"
res = ActiveRecord::Base.connection.execute(sql)
puts "#{name} #{res[0]['pg_size_pretty']}"
tables_size["#{name}"] = res[0]['pg_size_pretty']
puts tables_size
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment