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 C07AA52864 for ; Tue, 24 May 2005 07:59:35 -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 95089-04 for ; Tue, 24 May 2005 10:59:26 +0000 (GMT) Received: from srv04.flexidesk.de (h97696.serverkompetenz.net [81.169.138.170]) by svr1.postgresql.org (Postfix) with ESMTP id 8E392528A5 for ; Tue, 24 May 2005 07:59:25 -0300 (ADT) Received: from p54bd7928.dip.t-dialin.net ([84.189.121.40] ident=c5to3a85uelao9p4) by srv04.flexidesk.de with asmtp (Cipher TLSv1:RC4-MD5:128) (Exim 3.35 #1 (Debian)) id 1DaWyU-0004xL-00; Tue, 24 May 2005 12:49:27 +0200 Message-ID: <42930954.2090201@monso.de> Date: Tue, 24 May 2005 11:00:36 +0000 From: "Jan B." User-Agent: Mozilla Thunderbird 1.0.2 (X11/20050514) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Rafa Couto Cc: Andrew Hammond , pgsql-sql@postgresql.org Subject: Re: Transaction in plpgslq References: <22df564b05052003479481da1@mail.gmail.com> <428E245B.4050204@ca.afilias.info> <22df564b050524034045f8c174@mail.gmail.com> In-Reply-To: <22df564b050524034045f8c174@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.298 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO, INFO_TLD X-Spam-Level: X-Archive-Number: 200505/200 X-Sequence-Number: 21513 Rafa Couto wrote: > 2005/5/20, Andrew Hammond : > > >>The solution to your problem is locking (or concurrency control if you >>prefer). While we're at it, we might as well optimize your statement a >>little too using ORDER BY with LIMIT instead of min(). >> >>SELECT id INTO _contacto_id >>FROM contactos >>WHERE contactos.operadora_id IS NULL >> AND contactos.actividad_id > = _actividad_id >>ORDER BY id LIMIT 1 >>FOR UPDATE; >> >>Take a look at the "FOR UPDATE" section of the SELECT description for an >>explanation of how this works. > > > > I understand "FOR UPDATE" clause is locking while is selecting rows > only. It does not secure atomic execution from 'SELECT' to 'UPDATE' in > next statement. Is not it? > > After the SELECT FOR UPDATE command locked some rows, other concurrent changes to the database could be made, but changes, which require to lock that rows will be deferred. The lock will be hold until the end of the transaction (that means at least until the function returns).