LOCK(7)

πŸ’… LOCK(7) β€” the "lock a table" page, bestie edition

okay so you want to know about LOCK?? in PostgreSQL 18.6?? say less, i got you 🫢 this is literally the manual page for LOCK, the one that locks a table, and we're gonna go through every single thing on it together. grab a snack. πŸ“


✨ NAME ✨

LOCK β€” lock a table πŸ”’

like. that's the whole thing. that's the vibe.


🌸 SYNOPSIS 🌸

LOCK [ TABLE ] [ ONLY ] name [ * ] [, ...] [ IN lockmode MODE ] [ NOWAIT ]

and where lockmode is one of:

ACCESS SHARE | ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE EXCLUSIVE
| SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE | ACCESS EXCLUSIVE

eight modes. EIGHT. she's got range, periodt. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

okay so LOCK TABLE grabs a table-level lock, and it'll wait if it has to, until any conflicting locks get released. no rushing her. ✨

if you say NOWAIT, though? she does NOT wait. if the lock can't be acquired immediately, the command is aborted and an error is emitted. no cap. πŸ’€

once you get the lock, you hold it for the remainder of the current transaction. and listen β€” there is no UNLOCK TABLE command. locks are always released at transaction end. that's just how she operates. πŸ™ˆ

when a view is locked, all relations appearing in the view definition query are also locked recursively with the same lock mode. yes, the whole friend group. 🫢

now, when PostgreSQL acquires locks automatically for commands that reference tables, it always uses the least restrictive lock mode possible. LOCK TABLE exists for the cases where you need something more restrictive. main character energy only when necessary.

for example β€” say an application runs a transaction at the READ COMMITTED isolation level and needs to make sure the data in a table stays stable for the duration of that transaction. to do that, you could obtain SHARE lock mode over the table before querying. this prevents concurrent data changes and ensures subsequent reads of the table see a stable view of committed data, because SHARE lock mode conflicts with the ROW EXCLUSIVE lock acquired by writers, and your LOCK TABLE name IN SHARE MODE statement will wait until any concurrent holders of ROW EXCLUSIVE mode locks commit or roll back. so once you obtain the lock, there are no uncommitted writes outstanding β€” and furthermore, none can begin until you release the lock. slay. πŸ”₯

to get a similar effect when running a transaction at the REPEATABLE READ or SERIALIZABLE isolation level, you have to execute the LOCK TABLE statement before executing any SELECT or data modification statement. why?? because a REPEATABLE READ or SERIALIZABLE transaction's view of data gets frozen when its first SELECT or data modification statement begins. a LOCK TABLE later in the transaction will still prevent concurrent writes β€” but it won't ensure that what the transaction reads corresponds to the latest committed values. it's giving "too late bestie". 😭

if a transaction of this sort is going to change the data in the table, then it should use SHARE ROW EXCLUSIVE lock mode instead of 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 SHARE mode, and then be unable to also acquire ROW EXCLUSIVE mode to actually perform their updates. (note that a transaction's own locks never conflict, so a transaction can acquire ROW EXCLUSIVE mode when it holds SHARE mode β€” but not if anyone else holds 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. πŸ”‘

more info about the lock modes and locking strategies lives in Section 13.3. iykyk. πŸ“–


πŸŽ€ PARAMETERS πŸŽ€

  • name β€” the name (optionally schema-qualified) of an existing table to lock. if ONLY is specified before the table name, only that table is locked. if ONLY is not specified, the table and all its descendant tables (if any) are locked. optionally, * can be specified after the table name to explicitly indicate that descendant tables are included.

also!! the command LOCK TABLE a, b; is equivalent to LOCK TABLE a; LOCK TABLE b;. the tables are locked one-by-one in the order specified in the LOCK TABLE command. orderly. we love that. πŸ’–

  • lockmode β€” the lock mode specifies which locks this lock conflicts with. lock modes are described in Section 13.3.

if no lock mode is specified, then ACCESS EXCLUSIVE, the most restrictive mode, is used. she's dramatic like that. πŸ‘‘

  • NOWAIT β€” specifies that LOCK TABLE 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.

🌈 NOTES 🌈

to lock a table, the user must have the right privilege for the specified lockmode. here's the tea β˜•:

  • if the user has MAINTAIN, UPDATE, DELETE, or TRUNCATE privileges on the table, any lockmode is permitted. πŸ”“
  • if the user has INSERT privileges on the table, ROW EXCLUSIVE MODE (or a less-conflicting mode as described in Section 13.3) is permitted.
  • if a user has SELECT privileges on the table, ACCESS SHARE MODE is permitted.

the user performing the lock on the view must have the corresponding privilege on the view. in addition, by default, the view's 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 security_invoker set to true (see CREATE VIEW), then the user performing the lock, rather than the view owner, must have the relevant privileges on the underlying base relations. plot twist. πŸŒ€

LOCK TABLE is useless outside a transaction block: the lock would remain held only to the completion of the statement. therefore PostgreSQL reports an error if LOCK is used outside a transaction block. use BEGIN and COMMIT (or ROLLBACK) to define a transaction block. fr fr. 🚨

LOCK TABLE only deals with table-level locks, and so the mode names involving 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, ROW EXCLUSIVE mode is a shareable table lock. keep in mind that all the lock modes have identical semantics so far as LOCK TABLE 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 Section 13.3.2 and The Locking Clause in the SELECT(7) documentation. πŸ’œ


⭐ EXAMPLES ⭐

1. obtain a SHARE lock on a primary key table when going to perform inserts into a foreign key table:

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;

2. take a SHARE ROW EXCLUSIVE lock on a primary key table when going to perform a delete operation:

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;

πŸ’« COMPATIBILITY πŸ’«

there is no LOCK TABLE in the SQL standard, which instead uses SET TRANSACTION to specify concurrency levels on transactions. PostgreSQL supports that too β€” see SET TRANSACTION (SET_TRANSACTION(7)) for details. πŸ‘€

except for ACCESS SHARE, ACCESS EXCLUSIVE, and SHARE UPDATE EXCLUSIVE lock modes, the PostgreSQL lock modes and the LOCK TABLE syntax are compatible with those present in Oracle. 🌍


🫢 SEE ALSO 🫢

no separate SEE ALSO section here bestie, but the page keeps pointing you at Section 13.3, Section 13.3.2, SELECT(7), CREATE VIEW, and SET_TRANSACTION(7) β€” go read those if you want the full lore. πŸ“š


and that's the whole page, no notes. πŸ’…βœ¨

this was PostgreSQL 18.6 Documentation, dated 2026, and it's the LOCK(7) page β€” mwah, iconic, no cap. πŸŽ€πŸ¦‹πŸ’–