Thursday, May 24, 2007

Quick copy-pastable SQL query to check PostgreSQL locks

We recently faced some lock-related issues with PostgreSQL (due to the "serializable" transaction isolation level we are using), resulting in processes locked in an INSERT WAITING state, and an unresponsive web frontend. The database itself was fine though, so we did some pocking around using psql in order to find the source of the lock. A simple select * from pg_locks; shows only not so helpful OIDs. It is of course straightforward to get meaningful names, it is just a pain to type it every time, so here is the quick copy-paste ready version we used during this debugging:


SELECT
c.relname,
l.transaction,
l.pid,
l.mode,
l.granted
FROM pg_locks AS l
LEFT OUTER JOIN pg_class AS c
ON l.relation = c.oid
ORDER BY l.pid, l.granted, c.relname;

No comments: