open-webui/backend/open_webui
Classic298 7839d043ff
fix: use efficient COUNT queries in telemetry metrics to prevent connection pool exhaustion (#20542)
fix: use efficient COUNT queries in telemetry metrics to prevent connection pool exhaustion

This fixes database connection pool exhaustion issues reported after v0.7.0,
particularly affecting PostgreSQL deployments on high-latency networks (e.g., AWS Aurora).

## The Problem

The telemetry metrics callbacks (running every 10 seconds via OpenTelemetry's
PeriodicExportingMetricReader) were using inefficient queries that loaded entire
database tables into memory just to count records:

    len(Users.get_users()["users"])  # Loads ALL user records to count them

On high-latency network-attached databases like AWS Aurora, this would:
1. Hold database connections for hundreds of milliseconds while transferring data
2. Deserialize all records into Python objects
3. Only then count the list length

Under concurrent load, these long-held connections would stack up and drain the
connection pool, resulting in:

    sqlalchemy.exc.TimeoutError: QueuePool limit of size 5 overflow 10 reached,
    connection timed out, timeout 30.00

## The Fix

Replace inefficient full-table loads with efficient COUNT(*) queries using
methods that already exist in the codebase:

- `len(Users.get_users()["users"])` → `Users.get_num_users()`
- Similar changes for other telemetry callbacks as needed

COUNT(*) queries use database indexes and return a single integer, completing in
~5-10ms even on Aurora, versus potentially 500ms+ for loading all records.

## Why v0.7.1's Session Sharing Disable "Helped"

The v0.7.1 change to disable DATABASE_ENABLE_SESSION_SHARING by default appeared
to fix the issue, but it was masking the root cause. Disabling session sharing
causes connections to be returned to the pool faster (more connection churn),
which reduced the window for pool exhaustion but didn't address the underlying
inefficient queries.

With this fix, session sharing can be safely re-enabled for deployments that
benefit from it (especially PostgreSQL), as telemetry will no longer hold
connections for extended periods.

## Impact

- Telemetry connection usage drops from potentially seconds to ~30ms total per
  collection cycle
- Connection pool pressure from telemetry becomes negligible (~0.3% utilization)
- Enterprise PostgreSQL deployments (Aurora, RDS, etc.) should no longer
  experience pool exhaustion under normal load
2026-01-10 15:33:42 +04:00
..
data refac: mv backend files to /open_webui dir 2024-09-04 16:54:48 +02:00
internal refac/fix: DATABASE_ENABLE_SESSION_SHARING env var 2026-01-10 00:16:04 +04:00
migrations feat: chat_file table 2025-12-21 23:17:53 +04:00
models chore: format 2026-01-09 20:44:31 +04:00
retrieval chore: format 2026-01-09 22:27:53 +04:00
routers fix: correct empty STT_ENGINE handling and improve TTS error response (#20534) 2026-01-10 15:32:22 +04:00
socket chore: format 2026-01-08 01:55:56 +04:00
static refac 2025-08-10 00:02:58 +04:00
storage chore/perf: Remove old SRC level log env vars with no impact (#20045) 2025-12-20 08:16:14 -05:00
test rm: outdated tests 2025-12-28 23:35:09 +04:00
tools chore: format 2026-01-09 22:27:53 +04:00
utils fix: use efficient COUNT queries in telemetry metrics to prevent connection pool exhaustion (#20542) 2026-01-10 15:33:42 +04:00
__init__.py Update __init__.py 2025-04-15 09:55:35 +02:00
alembic.ini fix: Alembic CLI commands from failing 2025-08-15 04:17:47 -04:00
config.py enh: WHISPER_MULTILINGUAL 2026-01-09 19:42:13 +04:00
constants.py feat/enh: optional password validation 2025-11-20 17:44:49 -05:00
env.py refac/fix: DATABASE_ENABLE_SESSION_SHARING env var 2026-01-10 00:16:04 +04:00
functions.py chore/perf: Remove old SRC level log env vars with no impact (#20045) 2025-12-20 08:16:14 -05:00
main.py enh: WHISPER_MULTILINGUAL 2026-01-09 19:42:13 +04:00
tasks.py chore/perf: Remove old SRC level log env vars with no impact (#20045) 2025-12-20 08:16:14 -05:00