X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id ED18F528A5 for ; Tue, 24 May 2005 09:00:31 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 04474-09 for ; Tue, 24 May 2005 12:00:26 +0000 (GMT) Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by svr1.postgresql.org (Postfix) with ESMTP id 2013252887 for ; Tue, 24 May 2005 09:00:22 -0300 (ADT) Received: from mainbox.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id 0973941458F; Tue, 24 May 2005 13:00:17 +0100 (BST) Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id 410FD15EA6; Tue, 24 May 2005 12:59:24 +0100 (BST) Received: from mainbox.archonet.com ([127.0.0.1]) by localhost (mainbox [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 26711-10; Tue, 24 May 2005 12:59:23 +0100 (BST) Received: from [192.168.1.17] (client17.office.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id D1EC615EA5; Tue, 24 May 2005 12:59:23 +0100 (BST) Message-ID: <4293171B.4040406@archonet.com> Date: Tue, 24 May 2005 12:59:23 +0100 From: Richard Huxton User-Agent: Mozilla Thunderbird 1.0 (X11/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Rafa Couto Cc: pgsql-sql@postgresql.org Subject: Re: Transaction in plpgslq References: <22df564b05052003479481da1@mail.gmail.com> In-Reply-To: <22df564b05052003479481da1@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.054 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200505/202 X-Sequence-Number: 21515 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