postgresql_16:7.1.153.3 Notes
PostgreSQL 16 > Reference > SQL Commands > LOCK > Notes
To lock a table, the user must have the right privilege for the specified *lockmode*, or be the table's owner or a superuser. If the user has 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 (see postgresql_16:3.10.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 (see postgresql_16:7.1.95)), the user performing the lock, rather than the view owner, must have the relevant privileges on the underlying base relations.
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 (see postgresql_16:7.1.45) and COMMIT (see postgresql_16:7.1.51) (or ROLLBACK (see postgresql_16:7.1.165)) to define a transaction block.
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 (see postgresql_16:3.10.3.2) and (see postgresql_16:7.1.170.2) in the (see postgresql_16:7.1.170) documentation.