Re: Correct usage of FOR UPDATE?

From: Dmitriy Igrishin <dmitigr(at)gmail(dot)com>
To: Joe Carr <joe(dot)carr(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Correct usage of FOR UPDATE?
Date: 2010-12-11 10:32:10
Message-ID: AANLkTim9x6prp_Vxpzadq3KV0Pd2RhVpUSjyT8ijsqUS@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hey Joe,

You can wrap the given query in the PL/pgSQL function,
catch the exception and return NULL if it occurs. Please see
http://www.postgresql.org/docs/9.0/static/plpgsql-control-structures.html#PLPGSQL-ERROR-TRAPPING

2010/12/9 Joe Carr <joe(dot)carr(at)gmail(dot)com>

> Hello,
> I have a simple table-based queue system, and I'd looking for some advice
> on improving my dequeue function. it boils down to:
>
> SELECT id
> FROM queue
> WHERE <whereclause>
> FOR UPDATE NOWAIT;
>
> which works well, in that no item gets dequeued more that once. The issue,
> however is that when a contention occurs, the error "could not obtain lock
> on row in relation" is raised.
>
> Is there a way that I can rewrite this so that when a contention occurs,
> there is no error? I would like the "winning" process to lock the row, and
> the "losing" process to select null rather than raise the rowlock error.
>
> thanks beforehand for any help!
>

--
// Dmitriy.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Thomas Kellerer 2010-12-11 10:50:06 Re: sqlplus reporting equivalent in postgres?
Previous Message Jasen Betts 2010-12-11 10:17:58 Re: The best option to insert data with primary id