From 73a52c8a24150ec0d313f5459b52525eb5104194 Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Thu, 16 Jul 2026 14:19:33 +0300 Subject: [PATCH] Make postgres_max_connections scale with the Synapse worker count With workers enabled, every Synapse process (the main one and each worker) maintains its own database connection pool of up to matrix_synapse_database_cp_max (default: 10) connections. The previous fixed limit of 500 left little headroom on setups running many workers, where exhausting it manifests as degraded performance and, reportedly, E2EE misbehavior. Size the limit as a 200-connection baseline (for everything else that talks to Postgres) plus cp_max connections per Synapse process, never going below the previous value of 500. The stock worker presets stay at 500; setups that raise worker counts (or cp_max) get a limit that grows accordingly. The new matrix_synapse_workers_total_count variable reflects what the matrix_synapse_workers_*_count variables ask for; it does not reflect a manually populated matrix_synapse_workers_enabled_list, and such setups should override postgres_max_connections themselves. Fixes #4442 Co-Authored-By: Claude Fable 5 --- group_vars/matrix_servers | 8 +++++++- roles/custom/matrix-synapse/defaults/main.yml | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/group_vars/matrix_servers b/group_vars/matrix_servers index 6febdd398..5e21de444 100755 --- a/group_vars/matrix_servers +++ b/group_vars/matrix_servers @@ -4046,7 +4046,13 @@ postgres_db_name: matrix postgres_systemd_services_to_stop_for_maintenance_list_auto: "{{ devture_systemd_service_manager_services_list_auto | map(attribute='name') | reject('equalto', (postgres_identifier + '.service')) }}" -postgres_max_connections: "{{ 500 if matrix_synapse_workers_enabled else 200 }}" +# When Synapse workers are enabled, connection demand grows with the number of Synapse processes: +# each process (the main one and every worker) maintains its own connection pool of up to `matrix_synapse_database_cp_max` connections. +# The formula below reserves a 200-connection baseline for everything else talking to Postgres and adds `cp_max` per Synapse process. +# It never goes below 500, the fixed value used previously, so existing worker setups are not downsized. +# Note: the worker count reflects the `matrix_synapse_workers_*_count` variables. +# If you populate `matrix_synapse_workers_enabled_list` manually, consider overriding `postgres_max_connections` yourself. +postgres_max_connections: "{{ [200 + (matrix_synapse_database_cp_max | int) * ((matrix_synapse_workers_total_count | int) + 1), 500] | max if matrix_synapse_workers_enabled else 200 }}" postgres_container_image_registry_prefix_upstream: "{{ matrix_container_global_registry_prefix_override if matrix_container_global_registry_prefix_override else postgres_container_image_registry_prefix_upstream_default }}" diff --git a/roles/custom/matrix-synapse/defaults/main.yml b/roles/custom/matrix-synapse/defaults/main.yml index 82350e456..82d406dab 100644 --- a/roles/custom/matrix-synapse/defaults/main.yml +++ b/roles/custom/matrix-synapse/defaults/main.yml @@ -1239,6 +1239,25 @@ matrix_synapse_workers_background_workers_container_arguments: [] # `run_background_tasks_on` is meant to point to a worker, which is dedicated to processing background tasks. matrix_synapse_run_background_tasks_on: "{{ (matrix_synapse_workers_enabled_list | selectattr('type', 'equalto', 'background') | list)[0].name if (matrix_synapse_workers_enabled and matrix_synapse_workers_enabled_list | selectattr('type', 'equalto', 'background') | list | length > 0) else '' }}" +# matrix_synapse_workers_total_count reflects the total number of Synapse worker processes that the `matrix_synapse_workers_*_count` variables ask for. +# This is useful for sizing resources that depend on the number of Synapse processes (e.g. the maximum number of Postgres connections). +# Note: if `matrix_synapse_workers_enabled_list` is populated manually (instead of via the count variables), this count does not reflect it. +matrix_synapse_workers_total_count: | + {{ + (matrix_synapse_workers_room_workers_count | int) + + (matrix_synapse_workers_sync_workers_count | int) + + (matrix_synapse_workers_client_reader_workers_count | int) + + (matrix_synapse_workers_federation_reader_workers_count | int) + + (matrix_synapse_workers_generic_workers_count | int) + + (matrix_synapse_workers_pusher_workers_count | int) + + (matrix_synapse_workers_federation_sender_workers_count | int) + + (matrix_synapse_workers_media_repository_workers_count | int) + + (matrix_synapse_workers_appservice_workers_count | int) + + (matrix_synapse_workers_user_dir_workers_count | int) + + (matrix_synapse_workers_background_workers_count | int) + + (matrix_synapse_workers_stream_writers | length) + }} + # Default list of workers to spawn. # # Unless you populate this manually, this list is dynamically generated