Re: Row Locking? - Mailing list pgsql-sql

From Richard Huxton
Subject Re: Row Locking?
Date
Msg-id 200210221251.47781.dev@archonet.com
Whole thread Raw
In response to Row Locking?  (eric soroos <eric-psql@soroos.net>)
List pgsql-sql
On Monday 21 Oct 2002 11:09 pm, eric soroos wrote:
> I have a long running process that performs outside actions on the content
> of a table. The actions could all be done in parallel (if I had n
> processors), but I need to ensure that the process is attempted exactly one
> time per applicable row.

> Extending this to multiple threads if proving problematic. No locking leads
> to a race condition between the select and update. If I change the select
> to a SELECT ... FOR UPDATE it apparently locks the table against all other
> select for updates, then when the update is committed, the second thread
> returns nothing, even when there are other rows in the table that could be
> returned.
>
> Is there a single row locking against select?  Or can I effeciently do the
> equivalent of update set pending, then select the row that I just updated
> to get the contents? (perhaps without doing a table scan to find the oid of
> the row that I just updated). I can't afford to lock the entire table.

I think the issue is that both threads are hanging about for the same row. If
the first thread rolled back rather than committing, the second thread might
return the same row. You've only locked the one row, but that's the one row
everyone wants.

How about something like:

1. SELECT FOR UPDATE...
2. IF no id returned, sleep, goto 1
3. UPDATE foo set pending='t' ...

A different solution might be to have one thread allocating rows to the
others. So - make "pending" something that holds a thread id. That way the
processing threads just sit there trying to fetch the first allocated row and
sleep if none available. Alternatively, the allocating thread could pass the
info directly to the processing threads.

Might be worth checking the archives for this list and pgsql-general - someone
had a similar question a few weeks/months ago.

--  Richard Huxton


pgsql-sql by date:

Previous
From: Ludwig Lim
Date:
Subject: Re: Locking that will delayed a SELECT
Next
From: Vernon Wu
Date:
Subject: Re: Fwd: Re: Can I search for an array in csf?