A platform for community discussion. Free, open, simple.
FIX: Reduce deadlocks in Topic.reset_highest updates (#36805)
Régis Hanoldd4650d609ad
diff --git a/app/models/topic.rb b/app/models/topic.rbindex 78be56d9db4..24a61a2f08c 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -1045,16 +1045,13 @@ class Topic < ActiveRecord::BaseRETURNING highest_post_numberSQL- highest_post_number = result.first.to_i+ highest = result.first.to_i- # Update the forum topic user records- DB.exec(<<~SQL, highest: highest_post_number, topic_id: topic_id)+ DB.exec(<<~SQL, highest:, topic_id:)UPDATE topic_users- SET last_read_post_number = CASE- WHEN last_read_post_number > :highest THEN :highest- ELSE last_read_post_number- END- WHERE topic_id = :topic_id+ SET last_read_post_number = :highest+ WHERE topic_id = :topic_id+ AND last_read_post_number > :highestSQLenddiff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rbindex ef46e9f303d..292fa037bd7 100644--- a/lib/post_destroyer.rb+++ b/lib/post_destroyer.rb@@ -195,7 +195,6 @@ class PostDestroyerclear_user_posted_flagend- Topic.reset_highest(@post.topic_id)trash_public_post_actionstrash_revisionstrash_user_actions@@ -236,6 +235,8 @@ class PostDestroyerendDB.after_commit do+ Topic.reset_highest(@post.topic_id)+if @opts[:reviewable]notify_deletion(@opts[:reviewable],