Skip to main content

CloudNativePG Tuning

The HavenPlus reference implementation uses PostgreSQL default values. For workloads requiring better performance, you can customize database parameters in the CloudNativePG Cluster specification.

Performance Tuning Parameters

For clusters with heavier workloads, you can tune the following postgresql.parameters. These should be set in proportion to the Cluster's configured resources (CPU and memory).

Use PGTune to calculate recommended starting values by entering your Cluster's resources, workload type, and storage type.

ParameterExample valueWhat it doesWhy
shared_buffers2GBPostgreSQL's shared memory for caching table and index dataRule of thumb: 25-50% of the memory request. Claimed at startup, leaving less headroom for connections and work_mem
effective_cache_size4GBPlanner estimate of total cacheable memory (shared_buffers + OS cache)Guides the planner toward index scans; typically 50-75% of total RAM, kept conservative relative to the memory limit
maintenance_work_mem512MBMemory allocated for maintenance operations like VACUUM, CREATE INDEXSpeeds up maintenance operations (default 64MB); used only during those operations, not per connection
work_mem4MBMemory per sort or hash operation per queryKeep close to default when max_connections is high (e.g., 500). Higher values multiplied by concurrent operations risk OOM
max_connections500Maximum number of concurrent connections to the databaseControls connection load. Higher values allow more clients but consume more memory per connection
max_worker_processes4Maximum number of background worker processesCeiling for all parallel worker settings
max_parallel_workers4Maximum parallel workers for parallel queriesSubset of max_worker_processes
max_parallel_workers_per_gather2Maximum parallel workers per single parallel queryMatch to the CPU limit of the Cluster
max_parallel_maintenance_workers2Maximum parallel workers for maintenance operationsMatch to the CPU limit of the Cluster
checkpoint_completion_target0.9Target fraction of checkpoint interval to completeSpreads checkpoint I/O over time to avoid I/O spikes on shared or cloud storage
random_page_cost1.1Planner's estimated cost of a random page read vs. sequentialLowered from the default 4.0 (HDD assumption) to reflect SSD or cloud-backed storage, favoring index scans
effective_io_concurrency200Number of concurrent I/O operations the storage can handleHigh value suits SSD or cloud storage (default 1 assumes a single spinning disk)
default_statistics_target100Default sample size for ANALYZE statisticsEqual to the PostgreSQL default; explicitly set for documentation clarity
huge_pagestryEnables use of OS huge pages for shared memoryReduces memory management overhead for large shared_buffers; try avoids hard dependency on node configuration
hot_standby_feedbackonEnables standby servers to send feedback to the primaryPrevents the primary from removing WAL files still needed by standbys; required for read replicas
caution

If shared_buffers is set equal to the memory request, total usage (buffers + work_mem × concurrent operations + overhead) can approach the memory limit under high connection load. Monitor usage and adjust the memory request, or lower shared_buffers and work_mem, if OOM restarts occur.

Configuring PostgreSQL Parameters in a CNPG Cluster Resource

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: example-pg-cluster
spec:
instances: 3
storage:
size: 10Gi
postgresql:
parameters:
shared_buffers: "2GB"
effective_cache_size: "4GB"
maintenance_work_mem: "512MB"
work_mem: "4MB"
max_connections: "500"
max_worker_processes: "4"
max_parallel_workers: "4"
max_parallel_workers_per_gather: "2"
max_parallel_maintenance_workers: "2"
checkpoint_completion_target: "0.9"
random_page_cost: "1.1"
effective_io_concurrency: "200"
default_statistics_target: "100"
huge_pages: "try"
hot_standby_feedback: "on"