Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
message: Enforce no parallel bitmap heap scans.
Alex Vandiver74d6f033b02d
diff --git a/zerver/lib/message.py b/zerver/lib/message.pyindex dcc270a3f4c..1d5d945f067 100644--- a/zerver/lib/message.py+++ b/zerver/lib/message.py@@ -871,37 +871,51 @@ def get_raw_unread_data(where=[UserMessage.where_unread()],)- # Limit unread messages for performance reasons. We do this- # inside a CTE, such that the join to Recipients, below, can't be- # implied to remove rows, and thus allows a Nested Loop join,- # potentially memoized to reduce the number of Recipient lookups.- cte = With(user_msgs[:MAX_UNREAD_MESSAGES])-- user_msgs = (- cte.join(Recipient, id=cte.col.recipient_id)- .with_cte(cte)- .annotate(- message_id=cte.col.message_id,- sender_id=cte.col.sender_id,- recipient_id=cte.col.recipient_id,- topic=cte.col.topic,- flags=cte.col.flags,- recipient__type=F("type"),- recipient__type_id=F("type_id"),- )- .values(- "message_id",- "sender_id",- "topic",- "flags",- "recipient_id",- "recipient__type",- "recipient__type_id",- )- )+ with connection.cursor() as cursor:+ try:+ # Force-disable (parallel) bitmap heap scans. The+ # parallel nature of this means that the LIMIT cannot be+ # pushed down into the walk of the index, and it also+ # requires an additional outer sort -- which is all+ # unnecessary, as the zerver_usermessage_unread_message_id+ # index is properly ordered already. This is all due to+ # statistics mis-estimations, since partial indexes do not+ # have their own statistics.+ cursor.execute("SET enable_bitmapscan TO off")++ # Limit unread messages for performance reasons. We do this+ # inside a CTE, such that the join to Recipients, below, can't be+ # implied to remove rows, and thus allows a Nested Loop join,+ # potentially memoized to reduce the number of Recipient lookups.+ cte = With(user_msgs[:MAX_UNREAD_MESSAGES])++ user_msgs = (+ cte.join(Recipient, id=cte.col.recipient_id)+ .with_cte(cte)+ .annotate(+ message_id=cte.col.message_id,+ sender_id=cte.col.sender_id,+ recipient_id=cte.col.recipient_id,+ topic=cte.col.topic,+ flags=cte.col.flags,+ recipient__type=F("type"),+ recipient__type_id=F("type_id"),+ )+ .values(+ "message_id",+ "sender_id",+ "topic",+ "flags",+ "recipient_id",+ "recipient__type",+ "recipient__type_id",+ )+ )- rows = list(reversed(user_msgs))- return extract_unread_data_from_um_rows(rows, user_profile)+ rows = list(reversed(user_msgs))+ finally:+ cursor.execute("SET enable_bitmapscan TO on")+ return extract_unread_data_from_um_rows(rows, user_profile)def extract_unread_data_from_um_rows(diff --git a/zerver/tests/test_event_system.py b/zerver/tests/test_event_system.pyindex 043f8cb236f..6ca2acec09a 100644--- a/zerver/tests/test_event_system.py+++ b/zerver/tests/test_event_system.py@@ -1213,7 +1213,7 @@ class FetchQueriesTest(ZulipTestCase):self.login_user(user)with (- self.assert_database_query_count(42),+ self.assert_database_query_count(44),mock.patch("zerver.lib.events.always_want") as want_mock,):fetch_initial_state_data(user, realm=user.realm)@@ -1259,7 +1259,7 @@ class FetchQueriesTest(ZulipTestCase):subscription=9,update_display_settings=0,update_global_notifications=0,- update_message_flags=5,+ update_message_flags=7,user_settings=0,user_status=1,user_topic=1,diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.pyindex 11c42d59787..ceea5a9bf4a 100644--- a/zerver/tests/test_home.py+++ b/zerver/tests/test_home.py@@ -277,7 +277,7 @@ class HomeTest(ZulipTestCase):# Verify succeeds once logged-inwith (- self.assert_database_query_count(52),+ self.assert_database_query_count(54),patch("zerver.lib.cache.cache_set") as cache_mock,):result = self._get_home_page(stream="Denmark")@@ -578,7 +578,7 @@ class HomeTest(ZulipTestCase):# Verify number of queries for Realm admin isn't much higher than for normal users.self.login("iago")with (- self.assert_database_query_count(51),+ self.assert_database_query_count(53),patch("zerver.lib.cache.cache_set") as cache_mock,):result = self._get_home_page()@@ -610,7 +610,7 @@ class HomeTest(ZulipTestCase):self._get_home_page()# Then for the second page load, measure the number of queries.- with self.assert_database_query_count(47):+ with self.assert_database_query_count(49):result = self._get_home_page()# Do a sanity check that our new streams were in the payload.