【问题标题】:Rails add_index algorithm: :concurrently still causes database lock up during migrationRails add_index 算法::迁移期间并发仍然导致数据库锁定
【发布时间】:2017-10-17 19:30:10
【问题描述】:

为了防止在我们迁移到生产站点期间发生数据库事务错误,我们遵循了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

【问题讨论】:

标签: ruby-on-rails ruby postgresql activerecord


【解决方案1】:

事实证明,该问题与该迁移无关,而是在同一批次中运行的一个先前问题与向同一个表中添加新列有关。

因为我们在表中添加了一个列,所以我们在 Active Record https://github.com/rails/rails/issues/12330 中遇到了这个错误,这实际上导致事务中的所有 ActiveRecord 操作由于过时的 PreparedStatement 而失败,直到服务器重新启动。

从现在开始,我们将在迁移中使用该问题中描述的解决方法。

【讨论】:

    猜你喜欢
    • 2019-02-25
    • 2015-10-27
    • 2018-10-29
    • 2021-06-17
    • 2018-03-12
    • 2018-04-30
    • 1970-01-01
    • 2012-04-11
    • 2012-04-14
    相关资源
    最近更新 更多