Skip to content

Instantly share code, notes, and snippets.

@merqlove
Created January 9, 2015 13:55
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save merqlove/48b372f3a9602a842544 to your computer and use it in GitHub Desktop.
Save merqlove/48b372f3a9602a842544 to your computer and use it in GitHub Desktop.
PostgreSQL optimization tasks for ActiveRecord
namespace :optimization do
desc "Provide DB vacuum for production environment"
task :vacuum => :environment do
begin
tables = ActiveRecord::Base.connection.tables
tables.each do |table|
ActiveRecord::Base.connection.execute("VACUUM FULL ANALYZE #{table};")
end
rescue Exception => exc
Rails.logger.error("Database VACUUM error: #{exc.message}")
end
end
desc "Provide DB reindex for production environment"
task :reindex => :environment do
begin
tables = ActiveRecord::Base.connection.tables
tables.each do |table|
ActiveRecord::Base.connection.execute("REINDEX TABLE #{table};")
end
rescue Exception => exc
Rails.logger.error("Database REINDEX error: #{exc.message}")
end
end
desc "Reset PRIMARY KEY sequences "
task :resetpk => :environment do
tables = ActiveRecord::Base.connection.tables
tables.each do |table|
ActiveRecord::Base.connection.reset_pk_sequence!(table)
end
end
end
@guilhermegazzinelli
Copy link

Great!!! Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment