Re: easy task: concurrent select-updates

From: Nickolay <nitro(at)zhukcity(dot)ru>
To: Andy Colson <andy(at)squeakycode(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: easy task: concurrent select-updates
Date: 2009-09-04 08:26:35
Message-ID: 4AA0CF3B.3010500@zhukcity.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Andy Colson wrote:
> Kevin McConnell wrote:
>> 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 *;
>>
>
> 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();
>

Thanks a lot for your solution! It works great for now.
Here is the thing I did following your advice:

CREATE TYPE queued_msg_row AS
(id bigint
,sender character varying
,"text" text
...
,msg_type integer);

CREATE OR REPLACE FUNCTION public.get_queued_msg
(_route_id integer
,_channel_id integer)
RETURNS queued_msg_row LANGUAGE plpgsql
AS $function$
declare
rec queued_msg_row;
begin
for rec in SELECT id,sender,"text", ... , msg_type
FROM msg_queue WHERE busy=false AND route_id=_route_id
ORDER BY priority DESC, date_time ASC LIMIT 10 loop
UPDATE msg_queue SET busy=true, channel_id=_channel_id WHERE id =
rec.id AND busy=false;
if found then
return rec;
end if;
end loop;
return NULL;
end;
$function$

The only problem that remains is that this function returns an empty row
when it should return NULL (no row), but that's not a critical issue.

Best regards, Nick.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message A. Kretschmer 2009-09-04 08:35:24 Re: Need help in copying a table from one database to other
Previous Message Rekha Ravi Pai 2009-09-04 08:23:12 Need help in copying a table from one database to other