If you just want to determine whether a particular named lock is currently held, you can use IS_USED_LOCK
:
SELECT IS_USED_LOCK('foobar');
If some connection holds the lock, that connection's ID will be returned; otherwise, the result is NULL
.