PostgreSQL – How to Get pg_locks for Specific Database

lockingpostgresql

This question is related to my previous one here

In SQL Server, I could get the lock type from a specific database using the code below:

SELECT request_mode FROM sys.dm_tran_locks WHERE resource_database_id = DB_ID() AND resource_associated_entity_id = OBJECT_ID(N'dbo.cars');

In Postgres 11, how to filter the pg_locks result to a specific database like how SQL Server's resource_database_id = DB_ID() AND resource_associated_entity_id = OBJECT_ID(N'dbo.cars') works?

Thanks

Best Answer

As documented in the manual pg_locks contains a column database which you can join to the pg_database view:

select l.*
from pg_locks l
  join pg_database d on d.oid = l.database
where d.datname = current_database();