Re: Row Locking?

From: Richard Huxton <dev(at)archonet(dot)com>
To: eric soroos <eric-psql(at)soroos(dot)net>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Row Locking?
Date: 2002-10-22 12:51:47
Message-ID: 200210221251.47781.dev@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Nicholas Barthelemy 2002-10-22 15:25:22 7.2 time format funtion issue
Previous Message Vernon Wu 2002-10-22 11:07:51 Re: Can I search for an array in csf?