Skip to content

Instantly share code, notes, and snippets.

@hooopo
Last active March 29, 2019 14:52
Show Gist options
  • Save hooopo/1af330c6d3ecf178caf22500421de655 to your computer and use it in GitHub Desktop.
Save hooopo/1af330c6d3ecf178caf22500421de655 to your computer and use it in GitHub Desktop.
sql = <<~SQL
SELECT n.nspname || '.' || c.relname as "name"
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','')
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND n.nspname !~ '^pg_toast'
AND pg_catalog.pg_table_is_visible(c.oid)
SQL
ActiveRecord::Base.connection.execute(sql).each do |r|
ActiveRecord::Base.connection.execute("vacuum analyze #{r['name']}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment