Postgres, concurrency, and rails

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?

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:

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

On Thu, Aug 19, 2010 at 3:01 PM, Zach B. [email protected] 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 | gregdonald.com

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.

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.