mirror of
https://github.com/postgres/postgres.git
synced 2025-11-07 19:06:32 +03:00
Use data directory inode number, not port, to select SysV resource keys.
This approach provides a much tighter binding between a data directory and the associated SysV shared memory block (and SysV or named-POSIX semaphores, if we're using those). Key collisions are still possible, but only between data directories stored on different filesystems, so the situation should be negligible in practice. More importantly, restarting the postmaster with a different port number no longer risks failing to identify a relevant shared memory block, even when postmaster.pid has been removed. A standalone backend is likewise much more certain to detect conflicting leftover backends. (In the longer term, we might now think about deprecating the port as a cluster-wide value, so that one postmaster could support sockets with varying port numbers. But that's for another day.) The hazards fixed here apply only on Unix systems; our Windows code paths already use identifiers derived from the data directory path name rather than the port. src/test/recovery/t/017_shm.pl, which intends to test key-collision cases, has been substantially rewritten since it can no longer use two postmasters with identical port numbers to trigger the case. Instead, use Perl's IPC::SharedMem module to create a conflicting shmem segment directly. The test script will be skipped if that module is not available. (This means that some older buildfarm members won't run it, but I don't think that that results in any meaningful coverage loss.) Patch by me; thanks to Noah Misch and Peter Eisentraut for discussion and review. Discussion: https://postgr.es/m/16908.1557521200@sss.pgh.pa.us
This commit is contained in:
@@ -385,7 +385,7 @@ static void getInstallationPaths(const char *argv0);
|
||||
static void checkControlFile(void);
|
||||
static Port *ConnCreate(int serverFd);
|
||||
static void ConnFree(Port *port);
|
||||
static void reset_shared(int port);
|
||||
static void reset_shared(void);
|
||||
static void SIGHUP_handler(SIGNAL_ARGS);
|
||||
static void pmdie(SIGNAL_ARGS);
|
||||
static void reaper(SIGNAL_ARGS);
|
||||
@@ -1175,7 +1175,7 @@ PostmasterMain(int argc, char *argv[])
|
||||
/*
|
||||
* Set up shared memory and semaphores.
|
||||
*/
|
||||
reset_shared(PostPortNumber);
|
||||
reset_shared();
|
||||
|
||||
/*
|
||||
* Estimate number of openable files. This must happen after setting up
|
||||
@@ -2599,17 +2599,16 @@ InitProcessGlobals(void)
|
||||
* reset_shared -- reset shared memory and semaphores
|
||||
*/
|
||||
static void
|
||||
reset_shared(int port)
|
||||
reset_shared(void)
|
||||
{
|
||||
/*
|
||||
* Create or re-create shared memory and semaphores.
|
||||
*
|
||||
* Note: in each "cycle of life" we will normally assign the same IPC keys
|
||||
* (if using SysV shmem and/or semas), since the port number is used to
|
||||
* determine IPC keys. This helps ensure that we will clean up dead IPC
|
||||
* objects if the postmaster crashes and is restarted.
|
||||
* (if using SysV shmem and/or semas). This helps ensure that we will
|
||||
* clean up dead IPC objects if the postmaster crashes and is restarted.
|
||||
*/
|
||||
CreateSharedMemoryAndSemaphores(port);
|
||||
CreateSharedMemoryAndSemaphores();
|
||||
}
|
||||
|
||||
|
||||
@@ -3934,7 +3933,7 @@ PostmasterStateMachine(void)
|
||||
/* re-read control file into local memory */
|
||||
LocalProcessControlFile(true);
|
||||
|
||||
reset_shared(PostPortNumber);
|
||||
reset_shared();
|
||||
|
||||
StartupPID = StartupDataBase();
|
||||
Assert(StartupPID != 0);
|
||||
@@ -4962,7 +4961,7 @@ SubPostmasterMain(int argc, char *argv[])
|
||||
InitProcess();
|
||||
|
||||
/* Attach process to shared data structures */
|
||||
CreateSharedMemoryAndSemaphores(0);
|
||||
CreateSharedMemoryAndSemaphores();
|
||||
|
||||
/* And run the backend */
|
||||
BackendRun(&port); /* does not return */
|
||||
@@ -4976,7 +4975,7 @@ SubPostmasterMain(int argc, char *argv[])
|
||||
InitAuxiliaryProcess();
|
||||
|
||||
/* Attach process to shared data structures */
|
||||
CreateSharedMemoryAndSemaphores(0);
|
||||
CreateSharedMemoryAndSemaphores();
|
||||
|
||||
AuxiliaryProcessMain(argc - 2, argv + 2); /* does not return */
|
||||
}
|
||||
@@ -4989,7 +4988,7 @@ SubPostmasterMain(int argc, char *argv[])
|
||||
InitProcess();
|
||||
|
||||
/* Attach process to shared data structures */
|
||||
CreateSharedMemoryAndSemaphores(0);
|
||||
CreateSharedMemoryAndSemaphores();
|
||||
|
||||
AutoVacLauncherMain(argc - 2, argv + 2); /* does not return */
|
||||
}
|
||||
@@ -5002,7 +5001,7 @@ SubPostmasterMain(int argc, char *argv[])
|
||||
InitProcess();
|
||||
|
||||
/* Attach process to shared data structures */
|
||||
CreateSharedMemoryAndSemaphores(0);
|
||||
CreateSharedMemoryAndSemaphores();
|
||||
|
||||
AutoVacWorkerMain(argc - 2, argv + 2); /* does not return */
|
||||
}
|
||||
@@ -5020,7 +5019,7 @@ SubPostmasterMain(int argc, char *argv[])
|
||||
InitProcess();
|
||||
|
||||
/* Attach process to shared data structures */
|
||||
CreateSharedMemoryAndSemaphores(0);
|
||||
CreateSharedMemoryAndSemaphores();
|
||||
|
||||
/* Fetch MyBgworkerEntry from shared memory */
|
||||
shmem_slot = atoi(argv[1] + 15);
|
||||
|
||||
Reference in New Issue
Block a user