Re: Transaction in plpgslq

From: Richard Huxton <dev(at)archonet(dot)com>
To: Rafa Couto <rafacouto(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Transaction in plpgslq
Date: 2005-05-24 11:59:23
Message-ID: 4293171B.4040406@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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 than using min(id)).

--
Richard Huxton
Archonet Ltd

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Retro-tyr-yandex 2005-05-24 13:01:21 Новое предложение для Российских туристических агентств – Северный Кипр
Previous Message Szűcs Gábor 2005-05-24 11:55:21 Re: [SQL] could not devise a query plan