Skip to content

PostgreSQL backup tool with Point-In-Time-Recovery and restore verification

FIX (physical backups): Match PostgreSQL's bitnami ZSTD-unsupported error so the codec fallback fires (issue #724)

Rostislav Duginf58833e0ed08
diff --git a/backend/internal/features/backups/backups/usecases/physical/postgresql/compression.go b/backend/internal/features/backups/backups/usecases/physical/postgresql/compression.go
index 768079e7..40d501f9 100644
--- a/backend/internal/features/backups/backups/usecases/physical/postgresql/compression.go
+++ b/backend/internal/features/backups/backups/usecases/physical/postgresql/compression.go
@@ -1,12 +1,16 @@
package usecases_physical_postgresql
-import physical_enums "databasus-backend/internal/features/backups/backups/core/physical/enums"
+import (
+ "strings"
+
+ physical_enums "databasus-backend/internal/features/backups/backups/core/physical/enums"
+)
// codecFallbackOrder is the server-side compression preference, highest ratio
// first. The real backup IS the probe: each attempt runs pg_basebackup, and a
-// "compression is not supported by this build" rejection (raised pre-stream, so
-// it costs only a sub-second round-trip) advances to the next codec. `none`
-// needs no library and never raises that error, so the loop always terminates.
+// source build that lacks the codec's library rejects it pre-stream, so the
+// wasted attempt costs only a sub-second round-trip. `none` needs no library
+// and never raises that rejection, so the loop always terminates.
var codecFallbackOrder = []physical_enums.PhysicalBackupCompression{
physical_enums.PhysicalBackupCompressionZstd,
physical_enums.PhysicalBackupCompressionGzip,
@@ -28,3 +32,14 @@ func compressFlag(codec physical_enums.PhysicalBackupCompression) string {
return "server-zstd:5"
}
}
+
+// The source server rejects a codec its build lacks inside
+// parse_compress_specification (src/common/compression.c), which fills in
+// "this build does not support compression with %s" and surfaces as
+// "invalid compression specification: ..." — the algorithm-specific sinks
+// (basebackup_zstd.c) are never reached. Only the inner clause is matched:
+// the outer "invalid compression specification" also covers a rejected
+// compression level, which is a configuration bug, not a missing library.
+func isCompressionUnsupportedError(stderr []byte) bool {
+ return strings.Contains(string(stderr), "does not support compression with")
+}
diff --git a/backend/internal/features/backups/backups/usecases/physical/postgresql/compression_test.go b/backend/internal/features/backups/backups/usecases/physical/postgresql/compression_test.go
new file mode 100644
index 00000000..6dd3fca7
--- /dev/null
+++ b/backend/internal/features/backups/backups/usecases/physical/postgresql/compression_test.go
@@ -0,0 +1,41 @@
+package usecases_physical_postgresql
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func Test_IsCompressionUnsupportedError_MatchesBuildRejection(t *testing.T) {
+ cases := []struct {
+ stderr string
+ isUnsupportedBuild bool
+ }{
+ {
+ "pg_basebackup: error: could not initiate base backup: ERROR: invalid compression specification: " +
+ "this build does not support compression with ZSTD",
+ true,
+ },
+ {
+ "pg_basebackup: error: could not initiate base backup: ERROR: invalid compression specification: " +
+ "this build does not support compression with gzip",
+ true,
+ },
+ {
+ `pg_basebackup: error: invalid compression specification: compression algorithm "zstd" ` +
+ `expects a compression level between 1 and 22 (default at 0)`,
+ false,
+ },
+ {"pg_basebackup: error: could not connect to server", false},
+ {"", false},
+ }
+
+ for _, testCase := range cases {
+ assert.Equal(
+ t,
+ testCase.isUnsupportedBuild,
+ isCompressionUnsupportedError([]byte(testCase.stderr)),
+ testCase.stderr,
+ )
+ }
+}
diff --git a/backend/internal/features/backups/backups/usecases/physical/postgresql/stream.go b/backend/internal/features/backups/backups/usecases/physical/postgresql/stream.go
index 10028f43..10feb3f0 100644
--- a/backend/internal/features/backups/backups/usecases/physical/postgresql/stream.go
+++ b/backend/internal/features/backups/backups/usecases/physical/postgresql/stream.go
@@ -8,7 +8,6 @@ import (
"io"
"log/slog"
"os/exec"
- "strings"
"time"
"github.com/google/uuid"
@@ -402,15 +401,6 @@ func saveManifestSidecar(
return saltBase64, nonceBase64, nil
}
-// isCompressionUnsupportedError reports whether stderr carries pg_basebackup's
-// definitive "this build cannot compress with the requested codec" handshake
-// error (basebackup_zstd.c / basebackup_gzip.c, raised before any data streams).
-// Both "zstd compression is not supported by this build" and the gzip variant
-// share this substring, across PG 17 and 18.
-func isCompressionUnsupportedError(stderr []byte) bool {
- return strings.Contains(string(stderr), "compression is not supported by this build")
-}
-
func outcomeNetworkStall(stderr []byte) streamOutcome {
reason := physical_enums.PhysicalBackupErrorNetworkStallTimeout
diff --git a/backend/internal/features/backups/backups/usecases/physical/postgresql/stream_test.go b/backend/internal/features/backups/backups/usecases/physical/postgresql/stream_test.go
index 17bea167..d8d79eb8 100644
--- a/backend/internal/features/backups/backups/usecases/physical/postgresql/stream_test.go
+++ b/backend/internal/features/backups/backups/usecases/physical/postgresql/stream_test.go
@@ -280,9 +280,15 @@ func Test_RunStream_WhenManifestWalkFails_ReturnsManifestCorrupted(t *testing.T)
func Test_RunStream_WhenCompressionUnsupported_ReturnsRetrySignal(t *testing.T) {
storage := newFakeStorage()
- outcome, err := runStream(t.Context(), testRunStreamParams(storage, physical_enums.PhysicalBackupCompressionZstd),
- shellBuildCmd(`echo "pg_basebackup: error: zstd compression is not supported by this build" >&2; exit 1`),
- classifyFullStreamError)
+ outcome, err := runStream(
+ t.Context(),
+ testRunStreamParams(storage, physical_enums.PhysicalBackupCompressionZstd),
+ shellBuildCmd(
+ `echo "pg_basebackup: error: could not initiate base backup: ERROR: invalid compression specification: `+
+ `this build does not support compression with ZSTD" >&2; exit 1`,
+ ),
+ classifyFullStreamError,
+ )
require.NoError(t, err)
assert.True(t, outcome.isCompressionUnsupported,
@@ -395,19 +401,3 @@ func Test_SaveManifestSidecar_WhenManifestIsLarge_ExtendsDeadlineBeyondFloor(t *
"a manifest past the floor's throughput budget must get more time")
assert.Len(t, storage.saved["obj.manifest"], len(largeManifest))
}
-
-func Test_IsCompressionUnsupportedError_MatchesBuildRejection(t *testing.T) {
- cases := []struct {
- stderr string
- expected bool
- }{
- {"pg_basebackup: error: zstd compression is not supported by this build", true},
- {"pg_basebackup: error: gzip compression is not supported by this build", true},
- {"pg_basebackup: error: could not connect to server", false},
- {"", false},
- }
-
- for _, testCase := range cases {
- assert.Equal(t, testCase.expected, isCompressionUnsupportedError([]byte(testCase.stderr)), testCase.stderr)
- }
-}