2 doc/src/sgml/ref/lock.sgml
3 PostgreSQL documentation
6 <refentry id="SQL-LOCK">
8 <refentrytitle>LOCK</refentrytitle>
9 <manvolnum>7</manvolnum>
10 <refmiscinfo>SQL - Language Statements</refmiscinfo>
14 <refname>LOCK</refname>
15 <refpurpose>lock a table</refpurpose>
18 <indexterm zone="sql-lock">
19 <primary>LOCK</primary>
24 LOCK [ TABLE ] [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ] [, ...] [ IN <replaceable class="PARAMETER">lockmode</replaceable> MODE ] [ NOWAIT ]
26 <phrase>where <replaceable class="PARAMETER">lockmode</replaceable> is one of:</phrase>
28 ACCESS SHARE | ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE EXCLUSIVE
29 | SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE | ACCESS EXCLUSIVE
34 <title>Description</title>
37 <command>LOCK TABLE</command> obtains a table-level lock, waiting
38 if necessary for any conflicting locks to be released. If
39 <literal>NOWAIT</literal> is specified, <command>LOCK
40 TABLE</command> does not wait to acquire the desired lock: if it
41 cannot be acquired immediately, the command is aborted and an
42 error is emitted. Once obtained, the lock is held for the
43 remainder of the current transaction. (There is no <command>UNLOCK
44 TABLE</command> command; locks are always released at transaction
49 When acquiring locks automatically for commands that reference
50 tables, <productname>PostgreSQL</productname> always uses the least
51 restrictive lock mode possible. <command>LOCK TABLE</command>
52 provides for cases when you might need more restrictive locking.
53 For example, suppose an application runs a transaction at the
54 Read Committed isolation level and needs to ensure that data in a
55 table remains stable for the duration of the transaction. To
56 achieve this you could obtain <literal>SHARE</> lock mode over the
57 table before querying. This will prevent concurrent data changes
58 and ensure subsequent reads of the table see a stable view of
59 committed data, because <literal>SHARE</> lock mode conflicts with
60 the <literal>ROW EXCLUSIVE</> lock acquired by writers, and your
61 <command>LOCK TABLE <replaceable
62 class="PARAMETER">name</replaceable> IN SHARE MODE</command>
63 statement will wait until any concurrent holders of <literal>ROW
64 EXCLUSIVE</literal> mode locks commit or roll back. Thus, once you
65 obtain the lock, there are no uncommitted writes outstanding;
66 furthermore none can begin until you release the lock.
70 To achieve a similar effect when running a transaction at the
71 <literal>REPEATABLE READ</> or <literal>SERIALIZABLE</>
72 isolation level, you have to execute the <command>LOCK TABLE</> statement
73 before executing any <command>SELECT</> or data modification statement.
74 A <literal>REPEATABLE READ</> or <literal>SERIALIZABLE</> transaction's
75 view of data will be frozen when its first
76 <command>SELECT</> or data modification statement begins. A <command>LOCK
77 TABLE</> later in the transaction will still prevent concurrent writes
78 — but it won't ensure that what the transaction reads corresponds to
79 the latest committed values.
83 If a transaction of this sort is going to change the data in the
84 table, then it should use <literal>SHARE ROW EXCLUSIVE</> lock mode
85 instead of <literal>SHARE</> mode. This ensures that only one
86 transaction of this type runs at a time. Without this, a deadlock
87 is possible: two transactions might both acquire <literal>SHARE</>
88 mode, and then be unable to also acquire <literal>ROW EXCLUSIVE</>
89 mode to actually perform their updates. (Note that a transaction's
90 own locks never conflict, so a transaction can acquire <literal>ROW
91 EXCLUSIVE</> mode when it holds <literal>SHARE</> mode — but not
92 if anyone else holds <literal>SHARE</> mode.) To avoid deadlocks,
93 make sure all transactions acquire locks on the same objects in the
94 same order, and if multiple lock modes are involved for a single
95 object, then transactions should always acquire the most
96 restrictive mode first.
100 More information about the lock modes and locking strategies can be
101 found in <xref linkend="explicit-locking">.
106 <title>Parameters</title>
110 <term><replaceable class="PARAMETER">name</replaceable></term>
113 The name (optionally schema-qualified) of an existing table to
114 lock. If <literal>ONLY</> is specified before the table name, only that
115 table is locked. If <literal>ONLY</> is not specified, the table and all
116 its descendant tables (if any) are locked. Optionally, <literal>*</>
117 can be specified after the table name to explicitly indicate that
118 descendant tables are included.
122 The command <literal>LOCK TABLE a, b;</> is equivalent to
123 <literal>LOCK TABLE a; LOCK TABLE b;</>. The tables are locked
124 one-by-one in the order specified in the <command>LOCK
125 TABLE</command> command.
131 <term><replaceable class="parameter">lockmode</replaceable></term>
134 The lock mode specifies which locks this lock conflicts with.
135 Lock modes are described in <xref linkend="explicit-locking">.
139 If no lock mode is specified, then <literal>ACCESS
140 EXCLUSIVE</literal>, the most restrictive mode, is used.
146 <term><literal>NOWAIT</literal></term>
149 Specifies that <command>LOCK TABLE</command> should not wait for
150 any conflicting locks to be released: if the specified lock(s)
151 cannot be acquired immediately without waiting, the transaction
163 <literal>LOCK TABLE ... IN ACCESS SHARE MODE</> requires <literal>SELECT</>
164 privileges on the target table. All other forms of <command>LOCK</>
165 require table-level <literal>UPDATE</>, <literal>DELETE</>, or
166 <literal>TRUNCATE</> privileges.
170 <command>LOCK TABLE</> is useless outside a transaction block: the lock
171 would remain held only to the completion of the statement. Therefore
172 <productname>PostgreSQL</productname> reports an error if <command>LOCK</>
173 is used outside a transaction block.
175 <xref linkend="sql-begin"> and
176 <xref linkend="sql-commit">
177 (or <xref linkend="sql-rollback">)
178 to define a transaction block.
182 <command>LOCK TABLE</> only deals with table-level locks, and so
183 the mode names involving <literal>ROW</> are all misnomers. These
184 mode names should generally be read as indicating the intention of
185 the user to acquire row-level locks within the locked table. Also,
186 <literal>ROW EXCLUSIVE</> mode is a sharable table lock. Keep in
187 mind that all the lock modes have identical semantics so far as
188 <command>LOCK TABLE</> is concerned, differing only in the rules
189 about which modes conflict with which. For information on how to
190 acquire an actual row-level lock, see <xref linkend="locking-rows">
191 and the <xref linkend="sql-for-update-share"
192 endterm="sql-for-update-share-title"> in the <command>SELECT</command>
193 reference documentation.
198 <title>Examples</title>
201 Obtain a <literal>SHARE</> lock on a primary key table when going to perform
202 inserts into a foreign key table:
206 LOCK TABLE films IN SHARE MODE;
208 WHERE name = 'Star Wars: Episode I - The Phantom Menace';
209 -- Do ROLLBACK if record was not returned
210 INSERT INTO films_user_comments VALUES
211 (_id_, 'GREAT! I was waiting for it for so long!');
217 Take a <literal>SHARE ROW EXCLUSIVE</> lock on a primary key table when going to perform
222 LOCK TABLE films IN SHARE ROW EXCLUSIVE MODE;
223 DELETE FROM films_user_comments WHERE id IN
224 (SELECT id FROM films WHERE rating < 5);
225 DELETE FROM films WHERE rating < 5;
227 </programlisting></para>
231 <title>Compatibility</title>
234 There is no <command>LOCK TABLE</command> in the SQL standard,
235 which instead uses <command>SET TRANSACTION</command> to specify
236 concurrency levels on transactions. <productname>PostgreSQL</productname> supports that too;
237 see <xref linkend="SQL-SET-TRANSACTION"> for details.
241 Except for <literal>ACCESS SHARE</>, <literal>ACCESS EXCLUSIVE</>,
242 and <literal>SHARE UPDATE EXCLUSIVE</> lock modes, the
243 <productname>PostgreSQL</productname> lock modes and the
244 <command>LOCK TABLE</command> syntax are compatible with those
245 present in <productname>Oracle</productname>.