pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bryn Llewellyn <bryn@yugabyte.com>
To: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Cc: adrian.klaver@aklaver.com
Cc: pgsql-general list <pgsql-general@lists.postgresql.org>
Subject: Re: Why must AUTOCOMMIT be ON to do txn control in plpgsql procedure?
Date: Wed, 7 Aug 2019 11:46:20 -0700
Message-ID: <5FA840F2-B596-46F6-960D-9705C45F3E9C@yugabyte.com> (raw)
In-Reply-To: <20190807.162639.263082453.horikyota.ntt@gmail.com>
References: <9E141BBF-29E8-4825-9E7D-5F96BAD4365B@yugabyte.com>
	<c864806b-6df5-5a28-4969-35f9ae36e360@aklaver.com>
	<EE6D19C1-1CA6-424B-91AC-63A1A64A5921@yugabyte.com>
	<20190807.162639.263082453.horikyota.ntt@gmail.com>

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 <https://twitter.com/petereisentraut/status/1158802910865756160;.

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).

So, logic tells me that once a txn is ended by issuing “commit” or “rollback”, you should be allowed to start the next one explicitly with “start transaction”. However, as mentioned, this causes a runtime error. I’ve decided simply not to care because I’ve 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’m interested in doing (including setting the isolation level) all implicitly start a txn and so “start transaction” isn’t 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’s how I’ve 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 “start transaction” inside a proc simply cause a runtime error under all circumstances. However, txns can be ended by “commit” or “rollback”. And new ones can be started—but 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=off, 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

view thread (20+ messages)  latest in thread

Message-ID: <5FA840F2-B596-46F6-960D-9705C45F3E9C@yugabyte.com>
Permalink:  ../5FA840F2-B596-46F6-960D-9705C45F3E9C@yugabyte.com/
Also on:    postgresql.org/message-id/5FA840F2-B596-46F6-960D-9705C45F3E9C@yugabyte.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: bryn@yugabyte.com, horikyota.ntt@gmail.com, adrian.klaver@aklaver.com
  Subject: Re: Why must AUTOCOMMIT be ON to do txn control in plpgsql procedure?
  In-Reply-To: <5FA840F2-B596-46F6-960D-9705C45F3E9C@yugabyte.com>
  References: <9E141BBF-29E8-4825-9E7D-5F96BAD4365B@yugabyte.com> <c864806b-6df5-5a28-4969-35f9ae36e360@aklaver.com> <EE6D19C1-1CA6-424B-91AC-63A1A64A5921@yugabyte.com> <20190807.162639.263082453.horikyota.ntt@gmail.com> <5FA840F2-B596-46F6-960D-9705C45F3E9C@yugabyte.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox