Sub topic 9
Finding Blocked and Blocking Sessions
Assume you know basic SQL, transactions, primary keys, foreign keys, and how to use two concurrent psql sessions. You should also know BEGIN, COMMIT, and ROLLBACK.
A blocked session is waiting for a lock. A blocking session holds a lock that another session needs. The blocking session might be running a query, or it might be idle inside an open transaction. That second case is especially easy to miss: the last SQL statement has finished, but the transaction still holds its locks.
PostgreSQL exposes live session and lock information through system views and functions:
pg_stat_activitydescribes sessions and their current queries.pg_locksdescribes locks that sessions hold or request.pg_blocking_pids(pid)returns the process IDs blocking a particular session.
Start with `pg_stat_activity`
This query lists sessions that are currently waiting for a lock:
SELECT pid,
usename,
application_name,
client_addr,
state,
wait_event_type,
wait_event,
xact_start,
query_start,
left(query, 120) AS query
FROM pg_stat_activity
WHERE wait_event_type = 'Lock'
ORDER BY xact_start NULLS LAST;When wait_event_type is Lock, the session is waiting for a lock. The state column tells you whether PostgreSQL considers the session active or idle. xact_start is useful because an old transaction can explain why locks remain held.
The wait_event value gives more detail about what PostgreSQL is waiting for. A session with state = 'active' can still be blocked. “Active” means the backend is working on behalf of the client, not that it is making progress.
Find the blockers directly
pg_blocking_pids() avoids matching lock rows manually:
SELECT a.pid,
a.usename,
a.state,
a.wait_event_type,
a.wait_event,
pg_blocking_pids(a.pid) AS blocked_by,
a.xact_start,
left(a.query, 120) AS query
FROM pg_stat_activity AS a
WHERE cardinality(pg_blocking_pids(a.pid)) > 0;The blocked_by column is an array because one session can wait for more than one process. Usually, the most useful next step is to inspect each listed PID in pg_stat_activity:
SELECT pid,
usename,
application_name,
client_addr,
state,
xact_start,
query_start,
left(query, 200) AS query
FROM pg_stat_activity
WHERE pid IN (12345, 12346);Replace the numbers with the PIDs from blocked_by.
A blocker can itself be waiting for another session. This creates a blocking chain:
Investigate the end of the chain first. The first session in the chain is often only a victim of an earlier transaction.
Inspect the lock requests
Use pg_locks when you need lock type and mode details:
SELECT locktype,
relation::regclass AS relation,
page,
tuple,
virtualxid,
transactionid,
mode,
granted,
pid
FROM pg_locks
WHERE NOT granted
ORDER BY pid;A row with granted = false is a lock request that is waiting. relation::regclass converts a table’s internal object identifier into a readable name when the lock applies to a relation. Some lock types have no relation, so that column can be null.
| Column | Meaning |
|---|---|
| `pid` | PostgreSQL process ID for the session |
| `mode` | Requested or held lock mode |
| `granted` | Whether the session currently holds the lock |
| `relation` | Table or index involved, when applicable |
| `locktype` | Kind of lock, such as relation or transaction |
Interpret the evidence safely
Do not terminate a session merely because it appears in a lock query. First check its application, user, transaction age, and query. A long-running report may be expected, while an abandoned transaction may indicate an application bug.
Exercise
Exercise
A session is waiting on a lock, and pg_blocking_pids() identifies a session whose state is idle in transaction. What should you inspect first?
Show answer
Inspect the idle session's transaction start time, application, user, and last query. Confirm that terminating or correcting that session is safe before taking action.