Row Locking?

From: eric soroos <eric-psql(at)soroos(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: Row Locking?
Date: 2002-10-21 23:09:35
Message-ID: 38241230.1176905921@[4.42.179.151]
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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.

My current design for one thread is the following (simplified to a test case)

create table foo (pending boolean, done boolean, idx serial);

select * from foo where pending='f' and done='f' limit 1;
update foo set pending='t' where idx=[[returned idx]];
commit;

do stuff outside database

update foo set pending='f', done='t' where idx=[[returned idx]];
commit;

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.

eric

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Vernon Wu 2002-10-22 02:55:39 Fwd: Re: Can I search for an array in csf?
Previous Message Josh Berkus 2002-10-21 22:50:23 Re: Delete/Replace Bug in Functions?