Skip to content

Commit

Permalink
K8SPG-681: Improve latest restorable time detection
Browse files Browse the repository at this point in the history
  • Loading branch information
egegunes committed Dec 12, 2024
1 parent 48eb7e5 commit c699b26
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
9 changes: 5 additions & 4 deletions internal/pgbackrest/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ func PostgreSQL(
// - https://pgbackrest.org/user-guide.html#quickstart/configure-archiving
// - https://pgbackrest.org/command.html#command-archive-push
// - https://www.postgresql.org/docs/current/runtime-config-wal.html
fixTimezone := `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/'`
extractCommitTime := `awk '{print $(NF-2) "T" $(NF-1) " " $(NF)}' | ` + fixTimezone + ``
validateCommitTime := `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}`

fixTimezone := `sed -E "s/([0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}) (UTC|[\\+\\-][0-9]{2})/\1T\2\3/" | sed "s/UTC/Z/"`
extractCommitTime := `grep -oP "COMMIT \K[^;]+" | ` + fixTimezone + ``
validateCommitTime := `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})$"`
archive := `pgbackrest --stanza=` + DefaultStanzaName + ` archive-push "%p"`
archive += ` && timestamp=$(pg_waldump "%p" | grep COMMIT | ` + extractCommitTime + ` | tail -n 1 | ` + validateCommitTime + `)$');`
archive += ` && timestamp=$(pg_waldump "%p" | ` + extractCommitTime + ` | tail -n 1 | ` + validateCommitTime + `);`
archive += ` if [ ! -z ${timestamp} ]; then echo ${timestamp} > /pgdata/latest_commit_timestamp.txt; fi`

outParameters.Mandatory.Add("archive_mode", "on")
Expand Down
32 changes: 16 additions & 16 deletions internal/pgbackrest/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ func TestPostgreSQLParameters(t *testing.T) {
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",
`pgbackrest --stanza=db archive-push "%p" `,
`&& timestamp=$(pg_waldump "%p" | `,
`grep -oP "COMMIT \K[^;]+" | `,
`sed -E "s/([0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}) (UTC|[\\+\\-][0-9]{2})/\1T\2\3/" | `,
`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/latest_commit_timestamp.txt; fi",
}, ""),
"restore_command": `pgbackrest --stanza=db archive-get %f "%p"`,
"track_commit_timestamp": "true",
Expand All @@ -48,14 +48,14 @@ func TestPostgreSQLParameters(t *testing.T) {
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",
`pgbackrest --stanza=db archive-push "%p" `,
`&& timestamp=$(pg_waldump "%p" | `,
`grep -oP "COMMIT \K[^;]+" | `,
`sed -E "s/([0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}) (UTC|[\\+\\-][0-9]{2})/\1T\2\3/" | `,
`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/latest_commit_timestamp.txt; fi",
}, ""),
"restore_command": `pgbackrest --stanza=db archive-get %f "%p" --repo=99`,
"track_commit_timestamp": "true",
Expand Down

0 comments on commit c699b26

Please sign in to comment.