Explanation
Resource Management¶
How to size CPU and memory for PostgreSQL clusters.
Default Resource Allocation¶
Recommended starting point for production workloads:
resources:
requests:
cpu: 100m
memory: 256Mi
limits:
cpu: 500m
memory: 512Mi
Sizing Guidelines¶
CPU¶
Requests: Guaranteed CPU allocation
Start with 100m per instance
Monitor actual usage:
kubectl top pods -n <namespace>Increase if consistently >80% of request
Limits: Maximum CPU burst
Set 2-5x requests (allows bursting)
PostgreSQL benefits from CPU bursting during queries
Avoid overly restrictive limits (causes throttling)
Memory¶
Requests: Guaranteed memory allocation
Minimum: 256Mi per instance
Formula:
shared_buffers + work_mem * max_connections + OS overheadPostgreSQL memory needs grow with data size and concurrency
Limits: Maximum memory allocation
Set 2x requests (prevents runaway memory growth)
PostgreSQL uses memory for caching (more = better)
OOM kills happen at limit (set conservatively)
PostgreSQL Configuration¶
Match resource limits to PostgreSQL parameters:
postgresql:
parameters:
# Should be ~25% of memory limit
shared_buffers: "128MB" # For 512Mi limit
# Work memory per query operation
work_mem: "4MB"
# Maximum concurrent connections
max_connections: "200"
# Maintenance operations memory
maintenance_work_mem: "64MB"
Monitoring Resource Usage¶
Check actual resource consumption:
# Pod resource usage
kubectl top pods -n <namespace> -l cnpg.io/cluster=<cluster-name>
# PostgreSQL memory usage
kubectl exec -it <pod> -n <namespace> -- \
psql -U postgres -c "SELECT * FROM pg_stat_database;"
Scaling Considerations¶
Vertical Scaling (increase resources)¶
Update Cluster spec with new resource values
CNPG performs rolling restart
Adjust PostgreSQL parameters if needed
Horizontal Scaling (add instances)¶
Increase
instancescount in Cluster specCNPG provisions new standby instances
Replication happens automatically
Example Configurations¶
Small Application¶
instances: 2
resources:
requests: { cpu: 100m, memory: 256Mi }
limits: { cpu: 500m, memory: 512Mi }
postgresql:
parameters:
shared_buffers: "128MB"
max_connections: "100"
Medium Application¶
instances: 2
resources:
requests: { cpu: 250m, memory: 1Gi }
limits: { cpu: 1000m, memory: 2Gi }
postgresql:
parameters:
shared_buffers: "512MB"
max_connections: "200"
Large Application¶
instances: 3
resources:
requests: { cpu: 500m, memory: 2Gi }
limits: { cpu: 2000m, memory: 4Gi }
postgresql:
parameters:
shared_buffers: "1GB"
max_connections: "300"