Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id C356D476241 for ; Mon, 21 Oct 2002 19:09:37 -0400 (EDT) Received: from l2.socialecology.com (unknown [4.42.179.131]) by postgresql.org (Postfix) with ESMTP id 418B3475FD9 for ; Mon, 21 Oct 2002 19:09:37 -0400 (EDT) Received: from 4.42.179.151 (broccoli.socialecology.com [4.42.179.151]) by l2.socialecology.com (Postfix) with SMTP id 6265A1F2A35 for ; Mon, 21 Oct 2002 16:09:39 -0700 (PDT) X-Mailer: UserLand Frontier 8.0.5 (Macintosh OS) (mailServer v1.1..142) Mime-Version: 1.0 Message-Id: <38241230.1176905921@[4.42.179.151]> X-authenticated-sender: erics X-GSgroup: private Date: Mon, 21 Oct 2002 16:09:35 -0700 To: pgsql-sql@postgresql.org From: eric soroos Subject: Row Locking? Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200210/266 X-Sequence-Number: 9983 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