Skip to content

Instantly share code, notes, and snippets.

@Azdaroth
Created March 13, 2024 20:48
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 Azdaroth/1c4f6f5f1988f92413c7c7296c40da17 to your computer and use it in GitHub Desktop.
Save Azdaroth/1c4f6f5f1988f92413c7c7296c40da17 to your computer and use it in GitHub Desktop.
query = "SELECT table_name, column_name FROM information_schema.columns WHERE data_type = 'jsonb';"
result = ActiveRecord::Base.connection.execute(query)
jsonb_aggregate = result.values.to_h.map do |table, column|
jsonb_result = ActiveRecord::Base.connection.execute("SELECT MAX(pg_column_size(#{column})) FROM #{table};").values
if jsonb_result.first
puts "done #{table}, column: #{column}"
{ size: jsonb_result.first, table: table, column: column }
end
end.compact
query = "SELECT table_name, column_name FROM information_schema.columns WHERE data_type = 'json';"
result = ActiveRecord::Base.connection.execute(query)
json_aggregate = result.values.to_h.map do |table, column|
json_result = ActiveRecord::Base.connection.execute("SELECT MAX(pg_column_size(#{column})) FROM #{table};").values
if json_result.first
puts "done #{table}, column: #{column}"
{ size: json_result.first, table: table, column: column }
end
end.compact
query = "SELECT table_name, column_name FROM information_schema.columns WHERE data_type = 'text';"
result = ActiveRecord::Base.connection.execute(query)
text_aggregate = result.values.to_h.map do |table, column|
next if table.start_with?("pg_")
text_result = ActiveRecord::Base.connection.execute("SELECT MAX(pg_column_size(#{column})) FROM #{table};").values
if text_result.first
puts "done #{table}, column: #{column}"
{ size: text_result.first, table: table, column: column }
end
end.compact
jsonb_aggregate.max_by { |h| h[:size].first.to_i }[:size].first
json_aggregate.max_by { |h| h[:size].first.to_i }[:size].first
text_aggregate.max_by { |h| h[:size].first.to_i }[:size].first
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment