CertGrid CertGrid
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

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.

Every command on this page ran on db-a01.
Server NameIP AddressOSRolesCPURAMHDD
db-a01192.168.0.81Ubuntu 26.04 LTSPrimary / Source / Replica Set Member 12 Core4 GB50 GB

Before you start

  1. Create a blocked query

  2. Read the locks underneath

  3. Name the blocker in one column

  4. Release it

  5. Set a limit instead of waiting forever

  6. Watch a read get blocked by a lock it does not conflict with

Official sources