Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase ActiveRecord thread pool size on heroku

Normally I would set the pool size as

development:
  adapter: postgresql
  encoding: unicode
  database: openkitchen_development
  username: rails
  host: localhost
  pool: 10
  password:

in database.yml. However heroku replaces the config file. I'm using girl_friday to do background db work and need to increase the thread pool size.

like image 935
bradgonesurfing Avatar asked Apr 06 '12 15:04

bradgonesurfing


3 Answers

Simply add a pool query parameter to the DATABASE_URL in your heroku config. To set the pool size to 15 in your heroku app use something like:

heroku config -s | awk '/^DATABASE_URL=/{print $0 "?pool=15"}' | xargs heroku config:add

like image 109
remvee Avatar answered Oct 19 '22 14:10

remvee


For what it's worth, using the URL params method as described in other answers here is not recommended by Heroku. They reserve the right to reset or change this URL at any time, and long term this behavior will likely be removed for the Rails build behavior, anyway.

Setting additional parameters via an after-initialize application callback is the recommended way to modify the configuration of your heroku-postgresql databases per this dev center article.

In config/initializers/database_connection.rb:

Rails.application.config.after_initialize do
  ActiveRecord::Base.connection_pool.disconnect!

  ActiveSupport.on_load(:active_record) do
    config = Rails.application.config.database_configuration[Rails.env]
    config['pool']              = 10
    ActiveRecord::Base.establish_connection(config)
  end
end
like image 20
eprothro Avatar answered Oct 19 '22 13:10

eprothro


Heroku now has a nice article on managing pool sizes - https://devcenter.heroku.com/articles/concurrency-and-database-connections#connection-pool

like image 43
DrewB Avatar answered Oct 19 '22 13:10

DrewB