Skip to content

Commit 957fade

Browse files
author
Commitfest Bot
committed
[CF 5546] v3 - Document how commit handles aborted transactions
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/5546 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/CAKFQuwaOevyJo8E7wi4Tp2MB5C+8v92ThbMPFFjheDFgfYvgLA@mail.gmail.com Author(s): David Johnston
2 parents 0a8a4be + 16e35fa commit 957fade

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

doc/src/sgml/advanced.sgml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ DETAIL: Key (city)=(Berkeley) is not present in table "cities".
149149
systems. The essential point of a transaction is that it bundles
150150
multiple steps into a single, all-or-nothing operation. The intermediate
151151
states between the steps are not visible to other concurrent transactions,
152-
and if some failure occurs that prevents the transaction from completing,
152+
and if <link linkend="tutorial-transactions-aborted">an error</link>
153+
occurs that prevents the transaction from completing,
153154
then none of the steps affect the database at all.
154155
</para>
155156

@@ -218,7 +219,8 @@ UPDATE branches SET balance = balance + 100.00
218219
<para>
219220
In <productname>PostgreSQL</productname>, a transaction is set up by surrounding
220221
the SQL commands of the transaction with
221-
<command>BEGIN</command> and <command>COMMIT</command> commands. So our banking
222+
<xref linkend="sql-begin"/> and
223+
<xref linkend="sql-commit"/> commands. So our banking
222224
transaction would actually look like:
223225

224226
<programlisting>
@@ -233,7 +235,7 @@ COMMIT;
233235
<para>
234236
If, partway through the transaction, we decide we do not want to
235237
commit (perhaps we just noticed that Alice's balance went negative),
236-
we can issue the command <command>ROLLBACK</command> instead of
238+
we can issue the command <xref linkend="sql-rollback"/> instead of
237239
<command>COMMIT</command>, and all our updates so far will be canceled.
238240
</para>
239241

@@ -256,6 +258,16 @@ COMMIT;
256258
</para>
257259
</note>
258260

261+
<para id="tutorial-transactions-aborted">
262+
When an error occurs within a transaction block the transaction is not ended
263+
but instead goes into an aborted state. While in this state all commands except
264+
<xref linkend="sql-commit"/> and <xref linkend="sql-rollback"/> are ignored and,
265+
importantly, both those commands behave identically - they roll-back and close
266+
the current transaction, returning the session to a state where new commands can
267+
be issued. They will also automatically begin a new transaction if executed
268+
with an <literal>AND CHAIN</literal> parameter.
269+
</para>
270+
259271
<para>
260272
It's possible to control the statements in a transaction in a more
261273
granular fashion through the use of <firstterm>savepoints</firstterm>. Savepoints

doc/src/sgml/ref/commit.sgml

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ COMMIT [ WORK | TRANSACTION ] [ AND [ NO ] CHAIN ]
3333
changes made by the transaction become visible to others
3434
and are guaranteed to be durable if a crash occurs.
3535
</para>
36+
<para>
37+
If the transaction is in an aborted state then no changes will be made
38+
and the effect of the <command>COMMIT</command> will be identical to that
39+
of <command>ROLLBACK</command>, including the command tag output.
40+
</para>
41+
<para>
42+
In either situation, if the <literal>AND CHAIN</literal> parameter is
43+
specified a new, identically configured, transaction is started.
44+
</para>
45+
<para>
46+
For more information regarding transactions see
47+
<xref linkend="tutorial-transactions"/>.
48+
</para>
3649
</refsect1>
3750

3851
<refsect1>
@@ -67,6 +80,25 @@ COMMIT [ WORK | TRANSACTION ] [ AND [ NO ] CHAIN ]
6780
</variablelist>
6881
</refsect1>
6982

83+
<refsect1>
84+
<title>Outputs</title>
85+
86+
<para>
87+
On successful completion on a non-aborted transaction, a <command>COMMIT</command>
88+
command returns a command tag of the form
89+
<screen>
90+
COMMIT
91+
</screen>
92+
</para>
93+
<para>
94+
However, on an aborted transaction, a <command>COMMIT</command>
95+
command returns a command tag of the form
96+
<screen>
97+
ROLLBACK
98+
</screen>
99+
</para>
100+
</refsect1>
101+
70102
<refsect1>
71103
<title>Notes</title>
72104

@@ -96,8 +128,13 @@ COMMIT;
96128
<title>Compatibility</title>
97129

98130
<para>
99-
The command <command>COMMIT</command> conforms to the SQL standard. The
100-
form <literal>COMMIT TRANSACTION</literal> is a PostgreSQL extension.
131+
The command <command>COMMIT</command> conforms to the SQL standard, except
132+
that no exception condition is raised in the case where an already aborted
133+
transaction is committed.
134+
</para>
135+
136+
<para>
137+
The form <literal>COMMIT TRANSACTION</literal> is a PostgreSQL extension.
101138
</para>
102139
</refsect1>
103140

@@ -107,6 +144,7 @@ COMMIT;
107144
<simplelist type="inline">
108145
<member><xref linkend="sql-begin"/></member>
109146
<member><xref linkend="sql-rollback"/></member>
147+
<member><xref linkend="tutorial-transactions"/></member>
110148
</simplelist>
111149
</refsect1>
112150
</refentry>

0 commit comments

Comments
 (0)