---
source: https://iinventedlight.com/library/notes/postgres-explicit-locking.md
title: "PostgreSQL explicit locking: LOCK TABLE, row locks and advisory locks"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PostgreSQL License; prose restates cited source lines"
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run for this note; only the fetched documentation pages were read."
category: coding-references
topic_area: "code-ai-eng: PostgreSQL"
tags: postgresql, locking, concurrency
sources:
  - id: S1
    title: "PostgreSQL: Documentation: 18: 13.3. Explicit Locking"
    publisher: PostgreSQL Global Development Group
    url: https://www.postgresql.org/docs/current/explicit-locking.html
    accessed: 2026-11-04
    license: "PostgreSQL License"
  - id: S2
    title: "PostgreSQL: Documentation: 18: LOCK"
    publisher: PostgreSQL Global Development Group
    url: https://www.postgresql.org/docs/current/sql-lock.html
    accessed: 2026-11-04
    license: "PostgreSQL License"
---
# 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 ... NOWAIT` errors instead of waiting. [S2]
- Deadlocks can happen from row locks even without explicit locking. [S1]
- Current locks, including advisory locks, appear in the `pg_locks` view. [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).
