Obtaining advisory lock using ORDER BY

From: Andreas Joseph Krogh <andreas(at)visena(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Obtaining advisory lock using ORDER BY
Date: 2015-07-13 14:15:40
Message-ID: VisenaEmail.d6.3439608014f9803b.14e87beacbf@tc7-visena
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi all.
 
I have this "dequeue" query which is working:
 
select qe.entity_id, qe.queue_id, qe.sequence_id, qe.tx_id , qe.payload_string
fromorigo_queue_entry qe WHERE qe.queue_id = 2 AND pg_try_advisory_xact_lock(
sequence_id) LIMIT 1 FOR UPDATE ;
I'm not sure is this is guaranteed to lock in ASC-order on column sequence_id,
is it?
 
To ensure this I've tried with an explicit ORDER BY on "sequence_id", like
this:
 
select qe.entity_id, qe.queue_id, qe.sequence_id, qe.tx_id , qe.payload_string
fromorigo_queue_entry qe WHERE qe.queue_id = 2 AND pg_try_advisory_xact_lock(
sequence_id) ORDER BY qe.sequence_id ASC LIMIT 1 FOR UPDATE ;

But the latter query results in all non-locked rows being locked (but it
returns only 1 row due to LIMIT 1), but I'd like the "lowest" non-loced one.
 
Is there a way to make the locking work on an custom ordered set, preserving
the "LIMIT 1"?
 
Thanks.
 
-- Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
andreas(at)visena(dot)com <mailto:andreas(at)visena(dot)com>
www.visena.com <https://www.visena.com>
<https://www.visena.com>

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2015-07-13 14:22:28 Re: Obtaining advisory lock using ORDER BY
Previous Message Raymond O'Donnell 2015-07-13 14:09:32 Re: timestamp check