PG Proxy: Datadog Open Sources Its PostgreSQL Proxy

PG Proxy open source PostgreSQL proxy from Datadog

Datadog has open sourced PG Proxy, a production-ready PostgreSQL proxy written in Go. Inside Datadog it fronts thousands of PostgreSQL hosts spread over hundreds of logical clusters, carrying about 4 trillion queries a month. PG Proxy folds connection pooling, routing and traffic management into one layer, with hooks for authentication, configuration and telemetry.

Teams can run the supplied application or embed the Go library in their own platform. It works with self-managed and cloud-hosted PostgreSQL, and capacity grows by adding instances or giving each instance more resources.

The tangle it replaces

Many Datadog products use PostgreSQL for transactional work. Applications used to reach their databases through a mix of paths. Some connected directly. Others passed through PgBouncer (pooling), HAProxy (balancing and shifting traffic) or Envoy (discovery and health checks). Each component had its own configuration, metrics and scaling behavior.

That made database changes hard to coordinate. Moving from cloud-managed PostgreSQL to Kubernetes meant DNS updates, proxy changes and application redeployments, often while waiting for teams to switch endpoints. Authentication relied on shared static credentials, so access couldn’t easily be tied to individual services.

Eddie Cai
Image: Datadog

Pooling and scaling

PG Proxy uses transaction pooling. Long-lived client connections share a smaller pool of backend connections, and each transaction holds its backend connection until it finishes. The proxy also tracks each client’s named prepared statements and prepares them on whichever backend connection a client gets. Datadog built that when PgBouncer didn’t support the combination. PgBouncer caught up with release 1.21.0 in October 2023.

PgBouncer is single-threaded, so using more cores means running more processes. A PG Proxy instance can use multiple cores. Under CPU or memory pressure, Datadog’s deployment uses load-shedding controls that reject some new connections to protect established ones. Those controls and Datadog’s autoscaling are internal and not in the open source release, though Datadog plans to add support in a future release.

Jonathan Hinds
Image: Datadog

Read routing and migrations

With dynamic routing, eligible reads go to replicas while writes stay on the primary. Each client turns it on with the startup option pg_proxy.dynamic_routing=true, and operators can weight read clusters. The proxy inspects the first statement of every transaction. A plain SELECT, SHOW or BEGIN READ ONLY sends the whole transaction to a replica, while locking reads, SELECT INTO, EXPLAIN and most function calls remain on the primary. If a replica can’t supply a connection quickly, the proxy falls back to the primary.

Because replicas can lag, a fresh write may not show up there yet, so apps that need to read their own writes should skip dynamic routing.

The name and address an application connects to are decoupled from the backend actually serving it. During a migration, operators can change the destination centrally. Datadog sends a small share of reads to a new cluster, checks its behavior and raises the share before the write cutover. Its Temporal-based workflows block new logins, let running transactions finish and then change routing. Data copying stays outside the proxy, and switching the main destination can interrupt transactions that haven’t drained.

Applications reach PostgreSQL through different combinations of PgBouncer, HAProxy, and Envoy, each with a distinct role.
Image: Datadog

Identity and tracing

Datadog authenticates services to PG Proxy with JSON Web Tokens, and the proxy checks which databases each service may use. That JWT system stays internal. The public library offers authentication hooks, so an integration can verify a caller and decide, for example, that an orders-api service may reach an orders database.

The proxy records spans for protocol handling, query execution and result delivery. With supported trace context from a client, they join the application trace, which helps separate time waiting on the backend from time returning data. Users can send telemetry to Datadog or OpenTelemetry providers. Optional bind-parameter capture is off by default, although query text and error messages may still hold values.

Getting started

Teams can run PG Proxy in place of PgBouncer using a quick start guide, or build on the Go library for custom destination selection, discovery and health checks. Datadog says it is also looking at database safeguards, tighter observability links and query tuning, and that Database Monitoring integration may reach a future open source release.

Source: Datadog

Written by

Saksham Bhargava

Saksham Bhargava runs Alteroid and has been writing about consumer technology for over five years, covering iPhone, Android, Windows, AI tools and gaming. With an MCA and six years of hands-on web development experience, he tests the features he writes about on his own devices and focuses on clear, practical guides that solve real problems. Away from the screen, he enjoys travelling to places most tourists skip.