1999-07-22 17:09:15 +02:00
|
|
|
<!--
|
2004-03-12 01:52:23 +01:00
|
|
|
$PostgreSQL: pgsql/doc/src/sgml/ref/lock.sgml,v 1.42 2004/03/12 00:52:23 neilc Exp $
|
2001-12-08 04:24:40 +01:00
|
|
|
PostgreSQL documentation
|
1999-07-22 17:09:15 +02:00
|
|
|
-->
|
|
|
|
|
1999-06-04 17:50:44 +02:00
|
|
|
<refentry id="SQL-LOCK">
|
|
|
|
<refmeta>
|
2002-04-21 21:02:39 +02:00
|
|
|
<refentrytitle id="sql-lock-title">LOCK</refentrytitle>
|
1999-06-04 17:50:44 +02:00
|
|
|
<refmiscinfo>SQL - Language Statements</refmiscinfo>
|
|
|
|
</refmeta>
|
2003-04-27 01:56:51 +02:00
|
|
|
|
1999-06-04 17:50:44 +02:00
|
|
|
<refnamediv>
|
2003-04-27 01:56:51 +02:00
|
|
|
<refname>LOCK</refname>
|
|
|
|
<refpurpose>lock a table</refpurpose>
|
1999-05-20 07:40:27 +02:00
|
|
|
</refnamediv>
|
2003-04-27 01:56:51 +02:00
|
|
|
|
2003-08-31 19:32:24 +02:00
|
|
|
<indexterm zone="sql-lock">
|
|
|
|
<primary>LOCK</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
1999-06-04 17:50:44 +02:00
|
|
|
<refsynopsisdiv>
|
2003-04-27 01:56:51 +02:00
|
|
|
<synopsis>
|
2004-03-11 02:47:41 +01:00
|
|
|
LOCK [ TABLE ] <replaceable class="PARAMETER">name</replaceable> [, ...] [ IN <replaceable class="PARAMETER">lockmode</replaceable> MODE ] [ NOWAIT ]
|
2001-07-10 00:18:34 +02:00
|
|
|
|
|
|
|
where <replaceable class="PARAMETER">lockmode</replaceable> is one of:
|
|
|
|
|
2003-04-27 01:56:51 +02:00
|
|
|
ACCESS SHARE | ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE EXCLUSIVE
|
|
|
|
| SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE | ACCESS EXCLUSIVE
|
|
|
|
</synopsis>
|
1999-06-04 17:50:44 +02:00
|
|
|
</refsynopsisdiv>
|
1998-12-29 03:24:47 +01:00
|
|
|
|
2003-04-27 01:56:51 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
2000-03-24 04:57:06 +01:00
|
|
|
|
2000-03-28 16:35:27 +02:00
|
|
|
<para>
|
2004-03-12 01:52:23 +01:00
|
|
|
<command>LOCK TABLE</command> obtains a table-level lock, waiting
|
|
|
|
if necessary for any conflicting locks to be released. If
|
|
|
|
<literal>NOWAIT</literal> is specified, <command>LOCK
|
|
|
|
TABLE</command> does not wait to acquire the desired lock: if it
|
|
|
|
cannot be immediately acquired, the transaction is aborted and an
|
|
|
|
error is emitted. Once obtained, the lock is held for the
|
|
|
|
remainder of the current transaction. (There is no <command>UNLOCK
|
|
|
|
TABLE</command> command; locks are always released at transaction
|
|
|
|
end.)
|
2000-03-28 16:35:27 +02:00
|
|
|
</para>
|
2000-03-24 04:57:06 +01:00
|
|
|
|
2000-03-28 16:35:27 +02:00
|
|
|
<para>
|
2003-04-27 01:56:51 +02:00
|
|
|
When acquiring locks automatically for commands that reference
|
|
|
|
tables, <productname>PostgreSQL</productname> always uses the least
|
|
|
|
restrictive lock mode possible. <command>LOCK TABLE</command>
|
2002-05-30 22:45:18 +02:00
|
|
|
provides for cases when you might need more restrictive locking.
|
2003-04-27 01:56:51 +02:00
|
|
|
For example, suppose an application runs a transaction at the
|
|
|
|
isolation level read committed and needs to ensure that data in a
|
|
|
|
table remains stable for the duration of the transaction. To
|
|
|
|
achieve this you could obtain <literal>SHARE</> lock mode over the
|
2002-05-30 22:45:18 +02:00
|
|
|
table before querying. This will prevent concurrent data changes
|
2003-04-27 01:56:51 +02:00
|
|
|
and ensure subsequent reads of the table see a stable view of
|
|
|
|
committed data, because <literal>SHARE</> lock mode conflicts with
|
|
|
|
the <literal>ROW EXCLUSIVE</> lock acquired by writers, and your
|
|
|
|
<command>LOCK TABLE <replaceable
|
|
|
|
class="PARAMETER">name</replaceable> IN SHARE MODE</command>
|
|
|
|
statement will wait until any concurrent holders of <literal>ROW
|
|
|
|
EXCLUSIVE</literal> mode locks commit or roll back. Thus, once you
|
|
|
|
obtain the lock, there are no uncommitted writes outstanding;
|
|
|
|
furthermore none can begin until you release the lock.
|
1999-06-08 22:01:01 +02:00
|
|
|
</para>
|
2003-04-27 01:56:51 +02:00
|
|
|
|
1999-05-20 07:40:27 +02:00
|
|
|
<para>
|
2003-04-27 01:56:51 +02:00
|
|
|
To achieve a similar effect when running a transaction at the
|
|
|
|
isolation level serializable, you have to execute the <command>LOCK
|
|
|
|
TABLE</> statement before executing any data modification
|
|
|
|
statement. A serializable transaction's view of data will be
|
|
|
|
frozen when its first data modification statement begins. A later
|
|
|
|
<command>LOCK TABLE</> will still prevent concurrent writes --- but it
|
|
|
|
won't ensure that what the transaction reads corresponds to the
|
|
|
|
latest committed values.
|
1999-05-20 07:40:27 +02:00
|
|
|
</para>
|
1999-06-08 22:01:01 +02:00
|
|
|
|
|
|
|
<para>
|
2003-04-27 01:56:51 +02:00
|
|
|
If a transaction of this sort is going to change the data in the
|
|
|
|
table, then it should use <literal>SHARE ROW EXCLUSIVE</> lock mode
|
|
|
|
instead of <literal>SHARE</> mode. This ensures that only one
|
|
|
|
transaction of this type runs at a time. Without this, a deadlock
|
|
|
|
is possible: two transactions might both acquire <literal>SHARE</>
|
|
|
|
mode, and then be unable to also acquire <literal>ROW EXCLUSIVE</>
|
|
|
|
mode to actually perform their updates. (Note that a transaction's
|
|
|
|
own locks never conflict, so a transaction can acquire <literal>ROW
|
|
|
|
EXCLUSIVE</> mode when it holds <literal>SHARE</> mode --- but not
|
|
|
|
if anyone else holds <literal>SHARE</> mode.) To avoid deadlocks,
|
|
|
|
make sure all transactions acquire locks on the same objects in the
|
|
|
|
same order, and if multiple lock modes are involved for a single
|
|
|
|
object, then transactions should always acquire the most
|
|
|
|
restrictive mode first.
|
1999-06-08 22:01:01 +02:00
|
|
|
</para>
|
1999-06-09 15:43:42 +02:00
|
|
|
|
2002-05-30 22:45:18 +02:00
|
|
|
<para>
|
2003-04-27 01:56:51 +02:00
|
|
|
More information about the lock modes and locking strategies can be
|
|
|
|
found in <xref linkend="explicit-locking">.
|
2002-05-30 22:45:18 +02:00
|
|
|
</para>
|
2003-04-27 01:56:51 +02:00
|
|
|
</refsect1>
|
1998-12-29 03:24:47 +01:00
|
|
|
|
2003-04-27 01:56:51 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Parameters</title>
|
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable class="PARAMETER">name</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The name (optionally schema-qualified) of an existing table to
|
|
|
|
lock.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2003-12-14 01:05:29 +01:00
|
|
|
The command <literal>LOCK TABLE a, b;</> is equivalent to
|
|
|
|
<literal>LOCK TABLE a; LOCK TABLE b;</>. The tables are locked
|
|
|
|
one-by-one in the order specified in the <command>LOCK
|
|
|
|
TABLE</command> command.
|
2003-04-27 01:56:51 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable class="parameter">lockmode</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The lock mode specifies which locks this lock conflicts with.
|
|
|
|
Lock modes are described in <xref linkend="explicit-locking">.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
If no lock mode is specified, then <literal>ACCESS
|
|
|
|
EXCLUSIVE</literal>, the most restrictive mode, is used.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2004-03-12 01:52:23 +01:00
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>NOWAIT</literal></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Specifies that <command>LOCK TABLE</command> should not wait for
|
|
|
|
any conflicting locks to be released: if the specified lock
|
|
|
|
cannot be immediately acquired without waiting, the transaction
|
|
|
|
is aborted.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2003-04-27 01:56:51 +02:00
|
|
|
</variablelist>
|
|
|
|
</refsect1>
|
2001-08-10 16:30:15 +02:00
|
|
|
|
2003-04-27 01:56:51 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Notes</title>
|
1999-06-09 15:43:42 +02:00
|
|
|
|
1999-05-20 07:40:27 +02:00
|
|
|
<para>
|
2003-12-14 01:05:29 +01:00
|
|
|
<literal>LOCK TABLE ... IN ACCESS SHARE MODE</> requires <literal>SELECT</>
|
2002-01-18 02:05:43 +01:00
|
|
|
privileges on the target table. All other forms of <command>LOCK</>
|
|
|
|
require <literal>UPDATE</> and/or <literal>DELETE</> privileges.
|
1999-05-20 07:40:27 +02:00
|
|
|
</para>
|
1999-06-09 15:43:42 +02:00
|
|
|
|
1999-06-08 22:01:01 +02:00
|
|
|
<para>
|
2003-12-14 01:05:29 +01:00
|
|
|
<command>LOCK TABLE</command> is useful only inside a transaction
|
|
|
|
block (<command>BEGIN</>/<command>COMMIT</> pair), since the lock
|
|
|
|
is dropped as soon as the transaction ends. A <command>LOCK
|
|
|
|
TABLE</> command appearing outside any transaction block forms a
|
|
|
|
self-contained transaction, so the lock will be dropped as soon as
|
|
|
|
it is obtained.
|
1999-05-20 07:40:27 +02:00
|
|
|
</para>
|
2001-08-10 16:30:15 +02:00
|
|
|
|
2002-05-30 22:45:18 +02:00
|
|
|
<para>
|
2003-04-27 01:56:51 +02:00
|
|
|
<command>LOCK TABLE</> only deals with table-level locks, and so
|
|
|
|
the mode names involving <literal>ROW</> are all misnomers. These
|
|
|
|
mode names should generally be read as indicating the intention of
|
|
|
|
the user to acquire row-level locks within the locked table. Also,
|
|
|
|
<literal>ROW EXCLUSIVE</> mode is a sharable table lock. Keep in
|
|
|
|
mind that all the lock modes have identical semantics so far as
|
|
|
|
<command>LOCK TABLE</> is concerned, differing only in the rules
|
2003-12-14 01:05:29 +01:00
|
|
|
about which modes conflict with which. For information on how to
|
|
|
|
acquire an actual row-level lock, see <xref linkend="locking-rows">
|
|
|
|
and the <xref linkend="sql-for-update"
|
|
|
|
endterm="sql-for-update-title"> in the <command>SELECT</command>
|
|
|
|
reference documentation.
|
2002-05-30 22:45:18 +02:00
|
|
|
</para>
|
1999-05-20 07:40:27 +02:00
|
|
|
</refsect1>
|
1998-12-29 03:24:47 +01:00
|
|
|
|
2003-04-27 01:56:51 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Examples</title>
|
1999-06-08 22:01:01 +02:00
|
|
|
|
1999-06-04 17:50:44 +02:00
|
|
|
<para>
|
2003-04-27 01:56:51 +02:00
|
|
|
Obtain a <literal>SHARE</> lock on a primary key table when going to perform
|
1999-07-06 19:16:42 +02:00
|
|
|
inserts into a foreign key table:
|
|
|
|
|
2003-04-27 01:56:51 +02:00
|
|
|
<programlisting>
|
1999-07-06 19:16:42 +02:00
|
|
|
BEGIN WORK;
|
|
|
|
LOCK TABLE films IN SHARE MODE;
|
|
|
|
SELECT id FROM films
|
|
|
|
WHERE name = 'Star Wars: Episode I - The Phantom Menace';
|
|
|
|
-- Do ROLLBACK if record was not returned
|
|
|
|
INSERT INTO films_user_comments VALUES
|
|
|
|
(_id_, 'GREAT! I was waiting for it for so long!');
|
|
|
|
COMMIT WORK;
|
2003-04-27 01:56:51 +02:00
|
|
|
</programlisting>
|
1999-06-04 17:50:44 +02:00
|
|
|
</para>
|
1999-06-08 22:01:01 +02:00
|
|
|
|
|
|
|
<para>
|
2003-04-27 01:56:51 +02:00
|
|
|
Take a <literal>SHARE ROW EXCLUSIVE</> lock on a primary key table when going to perform
|
1999-07-06 19:16:42 +02:00
|
|
|
a delete operation:
|
|
|
|
|
2003-04-27 01:56:51 +02:00
|
|
|
<programlisting>
|
1999-07-06 19:16:42 +02:00
|
|
|
BEGIN WORK;
|
|
|
|
LOCK TABLE films IN SHARE ROW EXCLUSIVE MODE;
|
|
|
|
DELETE FROM films_user_comments WHERE id IN
|
|
|
|
(SELECT id FROM films WHERE rating < 5);
|
|
|
|
DELETE FROM films WHERE rating < 5;
|
|
|
|
COMMIT WORK;
|
2003-04-27 01:56:51 +02:00
|
|
|
</programlisting>
|
1999-06-08 22:01:01 +02:00
|
|
|
</para>
|
1999-06-04 17:50:44 +02:00
|
|
|
</refsect1>
|
1998-12-29 03:24:47 +01:00
|
|
|
|
2003-04-27 01:56:51 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Compatibility</title>
|
1998-12-29 03:24:47 +01:00
|
|
|
|
2003-04-27 01:56:51 +02:00
|
|
|
<para>
|
|
|
|
There is no <command>LOCK TABLE</command> in the SQL standard,
|
|
|
|
which instead uses <command>SET TRANSACTION</command> to specify
|
2003-09-11 23:42:20 +02:00
|
|
|
concurrency levels on transactions. <productname>PostgreSQL</productname> supports that too;
|
2003-04-27 01:56:51 +02:00
|
|
|
see <xref linkend="SQL-SET-TRANSACTION"
|
|
|
|
endterm="SQL-SET-TRANSACTION-TITLE"> for details.
|
|
|
|
</para>
|
2002-01-18 02:05:43 +01:00
|
|
|
|
2003-04-27 01:56:51 +02:00
|
|
|
<para>
|
|
|
|
Except for <literal>ACCESS SHARE</>, <literal>ACCESS EXCLUSIVE</>,
|
|
|
|
and <literal>SHARE UPDATE EXCLUSIVE</> lock modes, the
|
|
|
|
<productname>PostgreSQL</productname> lock modes and the
|
|
|
|
<command>LOCK TABLE</command> syntax are compatible with those
|
|
|
|
present in <productname>Oracle</productname>.
|
|
|
|
</para>
|
1999-05-20 07:40:27 +02:00
|
|
|
</refsect1>
|
1999-06-04 17:50:44 +02:00
|
|
|
</refentry>
|
1999-05-20 07:40:27 +02:00
|
|
|
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
|
|
|
mode: sgml
|
|
|
|
sgml-omittag:nil
|
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
sgml-parent-document:nil
|
|
|
|
sgml-default-dtd-file:"../reference.ced"
|
|
|
|
sgml-exposed-tags:nil
|
1999-07-06 19:16:42 +02:00
|
|
|
sgml-local-catalogs:"/usr/lib/sgml/catalog"
|
1999-05-20 07:40:27 +02:00
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
-->
|