Skip to content

Commit 0c31783

Browse files
authoredFeb 17, 2025
Limit size of user directory search queries (#18172)
If a user search has many words we can end up creating really large queries that take a long time for the database to process. Generally, such searches don't return any results anyway (due to limits on user ID and display name length). We "fix" this by cheating and only searching for the first ten words.
1 parent e462950 commit 0c31783

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed
 

‎changelog.d/18172.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Reduce database load of user search when using large search terms.

‎synapse/storage/databases/main/user_directory.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,13 @@ def _parse_query_postgres(search_term: str) -> Tuple[str, str, str]:
12371237
search_term = _filter_text_for_index(search_term)
12381238

12391239
escaped_words = []
1240-
for word in _parse_words(search_term):
1240+
for index, word in enumerate(_parse_words(search_term)):
1241+
if index >= 10:
1242+
# We limit how many terms we include, as otherwise it can use
1243+
# excessive database time if people accidentally search for large
1244+
# strings.
1245+
break
1246+
12411247
# Postgres tsvector and tsquery quoting rules:
12421248
# words potentially containing punctuation should be quoted
12431249
# and then existing quotes and backslashes should be doubled

0 commit comments

Comments
 (0)