Last active
August 29, 2015 14:06
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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