Skip to content

Commit 0b9d3d4

Browse files
committed
Fix a problem in my recent patch to initialize cancel_key for autovac workers
as well as regular backends: if no regular backend launches before the autovac launcher tries to start an autovac worker, the postmaster would get an Assert fault due to calling PostmasterRandom before random_seed was initialized. Cleanest solution seems to be to take the initialization of random_seed out of ServerLoop and let PostmasterRandom do it for itself.
1 parent bdd6b62 commit 0b9d3d4

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.538 2007/08/03 20:06:50 tgl Exp $
40+
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.539 2007/08/04 03:15:49 tgl Exp $
4141
*
4242
* NOTES
4343
*
@@ -230,6 +230,7 @@ static volatile sig_atomic_t start_autovac_launcher = false;
230230
* backend from the postmaster to that backend (via fork).
231231
*/
232232
static unsigned int random_seed = 0;
233+
static struct timeval random_start_time;
233234

234235
extern char *optarg;
235236
extern int optind,
@@ -966,6 +967,8 @@ PostmasterMain(int argc, char *argv[])
966967
* Remember postmaster startup time
967968
*/
968969
PgStartTime = GetCurrentTimestamp();
970+
/* PostmasterRandom wants its own copy */
971+
gettimeofday(&random_start_time, NULL);
969972

970973
/*
971974
* We're ready to rock and roll...
@@ -1140,10 +1143,7 @@ ServerLoop(void)
11401143
int nSockets;
11411144
time_t now,
11421145
last_touch_time;
1143-
struct timeval earlier,
1144-
later;
11451146

1146-
gettimeofday(&earlier, NULL);
11471147
last_touch_time = time(NULL);
11481148

11491149
nSockets = initMasks(&readmask);
@@ -1194,24 +1194,6 @@ ServerLoop(void)
11941194
*/
11951195
if (selres > 0)
11961196
{
1197-
/*
1198-
* Select a random seed at the time of first receiving a request.
1199-
*/
1200-
while (random_seed == 0)
1201-
{
1202-
gettimeofday(&later, NULL);
1203-
1204-
/*
1205-
* We are not sure how much precision is in tv_usec, so we
1206-
* swap the high and low 16 bits of 'later' and XOR them with
1207-
* 'earlier'. On the off chance that the result is 0, we loop
1208-
* until it isn't.
1209-
*/
1210-
random_seed = earlier.tv_usec ^
1211-
((later.tv_usec << 16) |
1212-
((later.tv_usec >> 16) & 0xffff));
1213-
}
1214-
12151197
for (i = 0; i < MAXLISTEN; i++)
12161198
{
12171199
if (ListenSocket[i] == -1)
@@ -2970,6 +2952,7 @@ BackendRun(Port *port)
29702952
* a new random sequence in the random() library function.
29712953
*/
29722954
random_seed = 0;
2955+
random_start_time.tv_usec = 0;
29732956
/* slightly hacky way to get integer microseconds part of timestamptz */
29742957
TimestampDifference(0, port->SessionStartTime, &secs, &usecs);
29752958
srandom((unsigned int) (MyProcPid ^ usecs));
@@ -3778,13 +3761,29 @@ RandomSalt(char *cryptSalt, char *md5Salt)
37783761
static long
37793762
PostmasterRandom(void)
37803763
{
3781-
static bool initialized = false;
3782-
3783-
if (!initialized)
3764+
/*
3765+
* Select a random seed at the time of first receiving a request.
3766+
*/
3767+
if (random_seed == 0)
37843768
{
3785-
Assert(random_seed != 0);
3769+
do
3770+
{
3771+
struct timeval random_stop_time;
3772+
3773+
gettimeofday(&random_stop_time, NULL);
3774+
/*
3775+
* We are not sure how much precision is in tv_usec, so we swap
3776+
* the high and low 16 bits of 'random_stop_time' and XOR them
3777+
* with 'random_start_time'. On the off chance that the result is
3778+
* 0, we loop until it isn't.
3779+
*/
3780+
random_seed = random_start_time.tv_usec ^
3781+
((random_stop_time.tv_usec << 16) |
3782+
((random_stop_time.tv_usec >> 16) & 0xffff));
3783+
}
3784+
while (random_seed == 0);
3785+
37863786
srandom(random_seed);
3787-
initialized = true;
37883787
}
37893788

37903789
return random();

0 commit comments

Comments
 (0)