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

Commita745b93

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 parentea09a75 commita745b93

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)
@@ -594,7 +596,7 @@ start_postmaster(void)
594596
* manager checkpoint, it's got nothing to do with database checkpoints!!
595597
*/
596598
staticWaitPMResult
597-
wait_for_postmaster(pgpid_tpm_pid,booldo_checkpoint)
599+
wait_for_postmaster_start(pgpid_tpm_pid,booldo_checkpoint)
598600
{
599601
inti;
600602

@@ -704,6 +706,76 @@ wait_for_postmaster(pgpid_t pm_pid, bool do_checkpoint)
704706
}
705707

706708

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

918990
print_msg(_("waiting for server to start..."));
919991

920-
switch (wait_for_postmaster(pm_pid, false))
992+
switch (wait_for_postmaster_start(pm_pid, false))
921993
{
922994
casePOSTMASTER_READY:
923995
print_msg(_(" done\n"));
@@ -952,7 +1024,6 @@ do_start(void)
9521024
staticvoid
9531025
do_stop(void)
9541026
{
955-
intcnt;
9561027
pgpid_tpid;
9571028
structstatstatbuf;
9581029

@@ -1003,19 +1074,7 @@ do_stop(void)
10031074

10041075
print_msg(_("waiting for server to shut down..."));
10051076

1006-
for (cnt=0;cnt<wait_seconds*WAITS_PER_SEC;cnt++)
1007-
{
1008-
if ((pid=get_pgpid(false))!=0)
1009-
{
1010-
if (cnt %WAITS_PER_SEC==0)
1011-
print_msg(".");
1012-
pg_usleep(USEC_PER_SEC /WAITS_PER_SEC);
1013-
}
1014-
else
1015-
break;
1016-
}
1017-
1018-
if (pid!=0)/* pid file still exists */
1077+
if (!wait_for_postmaster_stop())
10191078
{
10201079
print_msg(_(" failed\n"));
10211080

@@ -1039,7 +1098,6 @@ do_stop(void)
10391098
staticvoid
10401099
do_restart(void)
10411100
{
1042-
intcnt;
10431101
pgpid_tpid;
10441102
structstatstatbuf;
10451103

@@ -1093,20 +1151,7 @@ do_restart(void)
10931151
print_msg(_("waiting for server to shut down..."));
10941152

10951153
/* always wait for restart */
1096-
1097-
for (cnt=0;cnt<wait_seconds*WAITS_PER_SEC;cnt++)
1098-
{
1099-
if ((pid=get_pgpid(false))!=0)
1100-
{
1101-
if (cnt %WAITS_PER_SEC==0)
1102-
print_msg(".");
1103-
pg_usleep(USEC_PER_SEC /WAITS_PER_SEC);
1104-
}
1105-
else
1106-
break;
1107-
}
1108-
1109-
if (pid!=0)/* pid file still exists */
1154+
if (!wait_for_postmaster_stop())
11101155
{
11111156
print_msg(_(" failed\n"));
11121157

@@ -1226,21 +1271,8 @@ do_promote(void)
12261271

12271272
if (do_wait)
12281273
{
1229-
DBStatestate=DB_STARTUP;
1230-
intcnt;
1231-
12321274
print_msg(_("waiting for server to promote..."));
1233-
for (cnt=0;cnt<wait_seconds*WAITS_PER_SEC;cnt++)
1234-
{
1235-
state=get_control_dbstate();
1236-
if (state==DB_IN_PRODUCTION)
1237-
break;
1238-
1239-
if (cnt %WAITS_PER_SEC==0)
1240-
print_msg(".");
1241-
pg_usleep(USEC_PER_SEC /WAITS_PER_SEC);
1242-
}
1243-
if (state==DB_IN_PRODUCTION)
1275+
if (wait_for_postmaster_promote())
12441276
{
12451277
print_msg(_(" done\n"));
12461278
print_msg(_("server promoted\n"));
@@ -1659,7 +1691,7 @@ pgwin32_ServiceMain(DWORD argc, LPTSTR *argv)
16591691
if (do_wait)
16601692
{
16611693
write_eventlog(EVENTLOG_INFORMATION_TYPE,_("Waiting for server startup...\n"));
1662-
if (wait_for_postmaster(postmasterPID, true)!=POSTMASTER_READY)
1694+
if (wait_for_postmaster_start(postmasterPID, true)!=POSTMASTER_READY)
16631695
{
16641696
write_eventlog(EVENTLOG_ERROR_TYPE,_("Timed out waiting for server startup\n"));
16651697
pgwin32_SetServiceStatus(SERVICE_STOPPED);
@@ -1680,7 +1712,7 @@ pgwin32_ServiceMain(DWORD argc, LPTSTR *argv)
16801712
{
16811713
/*
16821714
* status.dwCheckPoint can be incremented by
1683-
*wait_for_postmaster(), so it might not start from 0.
1715+
*wait_for_postmaster_start(), so it might not start from 0.
16841716
*/
16851717
intmaxShutdownCheckPoint=status.dwCheckPoint+12;
16861718

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp