MCPcopy Create free account
hub / github.com/cloudnative-pg/cloudnative-pg / PromoteAndWait

Method PromoteAndWait

pkg/management/postgres/promote.go:35–94  ·  view source on GitHub ↗

PromoteAndWait promotes this instance, and wait DefaultPgCtlTimeoutForPromotion seconds for it to happen

(ctx context.Context)

Source from the content-addressed store, hash-verified

33// PromoteAndWait promotes this instance, and wait DefaultPgCtlTimeoutForPromotion
34// seconds for it to happen
35func (instance *Instance) PromoteAndWait(ctx context.Context) error {
36 contextLogger := log.FromContext(ctx)
37
38 instance.ShutdownConnections()
39
40 instance.LogPgControldata(ctx, "promote")
41
42 options := []string{
43 "-D",
44 instance.PgData,
45 "-w",
46 "promote",
47 "-t " + strconv.Itoa(int(instance.GetClusterOrDefault().GetPgCtlTimeoutForPromotion())),
48 }
49
50 contextLogger.Info("Promoting instance", "pgctl_options", options)
51
52 pgCtlCmd := exec.Command(pgCtlName, options...) // #nosec
53 err := execlog.RunStreaming(pgCtlCmd, pgCtlName)
54 if err != nil {
55 return fmt.Errorf("error promoting the PostgreSQL instance: %w", err)
56 }
57
58 timeLimit := time.Now().Add(1 * time.Minute)
59 for {
60 if time.Now().After(timeLimit) {
61 contextLogger.Info("The standby.signal file still exists but timeout reached, " +
62 "error during PostgreSQL instance promotion")
63 return fmt.Errorf("standby.signal still existent")
64 }
65
66 if status, _ := instance.IsPrimary(); status {
67 break
68 }
69
70 time.Sleep(1 * time.Second)
71 }
72
73 contextLogger.Info("Requesting a checkpoint")
74
75 db, err := instance.GetSuperUserDB()
76 if err != nil {
77 return fmt.Errorf("after having promoted the instance: %v", err)
78 }
79
80 err = db.Ping()
81 if err != nil {
82 return fmt.Errorf("after having promoted the instance: %v", err)
83 }
84
85 // For pg_rewind to work we need to issue a checkpoint here
86 _, err = db.Exec("CHECKPOINT")
87 if err != nil {
88 return fmt.Errorf("checkpoint after instance promotion: %v", err)
89 }
90
91 contextLogger.Info("The PostgreSQL instance has been promoted successfully")
92

Callers 1

handlePromotionMethod · 0.80

Calls 9

ShutdownConnectionsMethod · 0.95
LogPgControldataMethod · 0.95
GetClusterOrDefaultMethod · 0.95
IsPrimaryMethod · 0.95
GetSuperUserDBMethod · 0.95
InfoMethod · 0.80
ExecMethod · 0.80
PingMethod · 0.65

Tested by

no test coverage detected