2

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がすでにACCESSEXCLUSIVEロックでテーブルをロックしていることに気付きました:http ://www.postgresql.org/docs /9.1/static/explicit-locking.html

変更を行うためにできることはありますか?

4

1 に答える 1

5

排他的アクセスを取得しようとしている2つのプロセスがあります。

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.

それらの1つは、移行タスクです。もう1つはあなたのサーバーだと思います。私は提案します:

  • 開発環境を実行している場合は、サーバーを終了し、移行を実行してサーバーを再起動します。
  • 本番環境を実行していて、サーバーをシャットダウンせずに移行を実行する必要がある場合は、サーバーアプリに#migrateメソッドを追加して、同じプロセスで実行できるようにすることができます。

(正直なところ、私はマルチプロセッシング環境でPostgreSQLを掘り下げ始めたばかりです。もっと学べば、より良い答えを投稿します。)

于 2012-07-09T13:42:09.440 に答える