Re: easy task: concurrent select-updates - Mailing list pgsql-general

From Nickolay
Subject Re: easy task: concurrent select-updates
Date
Msg-id 4AA09A87.1050201@zhukcity.ru
Whole thread Raw
In response to Re: easy task: concurrent select-updates  (Kevin McConnell <kevin.mcconnell@gmail.com>)
Responses Re: easy task: concurrent select-updates
List 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 =alse order by id loop
>>               update msg set busy =rue 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 =rue where id = (select min(id) from msg where
> busy =alse) returning *;
>
> Cheers,
> Kevin
>

Thank you guys! But what's min(id) for? Is it neccessary? Is there any
chance I can replace min(id) to LIMIT 1?

Best regards, Nick.

pgsql-general by date:

Previous
From: Juan Backson
Date:
Subject: N + 1 replication
Next
From: 纪晓曦
Date:
Subject: Moving avg using SQL