Skip to content
seraphicoPublic

About

PostgreSQL high availability with automatic failover, embedded Raft consensus, and a built-in TCP proxy. Written in Rust; no external etcd or HAProxy required.

Topics

Resources

Stars

4 stars

Watchers

0 watching

Forks

Repository files navigation

pg-ha

中文

PostgreSQL High Availability with built-in Raft consensus and load balancing.

Overview

pg-ha is a Rust-based PostgreSQL HA solution aimed at the same job as Patroni: automatic failover and synchronous replication, without etcd, ZooKeeper, Consul, or HAProxy. It does not implement every Patroni switch. Open gaps are listed in the TODO.

pg-ha overview: three nodes, each with PostgreSQL and one pg-ha process; clients connect to any proxy; the cluster elects a new primary when the primary stops

  • No etcd/ZooKeeper/Consul — embedded Raft consensus via openraft
  • No HAProxy — built-in TCP proxy with active health checking
  • Single binary deployment — one process per node, zero external services required
  • TLS encrypted — Raft RPC (2380) is always mutual TLS: raft.tls (cert + key + ca_cert) is required, and only certificates whose SAN matches a cluster member host are accepted (plaintext needs an explicit allow_insecure_raft: true, development only). The REST API is optional: set restapi.tls cert + key for HTTPS and add ca_cert + require_client_cert: true for mutual TLS; omitting it leaves REST as plaintext (default). Basic Auth credentials on the REST API are only protected once REST TLS is enabled.
  • Graceful shutdown — drain (renewing the leader lock), CHECKPOINT + stop bounded by the leader lease, lock release only after PostgreSQL stopped

Documentation

Document 中文 English
Architecture 架构 Architecture
Deployment 部署指南 Deployment Guide
Operations 运维指南 Operations Guide
Invariants 不变量 Invariants
TODO 待办 TODO

Architecture

Each node runs a single pg-ha agent process containing:

  1. HA Engine — periodic decision loop managing failover and promotion
  2. Raft DCS — embedded consensus cluster for leader election and state storage
  3. REST API — health checks (compatible with HAProxy) and management endpoints
  4. TCP Proxy — read/write splitting across PostgreSQL backends

Quick Start

# Clone and start a 3-node dev cluster (needs Docker; builds the Linux binaries first, PostgreSQL 18 by default)
git clone https://github.com/seraphico/pg-ha.git && cd pg-ha
make up

# Verify cluster
curl -s http://localhost:8008/cluster | jq .

# Connect via proxy (RW port); the dev password is `secret`
psql -h localhost -p 6432 -U postgres

# Stop cluster
make down

What it does on a real 3-node dev cluster (recorded output, PostgreSQL 18):

pg-ha terminal demo: cluster, read/write split, switchover, quorum replication, kill -9 on the primary, automatic failover with no acknowledged write lost

Workspace Crates

Crate Purpose
pg-ha Main binary (composes all subsystems)
pg-ha-core HA logic, PG lifecycle, cluster types, configuration
pg-ha-dcs Raft state machine, network transport, DCS trait impl
pg-ha-proxy TCP proxy for PostgreSQL connections with health checking
pg-ha-api axum REST API for health checks and management
pg-ha-ctl CLI tool for cluster administration

License

Apache-2.0

About

PostgreSQL high availability with automatic failover, embedded Raft consensus, and a built-in TCP proxy. Written in Rust; no external etcd or HAProxy required.

Topics

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages