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?


2 Answers 2


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();

Not sure it is better or not than accepted answer but I've done it without join:

SELECT * FROM pg_locks WHERE database = (SELECT oid FROM pg_database WHERE datname = current_database())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.