DocumentationpgBalancer Documentation

AI-Powered Load Balancing

Configure Load Balancing

pgBalancer supports multiple load balancing modes with AI-powered routing:

pgbalancer.conf Load Balancing Settings

# Enable load balancing for SELECT queries
load_balance_mode = auto
load_balance_weight_primary = 1.0
load_balance_weight_replicas = 1.5

# AI routing mode: predictive, adaptive, or classic
ai_routing_strategy = adaptive

# Response time prediction learning rate
ai_learning_rate = 0.08
ai_decay_factor = 0.92

# Promote replicas when healthy
auto_promote_replicas = on
replica_health_threshold = 0.85

Pick a Routing Strategy

Predictive

Uses historical query latencies to forecast the best backend.

Adaptive

Real-time feedback loop adjusts weights every 5 seconds.

Classic

Traditional round-robin / least-connections policy.

Query Classification

Tag workloads with routing hints

SELECT pgbalancer_set_query_class('analytics');
SELECT * FROM large_fact_table WHERE ...;

SELECT pgbalancer_set_query_class('oltp');
UPDATE orders SET status = 'shipped' WHERE id = 42;

Weighted Read Distribution

Assign replica weights

# Heavier weight -> receives more traffic
backend_weight0 = 1.0  # primary
backend_weight1 = 1.4  # replica 1
backend_weight2 = 1.2  # replica 2

Promote healthy replicas

# Replica health threshold for promotion
replica_health_threshold = 0.85
replica_sync_window = 5

Monitor Distribution

AI routing metrics

SELECT *
FROM pgbalancer_ai_metrics
ORDER BY sample_time DESC
LIMIT 20;

Alerting

Prometheus alert example

ALERT PgbalancerSkew
  IF max_over_time(pgbalancer_backend_qps{backend="replica1"}[5m])
     > 2 * max_over_time(pgbalancer_backend_qps{backend="replica2"}[5m])
  FOR 10m
  LABELS { severity = "warning" }
  ANNOTATIONS {
    summary = "pgBalancer traffic skew detected"
  }