Skip to content

K8SPG-628: Allow overriding restore_command #955

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

Merged
merged 7 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
21 changes: 20 additions & 1 deletion internal/pgbackrest/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ func PostgreSQL(
archive += ` if [ ! -z ${timestamp} ]; then echo ${timestamp} > /pgdata/latest_commit_timestamp.txt; fi`

outParameters.Mandatory.Add("archive_mode", "on")
outParameters.Mandatory.Add("archive_command", archive)

if backupsEnabled {
outParameters.Mandatory.Add("archive_command", archive)
} else {
// If backups are disabled, keep archive_mode on (to avoid a Postgres restart)
// and throw away WAL.
outParameters.Mandatory.Add("archive_command", `true`)
}

// K8SPG-518: This parameter is required to ensure that the commit timestamp is
// included in the WAL file. This is necessary for the WAL watcher to
Expand All @@ -63,6 +70,18 @@ func PostgreSQL(
// - https://pgbackrest.org/command.html#command-archive-get
// - https://www.postgresql.org/docs/current/runtime-config-wal.html
restore := `pgbackrest --stanza=` + DefaultStanzaName + ` archive-get %f "%p"`
if inCluster.Spec.Patroni != nil && inCluster.Spec.Patroni.DynamicConfiguration != nil {
postgresql, ok := inCluster.Spec.Patroni.DynamicConfiguration["postgresql"].(map[string]any)
if ok {
params, ok := postgresql["parameters"].(map[string]any)
if ok {
restore_command, ok := params["restore_command"].(string)
if ok {
restore = restore_command
}
}
}
}
outParameters.Mandatory.Add("restore_command", restore)

if inCluster.Spec.Standby != nil && inCluster.Spec.Standby.Enabled && inCluster.Spec.Standby.RepoName != "" {
Expand Down
30 changes: 30 additions & 0 deletions internal/pgbackrest/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,40 @@ func TestPostgreSQLParameters(t *testing.T) {
"archive_timeout": "60s",
})

dynamic := map[string]any{
"postgresql": map[string]any{
"parameters": map[string]any{
"restore_command": "/bin/true",
},
},
}
if cluster.Spec.Patroni == nil {
cluster.Spec.Patroni = &v1beta1.PatroniSpec{}
}
cluster.Spec.Patroni.DynamicConfiguration = dynamic

PostgreSQL(cluster, parameters, true)
assert.DeepEqual(t, parameters.Mandatory.AsMap(), map[string]string{
"archive_mode": "on",
"archive_command": strings.Join([]string{
`pgbackrest --stanza=db archive-push "%p"`,
` && timestamp=$(pg_waldump "%p" | grep COMMIT | awk '{print $(NF`,
`-2) "T" $(NF-1) " " $(NF)}' | sed -E 's/([0-9]{4}-[0-9]{2}-[0-9]`,
`{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}) (UTC|[\+\-][0-9]{2})/\`,
`1\2/' | sed 's/UTC/Z/' | tail -n 1 | grep -E '^[0-9]{4}-[0-9]{2}`,
`-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}(Z|[\+\-][0-9]{2})`,
"$'); if [ ! -z ${timestamp} ]; then echo ${timestamp} > /pgdata/l",
"atest_commit_timestamp.txt; fi",
}, ""),
"restore_command": "/bin/true",
"track_commit_timestamp": "true",
})

cluster.Spec.Standby = &v1beta1.PostgresStandbySpec{
Enabled: true,
RepoName: "repo99",
}
cluster.Spec.Patroni.DynamicConfiguration = nil

PostgreSQL(cluster, parameters, true)
assert.DeepEqual(t, parameters.Mandatory.AsMap(), map[string]string{
Expand Down
Loading