1
- $PostgreSQL: pgsql/src/backend/access/transam/README,v 1.1 2004/08/01 20:57:59 tgl Exp $
1
+ $PostgreSQL: pgsql/src/backend/access/transam/README,v 1.2 2004/09/16 16:58:26 tgl Exp $
2
2
3
3
The Transaction System
4
4
----------------------
@@ -9,7 +9,7 @@ the mainloop's control code, which in turn implements user-visible
9
9
transactions and savepoints.
10
10
11
11
The middle layer of code is called by postgres.c before and after the
12
- processing of each query:
12
+ processing of each query, or after detecting an error :
13
13
14
14
StartTransactionCommand
15
15
CommitTransactionCommand
@@ -44,9 +44,9 @@ effects of previous commands within the same transaction. Note that this is
44
44
done automatically by CommitTransactionCommand after each query inside a
45
45
transaction block, but some utility functions also do it internally to allow
46
46
some operations (usually in the system catalogs) to be seen by future
47
- operations in the same utility command (for example, in DefineRelation it is
47
+ operations in the same utility command. (For example, in DefineRelation it is
48
48
done after creating the heap so the pg_class row is visible, to be able to
49
- lock it).
49
+ lock it.)
50
50
51
51
52
52
For example, consider the following sequence of user commands:
@@ -60,26 +60,26 @@ In the main processing loop, this results in the following function call
60
60
sequence:
61
61
62
62
/ StartTransactionCommand;
63
- / ProcessUtility; << BEGIN
64
- 1) < BeginTransactionBlock;
65
- \ CommitTransactionCommand ;
66
- \ StartTransaction ;
63
+ / StartTransaction;
64
+ 1) < ProcessUtility; << BEGIN
65
+ \ BeginTransactionBlock ;
66
+ \ CommitTransactionCommand ;
67
67
68
68
/ StartTransactionCommand;
69
- 2) / ProcessQuery; << SELECT * FROM foo
69
+ 2) / ProcessQuery; << SELECT ...
70
70
\ CommitTransactionCommand;
71
71
\ CommandCounterIncrement;
72
72
73
73
/ StartTransactionCommand;
74
- 3) / ProcessQuery; << INSERT INTO foo VALUES ( ...)
74
+ 3) / ProcessQuery; << INSERT ...
75
75
\ CommitTransactionCommand;
76
76
\ CommandCounterIncrement;
77
77
78
78
/ StartTransactionCommand;
79
79
/ ProcessUtility; << COMMIT
80
80
4) < EndTransactionBlock;
81
- \ CommitTransaction ;
82
- \ CommitTransactionCommand ;
81
+ \ CommitTransactionCommand ;
82
+ \ CommitTransaction ;
83
83
84
84
The point of this example is to demonstrate the need for
85
85
StartTransactionCommand and CommitTransactionCommand to be state smart -- they
@@ -118,15 +118,15 @@ to do all the real work. The only difference is what state we enter after
118
118
AbortTransaction does its work:
119
119
120
120
* AbortCurrentTransaction leaves us in TBLOCK_ABORT,
121
- * UserAbortTransactionBlock leaves us in TBLOCK_ENDABORT
121
+ * UserAbortTransactionBlock leaves us in TBLOCK_ABORT_END
122
122
123
123
Low-level transaction abort handling is divided in two phases:
124
124
* AbortTransaction executes as soon as we realize the transaction has
125
125
failed. It should release all shared resources (locks etc) so that we do
126
126
not delay other backends unnecessarily.
127
127
* CleanupTransaction executes when we finally see a user COMMIT
128
128
or ROLLBACK command; it cleans things up and gets us out of the transaction
129
- internally . In particular, we mustn't destroy TopTransactionContext until
129
+ completely . In particular, we mustn't destroy TopTransactionContext until
130
130
this point.
131
131
132
132
Also, note that when a transaction is committed, we don't close it right away.
@@ -163,28 +163,48 @@ called so the system returns to the parent transaction.
163
163
One important point regarding subtransaction handling is that several may need
164
164
to be closed in response to a single user command. That's because savepoints
165
165
have names, and we allow to commit or rollback a savepoint by name, which is
166
- not necessarily the one that was last opened. In the case of subtransaction
167
- commit this is not a problem, and we close all the involved subtransactions
168
- right away by calling CommitTransactionToLevel, which in turn calls
169
- CommitSubTransaction and PopTransaction as many times as needed.
170
-
171
- In the case of subtransaction abort (when the user issues ROLLBACK TO
172
- <savepoint>), things are not so easy. We have to keep the subtransactions
173
- open and return control to the main loop. So what RollbackToSavepoint does is
174
- abort the innermost subtransaction and put it in TBLOCK_SUBENDABORT state, and
175
- put the rest in TBLOCK_SUBABORT_PENDING state. Then we return control to the
176
- main loop, which will in turn return control to us by calling
177
- CommitTransactionCommand. At this point we can close all subtransactions that
178
- are marked with the "abort pending" state. When that's done, the outermost
179
- subtransaction is created again, to conform to SQL's definition of ROLLBACK TO.
166
+ not necessarily the one that was last opened. Also a COMMIT or ROLLBACK
167
+ command must be able to close out the entire stack. We handle this by having
168
+ the utility command subroutine mark all the state stack entries as commit-
169
+ pending or abort-pending, and then when the main loop reaches
170
+ CommitTransactionCommand, the real work is done. The main point of doing
171
+ things this way is that if we get an error while popping state stack entries,
172
+ the remaining stack entries still show what we need to do to finish up.
173
+
174
+ In the case of ROLLBACK TO <savepoint>, we abort all the subtransactions up
175
+ through the one identified by the savepoint name, and then re-create that
176
+ subtransaction level with the same name. So it's a completely new
177
+ subtransaction as far as the internals are concerned.
180
178
181
179
Other subsystems are allowed to start "internal" subtransactions, which are
182
180
handled by BeginInternalSubtransaction. This is to allow implementing
183
181
exception handling, e.g. in PL/pgSQL. ReleaseCurrentSubTransaction and
184
182
RollbackAndReleaseCurrentSubTransaction allows the subsystem to close said
185
183
subtransactions. The main difference between this and the savepoint/release
186
- path is that BeginInternalSubtransaction is allowed when no explicit
187
- transaction block has been established, while DefineSavepoint is not.
184
+ path is that we execute the complete state transition immediately in each
185
+ subroutine, rather than deferring some work until CommitTransactionCommand.
186
+ Another difference is that BeginInternalSubtransaction is allowed when no
187
+ explicit transaction block has been established, while DefineSavepoint is not.
188
+
189
+
190
+ Subtransaction numbering
191
+ ------------------------
192
+
193
+ A top-level transaction is always given a TransactionId (XID) as soon as it is
194
+ created. This is necessary for a number of reasons, notably XMIN bookkeeping
195
+ for VACUUM. However, a subtransaction doesn't need its own XID unless it
196
+ (or one of its child subxacts) writes tuples into the database. Therefore,
197
+ we postpone assigning XIDs to subxacts until and unless they call
198
+ GetCurrentTransactionId. The subsidiary actions of obtaining a lock on the
199
+ XID and and entering it into pg_subtrans and PG_PROC are done at the same time.
200
+
201
+ Internally, a backend needs a way to identify subtransactions whether or not
202
+ they have XIDs; but this need only lasts as long as the parent top transaction
203
+ endures. Therefore, we have SubTransactionId, which is somewhat like
204
+ CommandId in that it's generated from a counter that we reset at the start of
205
+ each top transaction. The top-level transaction itself has SubTransactionId 1,
206
+ and subtransactions have IDs 2 and up. (Zero is reserved for
207
+ InvalidSubTransactionId.)
188
208
189
209
190
210
pg_clog and pg_subtrans
@@ -197,27 +217,28 @@ there's a long running transaction or a backend sitting idle with an open
197
217
transaction, it may be necessary to be able to read and write this information
198
218
from disk. They also allow information to be permanent across server restarts.
199
219
200
- pg_clog records the commit status for each transaction. A transaction can be
201
- in progress, committed, aborted, or "sub-committed". This last state means
202
- that it's a subtransaction that's no longer running, but its parent has not
203
- updated its state yet (either it is still running, or the backend crashed
204
- without updating its status). A sub-committed transaction's status will be
205
- updated again to the final value as soon as the parent commits or aborts, or
206
- when the parent is detected to be aborted.
220
+ pg_clog records the commit status for each transaction that has been assigned
221
+ an XID. A transaction can be in progress, committed, aborted, or
222
+ "sub-committed". This last state means that it's a subtransaction that's no
223
+ longer running, but its parent has not updated its state yet (either it is
224
+ still running, or the backend crashed without updating its status). A
225
+ sub-committed transaction's status will be updated again to the final value as
226
+ soon as the parent commits or aborts, or when the parent is detected to be
227
+ aborted.
207
228
208
229
Savepoints are implemented using subtransactions. A subtransaction is a
209
- transaction inside a transaction; it gets its own TransactionId, but its
210
- commit or abort status is not only dependent on whether it committed itself,
211
- but also whether its parent transaction committed. To implement multiple
212
- savepoints in a transaction we allow unlimited transaction nesting depth, so
213
- any particular subtransaction's commit state is dependent on the commit status
214
- of each and every ancestor transaction.
230
+ transaction inside a transaction; its commit or abort status is not only
231
+ dependent on whether it committed itself, but also whether its parent
232
+ transaction committed. To implement multiple savepoints in a transaction we
233
+ allow unlimited transaction nesting depth, so any particular subtransaction's
234
+ commit state is dependent on the commit status of each and every ancestor
235
+ transaction.
215
236
216
237
The "subtransaction parent" (pg_subtrans) mechanism records, for each
217
- transaction, the TransactionId of its parent transaction. This information is
218
- stored as soon as the subtransaction is created. Top-level transactions do
219
- not have a parent, so they leave their pg_subtrans entries set to the default
220
- value of zero (InvalidTransactionId).
238
+ transaction with an XID , the TransactionId of its parent transaction. This
239
+ information is stored as soon as the subtransaction is assigned an XID.
240
+ Top-level transactions do not have a parent, so they leave their pg_subtrans
241
+ entries set to the default value of zero (InvalidTransactionId).
221
242
222
243
pg_subtrans is used to check whether the transaction in question is still
223
244
running --- the main Xid of a transaction is recorded in the PGPROC struct,
0 commit comments