# Postgres, concurrency, and rails

**URL:** <https://www.ruby-forum.com/t/postgres-concurrency-and-rails/193560>\
**Category:** Rails\
**Created:** [August 19, 2010, 5:55pm UTC](https://www.ruby-forum.com/t/postgres-concurrency-and-rails/193560 "2010-08-19T17:55:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zbrod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/278dde/32.png) [@zbrod](https://www.ruby-forum.com/u/zbrod)\
**Post date:** [August 19, 2010, 5:55pm UTC](https://www.ruby-forum.com/t/postgres-concurrency-and-rails/193560/1 "2010-08-19T17:55:49Z")

</div>

I work on a fairly large website that uses Ruby 1.8.6 and Rails 1.2.3.  
We cannot upgrade as it will hopelessly break our code. We are working  
on a new version in Java but for the next year at least we are stuck  
with the legacy system. We use the latest version of Postgresql for the  
database using the postgres-pr gem.

I am attempting to implement cron tasks within the system using Rufus  
Scheduler, which is sort of a lighter-weight version of BackgrounDRb. I  
found very quickly that if I have 2 tasks (threads) running and they  
both attempt to use ActiveRecord, they deadlock. Supposedly ActiveRecord  
has a means of enabling concurrency, however the postgres-pr adapter  
does not support this.

I’ve been trying without luck to install one of the alternative postgres  
adapters that supposedly support concurrency. The ‘pg’ one appears to be  
the latest and official gem, and it installs fine. However, when I  
attempt to access a page, I get: “no such file to load – postgres”. I  
have scoured Google for this error string, but the results have not been  
helpful.

So my first question is: what’s the deal with this?  
Also: How do you guys use threads in your Rails apps?

By the way, I am developing on a Windows box, though our production  
systems are FreeBSD.

Thanks

If you want to continue reading, I have more random notes:

I did attempt to install some of the deprecated gems, with no success.  
The old postgres gem and ruby-pg fail when trying to build native  
extensions, even when I point them to the postgres install dir (I  
probably don’t have the necessary dev tools on my workstation).  
Ruby-postgres (the older version of pg) installs find but doesn’t play  
nicely with the latest version of postgresql, throwing operating system  
errors. Really all of this should matter though as the pg gem is the one  
I should be using.

Is there a way to get concurrency working with postgres-pr, or does  
using that adapter mean you just can’t use threads? Seems pretty piss  
poor, but typical I guess.

Even if I do get the adapter working, do I have to worry about deadlock  
in other parts of Rails besides ActiveRecord?

---

<div class="post-metadata">

**Author:** ![zbrod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/278dde/32.png) [@zbrod](https://www.ruby-forum.com/u/zbrod)\
**Post date:** [August 19, 2010, 10:01pm UTC](https://www.ruby-forum.com/t/postgres-concurrency-and-rails/193560/2 "2010-08-19T22:01:29Z")

</div>

After some more epic Googling (literally like 10+ hours of just slogging  
through crap) I’ve gotten a little further in my search, for anyone who  
is having similar problems.

It seems that the supposedly ‘latest’ Postgres adapter, pg, doesn’t work  
with Rails at all, or at least version 1.2.3. You need to use  
ruby-postgres.

You will have some issues with some DLL files because Postgres 8.4 uses  
a newer version of SSL or some sort. It is all explained here:

> **[Getting PostgreSQL to install on Windows](https://rubyonrailswin.wordpress.com/2010/03/12/getting-postgresql-to-install-on-windows/)**
>
> In the effort to migrate my existing Rails app from Slicehost to Heroku, I encountered some MySQL – PostgreSQL compatibility issues, I’ll write a separate post on Heroku gotchas later. …

If anyone has any more insight into the concurrency thing, I would still  
love feedback. Thanks.

---

<div class="post-metadata">

**Author:** ![zbrod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/278dde/32.png) [@zbrod](https://www.ruby-forum.com/u/zbrod)\
**Post date:** [August 19, 2010, 10:23pm UTC](https://www.ruby-forum.com/t/postgres-concurrency-and-rails/193560/3 "2010-08-19T22:23:22Z")

</div>

On Thu, Aug 19, 2010 at 3:01 PM, Zach B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It seems that the supposedly ‘latest’ Postgres adapter, pg, doesn’t work  
> with Rails at all, or at least version 1.2.3. You need to use  
> ruby-postgres.

I am using pg (0.9.0) with Rails 3.0-rc and it works fine. I did have  
to uninstall all other Postgres gems for it to work however.

–  
Greg D.  
[destiney.com](http://destiney.com) | [gregdonald.com](http://gregdonald.com)

---

<div class="post-metadata">

**Author:** ![zbrod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/278dde/32.png) [@zbrod](https://www.ruby-forum.com/u/zbrod)\
**Post date:** [August 20, 2010, 9:50am UTC](https://www.ruby-forum.com/t/postgres-concurrency-and-rails/193560/4 "2010-08-20T09:50:13Z")

</div>

Zach B. wrote:

> It must be the ancient version of Rails we are using then. The  
> postgresql\_adapter requires a gem (whatever version, postgres-pr,  
> ruby-postgres, etc) that calls itself ‘postgres’, and I imagine pg only  
> responds to ‘pg’. Perhaps Rails 3.0 requires either postgres OR pg. It  
> might be possible I could modify the adapter but that is probably asking  
> for trouble.

I remember the pain in finding the correct postgresql ruby gem. Indeed  
the latest gem is called “pg”, however I can’t remember it was  
compatible at the time of Rails 1.2 but I guess so. Maybe use an older  
“pg” gem as Rails has been modified a lot since and so was pg.

---

<div class="post-metadata">

**Author:** ![zbrod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/278dde/32.png) [@zbrod](https://www.ruby-forum.com/u/zbrod)\
**Post date:** [August 19, 2010, 10:34pm UTC](https://www.ruby-forum.com/t/postgres-concurrency-and-rails/193560/5 "2010-08-19T22:34:50Z")

</div>

It must be the ancient version of Rails we are using then. The  
postgresql\_adapter requires a gem (whatever version, postgres-pr,  
ruby-postgres, etc) that calls itself ‘postgres’, and I imagine pg only  
responds to ‘pg’. Perhaps Rails 3.0 requires either postgres OR pg. It  
might be possible I could modify the adapter but that is probably asking  
for trouble.

Either way though, ruby-postgres seems to work fine so far. It no longer  
deadlocks. However, calling allow\_concurrency = true on ActiveRecord  
seems to cause the server to hang. After I commented it out, however,  
things went fine and there was no deadlock. I do get messages from  
postgres such as “warning: transaction in progress”, which I’m guessing  
is the result of my threads hitting the same table at the same time (I  
did this on purpose to test). I’m guessing that allow\_concurrency is  
supposed to prevent this by making ActiveRecord queue up these  
transactions, but like I said it seems to make the server hang. So the  
question really is, can postgres handle these overlapping transactions?  
It seems to do a good job queueing them up so far, but I guess we’ll  
have to see.

Again, any insight is appreciated.
