1LOCK(7)                          SQL Commands                          LOCK(7)
2
3
4

NAME

6       LOCK - lock a table
7
8

SYNOPSIS

10       LOCK [ TABLE ] [ ONLY ] name [ * ] [, ...] [ IN lockmode MODE ] [ NOWAIT ]
11
12       where lockmode is one of:
13
14           ACCESS SHARE | ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE EXCLUSIVE
15           | SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE | ACCESS EXCLUSIVE
16
17

DESCRIPTION

19       LOCK  TABLE  obtains  a  table-level lock, waiting if necessary for any
20       conflicting locks to be released. If NOWAIT is  specified,  LOCK  TABLE
21       does  not  wait  to  acquire the desired lock: if it cannot be acquired
22       immediately, the command is aborted  and  an  error  is  emitted.  Once
23       obtained,  the  lock  is held for the remainder of the current transac‐
24       tion. (There is no UNLOCK TABLE command; locks are always  released  at
25       transaction end.)
26
27       When  acquiring locks automatically for commands that reference tables,
28       PostgreSQL always uses the least restrictive lock mode  possible.  LOCK
29       TABLE  provides for cases when you might need more restrictive locking.
30       For example, suppose an application runs a transaction at the Read Com‐
31       mitted isolation level and needs to ensure that data in a table remains
32       stable for the duration of the transaction. To achieve this  you  could
33       obtain  SHARE  lock mode over the table before querying. This will pre‐
34       vent concurrent data changes and ensure subsequent reads of  the  table
35       see  a stable view of committed data, because SHARE lock mode conflicts
36       with the ROW EXCLUSIVE lock acquired by writers, and  your  LOCK  TABLE
37       name  IN SHARE MODE statement will wait until any concurrent holders of
38       ROW EXCLUSIVE mode locks commit or roll back. Thus, once you obtain the
39       lock, there are no uncommitted writes outstanding; furthermore none can
40       begin until you release the lock.
41
42       To achieve a similar effect when running a transaction at the Serializ‐
43       able  isolation  level,  you  have  to execute the LOCK TABLE statement
44       before executing any SELECT or data modification statement.  A  serial‐
45       izable  transaction's view of data will be frozen when its first SELECT
46       or data modification statement begins. A LOCK TABLE later in the trans‐
47       action  will still prevent concurrent writes — but it won't ensure that
48       what the transaction reads corresponds to the latest committed values.
49
50       If a transaction of this sort is going to change the data in the table,
51       then it should use SHARE ROW EXCLUSIVE lock mode instead of SHARE mode.
52       This ensures that only one transaction of this type  runs  at  a  time.
53       Without  this,  a  deadlock  is  possible:  two transactions might both
54       acquire SHARE mode, and then be unable to also  acquire  ROW  EXCLUSIVE
55       mode  to actually perform their updates. (Note that a transaction's own
56       locks never conflict, so a transaction can acquire ROW  EXCLUSIVE  mode
57       when it holds SHARE mode — but not if anyone else holds SHARE mode.) To
58       avoid deadlocks, make sure all transactions acquire locks on  the  same
59       objects  in the same order, and if multiple lock modes are involved for
60       a single object, then  transactions  should  always  acquire  the  most
61       restrictive mode first.
62
63       More  information  about  the  lock modes and locking strategies can be
64       found in in the documentation.
65

PARAMETERS

67       name   The name (optionally schema-qualified) of an existing  table  to
68              lock.  If ONLY is specified before the table name, only that ta‐
69              ble is locked. If ONLY is not specified, the table and  all  its
70              descendant  tables  (if  any)  are  locked. Optionally, * can be
71              specified after the  table  name  to  explicitly  indicate  that
72              descendant tables are included.
73
74              The command LOCK TABLE a, b; is equivalent to LOCK TABLE a; LOCK
75              TABLE b;. The tables are locked one-by-one in the  order  speci‐
76              fied in the LOCK TABLE command.
77
78       lockmode
79              The  lock  mode  specifies which locks this lock conflicts with.
80              Lock modes are described in in the documentation.
81
82              If no lock mode is specified, then ACCESS  EXCLUSIVE,  the  most
83              restrictive mode, is used.
84
85       NOWAIT Specifies  that  LOCK  TABLE should not wait for any conflicting
86              locks to  be  released:  if  the  specified  lock(s)  cannot  be
87              acquired   immediately   without  waiting,  the  transaction  is
88              aborted.
89

NOTES

91       LOCK TABLE ... IN ACCESS SHARE MODE requires SELECT privileges  on  the
92       target  table.  All other forms of LOCK require at least one of UPDATE,
93       DELETE, or TRUNCATE privileges.
94
95       LOCK TABLE is useless outside  a  transaction  block:  the  lock  would
96       remain  held  only  to the completion of the statement. Therefore Post‐
97       greSQL reports an error if LOCK is used outside  a  transaction  block.
98       Use BEGIN [begin(7)] and COMMIT [commit(7)] (or ROLLBACK [rollback(7)])
99       to define a transaction block.
100
101       LOCK TABLE only deals with table-level locks, and  so  the  mode  names
102       involving  ROW  are all misnomers. These mode names should generally be
103       read as indicating the intention of the user to acquire row-level locks
104       within  the  locked table. Also, ROW EXCLUSIVE mode is a sharable table
105       lock. Keep in mind that all the lock modes have identical semantics  so
106       far as LOCK TABLE is concerned, differing only in the rules about which
107       modes conflict with which. For information on how to acquire an  actual
108       row-level  lock,  see in the documentation and the FOR UPDATE/FOR SHARE
109       Clause [select(7)] in the SELECT reference documentation.
110

EXAMPLES

112       Obtain a SHARE lock on a  primary  key  table  when  going  to  perform
113       inserts into a foreign key table:
114
115       BEGIN WORK;
116       LOCK TABLE films IN SHARE MODE;
117       SELECT id FROM films
118           WHERE name = 'Star Wars: Episode I - The Phantom Menace';
119       -- Do ROLLBACK if record was not returned
120       INSERT INTO films_user_comments VALUES
121           (_id_, 'GREAT! I was waiting for it for so long!');
122       COMMIT WORK;
123
124
125       Take  a  SHARE  ROW EXCLUSIVE lock on a primary key table when going to
126       perform a delete operation:
127
128       BEGIN WORK;
129       LOCK TABLE films IN SHARE ROW EXCLUSIVE MODE;
130       DELETE FROM films_user_comments WHERE id IN
131           (SELECT id FROM films WHERE rating < 5);
132       DELETE FROM films WHERE rating < 5;
133       COMMIT WORK;
134
135

COMPATIBILITY

137       There is no LOCK TABLE in the SQL  standard,  which  instead  uses  SET
138       TRANSACTION  to  specify concurrency levels on transactions. PostgreSQL
139       supports  that  too;  see  SET  TRANSACTION  [set_transaction(7)]   for
140       details.
141
142       Except  for  ACCESS SHARE, ACCESS EXCLUSIVE, and SHARE UPDATE EXCLUSIVE
143       lock modes, the PostgreSQL lock modes and the  LOCK  TABLE  syntax  are
144       compatible with those present in Oracle.
145
146
147
148SQL - Language Statements         2014-02-17                           LOCK(7)
Impressum