Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto pgtune #2796

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions internal/patroni/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"sigs.k8s.io/yaml"

"github.com/crunchydata/postgres-operator/internal/naming"
"github.com/crunchydata/postgres-operator/internal/pgtune"
"github.com/crunchydata/postgres-operator/internal/postgres"
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
)
Expand Down Expand Up @@ -243,6 +244,17 @@ func DynamicConfiguration(
}
}
}

//Add PGTuneConfiguration if enabled. Do not override patroni.DynamicConfiguration values
if cluster.Spec.AutoPGTune != nil {
PGTuneConfig := pgtune.GetPGTuneConfigParameters(cluster)
for k, v := range PGTuneConfig {
if _, ok := parameters[k]; !ok {
parameters[k] = v
}
}
}

postgresql["parameters"] = parameters

// Copy the "postgresql.pg_hba" section after any mandatory values.
Expand Down
102 changes: 102 additions & 0 deletions internal/patroni/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -867,3 +867,105 @@ func TestProbeTiming(t *testing.T) {
assert.Assert(t, actual.FailureThreshold >= 1) // Minimum value is 1.
}
}

func TestPatroniOverrides(t *testing.T) {
cluster := new(v1beta1.PostgresCluster)
cluster.Default()
cluster.Namespace = "some-namespace"
cluster.Name = "cluster-name"
cluster.Spec.PostgresVersion = 13
cluster.Spec.AutoPGTune = &v1beta1.PostgresClusterPGTune{}
cluster.Spec.InstanceSets = []v1beta1.PostgresInstanceSetSpec{{Name: "test"}}
for i := range cluster.Spec.InstanceSets {
cluster.Spec.InstanceSets[i].Default(i)
cluster.Spec.InstanceSets[i].Resources = v1.ResourceRequirements{Requests: v1.ResourceList{
"memory": resource.MustParse("4Gi"),
"cpu": resource.MustParse("2000m"),
},
}
}

patronidcInput := map[string]interface{}{
"postgresql": map[string]interface{}{
"parameters": map[string]interface{}{
"wal_buffers": "patroni",
"work_mem": "overrides",
"shared_buffers": "pgtune",
},
},
}

expected := map[string]interface{}{
"loop_wait": int32(10),
"ttl": int32(30),
"postgresql": map[string]interface{}{
"parameters": map[string]interface{}{
"wal_buffers": "patroni",
"work_mem": "overrides",
"shared_buffers": "pgtune",
"max_parallel_workers": "2",
"max_parallel_workers_per_gather": "1",
"max_worker_processes": "2",
"max_parallel_maintenance_workers": "1",
"default_statistics_target": "100",
"effective_cache_size": "3072MB",
"maintenance_work_mem": "256MB",
"min_wal_size": "1GB",
"max_wal_size": "4GB",
},
"pg_hba": []string{},
"use_pg_rewind": true,
"use_slots": false,
},
}

actual := DynamicConfiguration(cluster, patronidcInput, postgres.HBAs{}, postgres.Parameters{})

assert.DeepEqual(t, expected, actual)
}

func TestPatroniPGTuneDisabled(t *testing.T) {
cluster := new(v1beta1.PostgresCluster)
cluster.Default()
cluster.Namespace = "some-namespace"
cluster.Name = "cluster-name"
cluster.Spec.PostgresVersion = 13
cluster.Spec.InstanceSets = []v1beta1.PostgresInstanceSetSpec{{Name: "test"}}
for i := range cluster.Spec.InstanceSets {
cluster.Spec.InstanceSets[i].Default(i)
cluster.Spec.InstanceSets[i].Resources = v1.ResourceRequirements{Requests: v1.ResourceList{
"memory": resource.MustParse("4Gi"),
"cpu": resource.MustParse("2000m"),
},
}
}

patronidcInput := map[string]interface{}{
"postgresql": map[string]interface{}{
"parameters": map[string]interface{}{
"wal_buffers": "pgtune",
"work_mem": "is",
"shared_buffers": "disabled",
},
},
}

expected := map[string]interface{}{
"loop_wait": int32(10),
"ttl": int32(30),
"postgresql": map[string]interface{}{
"parameters": map[string]interface{}{
"wal_buffers": "pgtune",
"work_mem": "is",
"shared_buffers": "disabled",
},
"pg_hba": []string{},
"use_pg_rewind": true,
"use_slots": false,
},
}

actual := DynamicConfiguration(cluster, patronidcInput, postgres.HBAs{}, postgres.Parameters{})

assert.DeepEqual(t, expected, actual)
}
Loading