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

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

This guide includes

Use this when a query is blocked and you need the culprit rather than a theory. This matters because pg_blocking_pids() names it in one column - and a read can be blocked by a lock it does not even conflict with.

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