dep*_*ner 6 ruby postgresql activerecord ruby-on-rails
为了防止在迁移到生产站点期间发生数据库事务错误,我们遵循了https://github.com/LendingHome/zero_downtime_migrations(特别是https://robots.thoughtbot.com/how-to- create-postgres-indexes-concurrently-in),但是在特别大的表上创建索引的过程中,即使使用“并发”索引创建方法也锁定了该表,并导致该表上的任何ActiveRecord创建或更新都导致了各自的事务失败失败PG::InFailedSqlTransaction。
这是我们运行Rails 4.2(使用ActiveRecord 4.2.7.1)时的迁移情况:
class AddTypeIndexToModel < ActiveRecord::Migration
disable_ddl_transaction!
def change
add_index :model, :model_type_id, algorithm: :concurrently
end
end
Run Code Online (Sandbox Code Playgroud)
事实证明,该问题与该迁移无关,而是之前在同一批次中运行的一个问题,该批次向同一个表添加新列。
因为我们向表中添加了一列,所以我们在 Active Record https://github.com/rails/rails/issues/12330中遇到了此错误,这实际上导致事务中的所有 ActiveRecord 操作由于陈旧的PreparedStatement而失败,直到服务器已重新启动。
从现在开始,我们将在迁移中使用该问题中描述的解决方法。