Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 8C0D3641DB7 for ; Tue, 20 Jul 2010 23:05:41 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 84697-03 for ; Wed, 21 Jul 2010 02:05:32 +0000 (UTC) X-Greylist: delayed 05:38:11.514536 by SQLgrey-1.7.6 Received: from out01.mta.xmission.com (out01.mta.xmission.com [166.70.13.231]) by mail.postgresql.org (Postfix) with ESMTP id D992E641574 for ; Tue, 20 Jul 2010 23:05:33 -0300 (ADT) Received: from in02.mta.xmission.com ([166.70.13.52]) by out01.mta.xmission.com with esmtps (TLSv1:AES256-SHA:256) (Exim 4.69) (envelope-from ) id 1ObJPJ-0000sk-U1; Tue, 20 Jul 2010 14:27:17 -0600 Received: from [208.187.158.94] (helo=[172.16.1.223]) by in02.mta.xmission.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.69) (envelope-from ) id 1ObJPJ-0003Ym-PM; Tue, 20 Jul 2010 14:27:17 -0600 Message-ID: <4C46080A.4030904@gmail.com> Date: Tue, 20 Jul 2010 14:33:14 -0600 From: Rob Sargent User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.10) Gecko/20100520 SUSE/3.0.5 Lightning/1.0b1 Thunderbird/3.0.5 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: Problem with pg_try_advisory_lock and two connections (seemingly) getting the same lock References: In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-XM-SPF: eid=; ; ; mid=; ; ; hst=in02.mta.xmission.com; ; ; ip=208.187.158.94; ; ; frm=robjsargent@gmail.com; ; ; spf=neutral X-SA-Exim-Connect-IP: 208.187.158.94 X-SA-Exim-Mail-From: robjsargent@gmail.com X-SA-Exim-Scanned: No (on in02.mta.xmission.com); SAEximRunCond expanded to false X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.2 tagged_above=-5 required=5 tests=BAYES_05=-0.5, RCVD_IN_DNSWL_LOW=-0.7 X-Spam-Level: X-Archive-Number: 201007/79 X-Sequence-Number: 34392 You could implement an optimistic lock strategy by placing a 'version' column in the table and increment it on successful 'check-out' and test against the value the user has as he/she tried to act on the record. If some else got there first the second user fails to check-out the queue item. Triggers could do the work. On 07/19/2010 08:06 PM, Brett Hoerner wrote: > Hi, > > I currently have a simple queue written ontop of Postgres. Jobs are > inserted and workers periodically check for jobs they can do, do them, > and then delete the rows. pg_try_advisory_lock is used to (attempt > to) stop two workers from doing the same job. > > (I'm working on moving to a "real" messaging queue right now, this is > more a point of curiosity and education now.) > > Here is my queue table, > > CREATE TABLE queue ( > id serial NOT NULL PRIMARY KEY, > rcvd timestamp with time zone, > sent timestamp with time zone, > host character varying(32), > job character varying(32), > arg text > ); > > Here is an example query, > > SELECT q.* > FROM (SELECT id, job, arg > FROM queue > WHERE job = 'foo' OR job = 'bar' > OFFSET 0) AS q > WHERE pg_try_advisory_lock(1, q.id) > LIMIT 10 > > (For information on OFFSET 0 see: > http://blog.endpoint.com/2009/04/offset-0-ftw.html) > > Now if I have two workers running I will periodically see that each > worker gets a row with the same q.id (and thus does the work). How is > that possible? The outer query seemingly does a WHERE on an > advisory_lock. > > Does anyone have any ideas? Am I grossly misusing advisory_locks? > > Thanks, > Brett >