Skip to content
/ pgdog Public

Modern PostgreSQL pooler with support for load balancing, failover and sharding.

License

Notifications You must be signed in to change notification settings

levkk/pgdog

Repository files navigation

pgDog - Modern PostgreSQL pooler

Documentation CI

pgDog is a PostgreSQL proxy and transaction pooler written in Rust. Spiritual successor to pgcat, pgDog comes with a lot of classic features like basic sharding, load balancing and failover. In addition, pgDog makes improvements to query performance, and adds new features like plugins, cross-shard queries, and async protocol support.

Documentation

📘 pgDog documentation can be found here.

Features summary

Feature Status Summary
Load balancer Operational Spread SELECT queries across multiple replicas automatically, using algorithms like round robin.
Transaction pooling Operational Identical to pgbouncer, allows for thousands of clients to reuse a handful of server connections.
Session pooling Operational Exclusive use of server connections for clients needing session-level features.
Plugins Operational Control how pgDog routes queries and what results it sends to clients, through loading shared libraries at runtime.
Sharding Work in progress Automatically split data and queries between multiple databases, scaling writes horizonally.
Authentication Supports scram-sha-256 and trust Suppport for various PostgreSQL authentication mechanisms, like SCRAM, MD5, and LDAP.
Configuration Operational Configure pgDog without restarting the pooler or breaking connections.

Getting started

Install the latest version of the Rust compiler from rust-lang.org. Once you have Rust installed, clone this repository and build the project in release mode:

cargo build --release

It's important to use the release profile if you're deploying to production or want to run performance benchmarks.

Configuration

pgDog has two configuration files:

  • pgdog.toml which contains general settings and PostgreSQL servers information
  • users.toml for users and passwords

Most options have reasonable defaults, so a basic configuration for a single user and database running on the same machine is pretty short:

pgdog.toml

[general]
host = "0.0.0.0"
port = 6432

[[databases]]
name = "pgdog"
host = "127.0.0.1"

users.toml

[[users]]
name = "pgdog"
password = "pgdog"
database = "pgdog"

If you'd like to try this out, you can set it up like so:

CREATE DATABASE pgdog;
CREATE USER pgdog PASSWORD 'pgdog' LOGIN;

Running pgDog

Running pgDog can be done with Cargo:

cargo run --release

You can connect to pgDog with psql or any other PostgreSQL client:

psql postgres://pgdog:pgdog@127.0.0.1:6432/pgdog

Features

Load balancer

pgDog is an application layer (OSI Level 7) load balancer for PostgreSQL. It can proxy multiple replicas (and primary) and distribute transactions. It comes with support for multiple strategies, including round robin and random. Additionally, it can parse queries and send SELECT queries to replicas and all others to the primary. This allows to proxy all databases behind a single pgDog deployment.

📘 Load balancer

Healthchecks and failover

pgDog maintains a real time list of healthy hosts in its database configuration. When a host fails a healthcheck, it's removed from active rotation and queries are rerouted to other replicas. This is analogous to modern HTTP load balancing, except it's at the database layer.

Failover maximizes database availability and protects against intermittent issues like spotty network connectivity and temporary downtime.

📘 Healthchecks

Transaction pooling

Like pgbouncer, pgDog supports transaction-level connection pooling, allowing 1000s (even 100,000s) of clients to reuse just a few PostgreSQL server connections.

📘 Transactions

Plugins

pgDog comes with its own plugin system that loads them at runtime using a shared library interface. If a plugin can expose a predefined C API, it can be written in any language, including C/C++, Rust, Zig, Go, Python, Ruby, Java, and many more.

Plugins can be used to route queries to specific databases in a sharded configuration, or to split traffic between writes and reads in a mixed (primary & replicas) deployment. The plugin interface allows code execution at multiple stages of the request/response lifecycle, and can go as far as block or intercept queries and return custom results to the client.

Examples of plugins can be found in examples and plugins.

📘 Plugins

Sharding

This feature is a work in progress.

pgDog is able to handle databases with multiple shards by routing queries automatically to one or more shards. The pgdog-routing plugin parses queries, extracts tables and columns information, and calculates which shard(s) the query should go to based on the parameters. Not all operations are supported, but a lot of common use cases are working.

📘 Sharding

Local testing

The configuration files for a sharded database are provided in the repository. To make it work locally, create the required databases:

CREATE DATABASE shard_0;
CREATE DATABASE shard_1;

GRANT CONNECT ON DATABASE shard_0 TO pgdog;
GRANT CONNECT ON DATABASE shard_1 TO pgdog;

You can launch pgDog with the sharded configuration using the files provided in the repository:

cargo run -- --config pgdog-sharded.toml --users users-sharded.toml

Configuration

pgDog is highly configurable and many aspects of its operation can be tweaked at runtime, without having to restart the process and break PostgreSQL connections. If you've used pgbouncer (or pgcat) before, the options will be familiar. If not, options are documented with examples.

📘 Configuration

🚦 Status 🚦

While a lot of "classic" features of pgDog, like load balancing and healthchecks, have been well tested in production and at scale, the current codebase has not. This project is just getting started and early adopters are welcome to try pgDog internally.

Status on features stability will be updated regularly.

Performance

pgDog does its best to minimize its impact on overall database performance. Using Rust and Tokio is a great start for a fast network proxy, but additional care is also taken to perform as few operations as possible while moving data between client and server sockets. Some benchmarks are provided to help set a baseline.

📘 Architecture & benchmarks

License

pgDog is free and open source software, licensed under the AGPL v3. While often misunderstood, this license is very permissive and allows the following without any additional requirements from you or your organization:

  • Internal use
  • Private modifications for internal use without sharing any source code

You can freely use pgDog to power your PostgreSQL databases without having to share any source code, including proprietary work product or any pgDog modifications you make.

AGPL was written specifically for organizations that offer pgDog as a public service (e.g. database cloud providers) and require those organizations to share any modifications they make to pgDog, including new features and bug fixes.

Contributions

Contributions are welcome. If you see a bug, feel free to submit a PR with a fix or an issue to discuss. For any features, please open an issue to discuss first.

The code has tests, make sure they pass first with:

cargo nextest run && \
cargo fmt --check --all && \
cargo clippy

cargo-nextest is better because it runs tests in parallel and can help surface concurrency bugs.

About

Modern PostgreSQL pooler with support for load balancing, failover and sharding.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages