# Clarification on how the database search works in Mattermost

**URL:** <https://forum.mattermost.com/t/clarification-on-how-the-database-search-works-in-mattermost/25492>\
**Category:** Troubleshooting\
**Created:** [November 13, 2025, 4:59am UTC](https://forum.mattermost.com/t/clarification-on-how-the-database-search-works-in-mattermost/25492 "2025-11-13T04:59:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwdev](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/mwdev/32/9301_2.png) [@mwdev](https://forum.mattermost.com/u/mwdev)\
**Post date:** [November 13, 2025, 4:59am UTC](https://forum.mattermost.com/t/clarification-on-how-the-database-search-works-in-mattermost/25492/1 "2025-11-13T04:59:15Z")

</div>

**Summary**  
The ‘simple’ database search change aimed to prevent stemming for fully quoted strings, but it appears to impact some users even when the input isn’t fully quoted.

**Steps to reproduce**  
Hello everyone, I’m trying to understand how the internal database search works in Mattermost. After upgrading our test server from version `10.5.11` to `10.11.6`, the UI “_Recent mentions_” button starts returning HTTP 499 errors (Client Closed Request) when querying `api/v4/posts/search` about 30 seconds after a user clicks it in both Web and Desktop clients. The server logs show errors like:

> `Query error searching posts. caller=“sqlstore/post_store.go:2274” error=“pq: canceling statement due to user request”`

**Expected behavior**  
The system should return search results promptly for recent mentions without canceling due to user request, regardless of client (Web, Desktop, Mobile), and should apply the configured text search mechanism consistently across users unless explicitly overridden by input terms or locale.

**Observed behavior**  
For some users, this mechanism behaves as expected and completes quickly, with no error. In the PostgreSQL logs, the problematic query from Web/Desktop clients looks like this:

> `SELECT `_`, (SELECT COUNT(`_`) FROM Posts WHERE Posts.RootId = (CASE WHEN q2.RootId = ‘’ THEN q2.Id ELSE q2.RootId END) AND Posts.DeleteAt = 0) as ReplyCount FROM Posts q2 WHERE q2.DeleteAt = 0 AND q2.Type NOT LIKE ‘system_%’ AND to_tsvector(‘simple’, Message) @@ `**`to_tsquery(‘simple’,‘“@test.user”’)`**` AND ChannelId IN (SELECT Id FROM Channels, ChannelMembers WHERE Id = ChannelId AND ChannelMembers.UserId = ‘{REDACTED}’) ORDER BY q2.CreateAt DESC LIMIT 100`

Meanwhile, users who do not experience the issue see a different query, and their searches use a different text search configuration:

> `SELECT `_`, (SELECT COUNT(`_`) FROM Posts WHERE Posts.RootId = (CASE WHEN q2.RootId = ‘’ THEN q2.Id ELSE q2.RootId END) AND Posts.DeleteAt = 0) as ReplyCount FROM Posts q2 WHERE q2.DeleteAt = 0 AND q2.Type NOT LIKE ‘system_%’ AND to_tsvector(‘pg_catalog.english’, Message) @@ `**`to_tsquery(‘pg_catalog.english’,‘“@test.seconduser”’)`**` AND ChannelId IN (SELECT Id FROM Channels, ChannelMembers WHERE Id = ChannelId AND ChannelMembers.UserId = ‘{REDACTED}’) ORDER BY q2.CreateAt DESC LIMIT 100`

For all users, the “_Recent mentions_” view works normally on mobile, and the corresponding PostgreSQL queries use GIN `pg_catalog.english`. I’ve seen that the change to use the ‘simple’ search was introduced to address “Prevent stemming in DB search when search term is a fully quoted string” ([MM-60790](https://mattermost.atlassian.net/browse/MM-60790); PR [#30214](https://github.com/mattermost/mattermost/pull/30214)).

**My questions**

- How does the simple search mode operate in Mattermost, in terms of indexing, tokenization, and stemming behavior?
- Why can some users’ searches work using the simple configuration while the input is not fully quoted, whereas others fail under the same conditions?

Any explanation of the underlying mechanism and practical guidance to diagnose or reproduce consistently would be appreciated. Thank you.
