-
Notifications
You must be signed in to change notification settings - Fork 0
1974 lines (1541 loc) · 62.5 KB
/
docs.yml
File metadata and controls
1974 lines (1541 loc) · 62.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
name: Deploy Documentation
on:
workflow_dispatch:
permissions:
contents: write
pages: write
id-token: write
# Allow only one concurrent deployment
concurrency:
group: "pages"
cancel-in-progress: false
jobs:
authorize:
name: Check Authorization
runs-on: ubuntu-latest
steps:
- name: Verify user is pgelephant2025
run: |
if [ "${{ github.actor }}" != "pgelephant2025" ]; then
echo "❌ Unauthorized: Only pgelephant2025 can run this workflow"
echo "Current user: ${{ github.actor }}"
exit 1
fi
echo "✅ Authorized: ${{ github.actor }}"
build:
name: Build MkDocs Documentation
runs-on: ubuntu-latest
needs: authorize
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Cache dependencies
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-mkdocs
restore-keys: |
${{ runner.os }}-pip-
- name: Install MkDocs and dependencies
run: |
pip install --upgrade pip
pip install mkdocs mkdocs-material mkdocs-minify-plugin pymdown-extensions
- name: Create documentation structure
run: |
mkdir -p docs
# Create mkdocs.yml
cat > mkdocs.yml << 'EOFCONFIG'
site_name: pgbalancer Documentation
site_description: Modern PostgreSQL Connection Pooler with REST API
site_author: pgElephant, Inc.
repo_url: https://github.com/pgelephant/pgbalancer
repo_name: pgelephant/pgbalancer
theme:
name: material
palette:
- scheme: default
primary: indigo
accent: cyan
toggle:
icon: material/brightness-7
name: Switch to dark mode
- scheme: slate
primary: indigo
accent: cyan
toggle:
icon: material/brightness-4
name: Switch to light mode
features:
- navigation.instant
- navigation.tracking
- navigation.tabs
- navigation.tabs.sticky
- navigation.sections
- navigation.expand
- navigation.top
- search.suggest
- search.highlight
- content.code.copy
icon:
repo: fontawesome/brands/github
markdown_extensions:
- admonition
- pymdownx.details
- pymdownx.superfences
- pymdownx.highlight:
anchor_linenums: true
- pymdownx.inlinehilite
- pymdownx.snippets
- pymdownx.tabbed:
alternate_style: true
- tables
- attr_list
- md_in_html
- def_list
nav:
- Home: index.md
- Getting Started:
- Installation: installation.md
- Quick Start: quickstart.md
- Configuration: configuration.md
- Core Features:
- AI Load Balancing: ai-load-balancing.md
- REST API Reference: rest-api.md
- MQTT Event Streaming: mqtt-events.md
- CLI Tool (bctl): bctl.md
- Operations:
- Connection Pooling: connection-pooling.md
- Load Balancing: load-balancing.md
- Failover & Recovery: failover.md
- Health Monitoring: health-monitoring.md
- Performance Tuning: performance.md
- Advanced Topics:
- Watchdog: watchdog.md
- SSL/TLS: ssl.md
- Authentication: authentication.md
- Monitoring Integration: monitoring.md
- Reference:
- Configuration Parameters: parameters.md
- REST API Endpoints: api-endpoints.md
- CLI Commands: cli-commands.md
- Error Codes: error-codes.md
- Examples: examples.md
- Appendices:
- Release Notes: release-notes.md
- Migration Guide: migration.md
- FAQ: faq.md
- License: license.md
plugins:
- search
- minify:
minify_html: true
extra:
social:
- icon: fontawesome/brands/github
link: https://github.com/pgelephant/pgbalancer
EOFCONFIG
# Create comprehensive index.md
cat > docs/index.md << 'EOF'
# pgbalancer Documentation
**AI-Based PostgreSQL Load Balancer and Connection Pooler**
pgbalancer is a modern PostgreSQL connection pooler and load balancer built as a fork of pgpool-II with significant enhancements for intelligent query routing, HTTP-based management, and real-time event streaming.
## Overview
pgbalancer provides enterprise-grade connection pooling, load balancing, and high availability for PostgreSQL 13-18, enhanced with AI-powered load balancing, comprehensive REST API, MQTT event streaming, and professional CLI tools.
**Key Features:**
- **AI-Powered Load Balancing** — Machine learning algorithms for intelligent query routing
- **REST API Management** — 17 HTTP/JSON endpoints for complete cluster control
- **MQTT Event Streaming** — Real-time publishing of cluster events
- **Professional CLI Tool (bctl)** — Unified interface replacing 10+ pcp_* commands
- **Standard .conf Configuration** — pgpool-II compatible configuration format
- **JWT Authentication** — Secure API access with HMAC-SHA256 tokens
- **PostgreSQL 13-18** — Full support for latest versions
- **High Availability** — Automatic failover with watchdog support
- **Connection Pooling** — Efficient connection reuse and management
- **Health Monitoring** — Continuous backend monitoring and health checks
## Quick Install
```bash
# Prerequisites: PostgreSQL 13+ with development headers, autoconf, automake, libtool, make, gcc/clang
# Clone and configure
git clone https://github.com/pgelephant/pgbalancer.git
cd pgbalancer
# Generate configure script if needed
autoreconf -fi
# Configure with options
./configure --with-openssl --with-pam --with-ldap
# Build
make
# Install
sudo make install
# Configure
sudo cp /etc/pgbalancer/pgbalancer.conf.sample /etc/pgbalancer/pgbalancer.conf
sudo vi /etc/pgbalancer/pgbalancer.conf
# Start
pgbalancer -f /etc/pgbalancer/pgbalancer.conf -D
# Check status
bctl status
```
## Why pgbalancer?
- **Modern API** — HTTP/JSON REST API instead of binary PCP protocol
- **Unified CLI** — Single bctl tool instead of 10+ separate pcp_* commands
- **AI Load Balancing** — Machine learning algorithms for optimal query routing
- **MQTT Integration** — Real-time event streaming for monitoring and automation
- **Better Monitoring** — Prometheus metrics, Grafana dashboards, comprehensive health checks
- **Production Ready** — Based on battle-tested pgpool-II with modern enhancements
- **Standard Configuration** — Uses proven .conf format compatible with pgpool-II
## Documentation Sections
### Getting Started
- [Installation Guide](installation.md) - Complete setup instructions
- [Quick Start Guide](quickstart.md) - Get running in 5 minutes
- [Configuration Guide](configuration.md) - All parameters explained
### Core Features
- [AI Load Balancing](ai-load-balancing.md) - Machine learning algorithms
- [REST API Reference](rest-api.md) - Complete HTTP API documentation
- [MQTT Event Streaming](mqtt-events.md) - Real-time event publishing
- [CLI Tool (bctl)](bctl.md) - Professional command-line interface
### Operations
- [Connection Pooling](connection-pooling.md) - Efficient connection management
- [Load Balancing](load-balancing.md) - Query distribution strategies
- [Failover & Recovery](failover.md) - High availability features
- [Health Monitoring](health-monitoring.md) - Backend monitoring
- [Performance Tuning](performance.md) - Optimization guidelines
### Advanced Topics
- [Watchdog](watchdog.md) - High availability coordination
- [SSL/TLS](ssl.md) - Secure connections
- [Authentication](authentication.md) - PAM, LDAP, JWT
- [Monitoring Integration](monitoring.md) - Prometheus & Grafana
### Reference
- [Configuration Parameters](parameters.md) - Complete parameter reference
- [REST API Endpoints](api-endpoints.md) - All 17 HTTP endpoints
- [CLI Commands](cli-commands.md) - bctl command reference
- [Error Codes](error-codes.md) - Troubleshooting guide
- [Examples](examples.md) - Configuration examples
### Appendices
- [Release Notes](release-notes.md) - Version history
- [Migration Guide](migration.md) - From pgpool-II
- [FAQ](faq.md) - Frequently asked questions
- [License](license.md) - PostgreSQL License
## License
PostgreSQL License - see [License](license.md) for details.
Copyright (c) 2003-2021 PgPool Global Development Group
Copyright (c) 2024-2025, pgElephant, Inc.
EOF
# Create comprehensive installation guide
cat > docs/installation.md << 'EOF'
# Installation Guide
Complete installation instructions for pgbalancer on various platforms.
## Prerequisites
### System Requirements
- **Operating System**: Linux (Ubuntu 20.04+, CentOS 8+, Rocky Linux 8+), macOS 10.15+
- **PostgreSQL**: Version 13, 14, 15, 16, 17, or 18
- **Memory**: Minimum 512MB RAM, recommended 2GB+
- **Disk Space**: 100MB for installation, additional space for logs
### Build Dependencies
**Ubuntu/Debian:**
```bash
sudo apt-get update
sudo apt-get install -y \
build-essential \
autoconf \
automake \
libtool \
pkg-config \
libpq-dev \
libssl-dev \
libpam0g-dev \
libldap2-dev \
libmemcached-dev \
git
```
**CentOS/RHEL/Rocky Linux:**
```bash
sudo dnf groupinstall -y "Development Tools"
sudo dnf install -y \
autoconf \
automake \
libtool \
pkgconfig \
postgresql-devel \
openssl-devel \
pam-devel \
openldap-devel \
libmemcached-devel \
git
```
**macOS (Homebrew):**
```bash
brew install autoconf automake libtool pkg-config
brew install postgresql openssl pam openldap libmemcached
```
## Installation Methods
### Method 1: Build from Source (Recommended)
**Step 1: Clone Repository**
```bash
git clone https://github.com/pgelephant/pgbalancer.git
cd pgbalancer
```
**Step 2: Generate Configure Script**
```bash
# Generate configure script if it doesn't exist
autoreconf -fi
```
**Step 3: Configure Build**
```bash
# Basic configuration
./configure
# With all optional features
./configure \
--with-openssl \
--with-pam \
--with-ldap \
--with-memcached \
--prefix=/usr/local/pgbalancer
```
**Step 4: Build**
```bash
make
```
**Step 5: Install**
```bash
sudo make install
```
**Step 6: Verify Installation**
```bash
pgbalancer --version
bctl --version
```
### Method 2: Package Installation
**RPM Packages (CentOS/RHEL/Rocky):**
```bash
# Download appropriate RPM for your PostgreSQL version
wget https://github.com/pgelephant/pgbalancer/releases/download/v1.0.0/pgbalancer-1.0.0-1.el9.x86_64.rpm
sudo dnf install pgbalancer-1.0.0-1.el9.x86_64.rpm
```
**DEB Packages (Ubuntu/Debian):**
```bash
# Download appropriate DEB for your PostgreSQL version
wget https://github.com/pgelephant/pgbalancer/releases/download/v1.0.0/postgresql-17-pgbalancer_1.0.0-1_amd64.deb
sudo apt install ./postgresql-17-pgbalancer_1.0.0-1_amd64.deb
```
## PostgreSQL Configuration
**Step 1: Configure PostgreSQL**
Edit `postgresql.conf`:
```ini
# Enable connection pooling
max_connections = 200
# Enable streaming replication (for HA setups)
wal_level = replica
max_wal_senders = 10
hot_standby = on
# Enable SSL (optional)
ssl = on
ssl_cert_file = 'server.crt'
ssl_key_file = 'server.key'
```
**Step 2: Configure pg_hba.conf**
```ini
# Allow pgbalancer connections
host all all 127.0.0.1/32 md5
host all all 10.0.0.0/8 md5
# Allow replication (for HA setups)
host replication all 127.0.0.1/32 md5
host replication all 10.0.0.0/8 md5
```
**Step 3: Restart PostgreSQL**
```bash
# Linux (systemd)
sudo systemctl restart postgresql
# macOS
pg_ctl restart -D /usr/local/var/postgres
```
## pgbalancer Configuration
**Step 1: Create Configuration Directory**
```bash
sudo mkdir -p /etc/pgbalancer
sudo chown postgres:postgres /etc/pgbalancer
```
**Step 2: Create Configuration File**
```bash
sudo cp /usr/local/share/doc/pgbalancer/pgbalancer.conf.sample /etc/pgbalancer/pgbalancer.conf
sudo chown postgres:postgres /etc/pgbalancer/pgbalancer.conf
```
**Step 3: Basic Configuration**
Edit `/etc/pgbalancer/pgbalancer.conf`:
```conf
# Basic connection settings
listen_addresses = '*'
port = 5432
socket_dir = '/tmp'
# Backend PostgreSQL servers
backend_hostname0 = 'localhost'
backend_port0 = 5433
backend_weight0 = 1
backend_data_directory0 = '/var/lib/postgresql/data'
backend_flag0 = 'ALLOW_TO_FAILOVER'
# Connection pooling
num_init_children = 32
max_pool = 4
child_life_time = 300
child_max_connections = 0
connection_cache = on
# Load balancing
load_balance_mode = on
# Health checking
health_check_period = 30
health_check_timeout = 20
health_check_user = 'postgres'
health_check_password = 'postgres'
health_check_database = 'postgres'
# AI Load Balancing (NEW)
ai_load_balancing = on
ai_learning_rate = 0.01
ai_exploration_rate = 0.1
ai_health_weight = 0.4
ai_response_time_weight = 0.3
ai_load_weight = 0.3
# REST API Server (NEW)
rest_api_enabled = on
rest_api_port = 8080
rest_api_jwt_secret = 'your-secret-key-here'
rest_api_jwt_expiry = 3600
# MQTT Event Publishing (NEW)
mqtt_enabled = on
mqtt_broker = 'localhost'
mqtt_port = 1883
mqtt_client_id = 'pgbalancer'
mqtt_topic_prefix = 'pgbalancer'
```
## Service Configuration
**Step 1: Create Systemd Service (Linux)**
Create `/etc/systemd/system/pgbalancer.service`:
```ini
[Unit]
Description=pgbalancer - PostgreSQL Connection Pooler
After=postgresql.service
Requires=postgresql.service
[Service]
Type=forking
User=postgres
Group=postgres
ExecStart=/usr/local/bin/pgbalancer -f /etc/pgbalancer/pgbalancer.conf -D
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -TERM $MAINPID
PIDFile=/var/run/pgbalancer/pgbalancer.pid
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
```
**Step 2: Enable and Start Service**
```bash
sudo systemctl daemon-reload
sudo systemctl enable pgbalancer
sudo systemctl start pgbalancer
sudo systemctl status pgbalancer
```
**Step 3: Verify Installation**
```bash
# Check pgbalancer status
bctl status
# Check REST API
curl http://localhost:8080/api/v1/status
# Check MQTT events (if mosquitto is installed)
mosquitto_sub -h localhost -t 'pgbalancer/#' -v
```
## Troubleshooting Installation
**Common Issues:**
1. **Configure script not found**
```bash
autoreconf -fi
```
2. **Missing PostgreSQL headers**
```bash
# Ubuntu/Debian
sudo apt-get install libpq-dev
# CentOS/RHEL
sudo dnf install postgresql-devel
```
3. **Permission denied errors**
```bash
sudo chown -R postgres:postgres /etc/pgbalancer
sudo chmod 600 /etc/pgbalancer/pgbalancer.conf
```
4. **Port already in use**
```bash
# Check what's using port 5432
sudo netstat -tlnp | grep 5432
# Change pgbalancer port in configuration
port = 5433
```
5. **Backend connection failed**
```bash
# Test PostgreSQL connection
psql -h localhost -p 5433 -U postgres
# Check PostgreSQL is running
sudo systemctl status postgresql
```
**Log Files:**
- pgbalancer logs: `/var/log/pgbalancer/`
- System logs: `journalctl -u pgbalancer`
- PostgreSQL logs: `/var/log/postgresql/`
## Next Steps
- [Quick Start Guide](quickstart.md) - Get running in 5 minutes
- [Configuration Guide](configuration.md) - Detailed configuration options
- [REST API Reference](rest-api.md) - HTTP API documentation
- [CLI Tool (bctl)](bctl.md) - Command-line interface
EOF
# Create comprehensive quickstart guide
cat > docs/quickstart.md << 'EOF'
# Quick Start Guide
Get pgbalancer running in 5 minutes with AI load balancing, REST API, and MQTT events.
## Prerequisites
- PostgreSQL 13+ running
- pgbalancer installed (see [Installation Guide](installation.md))
- Basic understanding of PostgreSQL
## Step 1: Basic Configuration
**Create minimal configuration:**
```bash
sudo mkdir -p /etc/pgbalancer
sudo tee /etc/pgbalancer/pgbalancer.conf > /dev/null <<INNEREOF
# Basic settings
listen_addresses = '*'
port = 5432
socket_dir = '/tmp'
# Single backend (adjust hostname/port as needed)
backend_hostname0 = 'localhost'
backend_port0 = 5433
backend_weight0 = 1
backend_data_directory0 = '/var/lib/postgresql/data'
backend_flag0 = 'ALLOW_TO_FAILOVER'
# Connection pooling
num_init_children = 32
max_pool = 4
child_life_time = 300
connection_cache = on
# Load balancing
load_balance_mode = on
# Health checking
health_check_period = 30
health_check_timeout = 20
health_check_user = 'postgres'
health_check_password = 'postgres'
health_check_database = 'postgres'
# AI Load Balancing (NEW)
ai_load_balancing = on
ai_learning_rate = 0.01
ai_exploration_rate = 0.1
ai_health_weight = 0.4
ai_response_time_weight = 0.3
ai_load_weight = 0.3
# REST API Server (NEW)
rest_api_enabled = on
rest_api_port = 8080
rest_api_jwt_secret = 'quickstart-secret-key'
rest_api_jwt_expiry = 3600
# MQTT Event Publishing (NEW)
mqtt_enabled = on
mqtt_broker = 'localhost'
mqtt_port = 1883
mqtt_client_id = 'pgbalancer'
mqtt_topic_prefix = 'pgbalancer'
INNEREOF
```
## Step 2: Start pgbalancer
**Start in foreground (for testing):**
```bash
pgbalancer -f /etc/pgbalancer/pgbalancer.conf -n
```
**Start as daemon:**
```bash
pgbalancer -f /etc/pgbalancer/pgbalancer.conf -D
```
## Step 3: Verify Installation
**Check pgbalancer status:**
```bash
bctl status
```
Expected output:
```
Server: pgbalancer
Status: running
Version: 1.0.0
Uptime: 30 seconds
Connections: 0
Nodes: 1
Processes: 32
```
**Test PostgreSQL connection through pgbalancer:**
```bash
psql -h localhost -p 5432 -U postgres -d postgres
```
## Step 4: Use REST API
**Get server status:**
```bash
curl http://localhost:8080/api/v1/status
```
Expected response:
```json
{
"status": "running",
"uptime": 60,
"connections": 0,
"nodes": 1,
"healthy_nodes": 1,
"ai_enabled": true,
"rest_api_enabled": true,
"mqtt_enabled": true
}
```
**List backend nodes:**
```bash
curl http://localhost:8080/api/v1/nodes
```
**Get AI load balancing statistics:**
```bash
curl http://localhost:8080/api/v1/ai-stats
```
## Step 5: Monitor with MQTT
**Install Mosquitto client (optional):**
```bash
# Ubuntu/Debian
sudo apt-get install mosquitto-clients
# CentOS/RHEL
sudo dnf install mosquitto-clients
# macOS
brew install mosquitto
```
**Subscribe to all events:**
```bash
mosquitto_sub -h localhost -t 'pgbalancer/#' -v
```
**Subscribe to specific events:**
```bash
# Node status changes
mosquitto_sub -h localhost -t 'pgbalancer/nodes/status' -v
# Health check results
mosquitto_sub -h localhost -t 'pgbalancer/health' -v
# AI load balancing decisions
mosquitto_sub -h localhost -t 'pgbalancer/ai/decisions' -v
```
## Step 6: Test AI Load Balancing
**Create test workload:**
```bash
# Connect to PostgreSQL through pgbalancer
psql -h localhost -p 5432 -U postgres -d postgres
```
```sql
-- Create test table
CREATE TABLE test_data (id SERIAL PRIMARY KEY, data TEXT);
-- Insert test data
INSERT INTO test_data (data)
SELECT 'test_data_' || generate_series(1, 1000);
-- Run queries to generate load
SELECT COUNT(*) FROM test_data WHERE id % 2 = 0;
SELECT AVG(id) FROM test_data;
SELECT * FROM test_data WHERE id BETWEEN 100 AND 200;
```
**Monitor AI decisions:**
```bash
# Check AI statistics
curl http://localhost:8080/api/v1/ai-stats | jq '.'
# Watch MQTT events
mosquitto_sub -h localhost -t 'pgbalancer/ai/#' -v
```
## Step 7: Advanced Configuration
**Add multiple backends:**
```conf
# Backend 0 - Primary
backend_hostname0 = 'pg-primary'
backend_port0 = 5432
backend_weight0 = 1
backend_data_directory0 = '/var/lib/postgresql/data'
backend_flag0 = 'ALLOW_TO_FAILOVER'
# Backend 1 - Standby
backend_hostname1 = 'pg-standby'
backend_port1 = 5432
backend_weight1 = 1
backend_data_directory1 = '/var/lib/postgresql/data'
backend_flag1 = 'ALLOW_TO_FAILOVER'
```
**Enable JWT authentication:**
```conf
# REST API with JWT
rest_api_enabled = on
rest_api_port = 8080
rest_api_jwt_secret = 'your-secure-secret-key'
rest_api_jwt_expiry = 3600
rest_api_jwt_enabled = on
```
**Configure MQTT with authentication:**
```conf
# MQTT with authentication
mqtt_enabled = on
mqtt_broker = 'mqtt.example.com'
mqtt_port = 8883
mqtt_username = 'pgbalancer'
mqtt_password = 'secure-password'
mqtt_tls = on
mqtt_client_id = 'pgbalancer-prod'
mqtt_topic_prefix = 'pgbalancer/prod'
```
## Step 8: Production Considerations
**Security:**
- Change default passwords
- Use SSL/TLS for connections
- Enable JWT authentication
- Configure firewall rules
**Performance:**
- Tune `num_init_children` based on load
- Adjust `max_pool` for connection reuse
- Configure appropriate `child_life_time`
- Monitor AI learning parameters
**Monitoring:**
- Set up Prometheus metrics
- Configure Grafana dashboards
- Enable MQTT event monitoring
- Set up alerting rules
**High Availability:**
- Configure watchdog
- Set up multiple pgbalancer instances
- Enable automatic failover
- Configure online recovery
## Next Steps
- [Configuration Guide](configuration.md) - Detailed configuration options
- [AI Load Balancing](ai-load-balancing.md) - Machine learning algorithms
- [REST API Reference](rest-api.md) - Complete HTTP API documentation
- [MQTT Event Streaming](mqtt-events.md) - Real-time event publishing
- [CLI Tool (bctl)](bctl.md) - Professional command-line interface
- [Monitoring Integration](monitoring.md) - Prometheus & Grafana setup
EOF
# Create comprehensive configuration guide
cat > docs/configuration.md << 'EOF'
# Configuration Guide
Complete guide to configuring pgbalancer with all parameters explained in detail.
## Configuration File Format
pgbalancer uses standard `.conf` file format compatible with pgpool-II. The configuration file is typically located at `/etc/pgbalancer/pgbalancer.conf`.
**File Structure:**
```conf
# Comments start with #
# Parameters use key = value format
# Values can be quoted or unquoted
# Boolean values: on, off, true, false, yes, no, 1, 0
# Basic connection settings
listen_addresses = '*'
port = 5432
# Backend servers
backend_hostname0 = 'localhost'
backend_port0 = 5433
```
## Basic Connection Settings
### listen_addresses
**Type:** String
**Default:** 'localhost'
**Description:** IP addresses to listen on
```conf
# Listen on all interfaces
listen_addresses = '*'
# Listen on specific interfaces
listen_addresses = '127.0.0.1,192.168.1.100'
# Listen on localhost only
listen_addresses = 'localhost'
```
### port
**Type:** Integer
**Default:** 5432
**Description:** Port number to listen on
```conf
# Default PostgreSQL port
port = 5432
# Custom port
port = 5433
```
### socket_dir
**Type:** String
**Default:** '/tmp'
**Description:** Directory for Unix domain sockets
```conf
socket_dir = '/tmp'
socket_dir = '/var/run/pgbalancer'
```
## Backend Server Configuration
### backend_hostnameN
**Type:** String
**Description:** Hostname or IP address of backend server N
```conf
# Backend 0
backend_hostname0 = 'pg-primary'
backend_port0 = 5432
backend_weight0 = 1
backend_data_directory0 = '/var/lib/postgresql/data'
backend_flag0 = 'ALLOW_TO_FAILOVER'
# Backend 1
backend_hostname1 = 'pg-standby'
backend_port1 = 5432
backend_weight1 = 1
backend_data_directory1 = '/var/lib/postgresql/data'
backend_flag1 = 'ALLOW_TO_FAILOVER'
# Backend 2
backend_hostname2 = 'pg-replica'
backend_port2 = 5432
backend_weight2 = 1
backend_data_directory2 = '/var/lib/postgresql/data'
backend_flag2 = 'ALLOW_TO_FAILOVER'
```
### backend_portN
**Type:** Integer
**Description:** Port number of backend server N
### backend_weightN
**Type:** Integer
**Default:** 1
**Description:** Weight for load balancing (higher = more connections)
### backend_data_directoryN
**Type:** String
**Description:** Data directory path of backend server N
### backend_flagN
**Type:** String
**Description:** Flags for backend server N
**Available flags:**
- `ALLOW_TO_FAILOVER` - Allow automatic failover
- `DISALLOW_TO_FAILOVER` - Disable automatic failover
- `BACKEND_TYPE_PRIMARY` - Mark as primary server
- `BACKEND_TYPE_STANDBY` - Mark as standby server
## Connection Pooling
### num_init_children
**Type:** Integer
**Default:** 32
**Description:** Number of child processes to start