]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/lock.sgml
Add support for piping COPY to/from an external program.
[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 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.
119      </para>
120
121      <para>
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.
126      </para>
127     </listitem>
128    </varlistentry>
129
130    <varlistentry>
131     <term><replaceable class="parameter">lockmode</replaceable></term>
132     <listitem>
133      <para>
134       The lock mode specifies which locks this lock conflicts with.
135       Lock modes are described in <xref linkend="explicit-locking">.
136      </para>
137
138      <para>
139       If no lock mode is specified, then <literal>ACCESS
140       EXCLUSIVE</literal>, the most restrictive mode, is used.
141      </para>
142     </listitem>
143    </varlistentry>
144
145    <varlistentry>
146     <term><literal>NOWAIT</literal></term>
147     <listitem>
148      <para>
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
152       is aborted.
153      </para>
154     </listitem>
155    </varlistentry>
156   </variablelist>
157  </refsect1>
158
159  <refsect1>
160   <title>Notes</title>
161
162    <para>
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.
167    </para>
168
169    <para>
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.
174     Use
175     <xref linkend="sql-begin"> and
176     <xref linkend="sql-commit">
177     (or <xref linkend="sql-rollback">)
178     to define a transaction block.
179    </para>
180
181   <para>
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.
194   </para>
195  </refsect1>
196
197  <refsect1>
198   <title>Examples</title>
199
200   <para>
201    Obtain a <literal>SHARE</> lock on a primary key table when going to perform
202    inserts into a foreign key table:
203
204 <programlisting>
205 BEGIN WORK;
206 LOCK TABLE films IN SHARE MODE;
207 SELECT id FROM films
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!');
212 COMMIT WORK;
213 </programlisting>
214   </para>
215
216   <para>
217    Take a <literal>SHARE ROW EXCLUSIVE</> lock on a primary key table when going to perform
218    a delete operation:
219
220 <programlisting>
221 BEGIN WORK;
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 &lt; 5);
225 DELETE FROM films WHERE rating &lt; 5;
226 COMMIT WORK;
227 </programlisting></para>
228  </refsect1>
229
230  <refsect1>
231   <title>Compatibility</title>
232
233   <para>
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.
238   </para>
239
240   <para>
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>.
246   </para>
247  </refsect1>
248 </refentry>