RC RANDOM CHAOS

PgDog: A Rust Postgres pooler that stops connection pooling from breaking your app

· via Hacker News

Original source

Why we built yet another Postgres connection pooler

Hacker News →

Connection poolers like PgBouncer, RDS Proxy, and Pgpool-II let many clients share a database without blowing past its connection limit, but they impose a hidden tax: they change how you’re allowed to use Postgres. Because poolers recycle server connections across clients, per-session state leaks between them, so the standard advice is to stop using SET commands, LISTEN/NOTIFY, and other real Postgres features — often meaning rewrites of thousands of lines of lightly-tested production code. PgDog, a Rust proxy built on the Tokio async runtime, argues those trade-offs are unnecessary and sets out to make the pooler transparent.

Its core trick is a built-in SQL parser that tracks per-client session state. When a client issues a query, PgDog compares its expected state against the server connection and replays any missing SET statements — pipelining several at once to keep the round-trip cost low — so features like statement_timeout overrides and session-variable-based Row Level Security keep working under transaction pooling. It extends the same idea to LISTEN/NOTIFY, brokering pub/sub messages across multiple PgDog processes using Tokio broadcast channels internally and a dedicated Postgres connection to fan messages between containers, while preserving NOTIFY’s transactional semantics. Multithreading is the other differentiator: rather than sharding pools per single-threaded process (the PgBouncer SO_REUSEPORT / RDS Proxy model, where one overloaded process stalls its clients), PgDog spreads clients across CPUs, improving connection utilization, absorbing query bursts without waiting on autoscaling, and collapsing the operational surface to one set of metrics and health checks.

The pitch is that you shouldn’t have to abandon native database features to scale. PgDog is open source and free to self-host anywhere, and the team says it has run in production for over a year, pooling connections at roughly 2 million queries per second.

Read the full article

Continue reading at Hacker News →

This is an AI-generated summary. Read the original for the full story.