Skip to content

Commit c699b26

Browse files
committed
K8SPG-681: Improve latest restorable time detection
1 parent 48eb7e5 commit c699b26

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

internal/pgbackrest/postgres.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ func PostgreSQL(
2828
// - https://pgbackrest.org/user-guide.html#quickstart/configure-archiving
2929
// - https://pgbackrest.org/command.html#command-archive-push
3030
// - https://www.postgresql.org/docs/current/runtime-config-wal.html
31-
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/'`
32-
extractCommitTime := `awk '{print $(NF-2) "T" $(NF-1) " " $(NF)}' | ` + fixTimezone + ``
33-
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}`
31+
32+
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/"`
33+
extractCommitTime := `grep -oP "COMMIT \K[^;]+" | ` + fixTimezone + ``
34+
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})$"`
3435
archive := `pgbackrest --stanza=` + DefaultStanzaName + ` archive-push "%p"`
35-
archive += ` && timestamp=$(pg_waldump "%p" | grep COMMIT | ` + extractCommitTime + ` | tail -n 1 | ` + validateCommitTime + `)$');`
36+
archive += ` && timestamp=$(pg_waldump "%p" | ` + extractCommitTime + ` | tail -n 1 | ` + validateCommitTime + `);`
3637
archive += ` if [ ! -z ${timestamp} ]; then echo ${timestamp} > /pgdata/latest_commit_timestamp.txt; fi`
3738

3839
outParameters.Mandatory.Add("archive_mode", "on")

internal/pgbackrest/postgres_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ func TestPostgreSQLParameters(t *testing.T) {
2222
assert.DeepEqual(t, parameters.Mandatory.AsMap(), map[string]string{
2323
"archive_mode": "on",
2424
"archive_command": strings.Join([]string{
25-
`pgbackrest --stanza=db archive-push "%p"`,
26-
` && timestamp=$(pg_waldump "%p" | grep COMMIT | awk '{print $(NF`,
27-
`-2) "T" $(NF-1) " " $(NF)}' | sed -E 's/([0-9]{4}-[0-9]{2}-[0-9]`,
28-
`{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}) (UTC|[\+\-][0-9]{2})/\`,
29-
`1\2/' | sed 's/UTC/Z/' | tail -n 1 | grep -E '^[0-9]{4}-[0-9]{2}`,
30-
`-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}(Z|[\+\-][0-9]{2})`,
31-
"$'); if [ ! -z ${timestamp} ]; then echo ${timestamp} > /pgdata/l",
32-
"atest_commit_timestamp.txt; fi",
25+
`pgbackrest --stanza=db archive-push "%p" `,
26+
`&& timestamp=$(pg_waldump "%p" | `,
27+
`grep -oP "COMMIT \K[^;]+" | `,
28+
`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/" | `,
29+
`sed "s/UTC/Z/" | `,
30+
"tail -n 1 | ",
31+
`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})$"); `,
32+
"if [ ! -z ${timestamp} ]; then echo ${timestamp} > /pgdata/latest_commit_timestamp.txt; fi",
3333
}, ""),
3434
"restore_command": `pgbackrest --stanza=db archive-get %f "%p"`,
3535
"track_commit_timestamp": "true",
@@ -48,14 +48,14 @@ func TestPostgreSQLParameters(t *testing.T) {
4848
assert.DeepEqual(t, parameters.Mandatory.AsMap(), map[string]string{
4949
"archive_mode": "on",
5050
"archive_command": strings.Join([]string{
51-
`pgbackrest --stanza=db archive-push "%p"`,
52-
` && timestamp=$(pg_waldump "%p" | grep COMMIT | awk '{print $(NF`,
53-
`-2) "T" $(NF-1) " " $(NF)}' | sed -E 's/([0-9]{4}-[0-9]{2}-[0-9]`,
54-
`{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}) (UTC|[\+\-][0-9]{2})/\`,
55-
`1\2/' | sed 's/UTC/Z/' | tail -n 1 | grep -E '^[0-9]{4}-[0-9]{2}`,
56-
`-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}(Z|[\+\-][0-9]{2})`,
57-
"$'); if [ ! -z ${timestamp} ]; then echo ${timestamp} > /pgdata/l",
58-
"atest_commit_timestamp.txt; fi",
51+
`pgbackrest --stanza=db archive-push "%p" `,
52+
`&& timestamp=$(pg_waldump "%p" | `,
53+
`grep -oP "COMMIT \K[^;]+" | `,
54+
`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/" | `,
55+
`sed "s/UTC/Z/" | `,
56+
"tail -n 1 | ",
57+
`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})$"); `,
58+
"if [ ! -z ${timestamp} ]; then echo ${timestamp} > /pgdata/latest_commit_timestamp.txt; fi",
5959
}, ""),
6060
"restore_command": `pgbackrest --stanza=db archive-get %f "%p" --repo=99`,
6161
"track_commit_timestamp": "true",

0 commit comments

Comments
 (0)