Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Manually set n_distinct for event_search.room_id to avoid bad Postgres query plans #14402

Closed
erikjohnston opened this issue Nov 10, 2022 · 1 comment · Fixed by #14409
Closed
Assignees
Labels
A-Message-Search Searching messages O-Frequent Affects or can be seen by most users regularly or impacts most users' first experience S-Major Major functionality / product severely impaired, no satisfactory workaround. T-Defect Bugs, crashes, hangs, security vulnerabilities, or other reported issues.

Comments

@erikjohnston
Copy link
Member

On matrix.org we discovered that every now and again event_search queries would take forever due to using the wrong query plan. This was due to Postgres underestimating the number of room IDs that existed.

To fix this we should add a schema update with:

ALTER TABLE event_search ALTER COLUMN room_id SET (n_distinct = -0.01);

like we do for state_groups_state.state_group already.

@squahtx squahtx added S-Major Major functionality / product severely impaired, no satisfactory workaround. A-Message-Search Searching messages O-Frequent Affects or can be seen by most users regularly or impacts most users' first experience T-Defect Bugs, crashes, hangs, security vulnerabilities, or other reported issues. labels Nov 10, 2022
@squahtx squahtx self-assigned this Nov 10, 2022
@squahtx
Copy link
Contributor

squahtx commented Nov 10, 2022

like we do for state_groups_state.state_group already.

See #10359 for that fix.

squahtx pushed a commit that referenced this issue Nov 10, 2022
PostgreSQL may underestimate the number of distinct `room_id`s in
`event_search`, which can cause it to use table scans for queries for
multiple rooms.

Fix this by setting `n_distinct` on the column.

Resolves #14402.

Signed-off-by: Sean Quah <[email protected]>
squahtx added a commit that referenced this issue Nov 10, 2022
PostgreSQL may underestimate the number of distinct `room_id`s in
`event_search`, which can cause it to use table scans for queries for
multiple rooms.

Fix this by setting `n_distinct` on the column.

Resolves #14402.

Signed-off-by: Sean Quah <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-Message-Search Searching messages O-Frequent Affects or can be seen by most users regularly or impacts most users' first experience S-Major Major functionality / product severely impaired, no satisfactory workaround. T-Defect Bugs, crashes, hangs, security vulnerabilities, or other reported issues.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants