Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hvSP1-00023W-GV for pgsql-general@arkaria.postgresql.org; Wed, 07 Aug 2019 20:19:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hvSOy-0005B8-SI for pgsql-general@arkaria.postgresql.org; Wed, 07 Aug 2019 20:19:16 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hvSOy-00055o-Ft for pgsql-general@lists.postgresql.org; Wed, 07 Aug 2019 20:19:16 +0000 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hvSOo-0005v3-OC for pgsql-general@lists.postgresql.org; Wed, 07 Aug 2019 20:19:13 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id C4958246; Wed, 7 Aug 2019 16:19:03 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Wed, 07 Aug 2019 16:19:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=x oT62b2Cysn4zE8yPJAuP4aD3IJIsNixA/MIQteLER8=; b=s8AGJdE/Oa8+rUNAR ZSy9OxvuCeyY5LqjLyP+perRPPewr5mgkiOGIB6h8dV9YGjOybB3DQnWwO+wg+23 hQeCCb6+XFcbxteiTHCsNQQgsEOQxXbAo8tlevoDEgCFo6mb0e5VCsC7k/V7SK5x teen7fzyi7ZFasNYHG2f2FsSLXrOuNKyxZjthbegJIJDaxswn5W1hLer8rWv1wUf MENuwI5hC2Sl1feRppyAaeUquBupDkPPVf8wvFVOYQ2vlWanqVwrqsWrVvH3Qtsw DVKj8slWkxXspsK4rDets95AK2TxFPfvL2TUvVF9tU5VFE4xft6BlpYRRpBMHpRB WYBkA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=fm3; bh=xoT62b2Cysn4zE8yPJAuP4aD3IJIsNixA/MIQteLE R8=; b=BKyFTkOU1MMPala2s+GotnGaRF3HXSnAS1OxeDtwMtiMTv6K2Aheq+U/p yiUqZCOs1p39Y7EaHXpYeYwuysY0C4A6yOynwaBTTvAVRPbXlAByvVQb+I23J6mf IK33qRkqQLSCikCu/KWgKHtGToa5gBIDYEBNJ8FyaiSVhiLbLJZM3H472dRdcAkI p/0nHU9+hYawuwBGZKY0j+4jVD0y4D1+cM8BufWSZahn6DFw1du+KBNUYoBXXtMG jlUeQAjVWTLzlwg0mSBNrR+e/VbFi55eYwJd481hqYrDwSfrqT3Fxry0Hl5bAFiI /4KSPfNh9//+LezNG4bdifABS2O6w== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddrudduvddgudegjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecuffhomhgrihhnpehtfihithhtvghrrdgtohhmpdhpohhsthhgrhgvshhqlhdr ohhrghdpghhithhhuhgsrdgtohhmnecukfhppeejuddrvdduvddrudehtddrvddtvdenuc frrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgv rhdrtghomhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id 1254B8005A; Wed, 7 Aug 2019 16:19:01 -0400 (EDT) Subject: Re: Why must AUTOCOMMIT be ON to do txn control in plpgsql procedure? To: Bryn Llewellyn Cc: Kyotaro Horiguchi , pgsql-general list References: <9E141BBF-29E8-4825-9E7D-5F96BAD4365B@yugabyte.com> <20190807.162639.263082453.horikyota.ntt@gmail.com> <5FA840F2-B596-46F6-960D-9705C45F3E9C@yugabyte.com> From: Adrian Klaver Message-ID: <6d4cf2eb-bed7-d945-4bb7-a30edf42b96b@aklaver.com> Date: Wed, 7 Aug 2019 13:19:01 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; 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 8/7/19 12:54 PM, Bryn Llewellyn wrote: > Yes, I do believe that I understand this. But there’s no getting away > from the fact that the AUTOCOMMIT mode, and what this implies, is a > server-side phenomenon—at least as several PostgreSQL experts have > assured me. For example, when you use client-side Python with > the psycopg2 driver, then once you’ve done “my_session = > psycopg2.connect(connect_str)”, you can then > do “my_session.set_session(autocommit=False)”. And then everything we’ve > been saying in the psql context now applies in that context—yes? The server responds to instructions from the client. General rule: https://www.postgresql.org/docs/11/sql-call.html "If CALL is executed in a transaction block, then the called procedure cannot execute transaction control statements. Transaction control statements are only allowed if CALL is executed in its own transaction. " > > B.t.w., I’m guessing that the “begin” SQL command that you see in the > log that I mentioned is actually issued by (some) clients—at least psql > and Python-on-psycopg2—as an explicit call from the client. In other > words, it isn’t the server that generates this. Does anyone know for > sure how this works? psql: https://doxygen.postgresql.org/bin_2psql_2common_8c.html Starting ~ line 1355 if (transaction_status == PQTRANS_IDLE && !pset.autocommit && !command_no_begin(query)) { results = PQexec(pset.db, "BEGIN"); if (PQresultStatus(results) != PGRES_COMMAND_OK) ... psycopg2: https://github.com/psycopg/psycopg2/blob/master/psycopg/connection_int.c ~line 1294 > > On 07-Aug-2019, at 11:56, Adrian Klaver > wrote: > > On 8/7/19 11:46 AM, Bryn Llewellyn wrote: >> Thanks for your response, Kyotaro. I’m happy, now, to accept the rule >> that “call proc_that_does_txn_control()” is legal only when AUTOCOMMIT >> is ON. Esp. when I’m told (on twitter, by 2ndQuadrant’s Peter >> Eisentraut, that this rule is “an implementation restriction, for the >> most part.” See HERE >> . >> About your “In-procedure transaction control premises that no >> transaction is active before calling the procedure”… yes. >> Nevertheless, as the code that Umair Sahid showed us in the blog post >> that I referenced in my email that started this thread, you can indeed >> start end end transactions from an executing proc (as long as the >> session’s AUTOCOMMIT mode s ON). > > The key is that the AUTOCOMMIT status is just a specific case of the > general rule. The general rule being that a PROCEDURE cannot do > transaction ending commands when it it called within an outer > transaction. You can run into the same issue in other situations e.g. > ORM's that start a transaction behind the scenes. In other words this is > not psql specific.  As long as you understand the general rule then > things become clearer. > > > -- > Adrian Klaver > adrian.klaver@aklaver.com > -- Adrian Klaver adrian.klaver@aklaver.com