Skip to content

Commit 4012281

Browse files
committed
Initialize random() in bootstrap/stand-alone postgres and in initdb.
This removes a difference between the standard IsUnderPostmaster execution environment and that of --boot and --single. In a stand-alone backend, "SELECT random()" always started at the same seed. On a system capable of using posix shared memory, initdb could still conclude "selecting dynamic shared memory implementation ... sysv". Crashed --boot or --single postgres processes orphaned shared memory objects having names that collided with the not-actually-random names that initdb probed. The sysv fallback appeared after ten crashes of --boot or --single postgres. Since --boot and --single are rare in production use, systems used for PostgreSQL development are the principal candidate to notice this symptom. Back-patch to 9.3 (all supported versions). PostgreSQL 9.4 introduced dynamic shared memory, but 9.3 does share the "SELECT random()" problem. Reviewed by Tom Lane and Kyotaro HORIGUCHI. Discussion: https://postgr.es/m/20180915221546.GA3159382@rfd.leadboat.com
1 parent 38cb010 commit 4012281

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

src/backend/bootstrap/bootstrap.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ AuxiliaryProcessMain(int argc, char *argv[])
197197

198198
MyStartTime = time(NULL);
199199

200+
/*
201+
* Initialize random() for the first time, like PostmasterMain() would.
202+
* In a regular IsUnderPostmaster backend, BackendRun() computes a
203+
* high-entropy seed before any user query. Fewer distinct initial seeds
204+
* can occur here.
205+
*/
206+
srandom((unsigned int) (MyProcPid ^ MyStartTime));
207+
200208
/* Compute paths, if we didn't inherit them from postmaster */
201209
if (my_exec_path[0] == '\0')
202210
{

src/backend/tcop/postgres.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3611,6 +3611,14 @@ PostgresMain(int argc, char *argv[],
36113611
MyProcPid = getpid();
36123612

36133613
MyStartTime = time(NULL);
3614+
3615+
/*
3616+
* Initialize random() for the first time, like PostmasterMain()
3617+
* would. In a regular IsUnderPostmaster backend, BackendRun()
3618+
* computes a high-entropy seed before any user query. Fewer distinct
3619+
* initial seeds can occur here.
3620+
*/
3621+
srandom((unsigned int) (MyProcPid ^ MyStartTime));
36143622
}
36153623

36163624
SetProcessingMode(InitProcessing);

src/bin/initdb/initdb.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,9 @@ choose_dsm_implementation(void)
10551055
#ifdef HAVE_SHM_OPEN
10561056
int ntries = 10;
10571057

1058+
/* Initialize random(); this function is its only user in this program. */
1059+
srandom((unsigned int) (getpid() ^ time(NULL)));
1060+
10581061
while (ntries > 0)
10591062
{
10601063
uint32 handle;

0 commit comments

Comments
 (0)