PostgreSQL Lock Waits: Find the Blocking Session
Find the root PostgreSQL blocker, distinguish it from waiting queries, and investigate an idle transaction before intervening.
When several previously fast queries slow down at once and their wait type is Lock, start with the session holding up the chain. The longest-running statement on the screen might be the one waiting, not the one holding the lock.
Find the blocked sessions
Match the application’s timeout window with PostgreSQL lock waits. In pg_stat_activity, a Lock wait is a heavyweight lock wait. Use pg_blocking_pids(pid) to find the immediate blockers rather than trying to infer the queue from a self-join on pg_locks.
SELECT pid, application_name, query_start,
pg_blocking_pids(pid) AS blocked_by
FROM pg_stat_activity
WHERE wait_event_type = 'Lock';Use this as a point-in-time check, not a tight polling loop. Details for other users’ sessions depend on your permissions. See the PostgreSQL lock monitoring documentation.
Follow the chain back to its root
If A waits on B and B waits on C, inspect C before analyzing the SQL text of A. Compare its session state, transaction age and workload identity when available. An idle transaction may continue to hold a lock from a statement it already finished; the last query is evidence, not proof of exactly which statement took the lock.
In the WatchLog product example above, eight sessions wait while an idle inventory-sync transaction holds a row lock on stock. The root blocker is the transaction, not the checkout queries waiting behind it. In WatchLog, open the Locks view to see the blocking tree and open transactions.
Restore throughput, then fix the transaction lifecycle
Confirm the session owner and business effect first. Ask the application or migration owner to finish or roll back the transaction if possible. Cancelling a running statement and ending a connection are different interventions; ending a session may roll back work, so don't treat it as the default. Check whether waiters drain and application latency recovers.
Then look for a transaction held open across a network call, a missing commit or rollback on an error path, or a deploy running DDL during traffic. A lock timeout can bound how long callers wait, but it does not remove the cause.
Monitor locks before the next incident
Use the WatchLog PostgreSQL setup guide to connect the database and check what telemetry is enabled. PostgreSQL 14 or newer is required for WatchLog’s statement-level wait attribution. The pg_stat_statements extension adds statement history; basic blocking-session checks don't require it. See the PostgreSQL product example.