Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id A55D0475DBD for ; Sun, 19 Jan 2003 17:03:52 -0500 (EST) Received: from horkos.telenet-ops.be (horkos.telenet-ops.be [195.130.132.45]) by postgresql.org (Postfix) with ESMTP id 23C864758C9 for ; Sun, 19 Jan 2003 17:03:52 -0500 (EST) Received: from localhost (localhost.localdomain [127.0.0.1]) by horkos.telenet-ops.be (Postfix) with SMTP id 6B034853D1; Sun, 19 Jan 2003 23:03:53 +0100 (CET) Received: from kabel.telenet.be (D5775F15.kabel.telenet.be [213.119.95.21]) by horkos.telenet-ops.be (Postfix) with ESMTP id 6E4FA84E9F; Sun, 19 Jan 2003 23:03:49 +0100 (CET) Received: by kabel.telenet.be (Postfix, from userid 501) id D26DA26132; Sun, 19 Jan 2003 23:03:48 +0100 (CET) Date: Sun, 19 Jan 2003 23:03:48 +0100 From: Kurt Roeckx To: Josh Berkus Cc: pgsql-sql@postgresql.org Subject: Re: Selecting a non-locked row. Message-ID: <20030119220348.GA4944@ping.be> References: <20030119133320.GA3984@ping.be> <200301191326.10285.josh@agliodbs.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200301191326.10285.josh@agliodbs.com> User-Agent: Mutt/1.4i X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/288 X-Sequence-Number: 11153 On Sun, Jan 19, 2003 at 01:26:10PM -0800, Josh Berkus wrote: > Kurt, > > > I have a table with records in, and they either still need to be > > processed or not. Several people will be using the table at the > > same time. > > > > I was thinking about using a select .... for update limit 1, but > > the other persons will of course try to select the same record > > and wait until that one is released. The locks are held for a > > long period of time. > > > > Is there a way to select a row that is not locked yet? > > I'm a bit confused by your question. Could you try explaining it another way, > possibly with SQL code examples? I have data in the table that should only be used once. Several people will be using the table at the same time to get a new record out of it. After they're done with it the record gets marked as done. I have a query that looks something like: begin; select id, data from table where used IS NULL order by id for update limit 1; And after some time: update table set used = 1 where id = id; commit; Of course a second person doing the same thing will just wait for my commit. What I want is that he just gets the next non-locked record. Kurt