title: pg_advisory_lock works but not pg_try_advisory_lock
body: <p>I want to run a cron job from inside my application. But when I run multiple instance of this application, I wish to have mutual exclusion. i.e. the cron jobs are run only through a single process.  </p>
  
  <p>I am using <strong><em>advisory locks</em></strong> to achieve this.</p>
  
  <p>When I tried using advisory locks. This is what I noticed.</p>
  
  <p>I created dedicated table for this. For example a table called 'cron'. When I try   </p>
  
  <pre><code>SELECT pg_advisory_lock(123) FROM cron;
  </code></pre>
  
  <p>It doesn't acquire any lock. But When I inserted a row into this table and tried the same command again, then acquiring lock is successful.<br>
  So my question: is it necessary to have at least a row to get advisory lock? Or am I missing something here?</p>
  
  <p>Secondly When I went through the documentaion, I found that instead of <code>pg_advisory_lock</code> I can also use <code>pg_try_advisory_lock</code> which would return true or false (unlike <code>pg_advisory_lock</code> which throws an exception). </p>
  
  <p>But when I tried pg_try_advisory_lock what I observed is, when one process executes and gets the lock. I see three entries created in pg_locks with lock_type <em>relation</em>, <em>virtualxid</em>, and <em>advisory</em>) When another process runs in parallel and tries to get lock, it created two more entries in pg_locks with lock_type as <em>relation</em> and <em>virtualxid</em>. And hence the second process also return true.</p>
  
  <p><strong>With pg_try_advisory_lock both the process returns true.</strong> </p>
  
  <p>But If I use pg_advisory_lock and use lock_timeout(with very low value) , then the second process exits with exception. This way I could achieve mutual exclusion.  </p>
  
  <p><strong>So why is it not working with pg_try_advisory_lock but works with pg_advisory_lock (in conjunction with lock timeout)?</strong> </p>
  
  <p>Any hint is much appreciated</p>
  
  <p>I am using Postgresql. And connecting via a Java Application.</p>