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

From Kevin McConnell
Subject Re: easy task: concurrent select-updates
Date
Msg-id 372194e70909031205i7ca1074o46f3fdc67cf613c6@mail.gmail.com
Whole thread Raw
In response to Re: easy task: concurrent select-updates  (Andy Colson <andy@squeakycode.net>)
Responses Re: easy task: concurrent select-updates
Re: easy task: concurrent select-updates
List pgsql-general
> 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

pgsql-general by date:

Previous
From: "Shakil Shaikh"
Date:
Subject: Re: Create language PLPERL error
Next
From: Alvaro Herrera
Date:
Subject: Re: Create language PLPERL error