Skip to content

View locks and blocked queries in PostgreSQL

Use pg_blocking_pids to identify the sessions preventing a query from acquiring a lock. Run this snapshot query in the affected database:

SELECT pid, usename, state, wait_event_type, wait_event,
pg_blocking_pids(pid) AS blocker_pids,
query_start, xact_start, query
FROM pg_stat_activity
WHERE datname = current_database()
AND cardinality(pg_blocking_pids(pid)) > 0
ORDER BY query_start;

Each row is a blocked session; blocker_pids lists its blockers. A blocker can be idle inside an open transaction or ahead in the lock queue. See PostgreSQL’s blocker function reference.

To inspect individual lock requests:

SELECT l.pid, l.locktype, l.mode, l.granted
FROM pg_locks AS l
JOIN pg_stat_activity AS a ON a.pid = l.pid
WHERE a.datname = current_database()
ORDER BY l.granted, l.pid;

granted = false identifies a request still waiting for its lock.

View locks and blocked queries in PostgreSQL with TableAI

Section titled “View locks and blocked queries in PostgreSQL with TableAI”
  1. Open Admin › Locks.
  2. Select Waiting only to focus on waiting requests.
  3. Inspect the waiting session’s PID, lock mode, and query.
  4. Run the blocker query above to investigate the transaction holding up the work.
Locks section with a waiting lock request for a demo session

Ask the transaction owner to finish or roll back the blocking work. If intervention is necessary, follow cancel a query or terminate a session. That page covers target checks, confirmation, and termination risks.

No extension is required. Full activity details for other users can require pg_read_all_stats or superuser access. Permission to read activity is separate from permission to end a session.