gpt4 book ai didi

ruby-on-rails - 更改表时出现死锁

转载 作者:行者123 更新时间:2023-11-29 12:38:44 26 4
gpt4 key购买 nike

我正在我的 Rails 应用程序中的一个包含很多行的表上运行以下迁移:

rake db:migrate
*** [] rake aborted!
*** [] An error has occurred, this and all later migrations canceled:
*** []
*** [] PG::Error: ERROR: deadlock detected
*** [] DETAIL: Process 33319 waits for AccessExclusiveLock on relation 18486 of database 16948; blocked by process 29772.
*** [] Process 29772 waits for ShareLock on transaction 8652; blocked by process 33319.
*** [] HINT: See server log for query details.
*** [] : ALTER TABLE "topics" DROP "most_recent_post_id"
*** []
*** [] Tasks: TOP => db:migrate
*** [] (See full trace by running task with --trace)
** [] == RemoveMostRecentPostsColumnsOnTopics: migrating
** [] Updated 56875150 rows out of 568715 tries
** [] -- remove_column(:topics, :most_recent_post_id)

运行的代码是这样的:

def self.up
rows_updated = 0
rows_tried = 0

Topic.find(:all).each do |topic|
rows_tried += 1
rows_updated += 1 if topic.update_attribute :updated_at, topic.most_recent_post_created_at
end

puts "Updated #{rows_updated} rows out of #{rows_tried} tries"

remove_column :topics, :most_recent_post_id
remove_column :topics, :most_recent_post_created_at
end

然后我尝试将其作为显式锁来执行,但是在搜索有关问题的信息时,我意识到 ALTER TABLE 已经使用 ACCESS EXCLUSIVE 锁锁定了表,根据以下内容:http://www.postgresql.org/docs/9.1/static/explicit-locking.html

我可以做些什么来完成我的更改吗?

最佳答案

您有两个进程试图获得独占访问权:

Process 33319 waits for AccessExclusiveLock on relation 18486 of database 16948; blocked by process 29772.
Process 29772 waits for ShareLock on transaction 8652; blocked by process 33319.

其中之一是您的迁移任务。我假设另一个是您的服务器。我建议:

  • 如果您正在运行开发环境,请退出您的服务器,运行迁移并重新启动您的服务器。
  • 如果您运行的是生产环境并且需要在不关闭服务器的情况下运行迁移,您可以将#migrate 方法添加到您的服务器应用,以便它在同一进程中运行。

(老实说,我刚刚开始在多处理环境中深入研究 PostgreSQL——如果我了解更多,我会发布更好的答案。)

关于ruby-on-rails - 更改表时出现死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10331080/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com