Skip to content

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

FIX (verification): Prevent restore checks failing on disk budget

Rostislav Duginb5d5d529f38c
diff --git a/agent/verification/internal/features/container/dockerengine.go b/agent/verification/internal/features/container/dockerengine.go
index 3a5ec855..c5354957 100644
--- a/agent/verification/internal/features/container/dockerengine.go
+++ b/agent/verification/internal/features/container/dockerengine.go
@@ -161,6 +161,7 @@ func (e *dockerEngine) CreateContainer(ctx context.Context, spec SpawnSpec) (str
resp, err := e.cli.ContainerCreate(ctx, client.ContainerCreateOptions{
Config: &container.Config{
Image: spec.Image,
+ Cmd: spec.Cmd,
Env: spec.Env,
Labels: spec.Labels,
ExposedPorts: network.PortSet{pgPublishedPort: struct{}{}},
diff --git a/agent/verification/internal/features/container/manager.go b/agent/verification/internal/features/container/manager.go
index 3546e22a..91f16e81 100644
--- a/agent/verification/internal/features/container/manager.go
+++ b/agent/verification/internal/features/container/manager.go
@@ -28,6 +28,23 @@ const (
// a blanket keep.
var minimalCaps = []string{"CHOWN", "DAC_OVERRIDE", "FOWNER", "SETGID", "SETUID"}
+// restoreTunedPostgresCmd runs the restore container's Postgres with crash
+// safety traded for a smaller on-disk footprint. The container is destroyed
+// after verification, so durability is irrelevant: fsync=off and
+// full_page_writes=off cut WAL volume, faster checkpoints recycle pg_wal
+// sooner. max_wal_senders=0 is mandatory under wal_level=minimal — Postgres
+// refuses to start otherwise. The first arg stays "postgres" so the official
+// image entrypoint still runs initdb.
+var restoreTunedPostgresCmd = []string{
+ "postgres",
+ "-c", "fsync=off",
+ "-c", "full_page_writes=off",
+ "-c", "synchronous_commit=off",
+ "-c", "wal_level=minimal",
+ "-c", "max_wal_senders=0",
+ "-c", "max_wal_size=1GB",
+}
+
type Manager struct {
engine *dockerEngine
agentID string
@@ -150,6 +167,7 @@ func (m *Manager) buildSpec(plan spawnPlan) SpawnSpec {
return SpawnSpec{
Name: "databasus-verif-" + plan.verificationID.String(),
Image: plan.image,
+ Cmd: restoreTunedPostgresCmd,
Env: []string{"POSTGRES_PASSWORD=" + plan.password},
Labels: plan.labels,
NanoCPUs: int64(plan.cpuPerJob) * 1_000_000_000,
diff --git a/agent/verification/internal/features/container/postgres_test.go b/agent/verification/internal/features/container/postgres_test.go
index 429d0016..dfadf151 100644
--- a/agent/verification/internal/features/container/postgres_test.go
+++ b/agent/verification/internal/features/container/postgres_test.go
@@ -34,6 +34,23 @@ func Test_BuildSpec_AppliesHardeningControls(t *testing.T) {
assert.Equal(t, []string{"POSTGRES_PASSWORD=pw"}, spec.Env)
}
+func Test_BuildSpec_SetsRestoreTunedPostgresCmd(t *testing.T) {
+ containerManager := NewManager(nil, "agent-1", testutil.DiscardLogger())
+
+ spec := containerManager.buildSpec(spawnPlan{
+ verificationID: uuid.New(),
+ image: "postgres@sha256:x",
+ password: "pw",
+ cpuPerJob: 2,
+ ramMbPerJob: 1024,
+ networkID: "net-id",
+ labels: map[string]string{LabelAgentID: "agent-1"},
+ })
+
+ assert.Equal(t, restoreTunedPostgresCmd, spec.Cmd)
+ assert.Equal(t, "postgres", spec.Cmd[0])
+}
+
func Test_GetInContainerConn_UsesInternalPort(t *testing.T) {
c := &PostgresContainer{password: "pw"}
diff --git a/agent/verification/internal/features/container/spawnspec.go b/agent/verification/internal/features/container/spawnspec.go
index 84209503..9b5e55b6 100644
--- a/agent/verification/internal/features/container/spawnspec.go
+++ b/agent/verification/internal/features/container/spawnspec.go
@@ -26,6 +26,7 @@ type spawnPlan struct {
type SpawnSpec struct {
Name string
Image string
+ Cmd []string
Env []string
Labels map[string]string
NanoCPUs int64
diff --git a/backend/internal/features/verification/runs/agent_controller_test.go b/backend/internal/features/verification/runs/agent_controller_test.go
index a4d03739..9ca716b0 100644
--- a/backend/internal/features/verification/runs/agent_controller_test.go
+++ b/backend/internal/features/verification/runs/agent_controller_test.go
@@ -137,7 +137,7 @@ func Test_ClaimVerification_WhenMultiplePendingFit_ReturnsOldestThatFitsBudget(t
assignment := ClaimVerificationViaAPI(
t, router, agent.Agent.ID, agent.Token,
- AgentCapacity{MaxCPU: 4, MaxRAMMb: 4096, MaxDiskGb: 5, MaxConcurrentJobs: 2},
+ AgentCapacity{MaxCPU: 4, MaxRAMMb: 4096, MaxDiskGb: 6, MaxConcurrentJobs: 2},
)
assert.Equal(
diff --git a/backend/internal/features/verification/runs/capacity.go b/backend/internal/features/verification/runs/capacity.go
index e8221df1..42aea929 100644
--- a/backend/internal/features/verification/runs/capacity.go
+++ b/backend/internal/features/verification/runs/capacity.go
@@ -7,9 +7,12 @@ import (
)
// diskCostPerJobGapMb is the per-job safe gap added on top of the downloaded
-// backup file and the restored database. Covers WAL, indexes, sort/temp spills,
-// FS slack and small fixed costs that don't scale with backup size.
-const diskCostPerJobGapMb = 1024
+// backup file and the restored database. The restore's peak on-disk footprint
+// exceeds backup + final DB size: pg_restore writes WAL into pg_wal (inside
+// PGDATA, counted by the agent's disk watcher), parallel index builds spill
+// sort/temp space, and the whole-cluster baseline plus FS slack add fixed cost.
+// 5 GB of absolute headroom covers this across the realistic DB-size range.
+const diskCostPerJobGapMb = 5120
// IsVerificationFitWithinRemainedDiskCapacity reports whether
// running candidateBackup on this agent alongside runningBackups
diff --git a/backend/internal/features/verification/runs/capacity_test.go b/backend/internal/features/verification/runs/capacity_test.go
index f3ac461e..2d0ef761 100644
--- a/backend/internal/features/verification/runs/capacity_test.go
+++ b/backend/internal/features/verification/runs/capacity_test.go
@@ -35,19 +35,19 @@ func Test_DoesVerificationFit_AcrossScenarios_RespectsAgentBudget(t *testing.T)
capacity: AgentCapacity{MaxDiskGb: 10},
runningBackups: nil,
// archive + restored + per-job gap = 10240 (full 10 GB budget)
- candidate: backupWithSize(4000, 5216),
+ candidate: backupWithSize(4000, 1120),
wantFits: true,
},
{
name: "empty agent rejects candidate one MB over budget",
capacity: AgentCapacity{MaxDiskGb: 10},
runningBackups: nil,
- candidate: backupWithSize(4000, 5217),
+ candidate: backupWithSize(4000, 1121),
wantFits: false,
},
{
name: "one running, room for another small",
- capacity: AgentCapacity{MaxDiskGb: 10},
+ capacity: AgentCapacity{MaxDiskGb: 12},
runningBackups: []*backups_core.Backup{
backupWithSize(50, 200),
},
@@ -64,8 +64,8 @@ func Test_DoesVerificationFit_AcrossScenarios_RespectsAgentBudget(t *testing.T)
wantFits: false,
},
{
- name: "three concurrent small jobs fit in 20 GB",
- capacity: AgentCapacity{MaxDiskGb: 20},
+ name: "three concurrent small jobs fit in 25 GB",
+ capacity: AgentCapacity{MaxDiskGb: 25},
runningBackups: []*backups_core.Backup{
backupWithSize(100, 500),
backupWithSize(100, 500),