]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/lock.sgml
Create a "sort support" interface API for faster sorting.
[postgresql] / doc / src / sgml / ref / lock.sgml
1 <!--
2 doc/src/sgml/ref/lock.sgml
3 PostgreSQL documentation
4 -->
5
6 <refentry id="SQL-LOCK">
7  <refmeta>
8   <refentrytitle>LOCK</refentrytitle>
9   <manvolnum>7</manvolnum>
10   <refmiscinfo>SQL - Language Statements</refmiscinfo>
11  </refmeta>
12
13  <refnamediv>
14   <refname>LOCK</refname>
15   <refpurpose>lock a table</refpurpose>
16  </refnamediv>
17
18  <indexterm zone="sql-lock">
19   <primary>LOCK</primary>
20  </indexterm>
21
22  <refsynopsisdiv>
23 <synopsis>
24 LOCK [ TABLE ] [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [, ...] [ IN <replaceable class="PARAMETER">lockmode</replaceable> MODE ] [ NOWAIT ]
25
26 <phrase>where <replaceable class="PARAMETER">lockmode</replaceable> is one of:</phrase>
27
28     ACCESS SHARE | ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE EXCLUSIVE
29     | SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE | ACCESS EXCLUSIVE
30 </synopsis>
31  </refsynopsisdiv>
32
33  <refsect1>
34   <title>Description</title>
35
36   <para>
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
45    end.)
46   </para>
47
48   <para>
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.
67   </para>
68
69   <para>
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    &mdash; but it won't ensure that what the transaction reads corresponds to
79    the latest committed values.
80   </para>
81
82   <para>
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 &mdash; 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.
97   </para>
98
99   <para>
100    More information about the lock modes and locking strategies can be
101    found in <xref linkend="explicit-locking">.
102   </para>
103  </refsect1>
104
105  <refsect1>
106   <title>Parameters</title>
107
108   <variablelist>
109    <varlistentry>
110     <term><replaceable class="PARAMETER">name</replaceable></term>
111     <listitem>
112      <para>
113       The name (optionally schema-qualified) of an existing table to
114       lock.  If <literal>ONLY</> is specified, only that table is
115       locked.  If <literal>ONLY</> is not specified, the table and all
116       its descendant tables (if any) are locked.
117      </para>
118
119      <para>
120       The command <literal>LOCK TABLE a, b;</> is equivalent to
121       <literal>LOCK TABLE a; LOCK TABLE b;</>. The tables are locked
122       one-by-one in the order specified in the <command>LOCK
123       TABLE</command> command.
124      </para>
125     </listitem>
126    </varlistentry>
127
128    <varlistentry>
129     <term><replaceable class="parameter">lockmode</replaceable></term>
130     <listitem>
131      <para>
132       The lock mode specifies which locks this lock conflicts with.
133       Lock modes are described in <xref linkend="explicit-locking">.
134      </para>
135
136      <para>
137       If no lock mode is specified, then <literal>ACCESS
138       EXCLUSIVE</literal>, the most restrictive mode, is used.
139      </para>
140     </listitem>
141    </varlistentry>
142
143    <varlistentry>
144     <term><literal>NOWAIT</literal></term>
145     <listitem>
146      <para>
147       Specifies that <command>LOCK TABLE</command> should not wait for
148       any conflicting locks to be released: if the specified lock(s)
149       cannot be acquired immediately without waiting, the transaction
150       is aborted.
151      </para>
152     </listitem>
153    </varlistentry>
154   </variablelist>
155  </refsect1>
156
157  <refsect1>
158   <title>Notes</title>
159
160    <para>
161     <literal>LOCK TABLE ... IN ACCESS SHARE MODE</> requires <literal>SELECT</>
162     privileges on the target table.  All other forms of <command>LOCK</>
163     require table-level <literal>UPDATE</>, <literal>DELETE</>, or
164     <literal>TRUNCATE</> privileges.
165    </para>
166
167    <para>
168     <command>LOCK TABLE</> is useless outside a transaction block: the lock
169     would remain held only to the completion of the statement.  Therefore
170     <productname>PostgreSQL</productname> reports an error if <command>LOCK</>
171     is used outside a transaction block.
172     Use
173     <xref linkend="sql-begin"> and
174     <xref linkend="sql-commit">
175     (or <xref linkend="sql-rollback">)
176     to define a transaction block.
177    </para>
178
179   <para>
180    <command>LOCK TABLE</> only deals with table-level locks, and so
181    the mode names involving <literal>ROW</> are all misnomers.  These
182    mode names should generally be read as indicating the intention of
183    the user to acquire row-level locks within the locked table.  Also,
184    <literal>ROW EXCLUSIVE</> mode is a sharable table lock.  Keep in
185    mind that all the lock modes have identical semantics so far as
186    <command>LOCK TABLE</> is concerned, differing only in the rules
187    about which modes conflict with which. For information on how to
188    acquire an actual row-level lock, see <xref linkend="locking-rows">
189    and the <xref linkend="sql-for-update-share"
190    endterm="sql-for-update-share-title"> in the <command>SELECT</command>
191    reference documentation.
192   </para>
193  </refsect1>
194
195  <refsect1>
196   <title>Examples</title>
197
198   <para>
199    Obtain a <literal>SHARE</> lock on a primary key table when going to perform
200    inserts into a foreign key table:
201
202 <programlisting>
203 BEGIN WORK;
204 LOCK TABLE films IN SHARE MODE;
205 SELECT id FROM films
206     WHERE name = 'Star Wars: Episode I - The Phantom Menace';
207 -- Do ROLLBACK if record was not returned
208 INSERT INTO films_user_comments VALUES
209     (_id_, 'GREAT! I was waiting for it for so long!');
210 COMMIT WORK;
211 </programlisting>
212   </para>
213
214   <para>
215    Take a <literal>SHARE ROW EXCLUSIVE</> lock on a primary key table when going to perform
216    a delete operation:
217
218 <programlisting>
219 BEGIN WORK;
220 LOCK TABLE films IN SHARE ROW EXCLUSIVE MODE;
221 DELETE FROM films_user_comments WHERE id IN
222     (SELECT id FROM films WHERE rating &lt; 5);
223 DELETE FROM films WHERE rating &lt; 5;
224 COMMIT WORK;
225 </programlisting></para>
226  </refsect1>
227
228  <refsect1>
229   <title>Compatibility</title>
230
231   <para>
232    There is no <command>LOCK TABLE</command> in the SQL standard,
233    which instead uses <command>SET TRANSACTION</command> to specify
234    concurrency levels on transactions.  <productname>PostgreSQL</productname> supports that too;
235    see <xref linkend="SQL-SET-TRANSACTION"> for details.
236   </para>
237
238   <para>
239    Except for <literal>ACCESS SHARE</>, <literal>ACCESS EXCLUSIVE</>,
240    and <literal>SHARE UPDATE EXCLUSIVE</> lock modes, the
241    <productname>PostgreSQL</productname> lock modes and the
242    <command>LOCK TABLE</command> syntax are compatible with those
243    present in <productname>Oracle</productname>.
244   </para>
245  </refsect1>
246 </refentry>