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 1hvVtm-0000DD-0X for pgsql-general@arkaria.postgresql.org; Thu, 08 Aug 2019 00:03:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hvVtk-0007RU-PO for pgsql-general@arkaria.postgresql.org; Thu, 08 Aug 2019 00:03: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 1hvVtk-0007Qa-DS for pgsql-general@lists.postgresql.org; Thu, 08 Aug 2019 00:03:16 +0000 Received: from mail-pf1-x442.google.com ([2607:f8b0:4864:20::442]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hvVtg-0001uu-Tx for pgsql-general@lists.postgresql.org; Thu, 08 Aug 2019 00:03:15 +0000 Received: by mail-pf1-x442.google.com with SMTP id 19so42978368pfa.4 for ; Wed, 07 Aug 2019 17:03:12 -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=tXNeLIYCCKkI0uwLLpqnuJJS3ZLZGg9hKqlsdsnWa/k=; b=mixo7q3Z76q53rV7XZLkUFcQdqv5GL5g0jySC7EKXPktpRvzYnhIGVt/b6p9W/kZ25 223OS7cTXn9wWgcIdGeI4JcgSPXTbEgNcIL8nAOAdJ38oB7+SBSXu5tidwY+J1JD0Aon uFSTYVVLf8FjtpHDTotkrHgV9LlynKHhdBiSflsZBkqrtDvfnYZ0/0HivsyQ6Xhz8LAK k2/RBdWAck/oj4sP0T6G1eThnR1nsvyX1x7FAAq8GpQoA8gAEiUaRGicJTLuIKKdNeCk yUu7I7vth2wJc2hnurU0/RT/29SLgTIoe77Si5io0+9GyiCW6yWRobw/dYVmL0s5OpeU DbtA== 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=tXNeLIYCCKkI0uwLLpqnuJJS3ZLZGg9hKqlsdsnWa/k=; b=SA1WPLVr0VSVLL2sTRDa8s6qreTklkQhzWKJbA4YfurSVnYH52HsK3LqE4Gp7Cb/83 OoILrVuuOy3opfIbxZ+6TarpGyB/+z3PS/lxgkaATwLoq6mulmzEOVZ/JCclUfEY40fM dIE8FYHmHeZk0c9bZ9Xq8VIopEonsS69We9Pt7CEg0eyXFoA1uOT64iSa5qLG9eNfm3G Dd8s1new9yeD5egTOUuc+fg78Lt/sbkU0Xxuzhc1QosCk8emcRdSwa0WgLlgtY7Wn2wV 3z2ceNqsW4AOt1a25+4+jvr77J88JjairbZvz3MSZ3f2cpDuPda6MMkehFEm851kevd+ HUDg== X-Gm-Message-State: APjAAAWC+Fxt72r1kaB6c/6V5uKC28QCum1IxVC3DtpyYv0SyUxUGYoU ayNpvcZlTqDTgaNssUw6RVULZg== X-Google-Smtp-Source: APXvYqxvkQfzHFHYuSMK7UTYTNcanhkdsXqIxLNdUvQccVxLddoWpA8dkkxMu8MDBrniGSO2NQH7MA== X-Received: by 2002:a62:79c2:: with SMTP id u185mr12287850pfc.237.1565222590763; Wed, 07 Aug 2019 17:03:10 -0700 (PDT) Received: from ?IPv6:2600:380:8570:b61c:b054:6247:6349:9fce? ([2600:380:8570:b61c:b054:6247:6349:9fce]) by smtp.gmail.com with ESMTPSA id p67sm115648046pfg.124.2019.08.07.17.03.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Aug 2019 17:03:10 -0700 (PDT) From: Bryn Llewellyn Message-Id: <11A11802-242F-4198-AB73-955CF2BC39FC@yugabyte.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_AB50544B-B139-4532-9EF5-95318DDE6CF1" 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 17:02:45 -0700 In-Reply-To: Cc: Luca Ferrari , Adrian Klaver , pgsql-general list To: "David G. Johnston" References: <9E141BBF-29E8-4825-9E7D-5F96BAD4365B@yugabyte.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=_AB50544B-B139-4532-9EF5-95318DDE6CF1 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Thanks David. I=E2=80=99m relieved to hear that I=E2=80=99m not the only = one who finds it hard to predict the behavior of some things in = PostgreSQL just from reading the docs. Hypothesising and experimenting = seem to be needed for the trickier cases. You use the phrase "nested transaction=E2=80=9D. This Google search gets = no hits: "nested transaction" site:www.postgresql.org/docs/11/ = And as I=E2=80=99ve come to understand the phrase in the wider world, = "nested transaction=E2=80=9D is synonymous with =E2=80=9Cautonomous = transaction=E2=80=9D. PostgreSQL 11.2 doesn=E2=80=99t support these. But = I=E2=80=99ve heard that some future release might. So, as I see it, a = single session can do only one transaction after another with no = overlapping. My experiments using txid_current()=E2=80=94mentioned = elsewhere in the offshoots from my original post=E2=80=94are consistent = with this hypothesis. On 07-Aug-2019, at 13:40, David G. Johnston > wrote: On Wed, Aug 7, 2019 at 12:18 PM Luca Ferrari > wrote: On Wed, Aug 7, 2019 at 8:28 PM Bryn Llewellyn > wrote: > B.t.w., I noticed that =E2=80=9Cset transaction isolation level = serializable=E2=80=9D must be the very first statement after = =E2=80=9Crollback=E2=80=9D (or =E2=80=9Ccommit=E2=80=9D). Even an = invocation of txid_current() after the rollback and before the =E2=80=9C=E2= =80=9Cset transaction=E2=80=9D causes this runtime error: =E2=80=9CSET = TRANSACTION ISOLATION LEVEL must be called before any query=E2=80=9D. > Well, SET TRANSACTION ISOLATION must be the very first instruction of every transaction, not only within the case you describe. IMHO, The documentation, probably in chapter 13, could use some = exposition on this topic. What is being described here is basically: [implicit] BEGIN (using default transaction options) CALL >>>>BEGIN (with inherited default transactions options) >>>>ROLLBACK (to get rid of the nested transaction setup by the call = with the inherited default options) >>>>START WITH OPTIONS >>>>COMMIT; <<<
Thanks David. I=E2=80=99= m relieved to hear that I=E2=80=99m not the only one who finds it hard = to predict the behavior of some things in PostgreSQL just from reading = the docs. Hypothesising and experimenting seem to be needed for the = trickier cases.

You = use the phrase "nested transaction=E2=80=9D. This Google search = gets no hits:

  "nested transaction" site:www.postgresql.org/docs/11/

And as I=E2=80=99ve come to understand = the phrase in the wider world, "nested transaction=E2=80=9D is = synonymous with =E2=80=9Cautonomous transaction=E2=80=9D. PostgreSQL = 11.2 doesn=E2=80=99t support these. But I=E2=80=99ve heard that some = future release might. So, as I see it, a single session can do only one = transaction after another with no overlapping. My experiments using = txid_current()=E2=80=94mentioned elsewhere in the offshoots from my = original post=E2=80=94are consistent with this hypothesis.

On = 07-Aug-2019, at 13:40, David G. Johnston <david.g.johnston@gmail.com> wrote:

On Wed, Aug = 7, 2019 at 12:18 PM Luca Ferrari <fluca1978@gmail.com>= wrote:
On Wed, Aug 7, 2019 at 8:28 PM Bryn = Llewellyn <bryn@yugabyte.com> wrote:

> B.t.w., I noticed that =E2=80=9Cset transaction isolation level = serializable=E2=80=9D must be the very first statement after = =E2=80=9Crollback=E2=80=9D (or =E2=80=9Ccommit=E2=80=9D). Even an = invocation of txid_current() after the rollback and before the =E2=80=9C=E2= =80=9Cset transaction=E2=80=9D causes this runtime error: =E2=80=9CSET = TRANSACTION ISOLATION LEVEL must be called before any query=E2=80=9D.
>

Well, SET TRANSACTION ISOLATION must be the very first instruction of
every transaction, not only within the case you describe.

IMHO, The = documentation, probably in chapter 13, could use some exposition on this = topic.

What is being described = here is basically:

[implicit] BEGIN (using = default transaction options)
CALL
>>>>BEGIN = (with inherited default transactions options)
>>>>ROLLBACK = (to get rid of the nested transaction setup by the call with the = inherited default options)
>>>>START = WITH OPTIONS
>>>>COMMIT;
<<<<CALL = END
[implicit] = COMMIT

As far as psql is = concerned there is only one statement and once its executed psql issues = the implicit commit to match the implicit begin is sent = previously.

It should be better = documented which combinations of outer and inner transaction commands = are considered valid and which are not.  WIth examples.  The = current scattering of words leaves the user to perform trial-and-error, = just as the OP has, to determine what is allowed.

The nested transaction = seems like it has to be correct since otherwise the rollback as a first = statement would attempt to rollback the transaction the call itself is = executing within...

Note I only have access = to v10 at the moment so I haven't tried my own experiments.  To my = main point I shouldn't have to - the expected behavior should be = something I could directly interpret from the documentation and in my = admitted brief attempt I could not do so.

David J.



= --Apple-Mail=_AB50544B-B139-4532-9EF5-95318DDE6CF1--