From 9fe2e96568fced4fe08ee829ba385c2d3619517a Mon Sep 17 00:00:00 2001 From: "hadi.zamani" Date: Wed, 15 Oct 2025 16:12:12 +0330 Subject: [PATCH] Fix bug in PITR restore problem --- cmd/keeper/cmd/keeper.go | 2 +- internal/postgresql/postgresql.go | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/cmd/keeper/cmd/keeper.go b/cmd/keeper/cmd/keeper.go index dd9b2aafa..9d0f7041f 100644 --- a/cmd/keeper/cmd/keeper.go +++ b/cmd/keeper/cmd/keeper.go @@ -1287,7 +1287,7 @@ func (p *PostgresKeeper) postgresKeeperSM(pctx context.Context) { } } - if err = pgm.StopIfStarted(true); err != nil { + if err = pgm.StopIfStarted(true, 900); err != nil { log.Errorw("failed to stop pg instance", zap.Error(err)) return } diff --git a/internal/postgresql/postgresql.go b/internal/postgresql/postgresql.go index 00c14bcd7..c68080fff 100644 --- a/internal/postgresql/postgresql.go +++ b/internal/postgresql/postgresql.go @@ -379,14 +379,19 @@ func (p *Manager) start(args ...string) error { } // Stop tries to stop an instance. An error will be returned if the instance isn't started, stop fails or -// times out (60 second). -func (p *Manager) Stop(fast bool) error { +// times out (60 second). Optional timeout parameter can be provided. +func (p *Manager) Stop(fast bool, timeout ...int) error { log.Infow("stopping database") name := filepath.Join(p.pgBinPath, "pg_ctl") cmd := exec.Command(name, "stop", "-w", "-D", p.dataDir, "-o", "-c unix_socket_directories="+common.PgUnixSocketDirectories) if fast { cmd.Args = append(cmd.Args, "-m", "fast") } + + if len(timeout) > 0 && timeout[0] > 0 { + timeoutSeconds := timeout[0] + cmd.Args = append(cmd.Args, "-t", strconv.Itoa(timeoutSeconds)) + } log.Debugw("execing cmd", "cmd", cmd) // Pipe command's std[err|out] to parent. @@ -443,8 +448,8 @@ func (p *Manager) Reload() error { } // StopIfStarted checks if the instance is started, then calls stop and -// then check if the instance is really stopped -func (p *Manager) StopIfStarted(fast bool) error { +// then check if the instance is really stopped. Optional timeout parameter can be provided. +func (p *Manager) StopIfStarted(fast bool, timeout ...int) error { // Stop will return an error if the instance isn't started, so first check // if it's started started, err := p.IsStarted() @@ -459,7 +464,7 @@ func (p *Manager) StopIfStarted(fast bool) error { if !started { return nil } - if err = p.Stop(fast); err != nil { + if err = p.Stop(fast, timeout...); err != nil { return err } started, err = p.IsStarted()