mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
Get rid of the dynamic shared memory state file.
Instead of storing the ID of the dynamic shared memory control segment in a file within the data directory, store it in the main control segment. This avoids a number of nasty corner cases, most seriously that doing an online backup and then using it on the same machine (e.g. to fire up a standby) would result in the standby clobbering all of the master's dynamic shared memory segments. Per complaints from Heikki Linnakangas, Fujii Masao, and Tom Lane.
This commit is contained in:
@ -30,6 +30,7 @@
|
||||
|
||||
#include "miscadmin.h"
|
||||
#include "portability/mem.h"
|
||||
#include "storage/dsm.h"
|
||||
#include "storage/ipc.h"
|
||||
#include "storage/pg_shmem.h"
|
||||
#include "utils/guc.h"
|
||||
@ -421,7 +422,8 @@ CreateAnonymousSegment(Size *size)
|
||||
* zero will be passed.
|
||||
*/
|
||||
PGShmemHeader *
|
||||
PGSharedMemoryCreate(Size size, bool makePrivate, int port)
|
||||
PGSharedMemoryCreate(Size size, bool makePrivate, int port,
|
||||
PGShmemHeader **shim)
|
||||
{
|
||||
IpcMemoryKey NextShmemSegID;
|
||||
void *memAddress;
|
||||
@ -509,10 +511,13 @@ PGSharedMemoryCreate(Size size, bool makePrivate, int port)
|
||||
|
||||
/*
|
||||
* The segment appears to be from a dead Postgres process, or from a
|
||||
* previous cycle of life in this same process. Zap it, if possible.
|
||||
* previous cycle of life in this same process. Zap it, if possible,
|
||||
* and any associated dynamic shared memory segments, as well.
|
||||
* This probably shouldn't fail, but if it does, assume the segment
|
||||
* belongs to someone else after all, and continue quietly.
|
||||
*/
|
||||
if (hdr->dsm_control != 0)
|
||||
dsm_cleanup_using_control_segment(hdr->dsm_control);
|
||||
shmdt(memAddress);
|
||||
if (shmctl(shmid, IPC_RMID, NULL) < 0)
|
||||
continue;
|
||||
@ -539,6 +544,7 @@ PGSharedMemoryCreate(Size size, bool makePrivate, int port)
|
||||
hdr = (PGShmemHeader *) memAddress;
|
||||
hdr->creatorPID = getpid();
|
||||
hdr->magic = PGShmemMagic;
|
||||
hdr->dsm_control = 0;
|
||||
|
||||
/* Fill in the data directory ID info, too */
|
||||
if (stat(DataDir, &statbuf) < 0)
|
||||
@ -554,6 +560,7 @@ PGSharedMemoryCreate(Size size, bool makePrivate, int port)
|
||||
*/
|
||||
hdr->totalsize = size;
|
||||
hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
|
||||
*shim = hdr;
|
||||
|
||||
/* Save info for possible future use */
|
||||
UsedShmemSegAddr = memAddress;
|
||||
@ -608,6 +615,7 @@ PGSharedMemoryReAttach(void)
|
||||
if (hdr != origUsedShmemSegAddr)
|
||||
elog(FATAL, "reattaching to shared memory returned unexpected address (got %p, expected %p)",
|
||||
hdr, origUsedShmemSegAddr);
|
||||
dsm_set_control_handle(((PGShmemHeader *) hdr)->dsm_control);
|
||||
|
||||
UsedShmemSegAddr = hdr; /* probably redundant */
|
||||
}
|
||||
|
Reference in New Issue
Block a user