1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
5 >Explicit Locking</TITLE
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
10 HREF="mailto:pgsql-docs@postgresql.org"><LINK
12 TITLE="PostgreSQL 7.4.1 Documentation"
13 HREF="index.html"><LINK
15 TITLE="Concurrency Control"
16 HREF="mvcc.html"><LINK
18 TITLE="Transaction Isolation"
19 HREF="transaction-iso.html"><LINK
21 TITLE="Data Consistency Checks at the Application Level"
22 HREF="applevel-consistency.html"><LINK
25 HREF="stylesheet.css"><META
27 CONTENT="2003-12-22T03:48:47"></HEAD
33 SUMMARY="Header navigation table"
43 >PostgreSQL 7.4.1 Documentation</TH
51 HREF="transaction-iso.html"
67 >Chapter 12. Concurrency Control</TD
81 HREF="applevel-consistency.html"
95 NAME="EXPLICIT-LOCKING"
96 >12.3. Explicit Locking</A
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
111 > does not give the desired behavior. Also,
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
122 executed concurrently with other operations on the same table.)
125 > To examine a list of the currently outstanding locks in a database
131 HREF="view-pg-locks.html"
134 information on monitoring the status of the lock manager
135 subsystem, refer to <A
136 HREF="monitoring.html"
145 NAME="LOCKING-TABLES"
146 >12.3.1. Table-Level Locks</A
152 > The list below shows the available lock modes and the contexts in
153 which they are used automatically by
158 Remember that all of these lock modes are table-level locks,
159 even if the name contains the word
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
172 >ACCESS EXCLUSIVE</TT
173 > lock and later acquire
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,
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,
188 > lock can be held by multiple transactions).
189 Once acquired, a lock is held till end of transaction.
197 >Table-level lock modes</B
207 > Conflicts with the <TT
209 >ACCESS EXCLUSIVE</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.
233 > Conflicts with the <TT
239 >ACCESS EXCLUSIVE</TT
245 >SELECT FOR UPDATE</TT
247 lock of this mode on the target table(s) (in addition to
251 > locks on any other tables
252 that are referenced but not selected <VAR
265 > Conflicts with the <TT
278 >ACCESS EXCLUSIVE</TT
293 acquire this lock mode on the target table (in addition to
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.
305 >SHARE UPDATE EXCLUSIVE</TT
309 > Conflicts with the <TT
311 >SHARE UPDATE EXCLUSIVE</TT
326 >ACCESS EXCLUSIVE</TT
328 This mode protects a table against
329 concurrent schema changes and <TT
351 > Conflicts with the <TT
357 >SHARE UPDATE EXCLUSIVE</TT
368 >ACCESS EXCLUSIVE</TT
370 This mode protects a table against concurrent data changes.
382 >SHARE ROW EXCLUSIVE</TT
386 > Conflicts with the <TT
392 >SHARE UPDATE EXCLUSIVE</TT
407 >ACCESS EXCLUSIVE</TT
411 > This lock mode is not automatically acquired by any
425 > Conflicts with the <TT
449 >ACCESS EXCLUSIVE</TT
451 This mode allows only concurrent <TT
455 i.e., only reads from the table can proceed in parallel with a
456 transaction holding this lock mode.
459 > This lock mode is not automatically acquired by any
469 >ACCESS EXCLUSIVE</TT
473 > Conflicts with locks of all modes (<TT
501 >ACCESS EXCLUSIVE</TT
503 This mode guarantees that the
504 holder is the only transaction accessing the table in any way.
507 > Acquired by the <TT
525 commands. This is also the default lock mode for <TT
529 > statements that do not specify a mode explicitly.
543 >ACCESS EXCLUSIVE</TT
563 >12.3.2. Row-Level Locks</A
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
575 >writers to the same row</I
578 only. To acquire a row-level lock on a row without actually
579 modifying the row, select the row with <TT
583 >. Note that once a particular row-level lock is
584 acquired, the transaction may update the row multiple times without
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
598 > will modify selected rows to mark them and so
599 will result in disk writes.
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.
614 NAME="LOCKING-DEADLOCKS"
615 >12.3.3. Deadlocks</A
621 > The use of explicit locking can increase the likelihood of
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.
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.)
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:
648 >UPDATE accounts SET balance = balance + 100.00 WHERE acctnum = 11111;</PRE
651 This acquires a row-level lock on the row with the specified
652 account number. Then, the second transaction executes:
656 >UPDATE accounts SET balance = balance + 100.00 WHERE acctnum = 22222;
657 UPDATE accounts SET balance = balance - 100.00 WHERE acctnum = 11111;</PRE
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
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:
677 >UPDATE accounts SET balance = balance - 100.00 WHERE acctnum = 22222;</PRE
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
689 situation and abort one of the transactions.
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.
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).
717 SUMMARY="Footer navigation table"
728 HREF="transaction-iso.html"
746 HREF="applevel-consistency.html"
756 >Transaction Isolation</TD
770 >Data Consistency Checks at the Application Level</TD