title: Deadlock detected in PL/pgSQL function
body: <p>I am facing a deadlock problem from a PL/pgSQL function in my PostgreSQL database. Please find the SQL statement in the code block (just example):</p>
  
  <pre><code>BEGIN
  UPDATE accounts SET balance = 0 WHERE acct_name like 'A%';
  UPDATE accounts SET balance = balance + 100 WHERE acct_name like '%A';
  EXCEPTION WHEN OTHERS THEN RAISE NOTICE SQLERRM;
  END;
  </code></pre>
  
  <p>I've found that the deadlock occurred during this statement was running. But I'm not sure that there were other statements trying to update this table in the same time (because I didn't find any in my logging system).</p>
  
  <p>So, is it possible that the deadlock occurred within this statement? As far as I know, if we blocked whole statement with <code>BEGIN</code>/<code>END</code>. There will be the same transaction and should not be locked by itself.</p>