Skip to content

Commit 402da70

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 00011a6 commit 402da70

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/backend/bootstrap/bootstrap.c

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

203203
MyStartTime = time(NULL);
204204

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

src/backend/tcop/postgres.c

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

36223622
MyStartTime = time(NULL);
3623+
3624+
/*
3625+
* Initialize random() for the first time, like PostmasterMain()
3626+
* would. In a regular IsUnderPostmaster backend, BackendRun()
3627+
* computes a high-entropy seed before any user query. Fewer distinct
3628+
* initial seeds can occur here.
3629+
*/
3630+
srandom((unsigned int) (MyProcPid ^ MyStartTime));
36233631
}
36243632

36253633
SetProcessingMode(InitProcessing);

0 commit comments

Comments
 (0)