How to fix PG::DuplicatePstatement: ERROR?

£可爱£侵袭症+ 提交于 2019-12-04 01:49:43

I got the same issue on rails 4.2 fixed it by

For Rails > 4.1

1) Add PG bouncer buildpack of heroku

heroku buildpacks:add https://github.com/heroku/heroku-buildpack-pgbouncer

2) Add PGBOUNCER_PREPARED_STATEMENTS to false in heroku environment variable.

there are another solution is

Another option is to use `Thread.handle_interrupt` to prevent exceptions from being raised inside critical sections 

Are you using Rack::Timeout? If a timeout exception is raised unexpectedly, the potential exists to lose state. (https://github.com/heroku/rack-timeout/blob/master/doc/risks.md).

The incrementing of the prepared statement number may be caught in a loop, creating the duplicate statement error, and can't be recovered without restarting the process. Suggest implementing https://github.com/ankane/slowpoke to kill/restart the process when a Timeout occurs.

Read more about this issue:

Solution

Add an initializer with the following code:

# gist: https://gist.github.com/catsby/0b676949b6cd414357df
# source https://github.com/gregburek/heroku-buildpack-pgbouncer#disable-prepared-statements

require "active_record/connection_adapters/postgresql_adapter"

class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
  alias initialize_without_config_boolean_coercion initialize
  def initialize(connection, logger, connection_parameters, config)        
    config = config.merge(prepared_statements: false)        
    initialize_without_config_boolean_coercion(connection, logger, connection_parameters, config)
  end
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!