> 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