Skip to content

Commit 11f9f8a

Browse files
committed
Merge pull request sorintlab#541 from sgotti/postgres_consider_standby_if_recovery.conf_exists
postgres: simplify standby role detection
2 parents 0e8e6a6 + bf492e6 commit 11f9f8a

File tree

2 files changed

+5
-55
lines changed

2 files changed

+5
-55
lines changed

internal/postgresql/postgresql.go

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -650,18 +650,13 @@ func (p *Manager) IsInitialized() (bool, error) {
650650
return true, nil
651651
}
652652

653-
func (p *Manager) GetRoleFromDB() (common.Role, error) {
654-
ctx, cancel := context.WithTimeout(context.Background(), p.requestTimeout)
655-
defer cancel()
656-
return getRole(ctx, p.localConnParams)
657-
}
658-
659653
func (p *Manager) GetRole() (common.Role, error) {
660-
curConnParams, err := p.GetPrimaryConninfo()
661-
if err != nil {
662-
return "", fmt.Errorf("error retrieving primary conn info: %v", err)
654+
// if recovery.conf exists then consider it as a standby
655+
_, err := os.Stat(filepath.Join(p.dataDir, "recovery.conf"))
656+
if err != nil && !os.IsNotExist(err) {
657+
return "", fmt.Errorf("error determining if recovery.conf exists: %v", err)
663658
}
664-
if curConnParams == nil {
659+
if os.IsNotExist(err) {
665660
return common.RoleMaster, nil
666661
}
667662
return common.RoleStandby, nil
@@ -709,26 +704,6 @@ func (p *Manager) GetPrimarySlotName() (string, error) {
709704
return "", nil
710705
}
711706

712-
func (p *Manager) HasConnParams() (bool, error) {
713-
regex := regexp.MustCompile(`primary_conninfo`)
714-
715-
fh, err := os.Open(filepath.Join(p.dataDir, "recovery.conf"))
716-
if os.IsNotExist(err) {
717-
return false, nil
718-
}
719-
defer fh.Close()
720-
721-
scanner := bufio.NewScanner(fh)
722-
scanner.Split(bufio.ScanLines)
723-
724-
for scanner.Scan() {
725-
if regex.MatchString(scanner.Text()) {
726-
return true, nil
727-
}
728-
}
729-
return false, nil
730-
}
731-
732707
func (p *Manager) WriteConf() error {
733708
return common.WriteFileAtomicFunc(filepath.Join(p.dataDir, postgresConf), 0600,
734709
func(f io.Writer) error {

internal/postgresql/utils.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -206,31 +206,6 @@ func dropReplicationSlot(ctx context.Context, connParams ConnParams, name string
206206
return err
207207
}
208208

209-
func getRole(ctx context.Context, connParams ConnParams) (common.Role, error) {
210-
db, err := sql.Open("postgres", connParams.ConnString())
211-
if err != nil {
212-
return "", err
213-
}
214-
defer db.Close()
215-
216-
rows, err := query(ctx, db, "select pg_is_in_recovery from pg_is_in_recovery()")
217-
if err != nil {
218-
return "", err
219-
}
220-
defer rows.Close()
221-
for rows.Next() {
222-
var isInRecovery bool
223-
if err := rows.Scan(&isInRecovery); err != nil {
224-
return "", err
225-
}
226-
if isInRecovery {
227-
return common.RoleStandby, nil
228-
}
229-
return common.RoleMaster, nil
230-
}
231-
return "", fmt.Errorf("no rows returned")
232-
}
233-
234209
func getSyncStandbys(ctx context.Context, connParams ConnParams) ([]string, error) {
235210
db, err := sql.Open("postgres", connParams.ConnString())
236211
if err != nil {

0 commit comments

Comments
 (0)