title: Postgres UPDATE with ORDER BY, how to do it?
body: <p>I need to do a Postgres update on a collection of records &amp; I'm trying to prevent a deadlock which appeared in the stress tests. </p>
  
  <p>The typical resolution to this is to update records in a certain order, by ID for example - but it seems that Postgres doesn't allow ORDER BY for UPDATE.</p>
  
  <p>Assuming I need to do an update, for example:</p>
  
  <pre><code>UPDATE BALANCES WHERE ID IN (SELECT ID FROM some_function() ORDER BY ID);
  </code></pre>
  
  <p>results in deadlocks when you run 200 queries concurrently. What to do?</p>
  
  <p>I'm looking for a general solution, not case-specific workarounds like in <a href="https://stackoverflow.com/questions/16735950/update-with-order-by">UPDATE with ORDER BY</a></p>
  
  <p>It <em>feels</em> that there must be a better solution than writing a cursor function. Also, if there's no better way, how would that cursor function optimally look like? Update record-by-record</p>