jruby/activerecord-jdbc-adapter

PostgreSQL adapter sets invalid value "panic" for client_min_messages

bblack opened this issue · 1 comments

This has been a problem for us from 50.0 all the way up to 52.7. I see from the source in 60.x this may not be an issue anymore.

The postgresql docs (for pg versions that support client_min_messages, i.e. 10 through 13) specify the valid values for client_min_messages:

Valid values are DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1, LOG, NOTICE, WARNING, and ERROR.

Yet, the postgres adapter here (up to 52.7 at least) tries setting it to "panic":

# Enable standard-conforming strings if available.
def standard_conforming_strings=(enable)
client_min_messages = self.client_min_messages
begin
self.client_min_messages = 'panic'
value = enable ? "on" : "off"
execute("SET standard_conforming_strings = #{value}", 'SCHEMA')
@standard_conforming_strings = ( value == "on" )
rescue
@standard_conforming_strings = :unsupported
ensure
self.client_min_messages = client_min_messages
end
end

def standard_conforming_strings?
if @standard_conforming_strings.nil?
client_min_messages = self.client_min_messages
begin
self.client_min_messages = 'panic'
value = select_one('SHOW standard_conforming_strings', 'SCHEMA')['standard_conforming_strings']
@standard_conforming_strings = ( value == "on" )
rescue
@standard_conforming_strings = :unsupported
ensure
self.client_min_messages = client_min_messages
end
end
@standard_conforming_strings == true # return false if :unsupported
end

In our app this didn't actually present a problem until we upgraded postgres to either 11 or 12 (we're on 12 now), so perhaps "panic" actually was an acceptable value in a prior version, despite the docs. In any case, it's not an acceptable value now.

The effect is that trying to set it to "panic" raises an error, which is rescued, and @standard_conforming_strings is incorrectly set to :unsupported, which causes problems later when our app constructs properly-escaped queries.

We've worked around it by monkeypatching these functions to remove the extra bits:

module ArJdbc
  module PostgreSQL
    def standard_conforming_strings=(enable)
      value = enable ? "on" : "off"
      execute("SET standard_conforming_strings = #{value}", 'SCHEMA')
      @standard_conforming_strings = ( value == "on" )
    end

    def standard_conforming_strings?
      value = select_one('SHOW standard_conforming_strings', 'SCHEMA')['standard_conforming_strings']
      @standard_conforming_strings = ( value == "on" )
    end
  end
end

As I said, it looks like this may be fixed in 60.x. Can this be backported to 52.x (or further)?

This has been a problem for us from 50.0 all the way up to 52.7. I see from the source in 60.x this may not be an issue anymore.

Actually, this was a problem before 50.x when we were on Rails 4 too.