chained transactions

Lists: pgsql-hackers
From: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: chained transactions
Date: 2018-03-01 03:35:52
Message-ID: 28536681-324b-10dc-ade8-ab46f7645a5a@2ndquadrant.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

This feature is meant to help manage transaction isolation in
procedures. I proposed elsewhere a patch that allows running SET
TRANSACTION in PL/pgSQL. But if you have complex procedures that commit
many times in different branches perhaps, you'd have to do this in every
new transaction, which would create code that is difficult to manage.

The SQL standard offers the "chained transactions" feature to address
this. The new command variants COMMIT AND CHAIN and ROLLBACK AND CHAIN
immediately start a new transaction with the characteristics (isolation
level, read/write, deferrable) of the previous one. So code that has
particular requirements regard transaction isolation and such can use
this to simplify code management.

In the patch series, 0001 through 0006 is some preparatory code cleanup
that is useful independently. 0007 is the implementation of the feature
for the main SQL environment, 0008 is for PL/pgSQL. Support in other
PLs could be added easily.

The patch series also requires the "SET TRANSACTION in PL/pgSQL" patch
for use in the test suite.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachment Content-Type Size
v1-0001-Update-function-comments.patch text/plain 3.1 KB
v1-0002-Rename-TransactionChain-functions.patch text/plain 19.0 KB
v1-0003-Simplify-parse-representation-of-savepoint-comman.patch text/plain 7.7 KB
v1-0004-Improve-savepoint-error-messages.patch text/plain 2.9 KB
v1-0005-Change-transaction-state-debug-strings-to-match-e.patch text/plain 1.9 KB
v1-0006-Turn-transaction_isolation-into-GUC-enum.patch text/plain 5.4 KB
v1-0007-Transaction-chaining.patch text/plain 19.0 KB
v1-0008-Transaction-chaining-support-in-PL-pgSQL.patch text/plain 15.4 KB

From: Andres Freund <andres(at)anarazel(dot)de>
To: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2018-03-02 07:18:01
Message-ID: 20180302071801.pudwgajvy5h6m3bd@alap3.anarazel.de
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

Hi,

On 2018-02-28 22:35:52 -0500, Peter Eisentraut wrote:
> This feature is meant to help manage transaction isolation in
> procedures.

This is a major new feature, submitted the evening before the last CF
for v11 starts. Therefore I think it should just be moved to the next
fest, it doesn't seems realistic to attempt to get this into v11.

Greetings,

Andres Freund


From: Simon Riggs <simon(at)2ndquadrant(dot)com>
To: Andres Freund <andres(at)anarazel(dot)de>
Cc: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2018-03-05 09:21:33
Message-ID: CANP8+jLDje=KLh_R7_Nv=_=18sWyNAGOLFLiXMZC1zcDAdTzJw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

On 2 March 2018 at 07:18, Andres Freund <andres(at)anarazel(dot)de> wrote:
> Hi,
>
> On 2018-02-28 22:35:52 -0500, Peter Eisentraut wrote:
>> This feature is meant to help manage transaction isolation in
>> procedures.
>
> This is a major new feature, submitted the evening before the last CF
> for v11 starts. Therefore I think it should just be moved to the next
> fest, it doesn't seems realistic to attempt to get this into v11.

Looks like a useful patch that adds fairly minor syntax that follows
the SQL Standard.

It introduces no new internal infrastructure, so I can't call this a
major feature.

--
Simon Riggs http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


From: Andres Freund <andres(at)anarazel(dot)de>
To: Simon Riggs <simon(at)2ndquadrant(dot)com>
Cc: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2018-03-05 19:49:13
Message-ID: 20180305194913.g2olpd6tagmiv3qu@alap3.anarazel.de
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

On 2018-03-05 09:21:33 +0000, Simon Riggs wrote:
> On 2 March 2018 at 07:18, Andres Freund <andres(at)anarazel(dot)de> wrote:
> > Hi,
> >
> > On 2018-02-28 22:35:52 -0500, Peter Eisentraut wrote:
> >> This feature is meant to help manage transaction isolation in
> >> procedures.
> >
> > This is a major new feature, submitted the evening before the last CF
> > for v11 starts. Therefore I think it should just be moved to the next
> > fest, it doesn't seems realistic to attempt to get this into v11.
>
> Looks like a useful patch that adds fairly minor syntax that follows
> the SQL Standard.
>
> It introduces no new internal infrastructure, so I can't call this a
> major feature.

You can avoid calling it new infrastructure, but it certainly modifies
new one. And it adds quite some new user interface, which certainly make
s it important to get it right.

doc/src/sgml/plpgsql.sgml | 9
doc/src/sgml/ref/abort.sgml | 14 +
doc/src/sgml/ref/commit.sgml | 14 +
doc/src/sgml/ref/end.sgml | 14 +
doc/src/sgml/ref/rollback.sgml | 14 +
doc/src/sgml/spi.sgml | 14 -
src/backend/access/transam/xact.c | 210 +++++++++++---------
src/backend/commands/cluster.c | 2
src/backend/commands/dbcommands.c | 2
src/backend/commands/discard.c | 2
src/backend/commands/portalcmds.c | 2
src/backend/commands/subscriptioncmds.c | 4
src/backend/commands/typecmds.c | 2
src/backend/commands/vacuum.c | 4
src/backend/commands/variable.c | 57 -----
src/backend/executor/spi.c | 25 ++
src/backend/nodes/copyfuncs.c | 2
src/backend/nodes/equalfuncs.c | 2
src/backend/parser/gram.y | 34 +--
src/backend/replication/walsender.c | 6
src/backend/tcop/utility.c | 58 ++---
src/backend/utils/misc/guc.c | 35 +--
src/backend/utils/time/snapmgr.c | 2
src/bin/psql/common.c | 2
src/include/access/xact.h | 18 -
src/include/commands/variable.h | 4
src/include/executor/spi.h | 4
src/include/nodes/parsenodes.h | 4
src/pl/plperl/plperl.c | 4
src/pl/plpgsql/src/expected/plpgsql_transaction.out | 31 ++
src/pl/plpgsql/src/pl_exec.c | 10
src/pl/plpgsql/src/pl_funcs.c | 10
src/pl/plpgsql/src/pl_gram.y | 18 +
src/pl/plpgsql/src/pl_scanner.c | 2
src/pl/plpgsql/src/plpgsql.h | 2
src/pl/plpgsql/src/sql/plpgsql_transaction.sql | 23 ++
src/pl/plpython/plpy_plpymodule.c | 4
src/pl/tcl/pltcl.c | 4
src/test/regress/expected/transactions.out | 141 +++++++++++++
src/test/regress/sql/transactions.sql | 49 ++++
40 files changed, 596 insertions(+), 262 deletions(-)

Greetings,

Andres Freund


From: Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
To: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2018-03-15 16:39:52
Message-ID: 20180315163952.jqd7rvrxjtdqcd4x@alvherre.pgsql
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

Peter Eisentraut wrote:

> Subject: [PATCH v1 1/8] Update function comments
>
> After a6542a4b6870a019cd952d055d2e7af2da2fe102, some function comments
> were misplaced. Fix that.

Note typo WarnNoTranactionChain in one comment. The patch leaves
CheckTransactionChain with no comment whatsoever; maybe add four words
to indicate that it's implementation for the other two? The phrase
"Thus this is an inverse for PreventTransactionChain" seems to apply to
both functions, maybe it should be in plural? Or perhaps "thus this
behavior is the inverse of"?

> Subject: [PATCH v1 2/8] Rename TransactionChain functions
>
> We call this thing a "transaction block" everywhere except in a few
> functions, where it is mysteriously called a "transaction chain". In
> the SQL standard, a transaction chain is something different. So rename
> these functions to match the common terminology.

Seems reasonable to me; doesn't change any functionality.

> Subject: [PATCH v1 3/8] Simplify parse representation of savepoint commands
>
> Instead of embedding the savepoint name in a list and then requiring
> complex code to unpack it, just add another struct field to store it
> directly.

Obvious in hindsight.

> Subject: [PATCH v1 4/8] Improve savepoint error messages
>
> Include the savepoint name in the error message and rephrase it a bit to
> match common style.

A no-brainer. It's a bit disquieting that this changes so few test
results ...

> Subject: [PATCH v1 5/8] Change transaction state debug strings to match enum
> symbols
>
> In some cases, these were different for no apparent reason, making
> debugging unnecessarily mysterious.

I guess I was trying to save bytes (573a71a5da70) ... looks OK to me.

> From 517bc6d9fefdee9135857d9562f644f2984ace32 Mon Sep 17 00:00:00 2001
> From: Peter Eisentraut <peter_e(at)gmx(dot)net>
> Date: Sun, 18 Feb 2018 09:33:53 -0500
> Subject: [PATCH v1 6/8] Turn transaction_isolation into GUC enum
>
> XXX no idea why it was done the way it was, but this seems much simpler
> and apparently doesn't change any functionality.

Enums are recent -- 52a8d4f8f7e2, only 10 years old, and the commit
didn't convert all cases, leaving some for later. Funnily enough,
default_transaction_isolation was changed afterwards by ad6bf716baa7 but
not this one ... I guess "later" is now upon us for it.

No opinions (yet?) on the rest of it.

--
Álvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


From: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
To: Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2018-03-16 17:29:58
Message-ID: ad593571-08c4-fe86-fad5-ec881cdf0b27@2ndquadrant.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

On 3/15/18 12:39, Alvaro Herrera wrote:
>> Subject: [PATCH v1 1/8] Update function comments
>> Subject: [PATCH v1 2/8] Rename TransactionChain functions
>> Subject: [PATCH v1 3/8] Simplify parse representation of savepoint commands
>> Subject: [PATCH v1 4/8] Improve savepoint error messages
>> Subject: [PATCH v1 5/8] Change transaction state debug strings to match enum
>> symbols

I have committed these with your suggested edits.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>, Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2018-04-05 08:35:08
Message-ID: 457db615-e84c-4838-310e-43841eb806e5@iki.fi
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

On 15/03/18 18:39, Alvaro Herrera wrote:
>> From 517bc6d9fefdee9135857d9562f644f2984ace32 Mon Sep 17 00:00:00 2001
>> From: Peter Eisentraut<peter_e(at)gmx(dot)net>
>> Date: Sun, 18 Feb 2018 09:33:53 -0500
>> Subject: [PATCH v1 6/8] Turn transaction_isolation into GUC enum
>>
>> XXX no idea why it was done the way it was, but this seems much simpler
>> and apparently doesn't change any functionality.
> Enums are recent -- 52a8d4f8f7e2, only 10 years old, and the commit
> didn't convert all cases, leaving some for later. Funnily enough,
> default_transaction_isolation was changed afterwards by ad6bf716baa7 but
> not this one ... I guess "later" is now upon us for it.

With this patch, this stops working:

set transaction_isolation='default';

Other than that, +1 on this patch. I haven't looked closely at the rest
of the patches yet.

- Heikki


From: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
To: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2018-04-06 02:53:26
Message-ID: 69f81fb7-b227-e9a8-f64c-446bc997cdf8@2ndquadrant.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

On 4/5/18 04:35, Heikki Linnakangas wrote:
> With this patch, this stops working:
>
> set transaction_isolation='default';

But why is this supposed to work? This form is not documented anywhere.
You can use RESET or SET TO DEFAULT.

I suspect this is some artifact in the implementation that this patch is
proposing to get rid of.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2018-07-17 17:21:20
Message-ID: b989871c-b2d9-42b9-4461-ace590d1a7c0@iki.fi
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

On 01/03/18 05:35, Peter Eisentraut wrote:
> The SQL standard offers the "chained transactions" feature to address
> this. The new command variants COMMIT AND CHAIN and ROLLBACK AND CHAIN
> immediately start a new transaction with the characteristics (isolation
> level, read/write, deferrable) of the previous one. So code that has
> particular requirements regard transaction isolation and such can use
> this to simplify code management.

Oh, is that all it does? That's disappointing, because that's a lot less
powerful than how I understand chained transactions. And at the same
time relieving, because that's a lot simpler to implement :-).

In Gray & Reuter's classic book, Transaction Processing, they describe
chained transactions so that you also keep locks and cursors.
Unfortunately I don't have a copy at hand, but that's my recollection,
at least. I guess the SQL standard committee had a different idea.

- Heikki


From: Michael Paquier <michael(at)paquier(dot)xyz>
To: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
Cc: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2018-10-02 05:38:54
Message-ID: 20181002053854.GA11712@paquier.xyz
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Jul 17, 2018 at 08:21:20PM +0300, Heikki Linnakangas wrote:
> Oh, is that all it does? That's disappointing, because that's a lot less
> powerful than how I understand chained transactions. And at the same time
> relieving, because that's a lot simpler to implement :-).
>
> In Gray & Reuter's classic book, Transaction Processing, they describe
> chained transactions so that you also keep locks and cursors. Unfortunately
> I don't have a copy at hand, but that's my recollection, at least. I guess
> the SQL standard committee had a different idea.

The patch set does not apply anymore, so this patch is moved to next CF,
waiting on author.
--
Michael


From: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
To: Michael Paquier <michael(at)paquier(dot)xyz>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2018-10-08 14:27:19
Message-ID: ae851ab9-1e53-2200-cbdd-5823160a0367@2ndquadrant.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

On 02/10/2018 07:38, Michael Paquier wrote:
> The patch set does not apply anymore, so this patch is moved to next CF,
> waiting on author.

Attached is a rebased patch set. No functionality changes.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachment Content-Type Size
v2-0001-Transaction-chaining.patch text/plain 19.5 KB
v2-0002-Transaction-chaining-support-in-PL-pgSQL.patch text/plain 14.9 KB

From: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
To: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2018-10-09 19:33:41
Message-ID: 3cb74a0c-4135-2ffe-5d10-696745848bd8@2ndquadrant.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

On 05/04/2018 10:35, Heikki Linnakangas wrote:
> On 15/03/18 18:39, Alvaro Herrera wrote:
>>> From 517bc6d9fefdee9135857d9562f644f2984ace32 Mon Sep 17 00:00:00 2001
>>> From: Peter Eisentraut<peter_e(at)gmx(dot)net>
>>> Date: Sun, 18 Feb 2018 09:33:53 -0500
>>> Subject: [PATCH v1 6/8] Turn transaction_isolation into GUC enum
>>>
>>> XXX no idea why it was done the way it was, but this seems much simpler
>>> and apparently doesn't change any functionality.
>> Enums are recent -- 52a8d4f8f7e2, only 10 years old, and the commit
>> didn't convert all cases, leaving some for later. Funnily enough,
>> default_transaction_isolation was changed afterwards by ad6bf716baa7 but
>> not this one ... I guess "later" is now upon us for it.
>
> With this patch, this stops working:
>
> set transaction_isolation='default';
>
> Other than that, +1 on this patch. I haven't looked closely at the rest
> of the patches yet.

Based on this, I have committed this part of the patch series.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


From: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
To: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
Cc: Michael Paquier <michael(at)paquier(dot)xyz>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2018-11-04 11:20:48
Message-ID: alpine.DEB.2.21.1811041203540.7070@lancre
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers


Hello Peter,

> Attached is a rebased patch set. No functionality changes.

Patch applies cleanly, compile, global make check ok, doc gen ok.

Shouldn't psql tab completion be updated as well?

About the code:

I must admit that do not like much the three global variables &
Save/Restore functions. I'd suggest saving directly into 3 local variables
in function CommitTransactionCommand, and restoring them when needed. Code
should not be longer. I'd would not bother to skip saving when not
chaining.

Copying & comparing nodes are updated. Should making, outing and reading
nodes also be updated?

About the tests: I'd suggest to use more options on the different tests,
eg SERIALIZABLE, READ ONLY… Also ISTM that tests should show
transaction_read_only value as well.

--
Fabien.


From: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
To: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
Cc: Michael Paquier <michael(at)paquier(dot)xyz>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2018-11-28 20:58:09
Message-ID: adb990de-c96d-c685-ddc1-5489aaf49316@2ndquadrant.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

On 04/11/2018 12:20, Fabien COELHO wrote:
> Shouldn't psql tab completion be updated as well?

Done. (I only did the AND CHAIN, not the AND NO CHAIN.)

> I must admit that do not like much the three global variables &
> Save/Restore functions. I'd suggest saving directly into 3 local variables
> in function CommitTransactionCommand, and restoring them when needed. Code
> should not be longer. I'd would not bother to skip saving when not
> chaining.

We're also using those functions in the 0002 patch. Should we just
repeat the code three times, or use macros?

> Copying & comparing nodes are updated. Should making, outing and reading
> nodes also be updated?

TransactionStmt isn't covered by the node serialization functions, so I
didn't see anything to update. What did you have in mind?

> About the tests: I'd suggest to use more options on the different tests,
> eg SERIALIZABLE, READ ONLY… Also ISTM that tests should show
> transaction_read_only value as well.

OK, updated a bit. (Using READ ONLY doesn't work because then you can't
write anything inside the transaction.)

Updated patch attached. The previous (v2) patch apparently didn't apply
anymore.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachment Content-Type Size
v3-0001-Transaction-chaining.patch text/plain 22.0 KB
v3-0002-Transaction-chaining-support-in-PL-pgSQL.patch text/plain 14.9 KB

From: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
To: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
Cc: Michael Paquier <michael(at)paquier(dot)xyz>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2018-12-26 08:20:55
Message-ID: alpine.DEB.2.21.1812260824270.32444@lancre
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers


Hello Peter,

>> Shouldn't psql tab completion be updated as well?
>
> Done. (I only did the AND CHAIN, not the AND NO CHAIN.)

Sure, that is the useful part.

>> I must admit that do not like much the three global variables &
>> Save/Restore functions. I'd suggest saving directly into 3 local variables
>> in function CommitTransactionCommand, and restoring them when needed. Code
>> should not be longer. I'd would not bother to skip saving when not
>> chaining.
>
> We're also using those functions in the 0002 patch.

Hmmm. I have not looked at the second one yet.

> Should we just repeat the code three times, or use macros?

I try to avoid global variables when possible as a principle, because I
paid to learn that they are bad:-) Maybe I'd do a local struct, declare an
instance within the function, and write two functions to dump/restore the
transaction status variables into the referenced struct. Maybe this is not
worth the effort.

>> Copying & comparing nodes are updated. Should making, outing and reading
>> nodes also be updated?
>
> TransactionStmt isn't covered by the node serialization functions, so I
> didn't see anything to update. What did you have in mind?

Sigh. I had in mind that the serialization feature would work with all
possible nodes, not just some of them… which seems quite naïve. The whole
make/copy/cmp/in/out functions depress me, all this verbose code should be
automatically generated from struct declarations. I'm pretty sure there
are hidden bugs in there.

>> About the tests: I'd suggest to use more options on the different tests,
>> eg SERIALIZABLE, READ ONLY… Also ISTM that tests should show
>> transaction_read_only value as well.
>
> OK, updated a bit. (Using READ ONLY doesn't work because then you can't
> write anything inside the transaction.)

Sure. Within a read-only tx, it could check that transaction_read_only is
on, and still on when chained, though.

> Updated patch attached.

First patch applies cleanly, compiles, make check ok.

Further remarks, distinct from the above comments and suggestions:

The documentation should probably tell in the compatibility sections that
AND CHAIN is standard conforming.

In the automatic rollback tests, maybe I'd insert 3 just once, and use
another value for the chained transaction.

Tests may eventually vary BEGIN/START, COMMIT/END, ROLLBACK/ABORT.

As you added a SAVEPOINT, maybe I'd try rollbacking to it.

--
Fabien.


From: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
To: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
Cc: Michael Paquier <michael(at)paquier(dot)xyz>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2018-12-26 08:47:19
Message-ID: alpine.DEB.2.21.1812260923330.32444@lancre
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers


> Updated patch attached. The previous (v2) patch apparently didn't apply
> anymore.

Second patch applies cleanly, compiles, "make check" ok.

As I do not know much about the SPI stuff, some of the comments below may
be very stupid.

I'm wary of changing the SPI_commit and SPI_rollback interfaces which are
certainly being used outside the source tree and could break countless
code, and it seems quite unclean that commit and rollback would do
anything else but committing or rollbacking.

ISTM that it should be kept as is and only managed from the PL/pgsql
exec_stmt_* functions, which have to be adapted anyway. That would
minimise changes and not break existing code.

If SPI_* functions are modified, which I would advise against, I find
keeping the next assignment in the chained case doubtful:

_SPI_current->internal_xact = false;

--
Fabien.


From: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
To: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
Cc: Michael Paquier <michael(at)paquier(dot)xyz>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2018-12-26 08:57:53
Message-ID: alpine.DEB.2.21.1812260956450.32444@lancre
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers


>> Updated patch attached. The previous (v2) patch apparently didn't apply
>> anymore.
>
> Second patch applies cleanly, compiles, "make check" ok.

Also about testing, I'd do less rounds, 4 quite seems enough.

--
Fabien.


From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
Cc: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>, Michael Paquier <michael(at)paquier(dot)xyz>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2018-12-26 12:32:56
Message-ID: 20181226123256.yew55jvrjbkcwk2x@alvherre.pgsql
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

On 2018-Dec-26, Fabien COELHO wrote:

> > > Copying & comparing nodes are updated. Should making, outing and reading
> > > nodes also be updated?
> >
> > TransactionStmt isn't covered by the node serialization functions, so I
> > didn't see anything to update. What did you have in mind?
>
> Sigh. I had in mind that the serialization feature would work with all
> possible nodes, not just some of them… which seems quite naïve. The whole
> make/copy/cmp/in/out functions depress me, all this verbose code should be
> automatically generated from struct declarations. I'm pretty sure there are
> hidden bugs in there.

There may well be, but keep in mind that the nodes that have out and
read support are used in view declarations and such (stored rules); they
are used pretty extensively. Nodes that cannot be part of stored rules
don't need to have read support.

--
Álvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


From: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
To: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
Cc: Michael Paquier <michael(at)paquier(dot)xyz>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2019-01-02 15:02:21
Message-ID: a08a4da5-1e13-c4d1-6e80-86155f946928@2ndquadrant.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

On 26/12/2018 09:20, Fabien COELHO wrote:
> I try to avoid global variables when possible as a principle, because I
> paid to learn that they are bad:-) Maybe I'd do a local struct, declare an
> instance within the function, and write two functions to dump/restore the
> transaction status variables into the referenced struct. Maybe this is not
> worth the effort.

Those are reasonable alternatives, I think, but it's a bit overkill in
this case.

>>> About the tests: I'd suggest to use more options on the different tests,
>>> eg SERIALIZABLE, READ ONLY… Also ISTM that tests should show
>>> transaction_read_only value as well.
>>
>> OK, updated a bit. (Using READ ONLY doesn't work because then you can't
>> write anything inside the transaction.)
>
> Sure. Within a read-only tx, it could check that transaction_read_only is
> on, and still on when chained, though.

I think the tests prove the point that the values are set and unset and
reset in various scenarios. We don't need to test every single
combination, that's not the job of this patch.

> The documentation should probably tell in the compatibility sections that
> AND CHAIN is standard conforming.

Good point. Updated that.

> In the automatic rollback tests, maybe I'd insert 3 just once, and use
> another value for the chained transaction.

done

> Tests may eventually vary BEGIN/START, COMMIT/END, ROLLBACK/ABORT.

Those work on the parser level, so don't really affect this patch. It
would make the tests more confusing to read, I think.

> As you added a SAVEPOINT, maybe I'd try rollbacking to it.

That would error out and invalidate the subsequent tests, so it's not
easily possible. We could write a totally separate test for it, but I'm
not sure what that would be proving.

Updated patches attached.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachment Content-Type Size
v4-0001-Transaction-chaining.patch text/plain 22.9 KB
v4-0002-Transaction-chaining-support-in-PL-pgSQL.patch text/plain 14.8 KB

From: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
To: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
Cc: Michael Paquier <michael(at)paquier(dot)xyz>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2019-01-02 15:07:40
Message-ID: 6f81f979-3b36-73b9-ac94-49b0baaaeaab@2ndquadrant.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

On 26/12/2018 09:47, Fabien COELHO wrote:
> I'm wary of changing the SPI_commit and SPI_rollback interfaces which are
> certainly being used outside the source tree and could break countless
> code, and it seems quite unclean that commit and rollback would do
> anything else but committing or rollbacking.

These are new as of PG11 and are only used by PL implementations that
support transaction control in procedures, of which there are very few.
We could write separate functions for the "and chain" variants, but I
hope that eventually all PLs will support chaining (because that's
really what you ought to be using in procedures), and so then the
non-chaining interfaces would end up being unused.

> ISTM that it should be kept as is and only managed from the PL/pgsql
> exec_stmt_* functions, which have to be adapted anyway. That would
> minimise changes and not break existing code.

But we want other PLs to be able to use this too.

> If SPI_* functions are modified, which I would advise against, I find
> keeping the next assignment in the chained case doubtful:
>
> _SPI_current->internal_xact = false;

This is correct as is. The internal_xact flag prevents
CommitTransactionCommand() and AbortCurrentTransaction() from releasing
SPI memory, so it only needs to be set around those calls. Afterwards
it's unset again so that a top-level transaction end will end up freeing
SPI memory.

Maybe something like internal_xact_end would have been a clearer name.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


From: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
To: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
Cc: Michael Paquier <michael(at)paquier(dot)xyz>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2019-01-06 13:16:14
Message-ID: alpine.DEB.2.21.1901061407120.30093@lancre
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers


Hello Peter,

>> I'm wary of changing the SPI_commit and SPI_rollback interfaces which are
>> certainly being used outside the source tree and could break countless
>> code, and it seems quite unclean that commit and rollback would do
>> anything else but committing or rollbacking.
>
> These are new as of PG11 and are only used by PL implementations that

Ok, but that does not make it right to break them immediately in PG12.

> support transaction control in procedures, of which there are very few.
> We could write separate functions for the "and chain" variants, but I
> hope that eventually all PLs will support chaining (because that's
> really what you ought to be using in procedures), and so then the
> non-chaining interfaces would end up being unused.

One of my issue is that the function name does not really reflect its
updated behavior. I'd be okay with additional _and_chain functions,
although I'm unsure whether one is really needed because it seems that you
need to handle things differently afterwards anyway on the language side.

>> ISTM that it should be kept as is and only managed from the PL/pgsql
>> exec_stmt_* functions, which have to be adapted anyway. That would
>> minimise changes and not break existing code.
>
> But we want other PLs to be able to use this too.

Sure, but I do not see that as a particular issue. PLs need to be extended
to provide a syntax for the new feature anyway, it would not be automatic.
If you really feel there is an issue, then do _and_chain functions, but if
the afterwards code needs to check whether it was and chain and adjust
other internal settings, I'm not sure it is really worth it.

--
Fabien.


From: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
To: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
Cc: Michael Paquier <michael(at)paquier(dot)xyz>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2019-01-06 14:14:58
Message-ID: alpine.DEB.2.21.1901061417050.30093@lancre
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers


Hello Peter,

>> Sure. Within a read-only tx, it could check that transaction_read_only is
>> on, and still on when chained, though.
>
> I think the tests prove the point that the values are set and unset and
> reset in various scenarios. We don't need to test every single
> combination, that's not the job of this patch.

Hmmm... I've been quite unhappy with the status of non regression tests
for a long time, it seems that many contributors take the same approach.

ISTM that your patch saves three states, so I'd check that they are indeed
saved and restored, esp with non default values. I've noticed that you
change the default to read-only with a SET, but then this new default is
not tested afterwards, it is switched to READ WRITE on each transaction.

>> As you added a SAVEPOINT, maybe I'd try rollbacking to it.
>
> That would error out and invalidate the subsequent tests, so it's not
> easily possible. We could write a totally separate test for it, but I'm
> not sure what that would be proving.

Hmmm. Then why put a savepoint if it is not really used?

> Updated patches attached.

First patch applies cleanly, compiles, make check ok.

I do not understand the value of the SAVEPOINT in the tests.
I wish there was a also read-only transaction test.

Otherwise I'm okay with this patch.

About the second patch, I'm still unhappy with functions named commit &
rollback doing something else, which result in somehow strange code, where
you have to guess that the transaction is restarted in all cases, either
within the commit function or explicitely.

SPI_commit/rollback(chain);
if (!chain) SPI_start_transaction();

I think that the PL codes would be clearer with something like:

if (chain)
SPI_commit/rollback_and_chain();
else
{
SPI_commit/rollback();
SPI_start_transation();
}

And there would be no need to change existing SPI_commit/rollback calls to
add a false argument, for those PLs which do not support the extension
(yet).

--
Fabien.


From: Andres Freund <andres(at)anarazel(dot)de>
To: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
Cc: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>, Michael Paquier <michael(at)paquier(dot)xyz>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2019-02-16 05:22:22
Message-ID: 20190216052222.djehwrd3dtdauk6u@alap3.anarazel.de
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

Hi,

On 2019-01-02 16:02:21 +0100, Peter Eisentraut wrote:
> +++ b/src/backend/access/transam/xact.c
> @@ -189,6 +189,7 @@ typedef struct TransactionStateData
> bool startedInRecovery; /* did we start in recovery? */
> bool didLogXid; /* has xid been included in WAL record? */
> int parallelModeLevel; /* Enter/ExitParallelMode counter */
> + bool chain; /* start a new block after this one */
> struct TransactionStateData *parent; /* back link to parent */
> } TransactionStateData;
>
> @@ -2760,6 +2761,36 @@ StartTransactionCommand(void)
> MemoryContextSwitchTo(CurTransactionContext);
> }
>
> +
> +/*
> + * Simple system for saving and restoring transaction characteristics
> + * (isolation level, read only, deferrable). We need this for transaction
> + * chaining, so that we can set the characteristics of the new transaction to
> + * be the same as the previous one. (We need something like this because the
> + * GUC system resets the characteristics at transaction end, so for example
> + * just skipping the reset in StartTransaction() won't work.)
> + */
> +static int save_XactIsoLevel;
> +static bool save_XactReadOnly;
> +static bool save_XactDeferrable;

We normally don't define variables in the middle of a file? Also, why
do these need to be global vars rather than defined where we do
chaining? I'm imagining a SavedTransactionState struct declared on the
stack that's then passed to Save/Restore?

Not crucial, but I do wonder if we can come up with a prettier approach
for this.

Greetings,

Andres Freund


From: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
To: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2019-03-18 10:15:34
Message-ID: 05b14a9b-c2bb-aadb-e5f7-c53c2913c1cc@2ndquadrant.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

Updated patch. I have squashed the two previously separate patches
together in this one.

On 2019-01-06 15:14, Fabien COELHO wrote:
> I do not understand the value of the SAVEPOINT in the tests.

The purpose of the SAVEPOINT in the test is because it exercises
different switch cases in CommitTransactionCommand() and
AbortCurrentTransaction(). It's not entirely comprehensible from the
outside, but code coverage analysis confirms it.

> Otherwise I'm okay with this patch.
>
> About the second patch, I'm still unhappy with functions named commit &
> rollback doing something else, which result in somehow strange code, where
> you have to guess that the transaction is restarted in all cases, either
> within the commit function or explicitely.

I have updated the SPI interface with your suggestions. I agree it's
better that way.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachment Content-Type Size
v5-0001-Transaction-chaining.patch text/plain 35.3 KB

From: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
To: Andres Freund <andres(at)anarazel(dot)de>
Cc: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2019-03-18 10:22:40
Message-ID: db51c67a-3747-8a01-4a74-49b22aefb32d@2ndquadrant.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

On 2019-02-16 06:22, Andres Freund wrote:
>> +static int save_XactIsoLevel;
>> +static bool save_XactReadOnly;
>> +static bool save_XactDeferrable;
>
> We normally don't define variables in the middle of a file? Also, why
> do these need to be global vars rather than defined where we do
> chaining? I'm imagining a SavedTransactionState struct declared on the
> stack that's then passed to Save/Restore?
>
> Not crucial, but I do wonder if we can come up with a prettier approach
> for this.

If we do it with a struct that is passed to the functions, then either
the struct contents will be available outside of xact.c, which will
expose internals of xact.c that weren't previously exposed, or we do it
with an incomplete struct, but then this would involve palloc across
transaction commit, resulting in additional complexity. Neither of
these sound like obviously better alternatives.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


From: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
To: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2019-03-18 20:20:39
Message-ID: alpine.DEB.2.21.1903181919410.27990@lancre
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers


Hallo Peter,

> Updated patch. I have squashed the two previously separate patches
> together in this one.

Ok.

>> I do not understand the value of the SAVEPOINT in the tests.
>
> The purpose of the SAVEPOINT in the test is because it exercises
> different switch cases in CommitTransactionCommand() and
> AbortCurrentTransaction(). It's not entirely comprehensible from the
> outside, but code coverage analysis confirms it.

Ok.

>> Otherwise I'm okay with this patch.
>>
>> About the second patch, I'm still unhappy with functions named commit &
>> rollback doing something else, which result in somehow strange code, where
>> you have to guess that the transaction is restarted in all cases, either
>> within the commit function or explicitely.
>
> I have updated the SPI interface with your suggestions. I agree it's
> better that way.

Patch applies cleanly, compiles, make check ok, doc build ok.

Minor remarks:

In "xact.c", maybe I'd assign blockState in the else branch, instead of
overriding it?

About the static _SPI_{commit,rollback} functions: I'm fine with these
functions, but I'm not sure about their name. Maybe
_SPI_chainable_{commit,rollback} would be is clearer about their content?

Doc looks clear to me. ISTM "chain" should be added as an index term?

Tests look ok. Maybe I'd have series with mixed commit & rollback, instead
of only commit & only rollback?

--
Fabien.


From: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
To: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2019-03-24 11:04:12
Message-ID: 1133e035-72d0-b9ef-dee1-6b2efd65ec38@2ndquadrant.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

Patch has been committed, thanks.

On 2019-03-18 21:20, Fabien COELHO wrote:
> Minor remarks:
>
> In "xact.c", maybe I'd assign blockState in the else branch, instead of
> overriding it?

I think it was better the way it is, since logically the block state is
first set, then set again after the new transaction starts.

> About the static _SPI_{commit,rollback} functions: I'm fine with these
> functions, but I'm not sure about their name. Maybe
> _SPI_chainable_{commit,rollback} would be is clearer about their content?

I kept it as is, to mirror the names of the SQL commands.

> Doc looks clear to me. ISTM "chain" should be added as an index term?

Added, good idea.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


From: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
To: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: chained transactions
Date: 2019-03-24 14:41:10
Message-ID: alpine.DEB.2.21.1903241535220.9939@lancre
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers


Hallo Peter,

>> In "xact.c", maybe I'd assign blockState in the else branch, instead of
>> overriding it?
>
> I think it was better the way it is, since logically the block state is
> first set, then set again after the new transaction starts.

Ok.

>> About the static _SPI_{commit,rollback} functions: I'm fine with these
>> functions, but I'm not sure about their name. Maybe
>> _SPI_chainable_{commit,rollback} would be is clearer about their content?
>
> I kept it as is, to mirror the names of the SQL commands.

Hmmm. Function _SPI_commit does not implement just COMMIT, it implements
both "COMMIT" and "COMMIT AND CHAIN"?

I'm fine with SPI_commit and SPI_commit_and_chain, and the rollback
variants.

Maybe _SPI_commit_chainable? Well, you do as you please.

--
Fabie