Re: easy task: concurrent select-updates

From: Andy Colson <andy(at)squeakycode(dot)net>
To: Kevin McConnell <kevin(dot)mcconnell(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: easy task: concurrent select-updates
Date: 2009-09-03 19:16:21
Message-ID: 4AA01605.80802@squeakycode.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Kevin McConnell wrote:
>> CREATE OR REPLACE FUNCTION public.getmsg() RETURNS integer LANGUAGE plpgsql
>> AS $function$
>> declare
>> rec record;
>> begin
>> for rec in select id from msg where busy = false order by id loop
>> update msg set busy = true where id = rec.id and busy = false;
>> if found then
>> return rec.id;
>> end if;
>> end loop;
>> return -1;
>> end;
>> $function$
>
> I think you could also do something roughly similar in a statement by
> using a RETURNING clause on the update, such as:
>
> update msg set busy = true where id = (select min(id) from msg where
> busy = false) returning *;
>
> Cheers,
> Kevin
>

I had thought of that, but you'd need to add one thing, in the update ' and busy = false ', cuz two people may get the same id from the select min(id).

update msg set busy = true where busy = false and id = (select min(id) from msg where busy = false) returning *;

but then you'd have to fire it over-and-over until you actually got a row updated.

Seemed easer to put the loop in function, then you can:

select id from getmsg();

-Andy

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Josef Wolf 2009-09-03 19:20:02 Re: pg_ctl with unix domain socket?
Previous Message Steve Atkins 2009-09-03 19:10:59 Re: PL/Perl 64-bit and sending emails