You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/src/sgml/atx.sgml
+53-50Lines changed: 53 additions & 50 deletions
Original file line number
Diff line number
Diff line change
@@ -1,49 +1,51 @@
1
1
<!-- doc/src/sgml/atx.sgml -->
2
2
3
3
<chapter id="atx">
4
-
<title>Autonomous transactions.</title>
4
+
<title>Autonomous transactions</title>
5
5
6
6
<sect1 id="atx-overview">
7
7
<title>Overview of autonomous transactions</title>
8
-
8
+
9
9
<para>
10
-
Postgres supports nested transactions:them are rarely explicitly used by programmer and mostly used for error handling and stored
11
-
procedures. It is possible to rollback subtransaction without affecting parent transaction. But commit of subtraction is delayed until
10
+
&productname; supports nested transactions:they are rarely explicitly used by programmer and mostly used for error handling and stored
11
+
procedures. It is possible to rollback subtransaction without affecting parent transaction. But commit of subtraction is delayed until
12
12
commit of parent transaction.
13
13
</para>
14
14
<para>
15
15
But in some cases application needs to run several independent transactions in one session.
16
-
"Autonomous Subtransactions" (in short AST) denotes the capability of a single session to run multiple independent transactions, as if multiple different sessions were executing each transaction.
16
+
<quote>Autonomous Subtransactions</quote> (in short <acronym>AST</acronym>) denotes the capability of a single session
17
+
to run multiple independent transactions, as if multiple different sessions were executing each transaction.
17
18
</para>
18
19
<para>
19
20
Autonomous transactions are needed mostly for implementing audits, when the fact of performing audit should be reported regardless
20
21
status of audit itself: whether it was successfully completed or not.
21
-
Autonomous transactions are widely used in Oracle PL-SQL,at porting such procedures toPostgres is problematic without autonomous transaction support.
22
-
</para>
22
+
Autonomous transactions are widely used in Oracle PL-SQL,so porting such procedures to&productname; is problematic without autonomous transaction support.
23
+
</para>
23
24
24
25
</sect1>
25
26
26
-
<sect1 id="behaviour"><title>Behavior</title>
27
+
<sect1><title>Behavior</title>
27
28
<para>
28
-
An AST can happen only inside another transaction.
29
-
Inside an existing transaction (call it T0), the user can decide to start a subtransaction. Then T0 is paused and pushed in an AST stack, and a new transaction (call it T1) is started.
29
+
An<acronym>AST</acronym> can happen only inside another transaction.
30
+
Inside an existing transaction (call it T0), the user can decide to start a subtransaction. Then T0 is paused and pushed in an<acronym>AST</acronym> stack, and a new transaction (call it T1) is started.
30
31
</para>
31
32
<para>
32
-
At some point in the future the user can commit the subtransaction; after T1 is committed then T0 is popped from the AST stack and resumed.
33
-
The user can also decide to COMMIT the parent transaction T0, in which case T1 is committed, then T0 is popped from the AST stack and then committed.
33
+
At some point in the future the user can commit the subtransaction; after T1 is committed then T0 is popped from the<acronym>AST</acronym> stack and resumed.
34
+
The user can also decide to<command>COMMIT</command> the parent transaction T0, in which case T1 is committed, then T0 is popped from the<acronym>AST</acronym> stack and then committed.
34
35
</para>
35
36
<para>
36
37
All the transactions happen synchronously; at any time only one transaction can be active, while in the stack there are zero (or more) paused transactions in the stack.
37
-
All the possible combinations of COMMIT / ROLLBACK for T0 and T1 can happen; for instance, it is possible to COMMIT T1 and ROLLBACK T0.
38
-
It is possible to nest subtransactions, up to a global resource limit (e.g. the AST stack size) which can be set on the server.
38
+
All the possible combinations of <command>COMMIT</command> / <command>ROLLBACK</command> for T0 and T1 can happen;
39
+
for instance, it is possible to COMMIT T1 and ROLLBACK T0.
40
+
It is possible to nest subtransactions, up to a global resource limit (e.g. the <acronym>AST</acronym> stack size) which can be set on the server.
39
41
</para>
40
42
41
43
</sect1>
42
44
43
-
<sect1 id="example"><title>Example</title>
45
+
<sect1><title>Example</title>
44
46
45
47
<para>
46
-
The following figure describes an example where a transaction executes a subtransaction. A continuous line denotes an active transaction, while a dotted line denotes a transaction which has been paused and pushed in the AST stack. Time flows downwards.
48
+
The following figure describes an example where a transaction executes a subtransaction. A continuous line denotes an active transaction, while a dotted line denotes a transaction which has been paused and pushed in the<acronym>AST</acronym> stack. Time flows downwards.
Visibility rules work as in the case of independent transactions executed via dblink. T1 does not see the effects of T0, because the latter has not been committed yet. T0 might see the effects of T1, depending on its own transaction isolation mode.
106
-
In case of read committed isolation level parent transaction will see changes made by autonomous subtransaction.
107
-
But in case of repeatable read isolation level parent transaction will not see changes made by autonomous subtransaction.
107
+
Visibility rules work as in the case of independent transactions executed via <literal>dblink</literal>.
108
+
T1 does not see the effects of T0, because the latter has not been committed yet. T0 might see the effects of T1, depending on its own transaction isolation mode.
109
+
In case of Read Committed isolation level the parent transaction will see changes made by autonomous subtransaction.
110
+
But in case of Repeatable Read isolation level the parent transaction will not see changes made by autonomous subtransaction.
108
111
</para>
109
112
110
113
<para>
111
-
Now single-session deadlocks become possible, because an AST can become entangled with one of the paused transactions inits session.
112
-
Autonomous transaction T1 is assumed to depend on parent transactionT1 and if itattempt to obtain any resource locked by T0, then
114
+
Now single-session deadlocks become possible, because an<acronym>AST</acronym> can become entangled with one of the paused transactions init's session.
115
+
Autonomous transaction T1 is assumed to depend on parent transactionT0 and if itattempts to obtain any resource locked by T0, then
113
116
deadlock is reported.
114
117
</para>
115
118
116
119
</sect1>
117
120
118
-
<sect1 id="sql-grammar"><title>SQL grammar extension for autonomous transactions</title>
121
+
<sect1><title>SQL grammar extension for autonomous transactions</title>
119
122
120
123
<para>
121
-
Postgres <literal>BEGIN</literal>/<literal>END</literal> transaction statements are extended bybyoptional keyword <literal>autonomous</literal>:
124
+
&productname; <literal>BEGIN</literal>/<literal>END</literal> transaction statements are extended by optional keyword <literal>AUTONOMOUS</literal>:
122
125
</para>
123
126
124
127
<programlisting>
125
-
BEGIN [AUTONOMOUS] [TRANSACTION] [isolation-level]
126
-
END [AUTONOMOUS] [TRANSACTION]
128
+
BEGIN [AUTONOMOUS] [TRANSACTION] [<replaceable>isolation-level</replaceable>]
129
+
END [AUTONOMOUS] [TRANSACTION]
127
130
</programlisting>
128
131
129
132
<para>
130
-
Specifyingautonomous keyword inend transaction clause is optional.
133
+
Specifying<literal>AUTONOMOUS</literal> keyword in<literal>END TRANSACTION</literal> clause is optional.
131
134
It is possible to have several nesting levels of autonomous transactions, but top level transaction can not be autonomous.
132
135
</para>
133
136
134
137
</sect1>
135
138
136
-
<sect1 id="plpgsql-grammar"><title>PL/pgSQL grammar extension for autonomous transactions</title>
139
+
<sect1><title>PL/pgSQL grammar extension for autonomous transactions</title>
137
140
138
141
<para>
139
-
Block construction in PL/pgSQL is extended by optional autonomous keyword.
142
+
Block construction in PL/pgSQL is extended by optional<literal>autonomous</literal> keyword.
140
143
It is possible to treat all function body as autonomous transaction:
141
144
</para>
142
145
@@ -155,49 +158,49 @@ or create separate begin-end block:
155
158
<programlisting>
156
159
create or replace function myaudit() returns boolean as $$
157
160
begin autonomous
158
-
begin autonomous
161
+
begin autonomous
159
162
insert into audit_schedule values ('new audit',now());
160
-
end;
161
-
... -- do audit itself
162
-
return true;
163
+
end;
164
+
... -- do audit itself
165
+
return true;
163
166
end;
164
167
$$ language plpgsql;
165
168
</programlisting>
166
169
167
170
<para>
168
-
When exception is raised inside BEGIN AUTONOMOUS block, this autonomous transaction is aborted and standard
171
+
When exception is raised inside<command>BEGIN AUTONOMOUS</command> block, this autonomous transaction is aborted and standard
169
172
exception handling procedure is started, unwinding stack and executing exception handlers until exception is caught.
170
-
So exception handling is donei the same way as with normalPostgres subtransactions.
173
+
So exception handling is donein the same way as with normal&productname; subtransactions.
171
174
</para>
172
175
<para>
173
-
When an error is caught by an EXCEPTION clause, the local variables of the PL/pgSQL function remain as they were when the error occurred, but all changes to persistent database state within the block are rolled back.
176
+
When an error is caught by an<literal>EXCEPTION</literal> clause, the local variables of the PL/pgSQL function remain as they were when the error occurred, but all changes to persistent database state within the block are rolled back.
174
177
</para>
175
178
176
179
</sect1>
177
180
178
-
<sect1 id="plpython-grammar"><title>PL/python extension for autonomous transactions</title>
181
+
<sect1><title>PL/Python extension for autonomous transactions</title>
179
182
180
183
<para>
181
-
In addition to <varname>subtransaction</varname> method,Plpython module provides new <varname>autonomous</varname> method
182
-
which can be used in <varname>>WITH</varname> clause to start autonomous transaction:
184
+
In addition to <varname>subtransaction</varname> method,PL/Python module provides new <varname>autonomous</varname> method
185
+
which can be used in <varname>WITH</varname> clause to start autonomous transaction:
183
186
</para>
184
187
185
188
<programlisting>
186
189
create or replace function pythonomous() returns void as $$
187
-
plpy.execute("insert into atx_test values ('asd', 123)")
190
+
plpy.execute("insert into atx_test values ('asd', 123)")
188
191
189
-
try:
190
-
with plpy.autonomous():
191
-
plpy.execute("insert into atx_test values ('bsd', 456)")
192
-
except plpy.SPIError, e:
193
-
print("error: %s" % e.args)
192
+
try:
193
+
with plpy.autonomous():
194
+
plpy.execute("insert into atx_test values ('bsd', 456)")
195
+
except plpy.SPIError, e:
196
+
print("error: %s" % e.args)
194
197
195
-
plpy.execute("insert into atx_test values ('csd', 'csd')")
198
+
plpy.execute("insert into atx_test values ('csd', 'csd')")
196
199
$$ language plpythonu;
197
200
</programlisting>
198
201
199
202
<para>
200
-
Exception handling for autonomous transaction inplpython is done in the same way as for subtransactions.
203
+
Exception handling for autonomous transaction inPL/Python is done in the same way as for subtransactions.