Skip to content

Instantly share code, notes, and snippets.

@benoittgt
Last active June 26, 2023 16:22
Show Gist options
  • Save benoittgt/a8e21f74e28378c9ac894b29bc24d3ca to your computer and use it in GitHub Desktop.
Save benoittgt/a8e21f74e28378c9ac894b29bc24d3ca to your computer and use it in GitHub Desktop.
PSQL table in Rails console
results = ActiveRecord::Base.connection.execute("SELECT * FROM pg_statio_user_tables;").to_a
@columns = results.first.keys.each_with_object({}) do |col,h|
h[col] = { col: col, width: [results.map { |g| g[col]&.size || 2 }.max, col.size].max }
end
def write_line(h)
str = h.keys.map { |k| h[k].to_s.ljust(@columns[k][:width]) }.join(" | ")
puts "| #{str} |"
end
def write_header
puts "| #{ @columns.map { |_,g| g[:col].ljust(g[:width]) }.join(' | ') } |"
end
write_header
results.each{|a| write_line(a) }; nil
=begin
| relid | schemaname | relname | heap_blks_read | heap_blks_hit | idx_blks_read | idx_blks_hit | toast_blks_read | toast_blks_hit | tidx_blks_read | tidx_blks_hit |
| 16419 | public | schema_migrations | 101 | 68 | 67 | 62 | 0 | 0 | 1 | 0 |
# more raws
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment