title: How to obtain an indefinite lock on database in Rails (postgres database) for QA purposes?
body: <p>I'm trying to obtain an indefinite lock on my Postgresql database (specifically on a table called orders) for QA purposes. In short, I want to know if certain locks on a table prevent or indefinitely block database migrations for adding columns (I think <code>ALTER TABLE</code> grabs an <code>ACCESS EXCLUSIVE LOCK</code>).</p>
  
  <p>My plan is to:  </p>
  
  <ol>
  <li>grab a table lock or a row lock on the orders table  </li>
  <li>run the migration to add a column (an ALTER TABLE statement that grabs an ACCESS EXCLUSIVE LOCK)  </li>
  <li>issue a read statement to see if (2) is blocked (the ACCESS EXCLUSIVE LOCK blocks reads, and so this would be a problem that I'm trying to QA).  </li>
  </ol>
  
  <p>How would one do this? How do I grab a row lock on a table called orders via the Rails Console? How else could I do this?</p>
  
  <p>Does my plan make sense?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>It turns out open row-level transactions actually do block ALTER TABLE statements that grab an ACCESS EXCLUSIVE LOCK like table migrations that add columns. For example, when I run this code in one process:</p>
  
  <pre><code> Order.first.with_lock do
      binding.pry
   end
  </code></pre>
  
  <p>It blocks my migration in another process to add a column to the orders table. That migration's ACCESS EXCLUSIVE LOCK blocks all reads and select statements to the orders table, causing problems for end users.</p>
  
  <p>Why is this?</p>