title: How can I prevent any ActiveRecord::PreparedStatementCacheExpired errors immediately after running `rake db:migrate`?
body: <p>I am working on a Rails 5.x application, and I use Postgres as my database.</p>
  
  <p>I often run <code>rake db:migrate</code> on my production servers. Sometimes the migration will add a new column to the database, and this causes some controller actions to crash with the following error:</p>
  
  <pre><code>ActiveRecord::PreparedStatementCacheExpired: ERROR: cached plan must not change result type
  </code></pre>
  
  <p>This is happening in a critical controller action that needs to have zero downtime, so I need to find a way to prevent this crash from ever happening.</p>
  
  <p>Should I catch the <code>ActiveRecord::PreparedStatementCacheExpired</code> error and retry the <code>save</code>? Or should I add some locking to this particular controller action, so that I don't start serving any new requests while a database migration is running?</p>
  
  <p>What would be the best way to prevent this crash from ever happening again?</p>