375 lines
4.5 KiB
Markdown
375 lines
4.5 KiB
Markdown
Untuk server PostgreSQL dengan spesifikasi:
|
||
|
||
* RAM: **4 GB**
|
||
* CPU: **4 Core**
|
||
* Disk: **50 GB SSD** (diasumsikan SSD)
|
||
* OS: Ubuntu Server
|
||
* Fungsi: Database Server PostgreSQL
|
||
|
||
Berikut optimasi yang cukup aman untuk beban aplikasi bisnis (koperasi, ERP, inventory, web app internal, dll).
|
||
|
||
## 1. Optimasi Linux
|
||
|
||
### Disable swapiness tinggi
|
||
|
||
Cek:
|
||
|
||
```bash
|
||
cat /proc/sys/vm/swappiness
|
||
```
|
||
|
||
Ubah menjadi 10:
|
||
|
||
```bash
|
||
sudo nano /etc/sysctl.conf
|
||
```
|
||
|
||
Tambahkan:
|
||
|
||
```conf
|
||
vm.swappiness=10
|
||
vm.vfs_cache_pressure=50
|
||
```
|
||
|
||
Aktifkan:
|
||
|
||
```bash
|
||
sudo sysctl -p
|
||
```
|
||
|
||
---
|
||
|
||
### Limit open files
|
||
|
||
```bash
|
||
sudo nano /etc/security/limits.conf
|
||
```
|
||
|
||
Tambahkan:
|
||
|
||
```conf
|
||
* soft nofile 65535
|
||
* hard nofile 65535
|
||
postgres soft nofile 65535
|
||
postgres hard nofile 65535
|
||
```
|
||
|
||
---
|
||
|
||
### Install tools monitoring
|
||
|
||
```bash
|
||
sudo apt install htop iotop sysstat net-tools curl unzip -y
|
||
```
|
||
|
||
---
|
||
|
||
## 2. Optimasi PostgreSQL
|
||
|
||
Cari lokasi konfigurasi:
|
||
|
||
```bash
|
||
sudo -u postgres psql -c "SHOW config_file;"
|
||
```
|
||
|
||
Biasanya:
|
||
|
||
```bash
|
||
/etc/postgresql/17/main/postgresql.conf
|
||
```
|
||
|
||
Edit:
|
||
|
||
```bash
|
||
sudo nano /etc/postgresql/*/main/postgresql.conf
|
||
```
|
||
|
||
---
|
||
|
||
### Memory
|
||
|
||
Untuk RAM 4GB:
|
||
|
||
```conf
|
||
shared_buffers = 1GB
|
||
effective_cache_size = 3GB
|
||
|
||
work_mem = 16MB
|
||
maintenance_work_mem = 256MB
|
||
```
|
||
|
||
Penjelasan:
|
||
|
||
* shared_buffers ≈ 25% RAM
|
||
* effective_cache_size ≈ 75% RAM
|
||
* work_mem jangan terlalu besar karena dikalikan jumlah query.
|
||
|
||
---
|
||
|
||
### WAL
|
||
|
||
```conf
|
||
wal_buffers = 16MB
|
||
|
||
min_wal_size = 1GB
|
||
max_wal_size = 4GB
|
||
|
||
checkpoint_completion_target = 0.9
|
||
```
|
||
|
||
Mengurangi checkpoint berlebihan.
|
||
|
||
---
|
||
|
||
### Parallel Query
|
||
|
||
CPU 4 Core:
|
||
|
||
```conf
|
||
max_worker_processes = 4
|
||
max_parallel_workers = 4
|
||
max_parallel_workers_per_gather = 2
|
||
max_parallel_maintenance_workers = 2
|
||
```
|
||
|
||
---
|
||
|
||
### Connection
|
||
|
||
Jika aplikasi web biasa:
|
||
|
||
```conf
|
||
max_connections = 100
|
||
```
|
||
|
||
Jika banyak user:
|
||
|
||
Lebih baik gunakan connection pooler seperti PgBouncer.
|
||
|
||
---
|
||
|
||
### Planner
|
||
|
||
```conf
|
||
random_page_cost = 1.1
|
||
effective_io_concurrency = 200
|
||
```
|
||
|
||
Khusus SSD.
|
||
|
||
---
|
||
|
||
### Logging
|
||
|
||
```conf
|
||
log_min_duration_statement = 1000
|
||
log_checkpoints = on
|
||
log_connections = on
|
||
log_disconnections = on
|
||
```
|
||
|
||
Log query yang lebih dari 1 detik.
|
||
|
||
---
|
||
|
||
## 3. Autovacuum
|
||
|
||
Pastikan aktif:
|
||
|
||
```conf
|
||
autovacuum = on
|
||
```
|
||
|
||
Atur:
|
||
|
||
```conf
|
||
autovacuum_max_workers = 3
|
||
autovacuum_naptime = 30s
|
||
|
||
autovacuum_vacuum_scale_factor = 0.05
|
||
autovacuum_analyze_scale_factor = 0.02
|
||
```
|
||
|
||
Sangat membantu performa tabel transaksi.
|
||
|
||
---
|
||
|
||
## 4. Optimasi Disk
|
||
|
||
Jika SSD:
|
||
|
||
Cek scheduler:
|
||
|
||
```bash
|
||
cat /sys/block/sda/queue/scheduler
|
||
```
|
||
|
||
Gunakan:
|
||
|
||
```bash
|
||
mq-deadline
|
||
```
|
||
|
||
Contoh:
|
||
|
||
```bash
|
||
echo mq-deadline | sudo tee /sys/block/sda/queue/scheduler
|
||
```
|
||
|
||
---
|
||
|
||
## 5. Optimasi Network
|
||
|
||
Edit:
|
||
|
||
```bash
|
||
sudo nano /etc/sysctl.conf
|
||
```
|
||
|
||
Tambahkan:
|
||
|
||
```conf
|
||
net.core.somaxconn=1024
|
||
|
||
net.ipv4.tcp_keepalive_time=600
|
||
|
||
net.ipv4.tcp_keepalive_intvl=30
|
||
|
||
net.ipv4.tcp_keepalive_probes=10
|
||
```
|
||
|
||
Aktifkan:
|
||
|
||
```bash
|
||
sudo sysctl -p
|
||
```
|
||
|
||
---
|
||
|
||
## 6. PostgreSQL Extension Wajib
|
||
|
||
Masuk:
|
||
|
||
```bash
|
||
sudo -u postgres psql
|
||
```
|
||
|
||
Aktifkan:
|
||
|
||
```sql
|
||
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
|
||
```
|
||
|
||
Tambahkan pada `postgresql.conf`:
|
||
|
||
```conf
|
||
shared_preload_libraries = 'pg_stat_statements'
|
||
```
|
||
|
||
Restart:
|
||
|
||
```bash
|
||
sudo systemctl restart postgresql
|
||
```
|
||
|
||
---
|
||
|
||
## 7. Backup Harian
|
||
|
||
Buat script:
|
||
|
||
```bash
|
||
sudo nano /opt/backup-postgres.sh
|
||
```
|
||
|
||
Isi:
|
||
|
||
```bash
|
||
#!/bin/bash
|
||
|
||
DATE=$(date +%F)
|
||
|
||
pg_dump -U postgres koperasi \
|
||
| gzip > /backup/koperasi-$DATE.sql.gz
|
||
|
||
find /backup -type f -mtime +7 -delete
|
||
```
|
||
|
||
Jalankan otomatis:
|
||
|
||
```bash
|
||
crontab -e
|
||
```
|
||
|
||
```cron
|
||
0 1 * * * /opt/backup-postgres.sh
|
||
```
|
||
|
||
---
|
||
|
||
## 8. Monitoring Database
|
||
|
||
Install:
|
||
|
||
```bash
|
||
sudo apt install postgresql-contrib -y
|
||
```
|
||
|
||
Monitoring:
|
||
|
||
```sql
|
||
SELECT * FROM pg_stat_activity;
|
||
```
|
||
|
||
Ukuran database:
|
||
|
||
```sql
|
||
SELECT pg_size_pretty(pg_database_size('koperasi'));
|
||
```
|
||
|
||
Query lambat:
|
||
|
||
```sql
|
||
SELECT *
|
||
FROM pg_stat_statements
|
||
ORDER BY total_exec_time DESC
|
||
LIMIT 20;
|
||
```
|
||
|
||
---
|
||
|
||
## Konfigurasi Final yang Saya Rekomendasikan
|
||
|
||
```conf
|
||
max_connections = 100
|
||
|
||
shared_buffers = 1GB
|
||
effective_cache_size = 3GB
|
||
|
||
work_mem = 16MB
|
||
maintenance_work_mem = 256MB
|
||
|
||
wal_buffers = 16MB
|
||
|
||
min_wal_size = 1GB
|
||
max_wal_size = 4GB
|
||
|
||
checkpoint_completion_target = 0.9
|
||
|
||
random_page_cost = 1.1
|
||
effective_io_concurrency = 200
|
||
|
||
max_worker_processes = 4
|
||
max_parallel_workers = 4
|
||
max_parallel_workers_per_gather = 2
|
||
|
||
autovacuum = on
|
||
autovacuum_max_workers = 3
|
||
|
||
log_min_duration_statement = 1000
|
||
|
||
shared_preload_libraries = 'pg_stat_statements'
|
||
```
|
||
|
||
Dengan spesifikasi 4 Core / 4 GB RAM, konfigurasi ini biasanya mampu menangani aplikasi bisnis dengan **50–200 user aktif** dan database berukuran beberapa hingga puluhan GB, terutama jika menggunakan SSD dan query sudah diindeks dengan baik.
|