def with_synchronous_commit_off(&block)
ActiveRecord::Base.connection.exec_query("SET synchronous_commit = off")
yield
ensure
ActiveRecord::Base.connection.exec_query("SET synchronous_commit = on")
end
I'm not a Rails expert: does this reliably give you the same connection from the pool? If not, you could be setting `synchronous_commit = off` on one "physical" connection, and clearing it on another. This is setting it at the Postgres session level, so it will persist for the lifetime of the connection. If it's not managed reliably, it could lead to some very confusing bugs (only triggered in a very small window when Postgres shuts down unexpectedly while the transaction is still unsynced to durable storage, making it extra fun to debug).In general, though, this is a very handy feature.