3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "LOCK" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
35 LOCK [ TABLE ] [ ONLY ] \fIname\fR [ * ] [, \&.\&.\&.] [ IN \fIlockmode\fR MODE ] [ NOWAIT ]
37 where \fIlockmode\fR is one of:
39 ACCESS SHARE | ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE EXCLUSIVE
40 | SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE | ACCESS EXCLUSIVE
45 obtains a table\-level lock, waiting if necessary for any conflicting locks to be released\&. If
49 does not wait to acquire the desired lock: if it cannot be acquired immediately, the command is aborted and an error is emitted\&. Once obtained, the lock is held for the remainder of the current transaction\&. (There is no
51 command; locks are always released at transaction end\&.)
53 When a view is locked, all relations appearing in the view definition query are also locked recursively with the same lock mode\&.
55 When acquiring locks automatically for commands that reference tables,
57 always uses the least restrictive lock mode possible\&.
59 provides for cases when you might need more restrictive locking\&. For example, suppose an application runs a transaction at the
61 isolation level and needs to ensure that data in a table remains stable for the duration of the transaction\&. To achieve this you could obtain
63 lock mode over the table before querying\&. This will prevent concurrent data changes and ensure subsequent reads of the table see a stable view of committed data, because
65 lock mode conflicts with the
67 lock acquired by writers, and your
68 \fBLOCK TABLE \fR\fB\fIname\fR\fR\fB IN SHARE MODE\fR
69 statement will wait until any concurrent holders of
71 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\&.
73 To achieve a similar effect when running a transaction at the
77 isolation level, you have to execute the
79 statement before executing any
81 or data modification statement\&. A
85 transaction\*(Aqs view of data will be frozen when its first
87 or data modification statement begins\&. A
89 later in the transaction will still prevent concurrent writes \(em but it won\*(Aqt ensure that what the transaction reads corresponds to the latest committed values\&.
91 If a transaction of this sort is going to change the data in the table, then it should use
95 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
97 mode, and then be unable to also acquire
99 mode to actually perform their updates\&. (Note that a transaction\*(Aqs own locks never conflict, so a transaction can acquire
103 mode \(em but not if anyone else holds
105 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\&.
107 More information about the lock modes and locking strategies can be found in
113 The name (optionally schema\-qualified) of an existing table to lock\&. If
115 is specified before the table name, only that table is locked\&. If
117 is not specified, the table and all its descendant tables (if any) are locked\&. Optionally,
119 can be specified after the table name to explicitly indicate that descendant tables are included\&.
124 LOCK TABLE a; LOCK TABLE b;\&. The tables are locked one\-by\-one in the order specified in the
131 The lock mode specifies which locks this lock conflicts with\&. Lock modes are described in
134 If no lock mode is specified, then
135 ACCESS EXCLUSIVE, the most restrictive mode, is used\&.
142 should not wait for any conflicting locks to be released: if the specified lock(s) cannot be acquired immediately without waiting, the transaction is aborted\&.
146 To lock a table, the user must have the right privilege for the specified
147 \fIlockmode\fR\&. If the user has
152 privileges on the table, any
154 is permitted\&. If the user has
156 privileges on the table,
158 (or a less\-conflicting mode as described in
159 Section\ \&13.3) is permitted\&. If a user has
161 privileges on the table,
165 The user performing the lock on the view must have the corresponding privilege on the view\&. In addition, by default, the view\*(Aqs owner must have the relevant privileges on the underlying base relations, whereas the user performing the lock does not need any permissions on the underlying base relations\&. However, if the view has
170 \fBCREATE VIEW\fR), the user performing the lock, rather than the view owner, must have the relevant privileges on the underlying base relations\&.
173 is useless outside a transaction block: the lock would remain held only to the completion of the statement\&. Therefore
177 is used outside a transaction block\&. Use
182 \fBROLLBACK\fR) to define a transaction block\&.
185 only deals with table\-level locks, and so the mode names involving
187 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,
189 mode is a shareable table lock\&. Keep in mind that all the lock modes have identical semantics so far as
191 is concerned, differing only in the rules about which modes conflict with which\&. For information on how to acquire an actual row\-level lock, see
202 lock on a primary key table when going to perform inserts into a foreign key table:
209 LOCK TABLE films IN SHARE MODE;
211 WHERE name = \*(AqStar Wars: Episode I \- The Phantom Menace\*(Aq;
212 \-\- Do ROLLBACK if record was not returned
213 INSERT INTO films_user_comments VALUES
214 (_id_, \*(AqGREAT! I was waiting for it for so long!\*(Aq);
223 lock on a primary key table when going to perform a delete operation:
230 LOCK TABLE films IN SHARE ROW EXCLUSIVE MODE;
231 DELETE FROM films_user_comments WHERE id IN
232 (SELECT id FROM films WHERE rating < 5);
233 DELETE FROM films WHERE rating < 5;
243 in the SQL standard, which instead uses
244 \fBSET TRANSACTION\fR
245 to specify concurrency levels on transactions\&.
247 supports that too; see
248 SET TRANSACTION (\fBSET_TRANSACTION\fR(7))
253 ACCESS EXCLUSIVE, and
254 SHARE UPDATE EXCLUSIVE
259 syntax are compatible with those present in