Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 11a65ee

Browse files
committed
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.
1 parent 0886fc6 commit 11a65ee

File tree

6 files changed

+71
-166
lines changed

6 files changed

+71
-166
lines changed

src/backend/port/sysv_shmem.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include "miscadmin.h"
3232
#include "portability/mem.h"
33+
#include "storage/dsm.h"
3334
#include "storage/ipc.h"
3435
#include "storage/pg_shmem.h"
3536
#include "utils/guc.h"
@@ -421,7 +422,8 @@ CreateAnonymousSegment(Size *size)
421422
* zero will be passed.
422423
*/
423424
PGShmemHeader *
424-
PGSharedMemoryCreate(Size size, bool makePrivate, int port)
425+
PGSharedMemoryCreate(Size size, bool makePrivate, int port,
426+
PGShmemHeader **shim)
425427
{
426428
IpcMemoryKey NextShmemSegID;
427429
void *memAddress;
@@ -509,10 +511,13 @@ PGSharedMemoryCreate(Size size, bool makePrivate, int port)
509511

510512
/*
511513
* The segment appears to be from a dead Postgres process, or from a
512-
* previous cycle of life in this same process. Zap it, if possible.
514+
* previous cycle of life in this same process. Zap it, if possible,
515+
* and any associated dynamic shared memory segments, as well.
513516
* This probably shouldn't fail, but if it does, assume the segment
514517
* belongs to someone else after all, and continue quietly.
515518
*/
519+
if (hdr->dsm_control != 0)
520+
dsm_cleanup_using_control_segment(hdr->dsm_control);
516521
shmdt(memAddress);
517522
if (shmctl(shmid, IPC_RMID, NULL) < 0)
518523
continue;
@@ -539,6 +544,7 @@ PGSharedMemoryCreate(Size size, bool makePrivate, int port)
539544
hdr = (PGShmemHeader *) memAddress;
540545
hdr->creatorPID = getpid();
541546
hdr->magic = PGShmemMagic;
547+
hdr->dsm_control = 0;
542548

543549
/* Fill in the data directory ID info, too */
544550
if (stat(DataDir, &statbuf) < 0)
@@ -554,6 +560,7 @@ PGSharedMemoryCreate(Size size, bool makePrivate, int port)
554560
*/
555561
hdr->totalsize = size;
556562
hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
563+
*shim = hdr;
557564

558565
/* Save info for possible future use */
559566
UsedShmemSegAddr = memAddress;
@@ -608,6 +615,7 @@ PGSharedMemoryReAttach(void)
608615
if (hdr != origUsedShmemSegAddr)
609616
elog(FATAL, "reattaching to shared memory returned unexpected address (got %p, expected %p)",
610617
hdr, origUsedShmemSegAddr);
618+
dsm_set_control_handle(((PGShmemHeader *) hdr)->dsm_control);
611619

612620
UsedShmemSegAddr = hdr; /* probably redundant */
613621
}

src/backend/port/win32_shmem.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2)
117117
*
118118
*/
119119
PGShmemHeader *
120-
PGSharedMemoryCreate(Size size, bool makePrivate, int port)
120+
PGSharedMemoryCreate(Size size, bool makePrivate, int port,
121+
PGShmemHeader **shim)
121122
{
122123
void *memAddress;
123124
PGShmemHeader *hdr;
@@ -245,12 +246,14 @@ PGSharedMemoryCreate(Size size, bool makePrivate, int port)
245246
*/
246247
hdr->totalsize = size;
247248
hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
249+
hdr->dsm_control = 0;
248250

249251
/* Save info for possible future use */
250252
UsedShmemSegAddr = memAddress;
251253
UsedShmemSegSize = size;
252254
UsedShmemSegID = hmap2;
253255

256+
*shim = NULL;
254257
return hdr;
255258
}
256259

@@ -289,6 +292,7 @@ PGSharedMemoryReAttach(void)
289292
hdr, origUsedShmemSegAddr);
290293
if (hdr->magic != PGShmemMagic)
291294
elog(FATAL, "reattaching to shared memory returned non-PostgreSQL memory");
295+
dsm_set_control_handle(hdr->dsm_control);
292296

293297
UsedShmemSegAddr = hdr; /* probably redundant */
294298
}

0 commit comments

Comments
 (0)