SELECT 
    a.pid AS blocking_pid, a.usename AS blocking_user, a.client_addr AS blocking_ip,
    b.pid AS blocked_pid, b.usename AS blocked_user, b.query AS blocked_statement,
    age(now(), a.query_start) AS age_of_lock
FROM pg_stat_activity a
JOIN pg_stat_activity b ON a.datid = b.datid AND a.pid != b.pid
JOIN pg_locks l1 ON a.pid = l1.pid
JOIN pg_locks l2 ON b.pid = l2.pid AND l1.relation = l2.relation
WHERE NOT l2.granted AND l1.granted;
