Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 280a408

Browse files
committed
Transaction chaining
Add command variants COMMIT AND CHAIN and ROLLBACK AND CHAIN, which start new transactions with the same transaction characteristics as the just finished one, per SQL standard. Support for transaction chaining in PL/pgSQL is also added. This functionality is especially useful when running COMMIT in a loop in PL/pgSQL. Reviewed-by: Fabien COELHO <coelho@cri.ensmp.fr> Discussion: https://www.postgresql.org/message-id/flat/28536681-324b-10dc-ade8-ab46f7645a5a@2ndquadrant.com
1 parent b2db277 commit 280a408

File tree

26 files changed

+601
-38
lines changed

26 files changed

+601
-38
lines changed

doc/src/sgml/plpgsql.sgml

+14
Original file line numberDiff line numberDiff line change
@@ -3490,6 +3490,20 @@ CALL transaction_test1();
34903490
</programlisting>
34913491
</para>
34923492

3493+
<indexterm zone="plpgsql-transaction-chain">
3494+
<primary>chained transactions</primary>
3495+
<secondary>in PL/pgSQL</secondary>
3496+
</indexterm>
3497+
3498+
<para id="plpgsql-transaction-chain">
3499+
A new transaction starts out with default transaction characteristics such
3500+
as transaction isolation level. In cases where transactions are committed
3501+
in a loop, it might be desirable to start new transactions automatically
3502+
with the same characteristics as the previous one. The commands
3503+
<command>COMMIT AND CHAIN</command> and <command>ROLLBACK AND
3504+
CHAIN</command> accomplish this.
3505+
</para>
3506+
34933507
<para>
34943508
Transaction control is only possible in <command>CALL</command> or
34953509
<command>DO</command> invocations from the top level or nested

doc/src/sgml/ref/abort.sgml

+13-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ PostgreSQL documentation
2121

2222
<refsynopsisdiv>
2323
<synopsis>
24-
ABORT [ WORK | TRANSACTION ]
24+
ABORT [ WORK | TRANSACTION ] [ AND [ NO ] CHAIN ]
2525
</synopsis>
2626
</refsynopsisdiv>
2727

@@ -51,6 +51,18 @@ ABORT [ WORK | TRANSACTION ]
5151
</para>
5252
</listitem>
5353
</varlistentry>
54+
55+
<varlistentry>
56+
<term><literal>AND CHAIN</literal></term>
57+
<listitem>
58+
<para>
59+
If <literal>AND CHAIN</literal> is specified, a new transaction is
60+
immediately started with the same transaction characteristics (see <xref
61+
linkend="sql-set-transaction"/>) as the just finished one. Otherwise,
62+
no new transaction is started.
63+
</para>
64+
</listitem>
65+
</varlistentry>
5466
</variablelist>
5567
</refsect1>
5668

doc/src/sgml/ref/commit.sgml

+19-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ PostgreSQL documentation
2121

2222
<refsynopsisdiv>
2323
<synopsis>
24-
COMMIT [ WORK | TRANSACTION ]
24+
COMMIT [ WORK | TRANSACTION ] [ AND [ NO ] CHAIN ]
2525
</synopsis>
2626
</refsynopsisdiv>
2727

@@ -38,6 +38,10 @@ COMMIT [ WORK | TRANSACTION ]
3838
<refsect1>
3939
<title>Parameters</title>
4040

41+
<indexterm zone="sql-commit-chain">
42+
<primary>chained transactions</primary>
43+
</indexterm>
44+
4145
<variablelist>
4246
<varlistentry>
4347
<term><literal>WORK</literal></term>
@@ -48,6 +52,18 @@ COMMIT [ WORK | TRANSACTION ]
4852
</para>
4953
</listitem>
5054
</varlistentry>
55+
56+
<varlistentry id="sql-commit-chain">
57+
<term><literal>AND CHAIN</literal></term>
58+
<listitem>
59+
<para>
60+
If <literal>AND CHAIN</literal> is specified, a new transaction is
61+
immediately started with the same transaction characteristics (see <xref
62+
linkend="sql-set-transaction"/>) as the just finished one. Otherwise,
63+
no new transaction is started.
64+
</para>
65+
</listitem>
66+
</varlistentry>
5167
</variablelist>
5268
</refsect1>
5369

@@ -79,9 +95,8 @@ COMMIT;
7995
<title>Compatibility</title>
8096

8197
<para>
82-
The SQL standard only specifies the two forms
83-
<literal>COMMIT</literal> and <literal>COMMIT
84-
WORK</literal>. Otherwise, this command is fully conforming.
98+
The command <command>COMMIT</command> conforms to the SQL standard. The
99+
form <literal>COMMIT TRANSACTION</literal> is a PostgreSQL extension.
85100
</para>
86101
</refsect1>
87102

doc/src/sgml/ref/end.sgml

+13-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ PostgreSQL documentation
2121

2222
<refsynopsisdiv>
2323
<synopsis>
24-
END [ WORK | TRANSACTION ]
24+
END [ WORK | TRANSACTION ] [ AND [ NO ] CHAIN ]
2525
</synopsis>
2626
</refsynopsisdiv>
2727

@@ -50,6 +50,18 @@ END [ WORK | TRANSACTION ]
5050
</para>
5151
</listitem>
5252
</varlistentry>
53+
54+
<varlistentry>
55+
<term><literal>AND CHAIN</literal></term>
56+
<listitem>
57+
<para>
58+
If <literal>AND CHAIN</literal> is specified, a new transaction is
59+
immediately started with the same transaction characteristics (see <xref
60+
linkend="sql-set-transaction"/>) as the just finished one. Otherwise,
61+
no new transaction is started.
62+
</para>
63+
</listitem>
64+
</varlistentry>
5365
</variablelist>
5466
</refsect1>
5567

doc/src/sgml/ref/rollback.sgml

+19-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ PostgreSQL documentation
2121

2222
<refsynopsisdiv>
2323
<synopsis>
24-
ROLLBACK [ WORK | TRANSACTION ]
24+
ROLLBACK [ WORK | TRANSACTION ] [ AND [ NO ] CHAIN ]
2525
</synopsis>
2626
</refsynopsisdiv>
2727

@@ -37,6 +37,10 @@ ROLLBACK [ WORK | TRANSACTION ]
3737
<refsect1>
3838
<title>Parameters</title>
3939

40+
<indexterm zone="sql-rollback-chain">
41+
<primary>chained transactions</primary>
42+
</indexterm>
43+
4044
<variablelist>
4145
<varlistentry>
4246
<term><literal>WORK</literal></term>
@@ -47,6 +51,18 @@ ROLLBACK [ WORK | TRANSACTION ]
4751
</para>
4852
</listitem>
4953
</varlistentry>
54+
55+
<varlistentry id="sql-rollback-chain">
56+
<term><literal>AND CHAIN</literal></term>
57+
<listitem>
58+
<para>
59+
If <literal>AND CHAIN</literal> is specified, a new transaction is
60+
immediately started with the same transaction characteristics (see <xref
61+
linkend="sql-set-transaction"/>) as the just finished one. Otherwise,
62+
no new transaction is started.
63+
</para>
64+
</listitem>
65+
</varlistentry>
5066
</variablelist>
5167
</refsect1>
5268

@@ -78,9 +94,8 @@ ROLLBACK;
7894
<title>Compatibility</title>
7995

8096
<para>
81-
The SQL standard only specifies the two forms
82-
<literal>ROLLBACK</literal> and <literal>ROLLBACK
83-
WORK</literal>. Otherwise, this command is fully conforming.
97+
The command <command>ROLLBACK</command> conforms to the SQL standard. The
98+
form <literal>ROLLBACK TRANSACTION</literal> is a PostgreSQL extension.
8499
</para>
85100
</refsect1>
86101

doc/src/sgml/spi.sgml

+27-2
Original file line numberDiff line numberDiff line change
@@ -4376,6 +4376,7 @@ int SPI_freeplan(SPIPlanPtr <parameter>plan</parameter>)
43764376

43774377
<refentry id="spi-spi-commit">
43784378
<indexterm><primary>SPI_commit</primary></indexterm>
4379+
<indexterm><primary>SPI_commit_and_chain</primary></indexterm>
43794380

43804381
<refmeta>
43814382
<refentrytitle>SPI_commit</refentrytitle>
@@ -4384,12 +4385,17 @@ int SPI_freeplan(SPIPlanPtr <parameter>plan</parameter>)
43844385

43854386
<refnamediv>
43864387
<refname>SPI_commit</refname>
4388+
<refname>SPI_commit_and_chain</refname>
43874389
<refpurpose>commit the current transaction</refpurpose>
43884390
</refnamediv>
43894391

43904392
<refsynopsisdiv>
43914393
<synopsis>
43924394
void SPI_commit(void)
4395+
</synopsis>
4396+
4397+
<synopsis>
4398+
void SPI_commit_and_chain(void)
43934399
</synopsis>
43944400
</refsynopsisdiv>
43954401

@@ -4406,7 +4412,14 @@ void SPI_commit(void)
44064412
</para>
44074413

44084414
<para>
4409-
This function can only be executed if the SPI connection has been set as
4415+
<function>SPI_commit_and_chain</function> is the same, but a new
4416+
transaction is immediately started with the same transaction
4417+
characteristics as the just finished one, like with the SQL command
4418+
<command>COMMIT AND CHAIN</command>.
4419+
</para>
4420+
4421+
<para>
4422+
These functions can only be executed if the SPI connection has been set as
44104423
nonatomic in the call to <function>SPI_connect_ext</function>.
44114424
</para>
44124425
</refsect1>
@@ -4416,6 +4429,7 @@ void SPI_commit(void)
44164429

44174430
<refentry id="spi-spi-rollback">
44184431
<indexterm><primary>SPI_rollback</primary></indexterm>
4432+
<indexterm><primary>SPI_rollback_and_chain</primary></indexterm>
44194433

44204434
<refmeta>
44214435
<refentrytitle>SPI_rollback</refentrytitle>
@@ -4424,12 +4438,17 @@ void SPI_commit(void)
44244438

44254439
<refnamediv>
44264440
<refname>SPI_rollback</refname>
4441+
<refname>SPI_rollback_and_chain</refname>
44274442
<refpurpose>abort the current transaction</refpurpose>
44284443
</refnamediv>
44294444

44304445
<refsynopsisdiv>
44314446
<synopsis>
44324447
void SPI_rollback(void)
4448+
</synopsis>
4449+
4450+
<synopsis>
4451+
void SPI_rollback_and_chain(void)
44334452
</synopsis>
44344453
</refsynopsisdiv>
44354454

@@ -4444,9 +4463,15 @@ void SPI_rollback(void)
44444463
using <function>SPI_start_transaction</function> before further database
44454464
actions can be executed.
44464465
</para>
4466+
<para>
4467+
<function>SPI_rollback_and_chain</function> is the same, but a new
4468+
transaction is immediately started with the same transaction
4469+
characteristics as the just finished one, like with the SQL command
4470+
<command>ROLLBACK AND CHAIN</command>.
4471+
</para>
44474472

44484473
<para>
4449-
This function can only be executed if the SPI connection has been set as
4474+
These functions can only be executed if the SPI connection has been set as
44504475
nonatomic in the call to <function>SPI_connect_ext</function>.
44514476
</para>
44524477
</refsect1>

0 commit comments

Comments
 (0)