From: Richard Huxton <dev@archonet.com>
To: Rafa Couto <rafacouto@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Transaction in plpgslq
Date: Tue, 24 May 2005 12:59:23 +0100
Message-ID: <4293171B.4040406@archonet.com> (raw)
In-Reply-To: <22df564b05052003479481da1@mail.gmail.com>
References: <22df564b05052003479481da1@mail.gmail.com>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: dev@archonet.com, rafacouto@gmail.com
Subject: Re: Transaction in plpgslq
In-Reply-To: <4293171B.4040406@archonet.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox