OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / I686LINUX / util / I686LINUX / doc / postgresql / html / tutorial-transactions.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Transactions</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="Advanced Features"
16 HREF="tutorial-advanced.html"><LINK
17 REL="PREVIOUS"
18 TITLE="Foreign Keys"
19 HREF="tutorial-fk.html"><LINK
20 REL="NEXT"
21 TITLE="Inheritance"
22 HREF="tutorial-inheritance.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="tutorial-fk.html"
52 ACCESSKEY="P"
53 >Prev</A
54 ></TD
55 ><TD
56 WIDTH="10%"
57 ALIGN="left"
58 VALIGN="top"
59 ><A
60 HREF="tutorial-advanced.html"
61 >Fast Backward</A
62 ></TD
63 ><TD
64 WIDTH="60%"
65 ALIGN="center"
66 VALIGN="bottom"
67 >Chapter 3. Advanced Features</TD
68 ><TD
69 WIDTH="10%"
70 ALIGN="right"
71 VALIGN="top"
72 ><A
73 HREF="tutorial-advanced.html"
74 >Fast Forward</A
75 ></TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="top"
80 ><A
81 HREF="tutorial-inheritance.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="TUTORIAL-TRANSACTIONS"
96 >3.4. Transactions</A
97 ></H1
98 ><A
99 NAME="AEN919"
100 ></A
101 ><P
102 >    <I
103 CLASS="FIRSTTERM"
104 >Transactions</I
105 > are a fundamental concept of all database
106     systems.  The essential point of a transaction is that it bundles
107     multiple steps into a single, all-or-nothing operation.  The intermediate
108     states between the steps are not visible to other concurrent transactions,
109     and if some failure occurs that prevents the transaction from completing,
110     then none of the steps affect the database at all.
111    </P
112 ><P
113 >    For example, consider a bank database that contains balances for various
114     customer accounts, as well as total deposit balances for branches.
115     Suppose that we want to record a payment of $100.00 from Alice's account
116     to Bob's account.  Simplifying outrageously, the SQL commands for this
117     might look like
118
119 </P><PRE
120 CLASS="PROGRAMLISTING"
121 >UPDATE accounts SET balance = balance - 100.00
122     WHERE name = 'Alice';
123 UPDATE branches SET balance = balance - 100.00
124     WHERE name = (SELECT branch_name FROM accounts WHERE name = 'Alice');
125 UPDATE accounts SET balance = balance + 100.00
126     WHERE name = 'Bob';
127 UPDATE branches SET balance = balance + 100.00
128     WHERE name = (SELECT branch_name FROM accounts WHERE name = 'Bob');</PRE
129 ><P>
130    </P
131 ><P
132 >    The details of these commands are not important here; the important
133     point is that there are several separate updates involved to accomplish
134     this rather simple operation.  Our bank's officers will want to be
135     assured that either all these updates happen, or none of them happen.
136     It would certainly not do for a system failure to result in Bob
137     receiving $100.00 that was not debited from Alice.  Nor would Alice long
138     remain a happy customer if she was debited without Bob being credited.
139     We need a guarantee that if something goes wrong partway through the
140     operation, none of the steps executed so far will take effect.  Grouping
141     the updates into a <I
142 CLASS="FIRSTTERM"
143 >transaction</I
144 > gives us this guarantee.
145     A transaction is said to be <I
146 CLASS="FIRSTTERM"
147 >atomic</I
148 >: from the point of
149     view of other transactions, it either happens completely or not at all.
150    </P
151 ><P
152 >    We also want a
153     guarantee that once a transaction is completed and acknowledged by
154     the database system, it has indeed been permanently recorded
155     and won't be lost even if a crash ensues shortly thereafter.
156     For example, if we are recording a cash withdrawal by Bob,
157     we do not want any chance that the debit to his account will
158     disappear in a crash just as he walks out the bank door.
159     A transactional database guarantees that all the updates made by
160     a transaction are logged in permanent storage (i.e., on disk) before
161     the transaction is reported complete.
162    </P
163 ><P
164 >    Another important property of transactional databases is closely
165     related to the notion of atomic updates: when multiple transactions
166     are running concurrently, each one should not be able to see the
167     incomplete changes made by others.  For example, if one transaction
168     is busy totalling all the branch balances, it would not do for it
169     to include the debit from Alice's branch but not the credit to
170     Bob's branch, nor vice versa.  So transactions must be all-or-nothing
171     not only in terms of their permanent effect on the database, but
172     also in terms of their visibility as they happen.  The updates made
173     so far by an open transaction are invisible to other transactions
174     until the transaction completes, whereupon all the updates become
175     visible simultaneously.
176    </P
177 ><P
178 >    In <SPAN
179 CLASS="PRODUCTNAME"
180 >PostgreSQL</SPAN
181 >, a transaction is set up by surrounding
182     the SQL commands of the transaction with
183     <TT
184 CLASS="COMMAND"
185 >BEGIN</TT
186 > and <TT
187 CLASS="COMMAND"
188 >COMMIT</TT
189 > commands.  So our banking
190     transaction would actually look like
191
192 </P><PRE
193 CLASS="PROGRAMLISTING"
194 >BEGIN;
195 UPDATE accounts SET balance = balance - 100.00
196     WHERE name = 'Alice';
197 -- etc etc
198 COMMIT;</PRE
199 ><P>
200    </P
201 ><P
202 >    If, partway through the transaction, we decide we do not want to
203     commit (perhaps we just noticed that Alice's balance went negative),
204     we can issue the command <TT
205 CLASS="COMMAND"
206 >ROLLBACK</TT
207 > instead of
208     <TT
209 CLASS="COMMAND"
210 >COMMIT</TT
211 >, and all our updates so far will be canceled.
212    </P
213 ><P
214 >    <SPAN
215 CLASS="PRODUCTNAME"
216 >PostgreSQL</SPAN
217 > actually treats every SQL statement as being
218     executed within a transaction.  If you do not issue a <TT
219 CLASS="COMMAND"
220 >BEGIN</TT
221 >
222     command, 
223     then each individual statement has an implicit <TT
224 CLASS="COMMAND"
225 >BEGIN</TT
226 > and
227     (if successful) <TT
228 CLASS="COMMAND"
229 >COMMIT</TT
230 > wrapped around it.  A group of
231     statements surrounded by <TT
232 CLASS="COMMAND"
233 >BEGIN</TT
234 > and <TT
235 CLASS="COMMAND"
236 >COMMIT</TT
237 >
238     is sometimes called a <I
239 CLASS="FIRSTTERM"
240 >transaction block</I
241 >.
242    </P
243 ><DIV
244 CLASS="NOTE"
245 ><BLOCKQUOTE
246 CLASS="NOTE"
247 ><P
248 ><B
249 >Note: </B
250 >     Some client libraries issue <TT
251 CLASS="COMMAND"
252 >BEGIN</TT
253 > and <TT
254 CLASS="COMMAND"
255 >COMMIT</TT
256 >
257      commands automatically, so that you may get the effect of transaction
258      blocks without asking.  Check the documentation for the interface
259      you are using.
260     </P
261 ></BLOCKQUOTE
262 ></DIV
263 ></DIV
264 ><DIV
265 CLASS="NAVFOOTER"
266 ><HR
267 ALIGN="LEFT"
268 WIDTH="100%"><TABLE
269 SUMMARY="Footer navigation table"
270 WIDTH="100%"
271 BORDER="0"
272 CELLPADDING="0"
273 CELLSPACING="0"
274 ><TR
275 ><TD
276 WIDTH="33%"
277 ALIGN="left"
278 VALIGN="top"
279 ><A
280 HREF="tutorial-fk.html"
281 ACCESSKEY="P"
282 >Prev</A
283 ></TD
284 ><TD
285 WIDTH="34%"
286 ALIGN="center"
287 VALIGN="top"
288 ><A
289 HREF="index.html"
290 ACCESSKEY="H"
291 >Home</A
292 ></TD
293 ><TD
294 WIDTH="33%"
295 ALIGN="right"
296 VALIGN="top"
297 ><A
298 HREF="tutorial-inheritance.html"
299 ACCESSKEY="N"
300 >Next</A
301 ></TD
302 ></TR
303 ><TR
304 ><TD
305 WIDTH="33%"
306 ALIGN="left"
307 VALIGN="top"
308 >Foreign Keys</TD
309 ><TD
310 WIDTH="34%"
311 ALIGN="center"
312 VALIGN="top"
313 ><A
314 HREF="tutorial-advanced.html"
315 ACCESSKEY="U"
316 >Up</A
317 ></TD
318 ><TD
319 WIDTH="33%"
320 ALIGN="right"
321 VALIGN="top"
322 >Inheritance</TD
323 ></TR
324 ></TABLE
325 ></DIV
326 ></BODY
327 ></HTML
328 >