Skip to content

Commit e42bccd

Browse files
committed
When requested, load the "citus" library first
Issue: PGO-284 Issue: #3194
1 parent c9e3267 commit e42bccd

File tree

6 files changed

+86
-23
lines changed

6 files changed

+86
-23
lines changed

internal/patroni/config.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -227,17 +227,24 @@ func DynamicConfiguration(
227227
// Override the above with mandatory parameters.
228228
if pgParameters.Mandatory != nil {
229229
for k, v := range pgParameters.Mandatory.AsMap() {
230-
// Unlike other PostgreSQL parameters that have mandatory values,
231-
// shared_preload_libraries is a comma separated list that can have
232-
// other values appended in addition to the mandatory values. Below,
233-
// any values provided in the CRD are appended after the mandatory
234-
// values.
235-
s, ok := parameters[k].(string)
236-
if k == "shared_preload_libraries" && ok {
237-
parameters[k] = v + "," + s
238-
} else {
239-
parameters[k] = v
230+
231+
// This parameter is a comma-separated list. Rather than overwrite the
232+
// user-defined value, we want to combine it with the mandatory one.
233+
// Some libraries belong at specific positions in the list, so figure
234+
// that out as well.
235+
if k == "shared_preload_libraries" {
236+
// Load mandatory libraries ahead of user-defined libraries.
237+
if s, ok := parameters[k].(string); ok && len(s) > 0 {
238+
v = v + "," + s
239+
}
240+
// Load "citus" ahead of any other libraries.
241+
// - https://github.com/citusdata/citus/blob/v12.0.0/src/backend/distributed/shared_library_init.c#L417-L419
242+
if strings.Contains(v, "citus") {
243+
v = "citus," + v
244+
}
240245
}
246+
247+
parameters[k] = v
241248
}
242249
}
243250
postgresql["parameters"] = parameters

internal/patroni/config_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ func TestDynamicConfiguration(t *testing.T) {
394394
},
395395
},
396396
{
397-
name: "postgresql.parameters: mandatory shared_preload_libraries bad type",
397+
name: "postgresql.parameters: mandatory shared_preload_libraries wrong-type is ignored",
398398
input: map[string]any{
399399
"postgresql": map[string]any{
400400
"parameters": map[string]any{
@@ -420,6 +420,33 @@ func TestDynamicConfiguration(t *testing.T) {
420420
},
421421
},
422422
},
423+
{
424+
name: "postgresql.parameters: shared_preload_libraries order",
425+
input: map[string]any{
426+
"postgresql": map[string]any{
427+
"parameters": map[string]any{
428+
"shared_preload_libraries": "given, citus, more",
429+
},
430+
},
431+
},
432+
params: postgres.Parameters{
433+
Mandatory: parameters(map[string]string{
434+
"shared_preload_libraries": "mandatory",
435+
}),
436+
},
437+
expected: map[string]any{
438+
"loop_wait": int32(10),
439+
"ttl": int32(30),
440+
"postgresql": map[string]any{
441+
"parameters": map[string]any{
442+
"shared_preload_libraries": "citus,mandatory,given, citus, more",
443+
},
444+
"pg_hba": []string{},
445+
"use_pg_rewind": true,
446+
"use_slots": false,
447+
},
448+
},
449+
},
423450
{
424451
name: "postgresql.pg_hba: wrong-type is ignored",
425452
input: map[string]any{

internal/pgaudit/postgres.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package pgaudit
1717

1818
import (
1919
"context"
20-
"strings"
2120

2221
"github.com/crunchydata/postgres-operator/internal/logging"
2322
"github.com/crunchydata/postgres-operator/internal/postgres"
@@ -67,7 +66,5 @@ func PostgreSQLParameters(outParameters *postgres.Parameters) {
6766
// PostgreSQL must be restarted when changing this value.
6867
// - https://github.com/pgaudit/pgaudit#settings
6968
// - https://www.postgresql.org/docs/current/runtime-config-client.html
70-
shared := outParameters.Mandatory.Value("shared_preload_libraries")
71-
outParameters.Mandatory.Add("shared_preload_libraries",
72-
strings.TrimPrefix(shared+",pgaudit", ","))
69+
outParameters.Mandatory.AppendToList("shared_preload_libraries", "pgaudit")
7370
}

internal/pgmonitor/postgres.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,7 @@ func PostgreSQLParameters(inCluster *v1beta1.PostgresCluster, outParameters *pos
5050
// Exporter expects that shared_preload_libraries are installed
5151
// pg_stat_statements: https://access.crunchydata.com/documentation/pgmonitor/latest/exporter/
5252
// pgnodemx: https://github.com/CrunchyData/pgnodemx
53-
libraries := []string{"pg_stat_statements", "pgnodemx"}
54-
55-
defined, found := outParameters.Mandatory.Get("shared_preload_libraries")
56-
if found {
57-
libraries = append(libraries, defined)
58-
}
59-
60-
outParameters.Mandatory.Add("shared_preload_libraries", strings.Join(libraries, ","))
53+
outParameters.Mandatory.AppendToList("shared_preload_libraries", "pg_stat_statements", "pgnodemx")
6154
outParameters.Mandatory.Add("pgnodemx.kdapi_path",
6255
postgres.DownwardAPIVolumeMount().MountPath)
6356
}

internal/postgres/parameters.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ func (ps *ParameterSet) Add(name, value string) {
9696
ps.values[ps.normalize(name)] = value
9797
}
9898

99+
// AppendToList adds each value to the right-hand side of parameter name
100+
// as a comma-separated list without quoting.
101+
func (ps *ParameterSet) AppendToList(name string, value ...string) {
102+
result := ps.Value(name)
103+
104+
if len(value) > 0 {
105+
if len(result) > 0 {
106+
result += "," + strings.Join(value, ",")
107+
} else {
108+
result = strings.Join(value, ",")
109+
}
110+
}
111+
112+
ps.Add(name, result)
113+
}
114+
99115
// Get returns the value of parameter name and whether or not it was present in ps.
100116
func (ps ParameterSet) Get(name string) (string, bool) {
101117
value, ok := ps.values[ps.normalize(name)]

internal/postgres/parameters_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,26 @@ func TestParameterSet(t *testing.T) {
6868
ps2.Add("x", "n")
6969
assert.Assert(t, ps2.Value("x") != ps.Value("x"))
7070
}
71+
72+
func TestParameterSetAppendToList(t *testing.T) {
73+
ps := NewParameterSet()
74+
75+
ps.AppendToList("empty")
76+
assert.Assert(t, ps.Has("empty"))
77+
assert.Equal(t, ps.Value("empty"), "")
78+
79+
ps.AppendToList("empty")
80+
assert.Equal(t, ps.Value("empty"), "", "expected no change")
81+
82+
ps.AppendToList("full", "a")
83+
assert.Equal(t, ps.Value("full"), "a")
84+
85+
ps.AppendToList("full", "b")
86+
assert.Equal(t, ps.Value("full"), "a,b")
87+
88+
ps.AppendToList("full")
89+
assert.Equal(t, ps.Value("full"), "a,b", "expected no change")
90+
91+
ps.AppendToList("full", "a", "cd", `"e"`)
92+
assert.Equal(t, ps.Value("full"), `a,b,a,cd,"e"`)
93+
}

0 commit comments

Comments
 (0)