Re: Obtaining advisory lock using ORDER BY

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andreas Joseph Krogh <andreas(at)visena(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Obtaining advisory lock using ORDER BY
Date: 2015-07-13 14:22:28
Message-ID: 9986.1436797348@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Andreas Joseph Krogh <andreas(at)visena(dot)com> writes:
> 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?

No. You could possibly do SELECT...ORDER BY...FOR UPDATE in a
subquery and do the pg_try_advisory_xact_lock call in the outer query.

It might take some fooling around to get a plan that doesn't lock
more rows than necessary; EXPLAIN is your friend.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alvaro Herrera 2015-07-13 14:37:10 Re: A table of magic constants
Previous Message Andreas Joseph Krogh 2015-07-13 14:15:40 Obtaining advisory lock using ORDER BY