mirror of
https://github.com/open-webui/open-webui.git
synced 2026-07-15 21:19:39 -05:00
[PR #20542] [MERGED] fix: use efficient COUNT queries in telemetry metrics to prevent connection pool exhaustion #64519
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/open-webui/open-webui/pull/20542
Author: @Classic298
Created: 1/10/2026
Status: ✅ Merged
Merged: 1/10/2026
Merged by: @tjbck
Base:
dev← Head:db📝 Commits (1)
e2aa65dfix: use efficient COUNT queries in telemetry metrics to prevent connection pool exhaustion📊 Changes
1 file changed (+4 additions, -1 deletions)
View changed files
📝
backend/open_webui/utils/telemetry/metrics.py(+4 -1)📄 Description
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:
On high-latency network-attached databases like AWS Aurora, this would:
Under concurrent load, these long-held connections would stack up and drain the connection pool, resulting in:
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()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
Contributor License Agreement
By submitting this pull request, I confirm that I have read and fully agree to the Contributor License Agreement (CLA), and I am providing my contributions under its terms.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.