PostgreSQL explicit locking: LOCK TABLE, row locks and advisory locks
Short answer
PostgreSQL takes most locks automatically. When you need more, use SELECT ... FOR UPDATE for row locks, LOCK TABLE for table locks, or advisory locks for app-defined locks. Table locks last until the transaction ends; there is no UNLOCK TABLE. [S1][S2]
Key points
LOCK TABLE ... NOWAITerrors instead of waiting. [S2]- Deadlocks can happen from row locks even without explicit locking. [S1]
- Current locks, including advisory locks, appear in the
pg_locksview. [S1]
Claims
- id: C1 claim: Deadlocks can occur from row-level locks even without explicit locking. source: S1 evidence: "deadlocks can also occur as the result of row-level locks"
- id: C2 claim: Advisory locks held are listed in pg_locks. source: S1 evidence: "advisory locks currently held by any session can be found in the pg_locks system view"
- id: C3 claim: LOCK TABLE obtains a table-level lock. source: S2 evidence: "LOCK TABLE obtains a table-level lock"
- id: C4 claim: Locks are always released at transaction end. source: S2 evidence: "There is no UNLOCK TABLE command; locks are always released at transaction end."
Sources
- [S1] PostgreSQL: Documentation: 18: 13.3. Explicit Locking. PostgreSQL Global Development Group. https://www.postgresql.org/docs/current/explicit-locking.html . License: PostgreSQL License. Accessed 2026-11-04.
- [S2] PostgreSQL: Documentation: 18: LOCK. PostgreSQL Global Development Group. https://www.postgresql.org/docs/current/sql-lock.html . License: PostgreSQL License. Accessed 2026-11-04.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: PostgreSQL Global Development Group, PostgreSQL: Documentation: 18: 13.3. Explicit Locking, PostgreSQL License; PostgreSQL Global Development Group, PostgreSQL: Documentation: 18: LOCK, PostgreSQL License.
Cite this
iinventedlight. 2026. "PostgreSQL explicit locking: LOCK TABLE, row locks and advisory locks." iinventedlight Library, iil:note:xxmrr9wd, version of 2026-11-04. https://iinventedlight.com/library/notes/postgres-explicit-locking/. License: CC-BY-4.0.
@misc{iil_note_xxmrr9wd,
author = {{iinventedlight}},
title = {{PostgreSQL explicit locking: LOCK TABLE, row locks and advisory locks}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/postgres-explicit-locking/},
note = {Permanent ID iil:note:xxmrr9wd. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/postgres-explicit-locking.md.