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 1hvQxB-0006lA-GV for pgsql-general@arkaria.postgresql.org; Wed, 07 Aug 2019 18:46:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hvQx9-0004c8-TS for pgsql-general@arkaria.postgresql.org; Wed, 07 Aug 2019 18:46:27 +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 1hvQx9-0004Ud-Ii for pgsql-general@lists.postgresql.org; Wed, 07 Aug 2019 18:46:27 +0000 Received: from mail-pl1-x642.google.com ([2607:f8b0:4864:20::642]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hvQx6-0004re-BO for pgsql-general@lists.postgresql.org; Wed, 07 Aug 2019 18:46:26 +0000 Received: by mail-pl1-x642.google.com with SMTP id w24so42101775plp.2 for ; Wed, 07 Aug 2019 11:46:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte-com.20150623.gappssmtp.com; s=20150623; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=oNrFGZfEhC8SM3DyDmqo9/+3flYkqI7MtR+BAFVRRfc=; b=bV6fBPzmt6KI5yD2otKtZV4yKgc4H6DB+75NWCiZNEra9wxC8Kr27mvezKyewZa2pm gfJz0Q5N9jImyh79+Z1O3wvtHQ9lvmIvgd9dWM4p1BzTsHeWnpcR/19hq+ZcVXoMlxLv OFeTV6jdxHqj+3aVig9hwLaArCOEqeuT6lFs7BMZNdYwtPAFbos3A7fCFKoLREvakvHc f004t78j6Yp2I1dkJv5k93lqNOgtQpC+TZP9vQh7lkqdAS87bF9HAE+dltVqpw3U5cZp c1WwToZYT4Xp9tWTSpBeHJPj5TXNrJoUXJtmz3qtzURhm5ZITixnLGBGLZ4eYBt666B/ d/VA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=oNrFGZfEhC8SM3DyDmqo9/+3flYkqI7MtR+BAFVRRfc=; b=oDUdT65J6lWMjelkPmLGPNZZ4nGlIgeHuN1X8sQSkfeAPtSg54yAIS5rbQ/KDFAzTl bJkYdBXau5T/GmQEBNL863WfhmsqbkmhrCq2hJo9OIebgG51XLDhDVSlR5NFlYvoR8fK O+tpvKlGa1JMyCT/0TUA3UU2cops3Hu0bZ/pRaS4uQ6omXRx5HEy4eEJMCHu37oDg4hg 0O8RgmPghJBjjq3ZK4jDUBBSdUJsLjdbGziEtqEKLdKIzDg5nH0pSDPxA7qpqflq6/C5 DqfPc0HQp/xvW/DTyL89jzTKeS5c951ktNlf1Q4EMazr/I4cOWNG4cP/J13HthXKgD2g p3lg== X-Gm-Message-State: APjAAAVTIDz2CD2Lgg7wS2YHHhpqe+9izUA1zF0KdCmKgHnAnEv8M9Kn ZD8HZpdor3W7hEJBuKEndU423A== X-Google-Smtp-Source: APXvYqzuNIv9qURghu8zXFU4hd0HtRQa9/LF41NF6Nt+Vgp4utYUKoOPRsHSYLskdPm/75K+hqqMBA== X-Received: by 2002:a63:7887:: with SMTP id t129mr9007244pgc.309.1565203581655; Wed, 07 Aug 2019 11:46:21 -0700 (PDT) Received: from ?IPv6:2601:646:101:7fda:f58c:fe68:b6a:f7fb? ([2601:646:101:7fda:f58c:fe68:b6a:f7fb]) by smtp.gmail.com with ESMTPSA id h6sm92715711pfb.20.2019.08.07.11.46.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Aug 2019 11:46:21 -0700 (PDT) From: Bryn Llewellyn Message-Id: <5FA840F2-B596-46F6-960D-9705C45F3E9C@yugabyte.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_C5FB27BD-4121-426D-8D9B-D94FEFACA706" Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Subject: Re: Why must AUTOCOMMIT be ON to do txn control in plpgsql procedure? Date: Wed, 7 Aug 2019 11:46:20 -0700 In-Reply-To: <20190807.162639.263082453.horikyota.ntt@gmail.com> Cc: adrian.klaver@aklaver.com, pgsql-general list To: Kyotaro Horiguchi References: <9E141BBF-29E8-4825-9E7D-5F96BAD4365B@yugabyte.com> <20190807.162639.263082453.horikyota.ntt@gmail.com> X-Mailer: Apple Mail (2.3445.104.11) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_C5FB27BD-4121-426D-8D9B-D94FEFACA706 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Thanks for your response, Kyotaro. I=E2=80=99m happy, now, to accept the = rule that =E2=80=9Ccall proc_that_does_txn_control()=E2=80=9D is legal = only when AUTOCOMMIT is ON. Esp. when I=E2=80=99m told (on twitter, by = 2ndQuadrant=E2=80=99s Peter Eisentraut, that this rule is =E2=80=9Can = implementation restriction, for the most part.=E2=80=9D See HERE = . About your =E2=80=9CIn-procedure transaction control premises that no = transaction is active before calling the procedure=E2=80=9D=E2=80=A6 = 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=E2=80=99s AUTOCOMMIT mode s ON). So, logic tells me that once a txn is ended by issuing =E2=80=9Ccommit=E2=80= =9D or =E2=80=9Crollback=E2=80=9D, you should be allowed to start the = next one explicitly with =E2=80=9Cstart transaction=E2=80=9D. However, = as mentioned, this causes a runtime error. I=E2=80=99ve decided simply = not to care because I=E2=80=99ve discovered how to write my proc so that = it passes the functionality tests that it ought to. I have to rely on = the fact the the statements I=E2=80=99m interested in doing (including = setting the isolation level) all implicitly start a txn and so =E2=80=9Cst= art transaction=E2=80=9D isn=E2=80=99t needed! Thanks to all who responded. The synthesis of what you all wrote helped = me enormously. Case closed. On 07-Aug-2019, at 00:26, Kyotaro Horiguchi = wrote: Hello, Bryn. At Tue, 6 Aug 2019 15:18:51 -0700, Bryn Llewellyn = wrote in > Here=E2=80=99s how I=E2=80=99ve tried to describe what I see for p2() = with AUTOCOMMIT ON for myself: >=20 > 1. my call p2() starts a txn. >=20 > 2. However, during the execution of the proc, the usual autocommit = behavior is programmatically turned off by explicit PostgreSQL code. >=20 > 3. Other explicit PostgreSQL code makes =E2=80=9Cstart transaction=E2=80= =9D inside a proc simply cause a runtime error under all circumstances. = However, txns can be ended by =E2=80=9Ccommit=E2=80=9D or = =E2=80=9Crollback=E2=80=9D. And new ones can be started=E2=80=94but only = implicitly by executing a SQL statement that, as a top level SQL, would = start a txn. In-procedure transaction control premises that no transaction is active before calling the procedure. 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. With AUTOCOMMIT=3Doff, implicit BEGIN is invoked just before CALLing p2() if no transaction is active. Thus p2() is always called having a transaction active, which inhibits in-procedure transaction control. I'm not sure why you want to turn AUTOCOMNIT off, but even with AUTOCOMMIT on, explict BEGIN prevents per-command COMMIT as you perhaps know. https://www.postgresql.org/docs/11/app-psql.html > When on (the default), each SQL command is automatically > committed upon successful completion. To postpone commit in > this mode, you must enter a BEGIN or START TRANSACTION SQL > command. regards. --=20 Kyotaro Horiguchi NTT Open Source Software Center --Apple-Mail=_C5FB27BD-4121-426D-8D9B-D94FEFACA706 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 Thanks for your response, Kyotaro. I=E2=80=99m = happy, now, to accept the rule that =E2=80=9Ccall = proc_that_does_txn_control()=E2=80=9D is legal only when AUTOCOMMIT is = ON. Esp. when I=E2=80=99m told (on twitter, by = 2ndQuadrant=E2=80=99s Peter Eisentraut, that this rule = is =E2=80=9Can implementation restriction, for the most part.=E2=80=9D= See HERE.

About = your =E2=80=9CIn-procedure transaction control premises that no = transaction is active before calling the procedure=E2=80=9D=E2=80=A6 = 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=E2=80=99s AUTOCOMMIT mode s ON).

So, logic tells = me that once a txn is ended by issuing =E2=80=9Ccommit=E2=80=9D = or =E2=80=9Crollback=E2=80=9D, you should be allowed to start the = next one explicitly with =E2=80=9Cstart transaction=E2=80=9D. = However, as mentioned, this causes a runtime error. I=E2=80=99ve decided = simply not to care because I=E2=80=99ve discovered how to write my proc = so that it passes the functionality tests that it ought to. I have to = rely on the fact the the statements I=E2=80=99m interested in doing = (including setting the isolation level) all implicitly start a txn and = so =E2=80=9Cstart transaction=E2=80=9D isn=E2=80=99t = needed!

Thanks to all who responded. The = synthesis of what you all wrote helped me enormously. Case = closed.

On = 07-Aug-2019, at 00:26, Kyotaro Horiguchi <horikyota.ntt@gmail.com> = wrote:

Hello, Bryn.

At Tue, 6 Aug 2019 = 15:18:51 -0700, Bryn Llewellyn <bryn@yugabyte.com> wrote in <EE6D19C1-1CA6-424B-91AC-63A1A64A5921@yugabyte.com>
Here=E2=80=99s how = I=E2=80=99ve tried to describe what I see for p2() with AUTOCOMMIT ON = for myself:

1. my call p2() starts a = txn.

2. However, during the execution of = the proc, the usual autocommit behavior is programmatically turned off = by explicit PostgreSQL code.

3. Other = explicit PostgreSQL code makes =E2=80=9Cstart transaction=E2=80=9D = inside a proc simply cause a runtime error under all circumstances. = However, txns can be ended by =E2=80=9Ccommit=E2=80=9D or = =E2=80=9Crollback=E2=80=9D. And new ones can be started=E2=80=94but only = implicitly by executing a SQL statement that, as a top level SQL, would = start a txn.

In-procedure = transaction control premises that no transaction is
active = before calling the procedure.

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.

With AUTOCOMMIT=3Doff, implicit = BEGIN is invoked just before
CALLing p2() if no = transaction is active. Thus p2() is always
called having a = transaction active, which inhibits in-procedure
transaction = control.

I'm not sure why you want to turn = AUTOCOMNIT off, but even with
AUTOCOMMIT on, explict BEGIN = prevents per-command COMMIT as you
perhaps know.

https://www.postgresql.org/docs/11/app-psql.html
When on (the default), = each SQL command is automatically
committed upon = successful completion. To postpone commit in
this mode, = you must enter a BEGIN or START TRANSACTION SQL
command.

regards.

--
Kyotaro Horiguchi
NTT Open = Source Software Center

= --Apple-Mail=_C5FB27BD-4121-426D-8D9B-D94FEFACA706--