Skip to content

Commit 3aafa93

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

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

internal/pgbackrest/postgres.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ 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 += ` if [ ! -z ${timestamp} ]; then echo ${timestamp} > /pgdata/latest_commit_timestamp.txt; fi`
36+
archive += ` && timestamp=$(pg_waldump "%p" | ` + extractCommitTime + ` | tail -n 1 | ` + validateCommitTime + `);`
37+
archive += ` if [ ! -z ${timestamp} ]; then echo ${timestamp} > /pgdata/latest_commit_timestamp.txt; else echo "failed" > /pgdata/commit_ts_error.txt; fi`
3738

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

0 commit comments

Comments
 (0)