Hands-on Lab·PostgreSQL
PostgreSQL Locks and Blocked Queries
`pg_blocking_pids()` names the session at fault in one column. Then a plain `SELECT count(*)` gets killed by a lock it does not conflict with.
Monitoring and Operations Guide 39 of 47 Intermediate
- OSUbuntu 26.04 LTS (resolute)
- PostgreSQL18.6-0ubuntu0.26.04.1
- Host2 core / 4 GB, 400,000-row test table
- TimeAbout 18 min
- Reviewed27 August 2026
Written against the versions above. `pg_blocking_pids()` has been available since PostgreSQL 9.6. Before that, finding a blocker meant self-joining `pg_locks` by hand.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| db-a01 | 192.168.0.81 | Ubuntu 26.04 LTS | Primary / Source / Replica Set Member 1 | 2 Core | 4 GB | 50 GB |
Before you start
- The
eventstable from the performance track. - Two shells, or the background jobs used here.
-
Create a blocked query
-
Read the locks underneath
-
Name the blocker in one column
-
Release it
-
Set a limit instead of waiting forever
-
Watch a read get blocked by a lock it does not conflict with