Polar Proxy redesigns PostgreSQL reader pool for exact connection reuse and lower lock contention
Polar Proxy replaces ProxySQL's generic connection pool with a server-first design that uses exact four-part matching and worker-local caching to eliminate per-query lock contention and allocation churn.
Summary
Vadim Iatsenko of the Polar Proxy team has published a detailed technical breakdown of a redesigned reader connection pool for PostgreSQL proxying that addresses performance bottlenecks in upstream ProxySQL. The new architecture centralizes ownership of reusable reader connections in a core pool with exact-match lookup on four attributes: startup profile generation, user/database hash, client startup identity, and startup options. This eliminates the generic pool's problem of either risking incorrect connection reuse or creating connections unnecessarily when it cannot distinguish PolarDB-specific state like LSN tracking and transaction metadata.
The redesign also introduces two micro-optimizations to cut per-query overhead: worker-local connection caching that avoids shared pool access for repeated reads to the same server within an event-loop pass, and power-of-two-choices load balancing that avoids scanning all replicas. Additional changes remove unrelated lock contention by decoupling query-digest statistics and logger flush checks from the hot path, and eliminate per-packet memory copying for large result sets.
Architecture and hot-path optimizations
- Exact-match connection key: The pool indexes connections by a four-part key combining startup profile generation, user/database hash, client startup identity (endpoint with TLS fields reserved), and startup options; mismatches on any field force new connection creation rather than risking incorrect reuse.
- Single ownership model: The core pool is the sole owner of all reusable reader connections; worker thread caches hold only reuse handles pointing into the pool's USED list, eliminating duplicate accounting and state synchronization between separate pool structures.
- Per-server mutex with integrated index: Connection list, exact-match index, and counters update together under one per-server mutex, replacing ProxySQL's process-wide hostgroup-manager lock that caused cross-thread contention on unrelated operations.
- Worker-local reuse cache: After selecting a server, a worker retains the matching connection in thread-local storage for the remainder of the event-loop pass, allowing subsequent reads to bypass shared pool lookup entirely.
- Eliminated auxiliary hot-path locks: Query-digest statistics updates and logger flush checks are removed from the per-query path, stopping workers from contending on a single process-wide lock for monitoring counters and no-op logging checks.
Why it matters
For database operators running PostgreSQL at scale with read replicas, this design demonstrates how to replace generic proxy pooling with state-aware exact matching without sacrificing throughput. The specific techniques—worker-local caching, power-of-two-choices replica selection, and isolating monitoring locks from the request path—are transferable to other connection pool implementations in the PostgreSQL ecosystem and beyond. As PolarDB's proxy components are open-source, these improvements may feed back into ProxySQL or inspire similar optimizations in competing proxies like PgBouncer.
Original reference: postgr.es →