Skip to content

Start a client in more than 32,767 rooms, by matching its rooms as one array (CHOO-3667) - #1238

Closed
abeldantas wants to merge 2 commits into
mainfrom
many-rooms-start
Closed

abeldantas wants to merge 2 commits into
mainfrom
many-rooms-start

Conversation

@abeldantas

Copy link
Copy Markdown
Collaborator

When a client starts reading its rooms, three reads (its memberships, the rooms' heads, its stored cursors) matched the rooms with one bind parameter each, and Postgres refuses a statement with more than 32,767. Switch's own client is in every room of its workspace, archived ones included, so in a workspace past that many rooms its reader failed at start, was retried as a transient error, and failed again for as long as the process ran, taking up rooms one at a time in between. A load-test environment reached it with 41,461 rooms, and the process holding that reader sat at 56% CPU while idle.

In this PR we bind the rooms as one array in those three reads (db/id_lists.any_of).

…e array (CHOO-3667)

The reads a client makes as it starts receiving (its memberships, the
rooms' heads, its stored cursors) matched the rooms with one bind
parameter each, and Postgres refuses more than 32,767. Switch's own
client is in every room of its workspace, archived ones included, so past
that many its reader failed at start, was retried as a transient error,
and failed again, re-adopting rooms one at a time in between. A load
test environment reached it with 41,461 rooms, and that process sat at
56% CPU while idle.

The three reads now bind the rooms as one text array (db/id_lists.any_of).
Copilot AI lite review requested due to automatic review settings November 1, 2026 17:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The implementation updates all affected room-list queries to use PostgreSQL array matching and includes regression coverage for 40,000 room IDs, with no remaining active review issues indicated.

No open issues 🎉

What changed in this PR

This PR fixes client startup for workspaces with more than 32,767 rooms by binding room IDs as a single PostgreSQL array instead of individual parameters.

Changes:

  • Adds the reusable db.id_lists.any_of array-matching helper.
  • Updates membership, message-head, and delivery-cursor queries to use array matching.
  • Adds regression coverage for 40,000 room IDs.
  • Documents the fix in the changelog.
File Change Review note
core/​tests/​switch_core/​db/​stores/​test_long_room_lists.py Adds regression coverage for large room lists. The cursor assertion should verify the expected room-to-cursor mapping rather than allowing an empty result.
core/​switch_core/​db/​stores/​room_store.py Uses array matching for client room memberships. —
core/​switch_core/​db/​stores/​message_store.py Uses array matching for room-head lookup. —
core/​switch_core/​db/​stores/​delivery_cursor_store.py Uses array matching for stored delivery cursors. —
core/​switch_core/​db/​id_lists.py Adds the PostgreSQL ANY array predicate helper. —
CHANGELOG.md Documents the startup failure fix. —

This pull request requires changes.

Review effort: Lite

@abeldantas

Copy link
Copy Markdown
Collaborator Author

Combined into #1307 with the other scaling PRs.

@abeldantas abeldantas closed this Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants