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



---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/users-lounge/docs/faq.html

Reply via email to