Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

PostgreSQL adapter should call thread safe quote_string function

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information...
commit c5c022c705e4179f74e063186ea68a1c58797822 1 parent d970733
@libc libc authored NZKoz committed
View
16 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -287,7 +287,13 @@ def table_alias_length
# Escapes binary strings for bytea input to the database.
def escape_bytea(value)
- if PGconn.respond_to?(:escape_bytea)
+ if @connection.respond_to?(:escape_bytea)
+ self.class.instance_eval do
+ define_method(:escape_bytea) do |value|
+ @connection.escape_bytea(value) if value
+ end
+ end
+ elsif PGconn.respond_to?(:escape_bytea)
self.class.instance_eval do
define_method(:escape_bytea) do |value|
PGconn.escape_bytea(value) if value
@@ -376,7 +382,13 @@ def quote(value, column = nil) #:nodoc:
# Quotes strings for use in SQL input in the postgres driver for better performance.
def quote_string(s) #:nodoc:
- if PGconn.respond_to?(:escape)
+ if @connection.respond_to?(:escape)
+ self.class.instance_eval do
+ define_method(:quote_string) do |s|
+ @connection.escape(s)
+ end
+ end
+ elsif PGconn.respond_to?(:escape)
self.class.instance_eval do
define_method(:quote_string) do |s|
PGconn.escape(s)

4 comments on commit c5c022c

@adacosta

Can someone please explain to me how this provides thread safety? I assume I don't grasp the solution.

@adacosta

Ah, I see the pg gem (in pg.c) notes,

  • Connection instance method for versions of 8.1 and higher of libpq
  • uses PQescapeStringConn, which is safer. Avoid calling as a class method,
  • the class method uses the deprecated PQescapeString() API function.
@adacosta

eh, both PQescapeStringConn and PQescapeString call on PQescapeStringInternal (in libpq) to do their dirty work, which wouldn't help much if any. Maybe we shouldn't call this "thread safe" if it's only "safer." Any comments?

@maxlapshin

This change was made because of some mysterious bugs, that were cured with it. I'll ask the author of patch to comment it.

Please sign in to comment.
Something went wrong with that request. Please try again.