title: Rails 3.2 frequent postgres prepared statement already exists errors
body: <p>I've been digging around stackoverflow trying to find others who get these prepared statements already exists errors. </p>
  
  <p>In most cases configuring unicorn properly with the after/before fork resolves these issues.</p>
  
  <p>However in my case we are still getting errors as such:</p>
  
  <pre><code>ActiveRecord::StatementInvalid: PG::Error: ERROR: prepared statement "a495" already exists: INSERT INTO "user_logins" ("account_id", "created_at", "ip_address", "user_agent", "user_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id"
  </code></pre>
  
  <p>This error gets thrown in different areas in our app but always seems to have the same statement number 'a495'.</p>
  
  <p>We are on rails 3.2.17, using postgres and we are on heroku.</p>
  
  <p>I really have no idea why this is happening, but its starting to happen more frequently now. </p>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>In the rails stack trace this error is being thrown in the .prepare call. I'm confused because its checking for the sql key in the statements collection. If it doesn't exist it prepares the new one....however when trying to prepare it, its throwing the error. </p>
  
  <pre><code>def prepare_statement(sql)
    sql_key = sql_key(sql)
    unless @statements.key? sql_key
      nextkey = @statements.next_key
      @connection.prepare nextkey, sql
      @statements[sql_key] = nextkey
    end
    @statements[sql_key]
  end
  </code></pre>