PGRaft is a PostgreSQL extension that provides Raft consensus algorithm integration for high availability and automatic failover.
PGRaft extends PostgreSQL with:
- Raft Consensus: Distributed consensus algorithm for leader election
- Shared Memory Integration: Persistent state across PostgreSQL processes
- Automatic Failover: Seamless primary/replica transitions
- Network Communication: Inter-node communication for consensus
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ PostgreSQL │ │ PostgreSQL │ │ PostgreSQL │
│ (Primary) │ │ (Replica) │ │ (Replica) │
│ │ │ │ │ │
│ ┌───────────┐ │ │ ┌───────────┐ │ │ ┌───────────┐ │
│ │ PGRaft │ │◄──►│ │ PGRaft │ │◄──►│ │ PGRaft │ │
│ │ Extension │ │ │ │ Extension │ │ │ │ Extension │ │
│ └───────────┘ │ │ └───────────┘ │ │ └───────────┘ │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │
└───────────────────────┼───────────────────────┘
│
┌─────────────────┐
│ Raft Network │
│ (Consensus) │
└─────────────────┘
- PostgreSQL 17 or later
- Go 1.21 or later
- Build tools (make, gcc)
# Navigate to pgraft directory
cd pgraft
# Build the extension
make
# Install the extension
make install
# Install Go dependencies
go mod tidy
go mod downloadAdd PGRaft to your PostgreSQL configuration:
# Edit postgresql.conf
echo "shared_preload_libraries = 'pgraft'" >> /usr/local/var/postgresql@17/postgresql.conf
# Restart PostgreSQL
brew services restart postgresql@17PGRaft uses the configuration file conf/pgraft.conf:
# Raft Configuration
raft_node_id = 1
raft_cluster_id = ram_cluster
raft_data_dir = /tmp/pgraft_data
raft_log_level = info
# Network Configuration
raft_port = 7400
raft_bind_address = 0.0.0.0
raft_peer_addresses = localhost:7401,localhost:7402
# PostgreSQL Integration
postgresql_integration_enabled = true
postgresql_shmem_key = 12345
postgresql_shmem_size = 1048576PGRaft provides several SQL functions for cluster management:
-- Initialize Raft cluster
SELECT pgraft_init();
-- Start Raft consensus
SELECT pgraft_start();
-- Stop Raft consensus
SELECT pgraft_stop();
-- Get cluster status
SELECT pgraft_status();
-- Check if node is leader
SELECT pgraft_is_leader();-- Add node to cluster
SELECT pgraft_add_node('localhost:7401', 2);
-- Remove node from cluster
SELECT pgraft_remove_node(2);
-- Get node information
SELECT pgraft_get_nodes();
-- Get leader information
SELECT pgraft_get_leader();-- Check cluster health
SELECT pgraft_is_healthy();
-- Get cluster metrics
SELECT pgraft_get_metrics();
-- Enable/disable debug logging
SELECT pgraft_set_debug(true);-- 1. Initialize the cluster
SELECT pgraft_init();
-- 2. Start Raft consensus
SELECT pgraft_start();
-- 3. Add replica nodes
SELECT pgraft_add_node('localhost:7401', 2);
SELECT pgraft_add_node('localhost:7402', 3);
-- 4. Check cluster status
SELECT pgraft_status();-- Check if cluster is healthy
SELECT pgraft_is_healthy();
-- Get detailed cluster information
SELECT * FROM pgraft_status();
-- Monitor leader elections
SELECT pgraft_get_leader();-- Check current leader
SELECT pgraft_get_leader();
-- Force leader election (if needed)
SELECT pgraft_force_election();
-- Check if this node is leader
SELECT pgraft_is_leader();PGRaft integrates seamlessly with the RAMD daemon:
# Start RAMD daemon
./ramd --config=conf/ramd.conf
# RAMD will automatically:
# 1. Connect to PGRaft via shared memory
# 2. Monitor cluster health
# 3. Handle failover operations
# 4. Provide HTTP API for cluster management-
Extension not loading
-- Check if extension is loaded SELECT * FROM pg_extension WHERE extname = 'pgraft'; -- Check shared_preload_libraries SHOW shared_preload_libraries;
-
Raft consensus not starting
-- Check Raft status SELECT pgraft_status(); -- Check logs SELECT pgraft_get_logs();
-
Network connectivity issues
# Test network connectivity telnet localhost 7400 telnet localhost 7401 telnet localhost 7402
Enable debug logging for troubleshooting:
-- Enable debug logging
SELECT pgraft_set_debug(true);
-- Check debug logs
SELECT pgraft_get_logs();
-- Disable debug logging
SELECT pgraft_set_debug(false);- Shared memory: 1MB default (configurable)
- Raft log: Grows with cluster operations
- Network buffers: 256KB per connection
- Low latency network recommended
- Bandwidth: 1Mbps per node minimum
- Ports: 7400-7500 range (configurable)
- Minimal impact on PostgreSQL performance
- Shared memory access is atomic
- Network I/O is asynchronous
- Bind to specific interfaces only
- Use firewall rules to restrict access
- Consider VPN for remote clusters
- No built-in authentication (relies on network security)
- Use PostgreSQL authentication for SQL functions
- Consider SSL/TLS for network communication
- Raft consensus metrics
- Network communication stats
- Leader election counts
- Health check results
# prometheus.yml
scrape_configs:
- job_name: 'pgraft'
static_configs:
- targets: ['localhost:9091']- Cluster Size: Use odd numbers (3, 5, 7) for better consensus
- Network: Ensure low latency between nodes
- Monitoring: Set up comprehensive monitoring
- Backups: Regular backups of Raft data
- Testing: Test failover scenarios regularly
For complete API documentation, see PGRaft API Reference.
Next Steps: Learn about RAMD Daemon for complete cluster management!