Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd0e1fd9

Browse files
committed
Make pg_ctl stop/restart/promote recheck postmaster aliveness.
"pg_ctl stop/restart" checked that the postmaster PID is valid justonce, as a side-effect of sending the stop signal, and then wouldwait-till-timeout for the postmaster.pid file to go away. Thisneglects the case wherein the postmaster dies uncleanly after wesignal it. Similarly, once "pg_ctl promote" has sent the signal,it'd wait for the corresponding on-disk state change to occureven if the postmaster dies.I'm not sure how we've managed not to notice this problem, but itseems to explain slow execution of the 017_shm.pl test script on AIXsince commit4fdbf9a, which added a speculative "pg_ctl stop" withthe idea of making real sure that the postmaster isn't there. In thetest steps that kill-9 and then restart the postmaster, it's possibleto get past the initial signal attempt before kill() stops workingfor the doomed postmaster. If that happens, pg_ctl waited tillPGCTLTIMEOUT before giving up ... and the buildfarm's AIX membershave that set very high.To fix, include a "kill(pid, 0)" test (similar to whatpostmaster_is_alive uses) in these wait loops, so that we'llgive up immediately if the postmaster PID disappears.While here, I chose to refactor those loops out of where they were.do_stop() and do_restart() can perfectly well share one copy of thewait-for-stop loop, and it seems desirable to put a similar functionbeside that for wait-for-promote.Back-patch to all supported versions, since pg_ctl's wait logicis substantially identical in all, and we're seeing the slow testbehavior in all branches.Discussion:https://postgr.es/m/20220210023537.GA3222837@rfd.leadboat.com
1 parenteec7c64 commitd0e1fd9

File tree

1 file changed

+80
-48
lines changed

1 file changed

+80
-48
lines changed

‎src/bin/pg_ctl/pg_ctl.c

Lines changed: 80 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ static void free_readfile(char **optlines);
155155
staticpgpid_tstart_postmaster(void);
156156
staticvoidread_post_opts(void);
157157

158-
staticWaitPMResultwait_for_postmaster(pgpid_tpm_pid,booldo_checkpoint);
158+
staticWaitPMResultwait_for_postmaster_start(pgpid_tpm_pid,booldo_checkpoint);
159+
staticboolwait_for_postmaster_stop(void);
160+
staticboolwait_for_postmaster_promote(void);
159161
staticboolpostmaster_is_alive(pid_tpid);
160162

161163
#if defined(HAVE_GETRLIMIT)&& defined(RLIMIT_CORE)
@@ -590,7 +592,7 @@ start_postmaster(void)
590592
* manager checkpoint, it's got nothing to do with database checkpoints!!
591593
*/
592594
staticWaitPMResult
593-
wait_for_postmaster(pgpid_tpm_pid,booldo_checkpoint)
595+
wait_for_postmaster_start(pgpid_tpm_pid,booldo_checkpoint)
594596
{
595597
inti;
596598

@@ -700,6 +702,76 @@ wait_for_postmaster(pgpid_t pm_pid, bool do_checkpoint)
700702
}
701703

702704

705+
/*
706+
* Wait for the postmaster to stop.
707+
*
708+
* Returns true if the postmaster stopped cleanly (i.e., removed its pidfile).
709+
* Returns false if the postmaster dies uncleanly, or if we time out.
710+
*/
711+
staticbool
712+
wait_for_postmaster_stop(void)
713+
{
714+
intcnt;
715+
716+
for (cnt=0;cnt<wait_seconds*WAITS_PER_SEC;cnt++)
717+
{
718+
pgpid_tpid;
719+
720+
if ((pid=get_pgpid(false))==0)
721+
return true;/* pid file is gone */
722+
723+
if (kill((pid_t)pid,0)!=0)
724+
{
725+
/*
726+
* Postmaster seems to have died. Check the pid file once more to
727+
* avoid a race condition, but give up waiting.
728+
*/
729+
if (get_pgpid(false)==0)
730+
return true;/* pid file is gone */
731+
return false;/* postmaster died untimely */
732+
}
733+
734+
if (cnt %WAITS_PER_SEC==0)
735+
print_msg(".");
736+
pg_usleep(USEC_PER_SEC /WAITS_PER_SEC);
737+
}
738+
return false;/* timeout reached */
739+
}
740+
741+
742+
/*
743+
* Wait for the postmaster to promote.
744+
*
745+
* Returns true on success, else false.
746+
* To avoid waiting uselessly, we check for postmaster death here too.
747+
*/
748+
staticbool
749+
wait_for_postmaster_promote(void)
750+
{
751+
intcnt;
752+
753+
for (cnt=0;cnt<wait_seconds*WAITS_PER_SEC;cnt++)
754+
{
755+
pgpid_tpid;
756+
DBStatestate;
757+
758+
if ((pid=get_pgpid(false))==0)
759+
return false;/* pid file is gone */
760+
if (kill((pid_t)pid,0)!=0)
761+
return false;/* postmaster died */
762+
763+
state=get_control_dbstate();
764+
if (state==DB_IN_PRODUCTION)
765+
return true;/* successful promotion */
766+
767+
if (cnt %WAITS_PER_SEC==0)
768+
print_msg(".");
769+
pg_usleep(USEC_PER_SEC /WAITS_PER_SEC);
770+
}
771+
return false;/* timeout reached */
772+
}
773+
774+
703775
#if defined(HAVE_GETRLIMIT)&& defined(RLIMIT_CORE)
704776
staticvoid
705777
unlimit_core_size(void)
@@ -914,7 +986,7 @@ do_start(void)
914986

915987
print_msg(_("waiting for server to start..."));
916988

917-
switch (wait_for_postmaster(pm_pid, false))
989+
switch (wait_for_postmaster_start(pm_pid, false))
918990
{
919991
casePOSTMASTER_READY:
920992
print_msg(_(" done\n"));
@@ -949,7 +1021,6 @@ do_start(void)
9491021
staticvoid
9501022
do_stop(void)
9511023
{
952-
intcnt;
9531024
pgpid_tpid;
9541025
structstatstatbuf;
9551026

@@ -1000,19 +1071,7 @@ do_stop(void)
10001071

10011072
print_msg(_("waiting for server to shut down..."));
10021073

1003-
for (cnt=0;cnt<wait_seconds*WAITS_PER_SEC;cnt++)
1004-
{
1005-
if ((pid=get_pgpid(false))!=0)
1006-
{
1007-
if (cnt %WAITS_PER_SEC==0)
1008-
print_msg(".");
1009-
pg_usleep(USEC_PER_SEC /WAITS_PER_SEC);
1010-
}
1011-
else
1012-
break;
1013-
}
1014-
1015-
if (pid!=0)/* pid file still exists */
1074+
if (!wait_for_postmaster_stop())
10161075
{
10171076
print_msg(_(" failed\n"));
10181077

@@ -1036,7 +1095,6 @@ do_stop(void)
10361095
staticvoid
10371096
do_restart(void)
10381097
{
1039-
intcnt;
10401098
pgpid_tpid;
10411099
structstatstatbuf;
10421100

@@ -1090,20 +1148,7 @@ do_restart(void)
10901148
print_msg(_("waiting for server to shut down..."));
10911149

10921150
/* always wait for restart */
1093-
1094-
for (cnt=0;cnt<wait_seconds*WAITS_PER_SEC;cnt++)
1095-
{
1096-
if ((pid=get_pgpid(false))!=0)
1097-
{
1098-
if (cnt %WAITS_PER_SEC==0)
1099-
print_msg(".");
1100-
pg_usleep(USEC_PER_SEC /WAITS_PER_SEC);
1101-
}
1102-
else
1103-
break;
1104-
}
1105-
1106-
if (pid!=0)/* pid file still exists */
1151+
if (!wait_for_postmaster_stop())
11071152
{
11081153
print_msg(_(" failed\n"));
11091154

@@ -1228,21 +1273,8 @@ do_promote(void)
12281273

12291274
if (do_wait)
12301275
{
1231-
DBStatestate=DB_STARTUP;
1232-
intcnt;
1233-
12341276
print_msg(_("waiting for server to promote..."));
1235-
for (cnt=0;cnt<wait_seconds*WAITS_PER_SEC;cnt++)
1236-
{
1237-
state=get_control_dbstate();
1238-
if (state==DB_IN_PRODUCTION)
1239-
break;
1240-
1241-
if (cnt %WAITS_PER_SEC==0)
1242-
print_msg(".");
1243-
pg_usleep(USEC_PER_SEC /WAITS_PER_SEC);
1244-
}
1245-
if (state==DB_IN_PRODUCTION)
1277+
if (wait_for_postmaster_promote())
12461278
{
12471279
print_msg(_(" done\n"));
12481280
print_msg(_("server promoted\n"));
@@ -1661,7 +1693,7 @@ pgwin32_ServiceMain(DWORD argc, LPTSTR *argv)
16611693
if (do_wait)
16621694
{
16631695
write_eventlog(EVENTLOG_INFORMATION_TYPE,_("Waiting for server startup...\n"));
1664-
if (wait_for_postmaster(postmasterPID, true)!=POSTMASTER_READY)
1696+
if (wait_for_postmaster_start(postmasterPID, true)!=POSTMASTER_READY)
16651697
{
16661698
write_eventlog(EVENTLOG_ERROR_TYPE,_("Timed out waiting for server startup\n"));
16671699
pgwin32_SetServiceStatus(SERVICE_STOPPED);
@@ -1682,7 +1714,7 @@ pgwin32_ServiceMain(DWORD argc, LPTSTR *argv)
16821714
{
16831715
/*
16841716
* status.dwCheckPoint can be incremented by
1685-
*wait_for_postmaster(), so it might not start from 0.
1717+
*wait_for_postmaster_start(), so it might not start from 0.
16861718
*/
16871719
intmaxShutdownCheckPoint=status.dwCheckPoint+12;
16881720

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp