Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL find locks including the table name

I'm trying to take a look at locks that are happening on specific tables in my PostgreSQL database.

I see there's a table called pg_locks

select * from pg_locks;

Which seems to give me a bunch of columns but is it possible to find the relation because I see one of the columns is the relation oid.

What table must I link that to to get the relation name?

like image 956
TheLovelySausage Avatar asked May 09 '16 07:05

TheLovelySausage


2 Answers

This is Remy's query, adjusted for Postgres 10:

select nspname, relname, l.* 
from pg_locks l 
    join pg_class c on (relation = c.oid) 
    join pg_namespace nsp on (c.relnamespace = nsp.oid)
where pid in (select pid 
              from pg_stat_activity
              where datname = current_database() 
                and query != current_query());
like image 71
Phil Horder Avatar answered Sep 27 '22 22:09

Phil Horder


If you just want the contents of pg_locks but with a human-friendly relation name,

select relation::regclass, * from pg_locks;
like image 25
Chris Chudzicki Avatar answered Sep 28 '22 00:09

Chris Chudzicki