bradgonesurfing
bradgonesurfing

Reputation: 32192

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.

Upvotes: 16

Views: 6157

Answers (5)

DrewB
DrewB

Reputation: 3341

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

Upvotes: 5

eprothro
eprothro

Reputation: 1117

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

Upvotes: 12

DrewB
DrewB

Reputation: 3341

remvee's answer gets to the heart of what is needed but since his command caused my console to hang I thought I would write up how to do this manually.

heroku config

Look for the DATABASE_URL key. For this example lets say it is:

DATABASE_URL:            mysql2://something.example.com/stuff?reconnect=true

Add "&pool=10" to the end of the URL (use & instead of ? because the url already has a parameter)

heroku config:add DATABASE_URL=mysql2://something.example.com/stuff?reconnect=true&pool=10

Upvotes: 3

remvee
remvee

Reputation: 829

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

Upvotes: 27

bcardarella
bcardarella

Reputation: 4745

It's not very straight forward but you could try creating your own buildpack.

You'll nee to fork: https://github.com/heroku/heroku-buildpack-ruby

Then modify the following: https://github.com/heroku/heroku-buildpack-ruby/blob/master/lib/language_pack/ruby.rb#L325-387

Just add the pool size you require.

Then you can create a new Heroku app with your custom buildpack:

heroku create --stack cedar --buildpack https://github.com/yourgithubusername/heroku-buildpack-ruby.git

That should be it!

Upvotes: 2

Related Questions