Is pg gem thread-safe?

663 views
Skip to first unread message

thehap...@gmail.com

unread,
Jun 30, 2013, 6:16:29 AM6/30/13
to rub...@googlegroups.com
Is pg gem thread-safe?

Aaron Patterson

unread,
Jul 1, 2013, 7:59:56 PM7/1/13
to rub...@googlegroups.com
On Sun, Jun 30, 2013 at 03:16:29AM -0700, thehap...@gmail.com wrote:
> Is pg gem thread-safe?

It depends on which methods you use. Can you be more specific?

There is no mutex around the underlying file descriptor for the socket,
if that is what you mean.

--
Aaron Patterson
http://tenderlovemaking.com/

Lars Kanis

unread,
Jul 2, 2013, 2:19:53 AM7/2/13
to rub...@googlegroups.com
On Sun, Jun 30, 2013 at 03:16:29AM -0700, thehap...@gmail.com wrote:
> Is pg gem thread-safe?

You can safely use different connections in different threads, if PG.threadsafe? returns true (this is the case on all modern OS). But you might not issue concurrent commands from different threads on the same PG::Connection object. Essentially the same rules as for libpq applies to ruby-pg, too: http://www.postgresql.org/docs/9.2/static/libpq-threading.html

You could build a mutex around the connection object or ensure to establish a new connection for each thread. The latter is what ActiveRecord does. So ActiveRecord::Base.connection.raw_connection gives you a separate PG::Connection object for each thread.

Regards,
Lars
Reply all
Reply to author
Forward
0 new messages