Re: Transaction in plpgslq - Mailing list pgsql-sql

From Richard Huxton
Subject Re: Transaction in plpgslq
Date
Msg-id 4293171B.4040406@archonet.com
Whole thread Raw
In response to Transaction in plpgslq  (Rafa Couto <rafacouto@gmail.com>)
List pgsql-sql
Rafa Couto wrote:
> I have got a plpgsql function:

>   -- BEGIN;
> 
>     SELECT min(id) INTO _contacto_id FROM contactos 
>       WHERE contactos.operadora_id IS NULL AND contactos.actividad_id
> = _actividad_id;
> 
>     UPDATE contactos SET operadora_id = _operadora_id WHERE id = _contacto_id;
> 
>   -- COMMIT;

> and it works right, but I need atomic execution from --BEGIN and
> --COMMIT, and manual says it is not possible to have transactions in
> PL/pgSQL procedures :-(

OK - the WHOLE FUNCTION takes place within one transaction. So, in that 
sense all changes are atomic.

However, if you want to prevent any changes to "contactos" in-between 
those two statements you'll want additional locks. Read the chapter on 
"concurrency control" for details. You might well want SELECT FOR UPDATE  (and also just ORDER BY id LIMIT 1 rather
thanusing min(id)).
 

--   Richard Huxton  Archonet Ltd


pgsql-sql by date:

Previous
From: Szűcs Gábor
Date:
Subject: Re: could not devise a query plan
Next
From: Andrew Hammond
Date:
Subject: Re: Transaction in plpgslq