pgraft - PostgreSQL Raft Consensus Extension¶
[](https://postgresql.org/) [](https://golang.org/) []() **Distributed consensus for PostgreSQL using the Raft algorithm** [Get Started](getting-started/installation.md){ .md-button .md-button--primary } [View on GitHub](https://github.com/pgelephant/pgraft){ .md-button }
Overview¶
pgraft is a PostgreSQL extension that implements the Raft consensus algorithm for distributed PostgreSQL clusters. It provides automatic leader election, log replication, and 100% split-brain protection.
Key Features¶
Production Ready
- 0 compilation errors/warnings
- PostgreSQL C coding standards compliant
- Persistent storage survives crashes
- Worker-driven architecture
Core Features¶
- Raft Consensus: Based on etcd-io/raft implementation
- Leader Election: Automatic with quorum-based voting
- Log Replication: Consistent state across all nodes
- Split-Brain Protection: 100% guaranteed via Raft quorum
- Leader-Only Node Addition: Configuration changes only on leader, automatically replicated
- Worker-Driven Architecture: PostgreSQL background worker actively drives Raft ticks
- Persistent Storage: HardState, log entries, and snapshots survive crashes
Quick Example¶
-- Create extension
CREATE EXTENSION pgraft;
-- Initialize node
SELECT pgraft_init();
-- Check if current node is leader
SELECT pgraft_is_leader();
-- If leader, add other nodes
SELECT pgraft_add_node(2, '127.0.0.1', 7002);
SELECT pgraft_add_node(3, '127.0.0.1', 7003);
-- Get cluster status
SELECT * FROM pgraft_get_cluster_status();
Architecture at a Glance¶
PostgreSQL Background Worker (C)
↓ Every 100ms
pgraft_go_tick() [C→Go]
↓
raftNode.Tick() [etcd-io/raft]
↓
Ready() messages
↓
raftProcessingLoop() [Goroutine]
↓
Persist → Send → Apply → Advance
Why pgraft?¶
Split-Brain Protection¶
pgraft provides 100% split-brain protection through:
- Quorum Requirement: Leader needs majority votes (N/2 + 1)
- Term Monotonicity: Higher term always wins
- Log Completeness: Only up-to-date nodes can be elected
- Single Leader Per Term: Mathematical guarantee from Raft algorithm
Automatic Replication¶
When you add a node to the leader, it automatically appears on ALL other nodes. You only need to run ONE command on the leader. The Raft consensus protocol handles everything else.
Production Quality¶
- 0 compilation errors
- 0 compilation warnings
- PostgreSQL C coding standards compliant
- All variables at function start (C89/C90)
- C-style comments only
- Tab indentation
- Production-ready error handling
Performance¶
- Tick Interval: 100ms (worker-driven)
- Election Timeout: 1000ms (default, configurable)
- Heartbeat: 100ms (default, configurable)
- Memory: ~50MB per node
- CPU: <1% idle, <5% during elections
Next Steps¶
-
Installation
Get pgraft installed and running
-
Quick Start
Set up your first cluster in minutes
-
Tutorial
Complete setup and usage guide
-
Architecture
Understand how pgraft works
License¶
MIT License - see LICENSE file for details.