Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gHUeq-00046H-2Y for pgsql-general@arkaria.postgresql.org; Tue, 30 Oct 2018 14:06:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gHUeo-0004xH-EK for pgsql-general@arkaria.postgresql.org; Tue, 30 Oct 2018 14:06:10 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gHUen-0004x9-BX for pgsql-general@lists.postgresql.org; Tue, 30 Oct 2018 14:06:10 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gHUee-00015S-QE for pgsql-general@postgresql.org; Tue, 30 Oct 2018 14:06:08 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id D0FDF22049; Tue, 30 Oct 2018 10:05:59 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Tue, 30 Oct 2018 10:05:59 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=c crWg6I88Gz5/hj88SkWa9VnuOIncwyah3mDbkp6W/0=; b=W4xIfoTxr++BCm2Af nx+rfSEjrQX+WiLwvj1FwRWvPMNqceXnAAMw7g0jgNNX3e3dbeoy6q1OyJNIFBoV 4qIjWkZNY+8BrJDSO7J8K60UYbe0fuPJk+RlalYyvbMIFI/YtgVKY1vcChW1czKL vO0bjB5MWPxRz7khLesaOymAFzQgZg3mwZzYlm4YXc2ABZO1Ol3JME2jvqj/BTvq 2gm89yYc0jHvv1azVd57oNRgn8BRq+7ja9Rz2aCiKKuyRrGOch/kv4YFfS/U6JgR lHZ+EZ3dtJdo6RKUpudeerNQmwy+WY0/ASqfyGSGZX/uDMSGU1mJrl+ngqW04hp5 gOaLw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=ccrWg6I88Gz5/hj88SkWa9VnuOIncwyah3mDbkp6W /0=; b=h6RNrt9eLiLcUSlaC2J0LyvHIgGjI9UCn3BeIQb66rIRUeyOO/vWRGBwa WDhQiLFegXBUpxRkYX/omKfUaC7PQ8EEFjxxqYMmngEj5zOMU8YxBNJirBX+fFgY qqg/RRgR1pOdA5E0iDrnQN12HGu7AzV0IjogKdoyDOOTg2oH9Ud8abEh+rul9uBk YIXbqSJ+sluSq7LYeabT1SNhApzqWfsXBRnI+Eg2LTarSCY27RKZhUM1qsgzrHt7 QjHX9blqBkuNLrk3PbkZrNvmELukkf1mwSxLiSq4kFkiOMw4ed8mk7Y4MeCm2G2C Wlyxy5J0WoBTbGaPT8VL2t3rK6VSA== X-ME-Sender: X-ME-Proxy: Received: from [192.168.1.3] (unknown [71.212.134.3]) by mail.messagingengine.com (Postfix) with ESMTPA id 6D543102DD; Tue, 30 Oct 2018 10:05:58 -0400 (EDT) Subject: Re: Problem with stored procedure and nested transactions To: p.pierotti@mmbb.it, pgsql-general@postgresql.org References: <008501d47059$48073860$d815a920$@mmbb.it> From: Adrian Klaver Message-ID: <26082fd6-fd38-b6cb-b77b-24620a91a7d3@aklaver.com> Date: Tue, 30 Oct 2018 07:05:57 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: <008501d47059$48073860$d815a920$@mmbb.it> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 10/30/18 7:03 AM, p.pierotti@mmbb.it wrote: > Hi everyone, > > I have problems with stored procedures introduced in version 11. > > I do not understand how to create a nested transaction, in this > semplified example: > > *create**or**replace**procedure*tst_prc(*inout*p_cod *text*) > *language*plpgsql *as**$procedure$* > > *begin* > > p_cod := 'a'; > > *begin* > > *update*aziende *set*mail = 'asd@asd.asd'*where*id = 11; --1st update > > *begin* > > *update*aziende *set*telefono = '0123456789'*where*id = 11; --2nd update > > *commit*; > > *raise**notice*'Inner'; > > *end*; > > *update*aziende *set*telefono = '089'*where*id = 11; --3rd update > > --commit; > > *rollback*; > > *raise**notice*'Outer'; > > *end*; > > p_cod := 'b'; > > *end*; > > *$procedure$* > > The third update goes, rightly, in rollback; the problem is that the > first 2 are committed. > > I thought that the “BEGIN/END” block was used to create new transactions > and that each of them could be managed individually. https://www.postgresql.org/docs/10/static/plpgsql-structure.html "It is important not to confuse the use of BEGIN/END for grouping statements in PL/pgSQL with the similarly-named SQL commands for transaction control. PL/pgSQL's BEGIN/END are only for grouping; they do not start or end a transaction. See Section 43.8 for information on managing transactions in PL/pgSQL. Also, a block containing an EXCEPTION clause effectively forms a subtransaction that can be rolled back without affecting the outer transaction. For more about that see " > > What am I doing wrong? > > Best regards. > > ** > > *Paolo Pierotti * > > ** > > > http://www.mmbb.it/wp-content/uploads/2018/02/MMBB-logo.png > > Viale Lombardia, 4 Lodi (LO) > M: +39 328 9035851 > > P: +39 075 8556435 > > W: www.mmbb.it > -- Adrian Klaver adrian.klaver@aklaver.com