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

Commit f8a769b

Browse files
committed
Cause stats processes to detach from shared memory when started, so that
they do not prevent the postmaster from deleting the shmem segment during a post-backend-crash restart cycle. Per recent discussion.
1 parent 7e4a629 commit f8a769b

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

src/backend/port/sysv_shmem.c

+28-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Portions Copyright (c) 1994, Regents of the University of California
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/port/sysv_shmem.c,v 1.24 2003/10/27 18:30:07 momjian Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/port/sysv_shmem.c,v 1.25 2003/11/07 21:55:49 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -134,7 +134,7 @@ InternalIpcMemoryCreate(IpcMemoryKey memKey, uint32 size)
134134

135135
/* OK, should be able to attach to the segment */
136136
#ifdef SHM_SHARE_MMU
137-
/* use intimate shared memory on SPARC Solaris */
137+
/* use intimate shared memory on Solaris */
138138
memAddress = shmat(shmid, 0, SHM_SHARE_MMU);
139139
#else
140140
memAddress = shmat(shmid, 0, 0);
@@ -244,7 +244,7 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
244244
/* Room for a header? */
245245
Assert(size > MAXALIGN(sizeof(PGShmemHeader)));
246246

247-
/* Just attach and return the pointer */
247+
/* If Exec case, just attach and return the pointer */
248248
if (ExecBackend && UsedShmemSegAddr != NULL && !makePrivate)
249249
{
250250
if ((hdr = PGSharedMemoryAttach(UsedShmemSegID, &shmid)) == NULL)
@@ -253,6 +253,9 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
253253
return hdr;
254254
}
255255

256+
/* Make sure PGSharedMemoryAttach doesn't fail without need */
257+
UsedShmemSegAddr = NULL;
258+
256259
/* Loop till we find a free IPC key */
257260
NextShmemSegID = port * 1000;
258261

@@ -326,16 +329,32 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
326329
hdr->totalsize = size;
327330
hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
328331

329-
330-
if (ExecBackend && UsedShmemSegAddr == NULL && !makePrivate)
331-
{
332-
UsedShmemSegAddr = memAddress;
333-
UsedShmemSegID = NextShmemSegID;
334-
}
332+
/* Save info for possible future use */
333+
UsedShmemSegAddr = memAddress;
334+
UsedShmemSegID = NextShmemSegID;
335335

336336
return hdr;
337337
}
338338

339+
/*
340+
* PGSharedMemoryDetach
341+
*
342+
* Detach from the shared memory segment, if still attached. This is not
343+
* intended for use by the process that originally created the segment
344+
* (it will have an on_shmem_exit callback registered to do that). Rather,
345+
* this is for subprocesses that have inherited an attachment and want to
346+
* get rid of it.
347+
*/
348+
void
349+
PGSharedMemoryDetach(void)
350+
{
351+
if (UsedShmemSegAddr != NULL)
352+
{
353+
if (shmdt(UsedShmemSegAddr) < 0)
354+
elog(LOG, "shmdt(%p) failed: %m", UsedShmemSegAddr);
355+
UsedShmemSegAddr = NULL;
356+
}
357+
}
339358

340359
/*
341360
* Attach to shared memory and make sure it has a Postgres header

src/backend/postmaster/pgstat.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* Copyright (c) 2001-2003, PostgreSQL Global Development Group
1515
*
16-
* $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.45 2003/09/25 06:58:01 petere Exp $
16+
* $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.46 2003/11/07 21:55:50 tgl Exp $
1717
* ----------
1818
*/
1919
#include "postgres.h"
@@ -44,6 +44,7 @@
4444
#include "utils/memutils.h"
4545
#include "storage/backendid.h"
4646
#include "storage/ipc.h"
47+
#include "storage/pg_shmem.h"
4748
#include "utils/rel.h"
4849
#include "utils/hsearch.h"
4950
#include "utils/ps_status.h"
@@ -400,6 +401,9 @@ pgstat_start(void)
400401
/* Close the postmaster's sockets, except for pgstat link */
401402
ClosePostmasterPorts(false);
402403

404+
/* Drop our connection to postmaster's shared memory, as well */
405+
PGSharedMemoryDetach();
406+
403407
pgstat_main();
404408

405409
exit(0);

src/include/storage/pg_shmem.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
1818
* Portions Copyright (c) 1994, Regents of the University of California
1919
*
20-
* $Id: pg_shmem.h,v 1.7 2003/08/04 02:40:15 momjian Exp $
20+
* $Id: pg_shmem.h,v 1.8 2003/11/07 21:55:50 tgl Exp $
2121
*
2222
*-------------------------------------------------------------------------
2323
*/
@@ -44,5 +44,6 @@ extern void *UsedShmemSegAddr;
4444
extern PGShmemHeader *PGSharedMemoryCreate(uint32 size, bool makePrivate,
4545
int port);
4646
extern bool PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2);
47+
extern void PGSharedMemoryDetach(void);
4748

4849
#endif /* PG_SHMEM_H */

0 commit comments

Comments
 (0)