Skip to content

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.rb
index 78be56d9db4..24a61a2f08c 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -1045,16 +1045,13 @@ class Topic < ActiveRecord::Base
RETURNING highest_post_number
SQL
- 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 > :highest
SQL
end
diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb
index ef46e9f303d..292fa037bd7 100644
--- a/lib/post_destroyer.rb
+++ b/lib/post_destroyer.rb
@@ -195,7 +195,6 @@ class PostDestroyer
clear_user_posted_flag
end
- Topic.reset_highest(@post.topic_id)
trash_public_post_actions
trash_revisions
trash_user_actions
@@ -236,6 +235,8 @@ class PostDestroyer
end
DB.after_commit do
+ Topic.reset_highest(@post.topic_id)
+
if @opts[:reviewable]
notify_deletion(
@opts[:reviewable],