OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / I386LINUX / util / I386LINUX / doc / postgresql / html / transaction-iso.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Transaction Isolation</TITLE
6 ><META
7 NAME="GENERATOR"
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
9 REV="MADE"
10 HREF="mailto:pgsql-docs@postgresql.org"><LINK
11 REL="HOME"
12 TITLE="PostgreSQL 7.4.1 Documentation"
13 HREF="index.html"><LINK
14 REL="UP"
15 TITLE="Concurrency Control"
16 HREF="mvcc.html"><LINK
17 REL="PREVIOUS"
18 TITLE="Concurrency Control"
19 HREF="mvcc.html"><LINK
20 REL="NEXT"
21 TITLE="Explicit Locking"
22 HREF="explicit-locking.html"><LINK
23 REL="STYLESHEET"
24 TYPE="text/css"
25 HREF="stylesheet.css"><META
26 NAME="creation"
27 CONTENT="2003-12-22T03:48:47"></HEAD
28 ><BODY
29 CLASS="SECT1"
30 ><DIV
31 CLASS="NAVHEADER"
32 ><TABLE
33 SUMMARY="Header navigation table"
34 WIDTH="100%"
35 BORDER="0"
36 CELLPADDING="0"
37 CELLSPACING="0"
38 ><TR
39 ><TH
40 COLSPAN="5"
41 ALIGN="center"
42 VALIGN="bottom"
43 >PostgreSQL 7.4.1 Documentation</TH
44 ></TR
45 ><TR
46 ><TD
47 WIDTH="10%"
48 ALIGN="left"
49 VALIGN="top"
50 ><A
51 HREF="mvcc.html"
52 ACCESSKEY="P"
53 >Prev</A
54 ></TD
55 ><TD
56 WIDTH="10%"
57 ALIGN="left"
58 VALIGN="top"
59 ><A
60 HREF="mvcc.html"
61 >Fast Backward</A
62 ></TD
63 ><TD
64 WIDTH="60%"
65 ALIGN="center"
66 VALIGN="bottom"
67 >Chapter 12. Concurrency Control</TD
68 ><TD
69 WIDTH="10%"
70 ALIGN="right"
71 VALIGN="top"
72 ><A
73 HREF="mvcc.html"
74 >Fast Forward</A
75 ></TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="top"
80 ><A
81 HREF="explicit-locking.html"
82 ACCESSKEY="N"
83 >Next</A
84 ></TD
85 ></TR
86 ></TABLE
87 ><HR
88 ALIGN="LEFT"
89 WIDTH="100%"></DIV
90 ><DIV
91 CLASS="SECT1"
92 ><H1
93 CLASS="SECT1"
94 ><A
95 NAME="TRANSACTION-ISO"
96 >12.2. Transaction Isolation</A
97 ></H1
98 ><A
99 NAME="AEN13427"
100 ></A
101 ><P
102 >    The <ACRONYM
103 CLASS="ACRONYM"
104 >SQL</ACRONYM
105 > standard defines four levels of
106     transaction isolation in terms of three phenomena that must be
107     prevented between concurrent transactions.  These undesirable
108     phenomena are:
109
110     <P
111 ></P
112 ></P><DIV
113 CLASS="VARIABLELIST"
114 ><DL
115 ><DT
116 >dirty read
117        <A
118 NAME="AEN13434"
119 ></A
120 ></DT
121 ><DD
122 ><P
123 >       A transaction reads data written by a concurrent uncommitted transaction.
124        </P
125 ></DD
126 ><DT
127 >nonrepeatable read
128        <A
129 NAME="AEN13440"
130 ></A
131 ></DT
132 ><DD
133 ><P
134 >       A transaction re-reads data it has previously read and finds that data
135         has been modified by another transaction (that committed since the
136         initial read).
137        </P
138 ></DD
139 ><DT
140 >phantom read
141        <A
142 NAME="AEN13446"
143 ></A
144 ></DT
145 ><DD
146 ><P
147 >       A transaction re-executes a query returning a set of rows that satisfy a
148         search condition and finds that the set of rows satisfying the condition
149         has changed due to another recently-committed transaction.
150        </P
151 ></DD
152 ></DL
153 ></DIV
154 ><P>
155    </P
156 ><P
157 >    <A
158 NAME="AEN13451"
159 ></A
160 >
161     The four transaction isolation levels and the corresponding
162     behaviors are described in <A
163 HREF="transaction-iso.html#MVCC-ISOLEVEL-TABLE"
164 >Table 12-1</A
165 >.
166    </P
167 ><DIV
168 CLASS="TABLE"
169 ><A
170 NAME="MVCC-ISOLEVEL-TABLE"
171 ></A
172 ><P
173 ><B
174 >Table 12-1. <ACRONYM
175 CLASS="ACRONYM"
176 >SQL</ACRONYM
177 > Transaction Isolation Levels</B
178 ></P
179 ><TABLE
180 BORDER="1"
181 CLASS="CALSTABLE"
182 ><COL><COL><COL><COL><THEAD
183 ><TR
184 ><TH
185 >         Isolation Level
186         </TH
187 ><TH
188 >        Dirty Read
189         </TH
190 ><TH
191 >        Nonrepeatable Read
192         </TH
193 ><TH
194 >        Phantom Read
195         </TH
196 ></TR
197 ></THEAD
198 ><TBODY
199 ><TR
200 ><TD
201 >        Read uncommitted
202         </TD
203 ><TD
204 >        Possible
205         </TD
206 ><TD
207 >        Possible
208         </TD
209 ><TD
210 >        Possible
211         </TD
212 ></TR
213 ><TR
214 ><TD
215 >        Read committed
216         </TD
217 ><TD
218 >        Not possible
219         </TD
220 ><TD
221 >        Possible
222         </TD
223 ><TD
224 >        Possible
225         </TD
226 ></TR
227 ><TR
228 ><TD
229 >        Repeatable read
230         </TD
231 ><TD
232 >        Not possible
233         </TD
234 ><TD
235 >        Not possible
236         </TD
237 ><TD
238 >        Possible
239         </TD
240 ></TR
241 ><TR
242 ><TD
243 >        Serializable
244         </TD
245 ><TD
246 >        Not possible
247         </TD
248 ><TD
249 >        Not possible
250         </TD
251 ><TD
252 >        Not possible
253         </TD
254 ></TR
255 ></TBODY
256 ></TABLE
257 ></DIV
258 ><P
259 >    <SPAN
260 CLASS="PRODUCTNAME"
261 >PostgreSQL</SPAN
262 >
263     offers the Read Committed and Serializable isolation levels.
264    </P
265 ><DIV
266 CLASS="SECT2"
267 ><H2
268 CLASS="SECT2"
269 ><A
270 NAME="XACT-READ-COMMITTED"
271 >12.2.1. Read Committed Isolation Level</A
272 ></H2
273 ><A
274 NAME="AEN13489"
275 ></A
276 ><P
277 >    <I
278 CLASS="FIRSTTERM"
279 >Read Committed</I
280 >
281     is the default isolation level in <SPAN
282 CLASS="PRODUCTNAME"
283 >PostgreSQL</SPAN
284 >. 
285     When a transaction runs on this isolation level,
286     a <TT
287 CLASS="COMMAND"
288 >SELECT</TT
289 > query sees only data committed before the
290     query began; it never sees either uncommitted data or changes committed
291     during query execution by concurrent transactions.  (However, the
292     <TT
293 CLASS="COMMAND"
294 >SELECT</TT
295 > does see the effects of previous updates
296     executed within its own transaction, even though they are not yet
297     committed.)  In effect, a <TT
298 CLASS="COMMAND"
299 >SELECT</TT
300 > query
301     sees a snapshot of the database as of the instant that that query
302     begins to run.  Notice that two successive <TT
303 CLASS="COMMAND"
304 >SELECT</TT
305 > commands can
306     see different data, even though they are within a single transaction, if
307     other transactions 
308     commit changes during execution of the first <TT
309 CLASS="COMMAND"
310 >SELECT</TT
311 >.
312    </P
313 ><P
314 >    <TT
315 CLASS="COMMAND"
316 >UPDATE</TT
317 >, <TT
318 CLASS="COMMAND"
319 >DELETE</TT
320 >, and <TT
321 CLASS="COMMAND"
322 >SELECT
323     FOR UPDATE</TT
324 > commands behave the same as <TT
325 CLASS="COMMAND"
326 >SELECT</TT
327 >
328     in terms of searching for target rows: they will only find target rows
329     that were committed as of the command start time.  However, such a target
330     row may have already been updated (or deleted or marked for update) by
331     another concurrent transaction by the time it is found.  In this case, the
332     would-be updater will wait for the first updating transaction to commit or
333     roll back (if it is still in progress).  If the first updater rolls back,
334     then its effects are negated and the second updater can proceed with
335     updating the originally found row.  If the first updater commits, the
336     second updater will ignore the row if the first updater deleted it,
337     otherwise it will attempt to apply its operation to the updated version of
338     the row.  The search condition of the command (the <TT
339 CLASS="LITERAL"
340 >WHERE</TT
341 > clause) is
342     re-evaluated to see if the updated version of the row still matches the
343     search condition.  If so, the second updater proceeds with its operation,
344     starting from the updated version of the row.
345    </P
346 ><P
347 >    Because of the above rule, it is possible for an updating command to see an
348     inconsistent snapshot: it can see the effects of concurrent updating
349     commands that affected the same rows it is trying to update, but it
350     does not see effects of those commands on other rows in the database.
351     This behavior makes Read Committed mode unsuitable for commands that
352     involve complex search conditions.  However, it is just right for simpler
353     cases.  For example, consider updating bank balances with transactions
354     like
355
356 </P><PRE
357 CLASS="SCREEN"
358 >BEGIN;
359 UPDATE accounts SET balance = balance + 100.00 WHERE acctnum = 12345;
360 UPDATE accounts SET balance = balance - 100.00 WHERE acctnum = 7534;
361 COMMIT;</PRE
362 ><P>
363
364     If two such transactions concurrently try to change the balance of account
365     12345, we clearly want the second transaction to start from the updated
366     version of the account's row.  Because each command is affecting only a
367     predetermined row, letting it see the updated version of the row does
368     not create any troublesome inconsistency.
369    </P
370 ><P
371 >    Since in Read Committed mode each new command starts with a new snapshot
372     that includes all transactions committed up to that instant, subsequent
373     commands in the same transaction will see the effects of the committed
374     concurrent transaction in any case.  The point at issue here is whether
375     or not within a <SPAN
376 CLASS="emphasis"
377 ><I
378 CLASS="EMPHASIS"
379 >single</I
380 ></SPAN
381 > command we see an absolutely consistent
382     view of the database.
383    </P
384 ><P
385 >    The partial transaction isolation provided by Read Committed mode is
386     adequate for many applications, and this mode is fast and simple to use.
387     However, for applications that do complex queries and updates, it may
388     be necessary to guarantee a more rigorously consistent view of the
389     database than the Read Committed mode provides.
390    </P
391 ></DIV
392 ><DIV
393 CLASS="SECT2"
394 ><H2
395 CLASS="SECT2"
396 ><A
397 NAME="XACT-SERIALIZABLE"
398 >12.2.2. Serializable Isolation Level</A
399 ></H2
400 ><A
401 NAME="AEN13513"
402 ></A
403 ><P
404 >    The level <I
405 CLASS="FIRSTTERM"
406 >Serializable</I
407 > provides the strictest transaction
408     isolation.  This level emulates serial transaction execution,
409     as if transactions had been executed one after another, serially,
410     rather than concurrently.  However, applications using this level must
411     be prepared to retry transactions due to serialization failures.
412    </P
413 ><P
414 >    When a transaction is on the serializable level,
415     a <TT
416 CLASS="COMMAND"
417 >SELECT</TT
418 > query sees only data committed before the
419     transaction began; it never sees either uncommitted data or changes
420     committed
421     during transaction execution by concurrent transactions.  (However, the
422     <TT
423 CLASS="COMMAND"
424 >SELECT</TT
425 > does see the effects of previous updates
426     executed within its own transaction, even though they are not yet
427     committed.)  This is different from Read Committed in that the
428     <TT
429 CLASS="COMMAND"
430 >SELECT</TT
431 >
432     sees a snapshot as of the start of the transaction, not as of the start
433     of the current query within the transaction.  Thus, successive
434     <TT
435 CLASS="COMMAND"
436 >SELECT</TT
437 > commands within a single transaction always see the same
438     data.
439    </P
440 ><P
441 >    <TT
442 CLASS="COMMAND"
443 >UPDATE</TT
444 >, <TT
445 CLASS="COMMAND"
446 >DELETE</TT
447 >, and <TT
448 CLASS="COMMAND"
449 >SELECT
450     FOR UPDATE</TT
451 > commands behave the same as <TT
452 CLASS="COMMAND"
453 >SELECT</TT
454 >
455     in terms of searching for target rows: they will only find target rows
456     that were committed as of the transaction start time.  However, such a
457     target
458     row may have already been updated (or deleted or marked for update) by
459     another concurrent transaction by the time it is found.  In this case, the
460     serializable transaction will wait for the first updating transaction to commit or
461     roll back (if it is still in progress).  If the first updater rolls back,
462     then its effects are negated and the serializable transaction can proceed
463     with updating the originally found row.  But if the first updater commits
464     (and actually updated or deleted the row, not just selected it for update)
465     then the serializable transaction will be rolled back with the message
466
467 </P><PRE
468 CLASS="SCREEN"
469 >ERROR:  could not serialize access due to concurrent update</PRE
470 ><P>
471
472     because a serializable transaction cannot modify rows changed by
473     other transactions after the serializable transaction began.
474    </P
475 ><P
476 >    When the application receives this error message, it should abort
477     the current transaction and then retry the whole transaction from
478     the beginning.  The second time through, the transaction sees the
479     previously-committed change as part of its initial view of the database,
480     so there is no logical conflict in using the new version of the row
481     as the starting point for the new transaction's update.
482    </P
483 ><P
484 >    Note that only updating transactions may need to be retried; read-only
485     transactions will never have serialization conflicts.
486    </P
487 ><P
488 >    The Serializable mode provides a rigorous guarantee that each
489     transaction sees a wholly consistent view of the database.  However,
490     the application has to be prepared to retry transactions when concurrent
491     updates make it impossible to sustain the illusion of serial execution.
492     Since the cost of redoing complex transactions may be significant,
493     this mode is recommended only when updating transactions contain logic
494     sufficiently complex that they may give wrong answers in Read
495     Committed mode.  Most commonly, Serializable mode is necessary when
496     a transaction executes several successive commands that must see
497     identical views of the database.
498    </P
499 ></DIV
500 ></DIV
501 ><DIV
502 CLASS="NAVFOOTER"
503 ><HR
504 ALIGN="LEFT"
505 WIDTH="100%"><TABLE
506 SUMMARY="Footer navigation table"
507 WIDTH="100%"
508 BORDER="0"
509 CELLPADDING="0"
510 CELLSPACING="0"
511 ><TR
512 ><TD
513 WIDTH="33%"
514 ALIGN="left"
515 VALIGN="top"
516 ><A
517 HREF="mvcc.html"
518 ACCESSKEY="P"
519 >Prev</A
520 ></TD
521 ><TD
522 WIDTH="34%"
523 ALIGN="center"
524 VALIGN="top"
525 ><A
526 HREF="index.html"
527 ACCESSKEY="H"
528 >Home</A
529 ></TD
530 ><TD
531 WIDTH="33%"
532 ALIGN="right"
533 VALIGN="top"
534 ><A
535 HREF="explicit-locking.html"
536 ACCESSKEY="N"
537 >Next</A
538 ></TD
539 ></TR
540 ><TR
541 ><TD
542 WIDTH="33%"
543 ALIGN="left"
544 VALIGN="top"
545 >Concurrency Control</TD
546 ><TD
547 WIDTH="34%"
548 ALIGN="center"
549 VALIGN="top"
550 ><A
551 HREF="mvcc.html"
552 ACCESSKEY="U"
553 >Up</A
554 ></TD
555 ><TD
556 WIDTH="33%"
557 ALIGN="right"
558 VALIGN="top"
559 >Explicit Locking</TD
560 ></TR
561 ></TABLE
562 ></DIV
563 ></BODY
564 ></HTML
565 >