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 1hvQj8-0006M8-Vq for pgsql-general@arkaria.postgresql.org; Wed, 07 Aug 2019 18:31:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hvQj7-0000dr-SD for pgsql-general@arkaria.postgresql.org; Wed, 07 Aug 2019 18:31:57 +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 1hvQfx-0003RT-Mg for pgsql-general@lists.postgresql.org; Wed, 07 Aug 2019 18:28:41 +0000 Received: from mail-pl1-x644.google.com ([2607:f8b0:4864:20::644]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hvQfu-0004bw-HX for pgsql-general@lists.postgresql.org; Wed, 07 Aug 2019 18:28:41 +0000 Received: by mail-pl1-x644.google.com with SMTP id m9so41887425pls.8 for ; Wed, 07 Aug 2019 11:28:38 -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=l22tFu8P8F2p/zV4Ip41ks9LodE39FLQpVtUbiShWDU=; b=BGjCzS/TgNoOdyk6WONRdxEs1VJxqD601Xgw3vyvGZqsOeCjlWzU43NSDxyhYYrdYA qblIOS3Q6Lsah5gYuUdGlmjeP4GdpfTmbU0Uwcqccf4Ms5z28h6pXyYOy45lDfbIk+oY ODAkFGMx1G79IzP57hXfaQaDFnQWUPbYPIy9tqgqHJ8/ti1Do/E35ZHMLeTvPidOXOfL HP4V9Rn8dcWeYBqwl+jEukk5qzzXH12FCMw3g4vK21ZjPX4GLf69+/onOnfc8+dGMHiN dLI5RrRE9IhdyRuxCUbrZctcv97YO5nfWQ/WpEgdJd19QUYXUfSh0btwn+EW9A/XF4Dh iddA== 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=l22tFu8P8F2p/zV4Ip41ks9LodE39FLQpVtUbiShWDU=; b=LwPQRJk3Wc0b33F7+H4NB6TAiYjQrvV0kLRzZb3eeHFfUDJVFAlkDsDDc4A/NOZnHN MjJgSl/uTD7s1hSLpovVxpnsPvLsAG2vj5LfAinwlQO3yYLzSBVGaP0BMhB0m4pnigb3 kxf+Yn/Zyj5+iME+28sOBb+i7FAoLC9gFE9ca1SvZdw2qMR567EeRIraN3DjUfzy3U5c xEqR5I59+VBjbE3fgsNiOXCbRhv9Xfb00qNdhPWeWagFHcz1nwbdxKwEUFw2XAEirQKX JmdttJ99/Cnq4uPUS4KUNznDVNgXSebOfkYrYi6d06ViB3/qVlgo9pml3656UM7R6D6v ADEQ== X-Gm-Message-State: APjAAAV0V+9ttTdAtMTEbmN3L/A3hnYS8sLIF2D2393s45nN+yiHZcpS VP9suSMOC67ghTwqKv7a1itmaQ== X-Google-Smtp-Source: APXvYqyFTdqXCbMm+K/hQuBd1YXGABxv0pd+40CGBzp4cydz2mIDxzApLlmYuI0GZanRiailh+OoWw== X-Received: by 2002:a63:4b02:: with SMTP id y2mr8677653pga.135.1565202516591; Wed, 07 Aug 2019 11:28:36 -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 q23sm21286246pgb.68.2019.08.07.11.28.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Aug 2019 11:28:35 -0700 (PDT) From: Bryn Llewellyn Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_C4B133A8-2A60-42D4-8E40-307927ADEABD" 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:28:34 -0700 In-Reply-To: Cc: Adrian Klaver , pgsql-general list To: Luca Ferrari 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=_C4B133A8-2A60-42D4-8E40-307927ADEABD Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 I have a version of the code that I attached with my first email in this = thread where I=E2=80=99ve added =E2=80=9Craise notice=E2=80=9D = invocations to show the txid as my p1() and p2() execute. Everything = that I see is consistent with what I wrote separately in reply to = adrian.klaver@aklaver.com : << 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. 4. This is why =E2=80=9Cset transaction isolation level repeatable = read=E2=80=9D in my p2() is legal immediately after =E2=80=9Crollback=E2=80= =9D=E2=80=94and produces the semantics I=E2=80=99d expect. At top level, = and with autocommit turned on, it implicitly starts a txn=E2=80=94and = you see the =E2=80=9Cbegin=E2=80=9D in the log file. 5. When the proc call ends, normal AUTOCOMMIT mode is turned on again, = and a =E2=80=9Ccommit=E2=80=9D is issued automatically. This may, or may = not, have something to do=E2=80=94as you can see by running p3() with = AUTOCOMMIT ON. >> I=E2=80=99m interested only in the case that my proc does txn = control=E2=80=94i.e. my p2()=E2=80=94and so I=E2=80=99m not interested = in how my p1() behaves. About your =E2=80=9CI believe there is no reason ever to begin a = procedure with a rollback=E2=80=9D, I already explained why I did that. = My higher goal is to take advantage of the =E2=80=9Cserializable=E2=80=9D = isolation level to safely enforce a multi-row data rule. And I want my = pspgsql proc=E2=80=94following the time-honored philosophy for stored = procs=E2=80=94to own the complete implementation. I discovered that = doing =E2=80=9Crollback=E2=80=9D as the first executable statement in my = proc allowed me to do =E2=80=9Cset transaction isolation level = serializable=E2=80=9D. And I=E2=80=99ve found no other way to do this. = As I mentioned, the =E2=80=9Creal=E2=80=9D version of my proc, written = this way does pass my functionality tests. 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. About your PS, I=E2=80=99m new to this list=E2=80=94so forgive me if I = didn=E2=80=99t follow proper etiquette. But as adrian.klaver@aklaver.com = pointed out, the functionality under = discussion here is part of the core PostgreSQL implementation. On 07-Aug-2019, at 00:15, Luca Ferrari wrote: On Wed, Aug 7, 2019 at 12:19 AM Bryn Llewellyn = wrote: > 1. my call p2() starts a txn. In my opinion this is the point: a procedure must be "owner" of the transaction to issue transaction control statements. You can watch different behaviors placing here and there txid_current() before and within p2 and re-run with autocommit off an on. On autocmmmit =3D on the call opens a transaction and the procedure is the only thing within the transaction, so it has full control. With autocommit off you gain an implicit begin and the procedure is possibly not the only thing you have within the transaction. You can test it with: testdb=3D# begin; BEGIN testdb=3D# call p2(); INFO: TXID 1300004994 ERROR: invalid transaction termination CONTEXT: PL/pgSQL function p2() line 9 at ROLLBACK testdb=3D# rollback; ROLLBACK Having said that, I believe there is no reason ever to begin a procedure with a rollback. Here's a small example that performs a few commits and rollback = Luca P.S: I don't believe that asking on a public mailing list for a company to answer is the right thing to do, you can always ask themselves on your own. --Apple-Mail=_C4B133A8-2A60-42D4-8E40-307927ADEABD Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
I have a = version of the code that I attached with my first email in this thread = where I=E2=80=99ve added =E2=80=9Craise notice=E2=80=9D invocations = to show the txid as my p1() and p2() execute. Everything that I see is = consistent with what I wrote separately in reply to adrian.klaver@aklaver.com:

<<
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.&nb= sp;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.

4. This is why =E2=80=9Cset transaction = isolation level repeatable read=E2=80=9D in my p2() is = legal immediately after =E2=80=9Crollback=E2=80=9D=E2=80=94and = produces the semantics I=E2=80=99d expect. At top level, and = with autocommit turned on, it implicitly starts a txn=E2=80=94and = you see the =E2=80=9Cbegin=E2=80=9D in the log file.

5. When the proc call ends, normal AUTOCOMMIT mode = is turned on again, and a =E2=80=9Ccommit=E2=80=9D is issued = automatically. This may, or may not, have something to do=E2=80=94as you = can see by running p3() with AUTOCOMMIT = ON.
>>

I=E2=80=99m interested only in = the case that my proc does txn control=E2=80=94i.e. my p2()=E2=80=94and = so I=E2=80=99m not interested in how my p1() behaves.

About your =E2=80=9CI believe there is no reason ever = to begin a procedure with a rollback=E2=80=9D, I already explained = why I did that. My higher goal is to take advantage of the = =E2=80=9Cserializable=E2=80=9D isolation level to safely enforce a = multi-row data rule. And I want my pspgsql proc=E2=80=94following the = time-honored philosophy for stored procs=E2=80=94to own the complete = implementation. I discovered that doing =E2=80=9Crollback=E2=80=9D = as the first executable statement in my proc allowed me to = do =E2=80=9Cset transaction isolation level serializable=E2=80=9D. = And I=E2=80=99ve found no other way to do this. As I mentioned, = the =E2=80=9Creal=E2=80=9D version of my proc, written this way = does pass my functionality tests.

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.

About your PS, I=E2=80=99m new = to this list=E2=80=94so forgive me if I didn=E2=80=99t follow proper = etiquette. But as adrian.klaver@aklaver.com pointed out, the = functionality under discussion here is part of the core PostgreSQL = implementation.

On 07-Aug-2019, at 00:15, Luca = Ferrari <fluca1978@gmail.com> wrote:

On Wed, Aug 7, 2019 at 12:19 AM Bryn Llewellyn <bryn@yugabyte.com> = wrote:
1. my call = p2() starts a txn.

In my = opinion this is the point: a procedure must be "owner" of the
transaction to issue transaction control statements. You can = watch
different behaviors placing here and there = txid_current() before and
within p2 and re-run with = autocommit off an on.
On autocmmmit =3D on the call opens = a transaction and the procedure is
the only thing within = the transaction, so it has full control. With
autocommit = off you gain an implicit begin and the procedure is
possibly= not the only thing you have within the transaction.
You = can test it with:

testdb=3D# begin;
BEGIN
testdb=3D# call p2();
INFO: =  TXID 1300004994
ERROR:  invalid transaction = termination
CONTEXT:  PL/pgSQL function p2() line 9 = at ROLLBACK
testdb=3D# rollback;
ROLLBACK


Having said that, I believe = there is no reason ever to begin a
procedure with a = rollback.

Here's a small example that = performs a few commits and rollback
<https://github.com/fluca1978/PostgreSQL-11-Quick-Start-Guide/bl= ob/master/Chapter04/Chapter04_Listing29.sql>

Luca

P.S:
I don't = believe that asking on a public mailing list for a company to
answer is the right thing to do, you can always ask = themselves on your
own.

= --Apple-Mail=_C4B133A8-2A60-42D4-8E40-307927ADEABD--