Skip to content

Commit

Permalink
Limit size of user directory search queries (#18172)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
erikjohnston authored Feb 17, 2025
1 parent e462950 commit 0c31783
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/18172.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Reduce database load of user search when using large search terms.
8 changes: 7 additions & 1 deletion synapse/storage/databases/main/user_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,13 @@ def _parse_query_postgres(search_term: str) -> Tuple[str, str, str]:
search_term = _filter_text_for_index(search_term)

escaped_words = []
for word in _parse_words(search_term):
for index, word in enumerate(_parse_words(search_term)):
if index >= 10:
# We limit how many terms we include, as otherwise it can use
# excessive database time if people accidentally search for large
# strings.
break

# Postgres tsvector and tsquery quoting rules:
# words potentially containing punctuation should be quoted
# and then existing quotes and backslashes should be doubled
Expand Down

0 comments on commit 0c31783

Please sign in to comment.