10

I fixed an issue on my rails project locally (with postgres config) while adding in database.yml this statement:

test:
  prepared_statements: false

The bug that I fixed was related to this issue:

 PG::ProtocolViolation: ERROR: bind message supplies 2 parameters, but prepared            statement "a24" requires 1 

Now, I want to fix it on my production app hosted on Heroku with a postgres database. I don't know how to disable prepared statement as the database.yml is automatically generated. I tried to append:

/database?prepared_statements=false

to my database's URI but it ends up with a DATABSE_URL that is wrong and so I can't connect to my database.

What is the process and the right syntax to disable prepared_statement?

1
  • You more "worked around" the issue than actually fixed it. If there's a real bug in ActiveRecord, the Pg gem, etc, it'd be nice to have a self-contained test case to demonstrate it. (Can't really help you apply the workaround on Heroku, haven't used its app hosting much). Apr 3, 2014 at 1:26

6 Answers 6

16

As of Feb 19th 2014, heroku no longer overrides database.yml so you can turn off prepared statements in your production and staging (or default) block of the database.yml file as recommended by the latest docs:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  prepared_statements: false

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

production:
  <<: *default
  url:  <%= ENV['DATABASE_URL'] %>
  pool: <%= ENV['DB_POOL'] || ENV['MAX_THREADS'] || 5 %>

staging:
  <<: *default
  url:  <%= ENV['DATABASE_URL'] %>
  pool: <%= ENV['DB_POOL'] || ENV['MAX_THREADS'] || 5 %>
2
  • 3
    This set prepared_statements to false correctly for me. You can run ActiveRecord::Base.configurations[Rails.env] in rails console to verify it.
    – Felix
    Feb 1, 2016 at 19:51
  • 1
    Link to heroku article on how to disable prepared statements as successfully described in markquezada's answer: devcenter.heroku.com/articles/…
    – Marklar
    Aug 3, 2016 at 2:53
7

We were worried about brittleness and maintaining consistency between staging/production (using DATABASE_URL on Heroku) and development/test (using database.yml/database.example.yml).

Inspired by Rails' tests, we put this in config/initializers/disable_prepared_statements.rb:

ActiveRecord::Base.establish_connection(
  ActiveRecord::Base.remove_connection.merge(
    :prepared_statements => false
  )
)

remove_connection returns a hash of the connection parameters of the connection being removed. This should let any database.yml or DATABASE_URL continue working.

1
  • 1
    This doesn't work for me: ActiveRecord::Base.configurations does not contain prepared_statements => false
    – Nico
    Jan 25, 2016 at 7:57
3

You should just be able to add ?prepared_statements=false to the existing database url and restart your dynos. This worked for us.

heroku config:add DATABASE_URL=[old database url here]?prepared_statements=false

To check that it's set after restarting your server you can open a console and query ActiveRecord::Base.connection_config.

1

You can pass in a configuration hash to ActiveRecord::Base.establish_connection, in an initializer. For example:

configure :production, :development, :test do
  db = URI.parse(ENV['DATABASE_URL']

  ActiveRecord::Base.establish_connection(
      :adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
      :host                => db.host,
      :username            => db.user,
      :password            => db.password,
      :database            => db.path[1..-1],
      :encoding            => 'utf8',
      :prepared_statements => false,
  )
end

http://apidock.com/rails/ActiveRecord/Base/establish_connection/class

1

Recently it seems that Heroku has disabled setting DATABASE_URL using heroku config:set DATABASE_URL=<blah>?prepared_statements=false, giving us back an error " ▸ Cannot overwrite attachment values DATABASE_URL."

In order to resolve this, we added an disabled_prepared_statements.rb initializer in config/ containing:

ActiveRecord::Base.configurations[Rails.env].merge!(prepared_statements: false)

-4

turning off prepared statements decreases performance because postgresql has to replan every query before executing so i do not suggest to turn it off for production servers - especially as rails is notorious for doing many small queries when you don't take really good care about eager loading everything. I suggest indead figuring out how to do restarts in live after each deployment without affecting the availability of your service. I am not a rails guru but i know that it's doable (our company does it exactly like that). Here is a bit more insight into why this is happening https://github.com/rails/rails/issues/12330

5
  • That issue means such Jan 8, 2015 at 21:25
  • @kristok Can you offer any advice on how to do gradual deploys without running into issues with prepared statements? I don't see anything in that issue offering a solution aside from disabling them. Saying it's possible and not giving any insight isn't very helpful.
    – lobati
    Jan 20, 2015 at 23:40
  • @lobati there is no easy solution to this, thats why it has not been offered. The generic thing to google for is "rails zero downtime deployment" basically everybody is building their own complicated procedures to overcome the shortcomings of rails.
    – kristok
    Jan 21, 2015 at 22:27
  • @kristok the google results for "rails zero downtime deployment" tend to tackle how to keep your database and code compatible across two versions of code. I don't see anything that tackles how to cope with active record issues with prepared statements. I do see some stuff recommending disabling it so that the database can be scaled with pg_bouncer.
    – lobati
    Jan 21, 2015 at 22:46
  • @lobati and as long as rails migrations are as broken as they are that is the way to go unless you intend to dig deep and fix it. The worst i've seen during the 9 months i wrote rails was that the migration that had been solid for 4 years suddenly broke one day on developer boxes because rails changed the DDL generation logic between versions. After that there was really no way to have a reproducable live structure based on existing migrations so we just hacked something into migrations to have dev environments resemble live. So my best answer would be to follow those code compatibility rules
    – kristok
    Jan 22, 2015 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.