There exist multiple kinds and levels of tuning and optimization for the system and PostgreSQL. This section will try to explain all of them briefly.
Disk:
Memory:
Virtual Machines:
wal_recycle and wal_init_zero when using COW filesystemsFor tuning PostgreSQL configuration parameters based on usage and hardware resources, see the comprehensive guide at PostgreSQL Configuration Tuning Guide.
Key parameters to consider:
shared_buffers: Typically 25% of available RAMwork_mem: Per-operation memory for sorts and hasheseffective_cache_size: Estimate of memory available for disk cachingmaintenance_work_mem: Memory for maintenance operationsImproving queries and adding indexes can greatly boost performance:
EXPLAIN ANALYZEFor a detailed guide on interpreting query plans, see Explaining PostgreSQL EXPLAIN.
PgBouncer in transaction mode can significantly improve performance by multiplexing connections:
apiVersion: stackgres.io/v1
kind: SGPoolingConfig
metadata:
name: optimized-pooling
spec:
pgBouncer:
pgbouncer.ini:
pgbouncer:
pool_mode: transaction
max_client_conn: '1000'
default_pool_size: '80'
Note: Transaction mode may require application changes since session objects cannot be used.
Identify bottlenecks (CPU, Memory, Disks, Network) and scale resources incrementally:
CPU:
Memory:
Disks:
Network:
Move read-only traffic to replicas to scale reads without sharding:
apiVersion: stackgres.io/v1
kind: SGCluster
metadata:
name: my-cluster
spec:
instances: 3
replication:
mode: sync
syncInstances: 1
groups:
- instances: 1
role: readonly
Using synchronous replication allows consistent reads from replicas while freeing primary resources.
After trying out all the other optimizations, you should consider sharding the database. Sharding is a technique that allows scaling horizontally a database by splitting its data into shards distributed among multiple database but capable of being used as a single database. This is achieved by defining a distribution key in each of the table so that the data for a specific distribution key is all contained in a single database. For more information about sharding and sharding technologies offered by StackGres see the sharded cluster section.