Skip to content

Commit ca79fba

Browse files
authored
Fix monitor postgres not starting up the first time (#752)
1 parent 18229bc commit ca79fba

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

src/bin/pg_autoctl/fsm_transition.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,10 +1463,19 @@ fsm_init_from_standby(Keeper *keeper)
14631463
/*
14641464
* fsm_drop_node is called to finish dropping a node on the client side.
14651465
*
1466-
* Nothing to do here, really.
1466+
* This stops postgres and updates the postgres state file to say that postgres
1467+
* should be stopped. It also cleans up any existing init file. Not doing these
1468+
* two things can confuse a possible future re-init of the node.
14671469
*/
14681470
bool
14691471
fsm_drop_node(Keeper *keeper)
14701472
{
1471-
return true;
1473+
KeeperConfig *config = &(keeper->config);
1474+
if (!fsm_stop_postgres(keeper))
1475+
{
1476+
/* errors have already been logged */
1477+
return false;
1478+
}
1479+
1480+
return unlink_file(config->pathnames.init);
14721481
}

src/bin/pg_autoctl/keeper.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,22 @@ keeper_register_and_init(Keeper *keeper, NodeState initialState)
16731673
goto rollback;
16741674
}
16751675

1676+
/*
1677+
* If we dropped a primary using --force, it's possible that the postgres
1678+
* state file still says that postgres should be running. In that case
1679+
* postgres would probably be running now. The problem is that our
1680+
* fsm_init_primary transation errors out when a postgres is running during
1681+
* initialization. So if we were dropped and this is the first time
1682+
* create is run after that, then we first stop postgres and record this
1683+
* in our postgres state file.
1684+
*/
1685+
if (keeper->state.current_role == DROPPED_STATE &&
1686+
!file_exists(keeper->config.pathnames.init))
1687+
{
1688+
log_info("Making sure postgres was stopped, when it was previously dropped");
1689+
ensure_postgres_service_is_stopped(&keeper->postgres);
1690+
}
1691+
16761692
/*
16771693
* Leave a track record that we're ok to initialize in PGDATA, so that in
16781694
* case of `pg_autoctl create` being interrupted, we may resume operations

src/bin/pg_autoctl/service_postgres_ctl.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,6 @@ service_postgres_ctl_loop(LocalPostgresServer *postgres)
188188
/* make sure to initialize the expected Postgres status to unknown */
189189
pgStatus->pgExpectedStatus = PG_EXPECTED_STATUS_UNKNOWN;
190190

191-
if (pg_setup_pgdata_exists(pgSetup))
192-
{
193-
if (!local_postgres_set_status_path(postgres, true))
194-
{
195-
log_error("Failed to clean-up postgres state file pathname, "
196-
"see above for details.");
197-
exit(EXIT_CODE_BAD_STATE);
198-
}
199-
}
200-
201191
for (;;)
202192
{
203193
int status;

0 commit comments

Comments
 (0)