OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / I686LINUX / util / I686LINUX / doc / postgresql / html / explicit-locking.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Explicit Locking</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="Transaction Isolation"
19 HREF="transaction-iso.html"><LINK
20 REL="NEXT"
21 TITLE="Data Consistency Checks at the Application Level"
22 HREF="applevel-consistency.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="transaction-iso.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="applevel-consistency.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="EXPLICIT-LOCKING"
96 >12.3. Explicit Locking</A
97 ></H1
98 ><A
99 NAME="AEN13534"
100 ></A
101 ><P
102 >    <SPAN
103 CLASS="PRODUCTNAME"
104 >PostgreSQL</SPAN
105 > provides various lock modes
106     to control concurrent access to data in tables.  These modes can
107     be used for application-controlled locking in situations where
108     <ACRONYM
109 CLASS="ACRONYM"
110 >MVCC</ACRONYM
111 > does not give the desired behavior.  Also,
112     most <SPAN
113 CLASS="PRODUCTNAME"
114 >PostgreSQL</SPAN
115 > commands automatically
116     acquire locks of appropriate modes to ensure that referenced
117     tables are not dropped or modified in incompatible ways while the
118     command executes.  (For example, <TT
119 CLASS="COMMAND"
120 >ALTER TABLE</TT
121 > cannot be
122     executed concurrently with other operations on the same table.)
123    </P
124 ><P
125 >    To examine a list of the currently outstanding locks in a database
126     server, use the <TT
127 CLASS="STRUCTNAME"
128 >pg_locks</TT
129 > system view
130     (<A
131 HREF="view-pg-locks.html"
132 >Section 43.32</A
133 >). For more
134     information on monitoring the status of the lock manager
135     subsystem, refer to <A
136 HREF="monitoring.html"
137 >Chapter 23</A
138 >.
139    </P
140 ><DIV
141 CLASS="SECT2"
142 ><H2
143 CLASS="SECT2"
144 ><A
145 NAME="LOCKING-TABLES"
146 >12.3.1. Table-Level Locks</A
147 ></H2
148 ><A
149 NAME="AEN13547"
150 ></A
151 ><P
152 >    The list below shows the available lock modes and the contexts in
153     which they are used automatically by
154     <SPAN
155 CLASS="PRODUCTNAME"
156 >PostgreSQL</SPAN
157 >.
158     Remember that all of these lock modes are table-level locks,
159     even if the name contains the word
160     <SPAN
161 CLASS="QUOTE"
162 >"row"</SPAN
163 >; the names of the lock modes are historical.
164     To some extent the names reflect the typical usage of each lock
165     mode --- but the semantics are all the same.  The only real difference
166     between one lock mode and another is the set of lock modes with
167     which each conflicts.  Two transactions cannot hold locks of conflicting
168     modes on the same table at the same time.  (However, a transaction
169     never conflicts with itself.  For example, it may acquire
170     <TT
171 CLASS="LITERAL"
172 >ACCESS EXCLUSIVE</TT
173 > lock and later acquire
174     <TT
175 CLASS="LITERAL"
176 >ACCESS SHARE</TT
177 > lock on the same table.)  Non-conflicting
178     lock modes may be held concurrently by many transactions.  Notice in
179     particular that some lock modes are self-conflicting (for example,
180     an <TT
181 CLASS="LITERAL"
182 >ACCESS EXCLUSIVE</TT
183 > lock cannot be held by more than one
184     transaction at a time) while others are not self-conflicting (for example,
185     an <TT
186 CLASS="LITERAL"
187 >ACCESS SHARE</TT
188 > lock can be held by multiple transactions).
189     Once acquired, a lock is held till end of transaction.
190    </P
191 ><P
192 ></P
193 ><DIV
194 CLASS="VARIABLELIST"
195 ><P
196 ><B
197 >Table-level lock modes</B
198 ></P
199 ><DL
200 ><DT
201 ><TT
202 CLASS="LITERAL"
203 >ACCESS SHARE</TT
204 ></DT
205 ><DD
206 ><P
207 >        Conflicts with the <TT
208 CLASS="LITERAL"
209 >ACCESS EXCLUSIVE</TT
210 > lock
211          mode only.
212         </P
213 ><P
214 >        The commands <TT
215 CLASS="COMMAND"
216 >SELECT</TT
217 > and
218          <TT
219 CLASS="COMMAND"
220 >ANALYZE</TT
221 > acquire a lock of this mode on
222          referenced tables.  In general, any query that only reads a table
223          and does not modify it will acquire this lock mode.
224         </P
225 ></DD
226 ><DT
227 ><TT
228 CLASS="LITERAL"
229 >ROW SHARE</TT
230 ></DT
231 ><DD
232 ><P
233 >        Conflicts with the <TT
234 CLASS="LITERAL"
235 >EXCLUSIVE</TT
236 > and
237          <TT
238 CLASS="LITERAL"
239 >ACCESS EXCLUSIVE</TT
240 > lock modes.
241         </P
242 ><P
243 >        The <TT
244 CLASS="COMMAND"
245 >SELECT FOR UPDATE</TT
246 > command acquires a
247          lock of this mode on the target table(s) (in addition to
248          <TT
249 CLASS="LITERAL"
250 >ACCESS SHARE</TT
251 > locks on any other tables
252          that are referenced but not selected <VAR
253 CLASS="OPTION"
254 >FOR UPDATE</VAR
255 >).
256         </P
257 ></DD
258 ><DT
259 ><TT
260 CLASS="LITERAL"
261 >ROW EXCLUSIVE</TT
262 ></DT
263 ><DD
264 ><P
265 >        Conflicts with the <TT
266 CLASS="LITERAL"
267 >SHARE</TT
268 >, <TT
269 CLASS="LITERAL"
270 >SHARE ROW
271          EXCLUSIVE</TT
272 >, <TT
273 CLASS="LITERAL"
274 >EXCLUSIVE</TT
275 >, and
276          <TT
277 CLASS="LITERAL"
278 >ACCESS EXCLUSIVE</TT
279 > lock modes.
280         </P
281 ><P
282 >        The commands <TT
283 CLASS="COMMAND"
284 >UPDATE</TT
285 >,
286          <TT
287 CLASS="COMMAND"
288 >DELETE</TT
289 >, and <TT
290 CLASS="COMMAND"
291 >INSERT</TT
292 >
293          acquire this lock mode on the target table (in addition to
294          <TT
295 CLASS="LITERAL"
296 >ACCESS SHARE</TT
297 > locks on any other referenced
298          tables).  In general, this lock mode will be acquired by any
299          command that modifies the data in a table.
300         </P
301 ></DD
302 ><DT
303 ><TT
304 CLASS="LITERAL"
305 >SHARE UPDATE EXCLUSIVE</TT
306 ></DT
307 ><DD
308 ><P
309 >        Conflicts with the <TT
310 CLASS="LITERAL"
311 >SHARE UPDATE EXCLUSIVE</TT
312 >,
313          <TT
314 CLASS="LITERAL"
315 >SHARE</TT
316 >, <TT
317 CLASS="LITERAL"
318 >SHARE ROW
319          EXCLUSIVE</TT
320 >, <TT
321 CLASS="LITERAL"
322 >EXCLUSIVE</TT
323 >, and
324          <TT
325 CLASS="LITERAL"
326 >ACCESS EXCLUSIVE</TT
327 > lock modes.
328          This mode protects a table against
329          concurrent schema changes and <TT
330 CLASS="COMMAND"
331 >VACUUM</TT
332 > runs.
333         </P
334 ><P
335 >        Acquired by <TT
336 CLASS="COMMAND"
337 >VACUUM</TT
338 > (without <VAR
339 CLASS="OPTION"
340 >FULL</VAR
341 >).
342         </P
343 ></DD
344 ><DT
345 ><TT
346 CLASS="LITERAL"
347 >SHARE</TT
348 ></DT
349 ><DD
350 ><P
351 >        Conflicts with the <TT
352 CLASS="LITERAL"
353 >ROW EXCLUSIVE</TT
354 >,
355          <TT
356 CLASS="LITERAL"
357 >SHARE UPDATE EXCLUSIVE</TT
358 >, <TT
359 CLASS="LITERAL"
360 >SHARE ROW
361          EXCLUSIVE</TT
362 >, <TT
363 CLASS="LITERAL"
364 >EXCLUSIVE</TT
365 >, and
366          <TT
367 CLASS="LITERAL"
368 >ACCESS EXCLUSIVE</TT
369 > lock modes.
370          This mode protects a table against concurrent data changes.
371         </P
372 ><P
373 >        Acquired by <TT
374 CLASS="COMMAND"
375 >CREATE INDEX</TT
376 >.
377         </P
378 ></DD
379 ><DT
380 ><TT
381 CLASS="LITERAL"
382 >SHARE ROW EXCLUSIVE</TT
383 ></DT
384 ><DD
385 ><P
386 >        Conflicts with the <TT
387 CLASS="LITERAL"
388 >ROW EXCLUSIVE</TT
389 >,
390          <TT
391 CLASS="LITERAL"
392 >SHARE UPDATE EXCLUSIVE</TT
393 >,
394          <TT
395 CLASS="LITERAL"
396 >SHARE</TT
397 >, <TT
398 CLASS="LITERAL"
399 >SHARE ROW
400          EXCLUSIVE</TT
401 >, <TT
402 CLASS="LITERAL"
403 >EXCLUSIVE</TT
404 >, and
405          <TT
406 CLASS="LITERAL"
407 >ACCESS EXCLUSIVE</TT
408 > lock modes.
409         </P
410 ><P
411 >         This lock mode is not automatically acquired by any
412          <SPAN
413 CLASS="PRODUCTNAME"
414 >PostgreSQL</SPAN
415 > command.
416         </P
417 ></DD
418 ><DT
419 ><TT
420 CLASS="LITERAL"
421 >EXCLUSIVE</TT
422 ></DT
423 ><DD
424 ><P
425 >        Conflicts with the <TT
426 CLASS="LITERAL"
427 >ROW SHARE</TT
428 >, <TT
429 CLASS="LITERAL"
430 >ROW
431          EXCLUSIVE</TT
432 >, <TT
433 CLASS="LITERAL"
434 >SHARE UPDATE
435          EXCLUSIVE</TT
436 >, <TT
437 CLASS="LITERAL"
438 >SHARE</TT
439 >, <TT
440 CLASS="LITERAL"
441 >SHARE
442          ROW EXCLUSIVE</TT
443 >, <TT
444 CLASS="LITERAL"
445 >EXCLUSIVE</TT
446 >, and
447          <TT
448 CLASS="LITERAL"
449 >ACCESS EXCLUSIVE</TT
450 > lock modes.
451          This mode allows only concurrent <TT
452 CLASS="LITERAL"
453 >ACCESS SHARE</TT
454 > locks,
455          i.e., only reads from the table can proceed in parallel with a
456          transaction holding this lock mode.
457         </P
458 ><P
459 >         This lock mode is not automatically acquired by any
460          <SPAN
461 CLASS="PRODUCTNAME"
462 >PostgreSQL</SPAN
463 > command.
464         </P
465 ></DD
466 ><DT
467 ><TT
468 CLASS="LITERAL"
469 >ACCESS EXCLUSIVE</TT
470 ></DT
471 ><DD
472 ><P
473 >        Conflicts with locks of all modes (<TT
474 CLASS="LITERAL"
475 >ACCESS
476          SHARE</TT
477 >, <TT
478 CLASS="LITERAL"
479 >ROW SHARE</TT
480 >, <TT
481 CLASS="LITERAL"
482 >ROW
483          EXCLUSIVE</TT
484 >, <TT
485 CLASS="LITERAL"
486 >SHARE UPDATE
487          EXCLUSIVE</TT
488 >, <TT
489 CLASS="LITERAL"
490 >SHARE</TT
491 >, <TT
492 CLASS="LITERAL"
493 >SHARE
494          ROW EXCLUSIVE</TT
495 >, <TT
496 CLASS="LITERAL"
497 >EXCLUSIVE</TT
498 >, and
499          <TT
500 CLASS="LITERAL"
501 >ACCESS EXCLUSIVE</TT
502 >).
503          This mode guarantees that the
504          holder is the only transaction accessing the table in any way.
505         </P
506 ><P
507 >        Acquired by the <TT
508 CLASS="COMMAND"
509 >ALTER TABLE</TT
510 >, <TT
511 CLASS="COMMAND"
512 >DROP
513          TABLE</TT
514 >, <TT
515 CLASS="COMMAND"
516 >REINDEX</TT
517 >,
518          <TT
519 CLASS="COMMAND"
520 >CLUSTER</TT
521 >, and <TT
522 CLASS="COMMAND"
523 >VACUUM FULL</TT
524 >
525          commands.  This is also the default lock mode for <TT
526 CLASS="COMMAND"
527 >LOCK
528          TABLE</TT
529 > statements that do not specify a mode explicitly.
530         </P
531 ></DD
532 ></DL
533 ></DIV
534 ><DIV
535 CLASS="TIP"
536 ><BLOCKQUOTE
537 CLASS="TIP"
538 ><P
539 ><B
540 >Tip: </B
541 >       Only an <TT
542 CLASS="LITERAL"
543 >ACCESS EXCLUSIVE</TT
544 > lock blocks a
545        <TT
546 CLASS="COMMAND"
547 >SELECT</TT
548 > (without <VAR
549 CLASS="OPTION"
550 >FOR UPDATE</VAR
551 >)
552        statement.
553       </P
554 ></BLOCKQUOTE
555 ></DIV
556 ></DIV
557 ><DIV
558 CLASS="SECT2"
559 ><H2
560 CLASS="SECT2"
561 ><A
562 NAME="LOCKING-ROWS"
563 >12.3.2. Row-Level Locks</A
564 ></H2
565 ><P
566 >     In addition to table-level locks, there are row-level locks.
567      A row-level lock on a specific row is automatically acquired when the
568      row is updated (or deleted or marked for update).  The lock is held
569      until the transaction commits or rolls back.
570      Row-level locks do not affect data
571      querying; they block <SPAN
572 CLASS="emphasis"
573 ><I
574 CLASS="EMPHASIS"
575 >writers to the same row</I
576 ></SPAN
577 >
578      only.  To acquire a row-level lock on a row without actually
579      modifying the row, select the row with <TT
580 CLASS="COMMAND"
581 >SELECT FOR
582      UPDATE</TT
583 >.  Note that once a particular row-level lock is
584      acquired, the transaction may update the row multiple times without
585      fear of conflicts.
586     </P
587 ><P
588 >     <SPAN
589 CLASS="PRODUCTNAME"
590 >PostgreSQL</SPAN
591 > doesn't remember any
592      information about modified rows in memory, so it has no limit to
593      the number of rows locked at one time.  However, locking a row
594      may cause a disk write; thus, for example, <TT
595 CLASS="COMMAND"
596 >SELECT FOR
597      UPDATE</TT
598 > will modify selected rows to mark them and so
599      will result in disk writes.
600     </P
601 ><P
602 >     In addition to table and row locks, page-level share/exclusive locks are
603      used to control read/write access to table pages in the shared buffer
604      pool.  These locks are released immediately after a row is fetched or
605      updated.  Application developers normally need not be concerned with
606      page-level locks, but we mention them for completeness.
607     </P
608 ></DIV
609 ><DIV
610 CLASS="SECT2"
611 ><H2
612 CLASS="SECT2"
613 ><A
614 NAME="LOCKING-DEADLOCKS"
615 >12.3.3. Deadlocks</A
616 ></H2
617 ><A
618 NAME="AEN13682"
619 ></A
620 ><P
621 >     The use of explicit locking can increase the likelihood of
622      <I
623 CLASS="FIRSTTERM"
624 >deadlocks</I
625 >, wherein two (or more) transactions each
626      hold locks that the other wants.  For example, if transaction 1
627      acquires an exclusive lock on table A and then tries to acquire
628      an exclusive lock on table B, while transaction 2 has already
629      exclusive-locked table B and now wants an exclusive lock on table
630      A, then neither one can proceed.
631      <SPAN
632 CLASS="PRODUCTNAME"
633 >PostgreSQL</SPAN
634 > automatically detects
635      deadlock situations and resolves them by aborting one of the
636      transactions involved, allowing the other(s) to complete.
637      (Exactly which transaction will be aborted is difficult to
638      predict and should not be relied on.)
639     </P
640 ><P
641 >     Note that deadlocks can also occur as the result of row-level
642      locks (and thus, they can occur even if explicit locking is not
643      used). Consider the case in which there are two concurrent
644      transactions modifying a table. The first transaction executes:
645
646 </P><PRE
647 CLASS="SCREEN"
648 >UPDATE accounts SET balance = balance + 100.00 WHERE acctnum = 11111;</PRE
649 ><P>
650
651      This acquires a row-level lock on the row with the specified
652      account number. Then, the second transaction executes:
653
654 </P><PRE
655 CLASS="SCREEN"
656 >UPDATE accounts SET balance = balance + 100.00 WHERE acctnum = 22222;
657 UPDATE accounts SET balance = balance - 100.00 WHERE acctnum = 11111;</PRE
658 ><P>
659
660      The first <TT
661 CLASS="COMMAND"
662 >UPDATE</TT
663 > statement successfully
664      acquires a row-level lock on the specified row, so it succeeds in
665      updating that row. However, the second <TT
666 CLASS="COMMAND"
667 >UPDATE</TT
668 >
669      statement finds that the row it is attempting to update has
670      already been locked, so it waits for the transaction that
671      acquired the lock to complete. Transaction two is now waiting on
672      transaction one to complete before it continues execution. Now,
673      transaction one executes:
674
675 </P><PRE
676 CLASS="SCREEN"
677 >UPDATE accounts SET balance = balance - 100.00 WHERE acctnum = 22222;</PRE
678 ><P>
679
680      Transaction one attempts to acquire a row-level lock on the
681      specified row, but it cannot: transaction two already holds such
682      a lock. So it waits for transaction two to complete. Thus,
683      transaction one is blocked on transaction two, and transaction
684      two is blocked on transaction one: a deadlock
685      condition. <SPAN
686 CLASS="PRODUCTNAME"
687 >PostgreSQL</SPAN
688 > will detect this
689      situation and abort one of the transactions.
690     </P
691 ><P
692 >     The best defense against deadlocks is generally to avoid them by
693      being certain that all applications using a database acquire
694      locks on multiple objects in a consistent order. That was the
695      reason for the previous deadlock example: if both transactions
696      had updated the rows in the same order, no deadlock would have
697      occurred. One should also ensure that the first lock acquired on
698      an object in a transaction is the highest mode that will be
699      needed for that object.  If it is not feasible to verify this in
700      advance, then deadlocks may be handled on-the-fly by retrying
701      transactions that are aborted due to deadlock.
702     </P
703 ><P
704 >     So long as no deadlock situation is detected, a transaction seeking
705      either a table-level or row-level lock will wait indefinitely for
706      conflicting locks to be released.  This means it is a bad idea for
707      applications to hold transactions open for long periods of time
708      (e.g., while waiting for user input).
709     </P
710 ></DIV
711 ></DIV
712 ><DIV
713 CLASS="NAVFOOTER"
714 ><HR
715 ALIGN="LEFT"
716 WIDTH="100%"><TABLE
717 SUMMARY="Footer navigation table"
718 WIDTH="100%"
719 BORDER="0"
720 CELLPADDING="0"
721 CELLSPACING="0"
722 ><TR
723 ><TD
724 WIDTH="33%"
725 ALIGN="left"
726 VALIGN="top"
727 ><A
728 HREF="transaction-iso.html"
729 ACCESSKEY="P"
730 >Prev</A
731 ></TD
732 ><TD
733 WIDTH="34%"
734 ALIGN="center"
735 VALIGN="top"
736 ><A
737 HREF="index.html"
738 ACCESSKEY="H"
739 >Home</A
740 ></TD
741 ><TD
742 WIDTH="33%"
743 ALIGN="right"
744 VALIGN="top"
745 ><A
746 HREF="applevel-consistency.html"
747 ACCESSKEY="N"
748 >Next</A
749 ></TD
750 ></TR
751 ><TR
752 ><TD
753 WIDTH="33%"
754 ALIGN="left"
755 VALIGN="top"
756 >Transaction Isolation</TD
757 ><TD
758 WIDTH="34%"
759 ALIGN="center"
760 VALIGN="top"
761 ><A
762 HREF="mvcc.html"
763 ACCESSKEY="U"
764 >Up</A
765 ></TD
766 ><TD
767 WIDTH="33%"
768 ALIGN="right"
769 VALIGN="top"
770 >Data Consistency Checks at the Application Level</TD
771 ></TR
772 ></TABLE
773 ></DIV
774 ></BODY
775 ></HTML
776 >