aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2003-11-30 21:56:36 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2003-11-30 21:56:36 +0000
commita0547f6ea2cd86264ae7931c9128ba241b8c9327 (patch)
treea7744445ccf702ad43d4564866dea3b840c670d9 /src
parent8582f21afdb0a9241b915e56fc2ddb50dcd0509c (diff)
downloadpostgresql-a0547f6ea2cd86264ae7931c9128ba241b8c9327.tar.gz
postgresql-a0547f6ea2cd86264ae7931c9128ba241b8c9327.zip
Back-patch fix to cause stats processes to detach from shared memory,
so that they do not prevent the postmaster from deleting the shmem segment during crash recovery.
Diffstat (limited to 'src')
-rw-r--r--src/backend/port/sysv_shmem.c29
-rw-r--r--src/backend/postmaster/pgstat.c6
-rw-r--r--src/include/storage/pg_shmem.h3
3 files changed, 35 insertions, 3 deletions
diff --git a/src/backend/port/sysv_shmem.c b/src/backend/port/sysv_shmem.c
index 60e5d0b478b..94e285ff2b1 100644
--- a/src/backend/port/sysv_shmem.c
+++ b/src/backend/port/sysv_shmem.c
@@ -10,7 +10,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/port/sysv_shmem.c,v 1.4 2002/09/04 20:31:24 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/port/sysv_shmem.c,v 1.4.2.1 2003/11/30 21:56:36 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -41,6 +41,9 @@ typedef int IpcMemoryId; /* shared memory ID returned by shmget(2) */
#define IPCProtection (0600) /* access/modify by user only */
+static IpcMemoryKey UsedShmemSegID = 0;
+static void *UsedShmemSegAddr = NULL;
+
static void *InternalIpcMemoryCreate(IpcMemoryKey memKey, uint32 size);
static void IpcMemoryDetach(int status, Datum shmaddr);
static void IpcMemoryDelete(int status, Datum shmId);
@@ -395,5 +398,29 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
hdr->totalsize = size;
hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
+ /* Save info for possible future use */
+ UsedShmemSegAddr = memAddress;
+ UsedShmemSegID = NextShmemSegID;
+
return hdr;
}
+
+/*
+ * PGSharedMemoryDetach
+ *
+ * Detach from the shared memory segment, if still attached. This is not
+ * intended for use by the process that originally created the segment
+ * (it will have an on_shmem_exit callback registered to do that). Rather,
+ * this is for subprocesses that have inherited an attachment and want to
+ * get rid of it.
+ */
+void
+PGSharedMemoryDetach(void)
+{
+ if (UsedShmemSegAddr != NULL)
+ {
+ if (shmdt(UsedShmemSegAddr) < 0)
+ elog(LOG, "shmdt(%p) failed: %m", UsedShmemSegAddr);
+ UsedShmemSegAddr = NULL;
+ }
+}
diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index 128d3b74b62..513d57101bc 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -16,7 +16,7 @@
*
* Copyright (c) 2001, PostgreSQL Global Development Group
*
- * $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.31.2.2 2003/07/22 19:13:25 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.31.2.3 2003/11/30 21:56:36 tgl Exp $
* ----------
*/
#include "postgres.h"
@@ -42,6 +42,7 @@
#include "utils/memutils.h"
#include "storage/backendid.h"
#include "storage/ipc.h"
+#include "storage/pg_shmem.h"
#include "utils/rel.h"
#include "utils/hsearch.h"
#include "utils/ps_status.h"
@@ -324,6 +325,9 @@ pgstat_start(void)
/* Close the postmaster's sockets, except for pgstat link */
ClosePostmasterPorts(false);
+ /* Drop our connection to postmaster's shared memory, as well */
+ PGSharedMemoryDetach();
+
pgstat_main();
exit(0);
diff --git a/src/include/storage/pg_shmem.h b/src/include/storage/pg_shmem.h
index c362757c0f6..e19acfa8562 100644
--- a/src/include/storage/pg_shmem.h
+++ b/src/include/storage/pg_shmem.h
@@ -17,7 +17,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: pg_shmem.h,v 1.4 2002/09/04 20:31:45 momjian Exp $
+ * $Id: pg_shmem.h,v 1.4.2.1 2003/11/30 21:56:36 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -37,5 +37,6 @@ typedef struct PGShmemHeader /* standard header for all Postgres shmem */
extern PGShmemHeader *PGSharedMemoryCreate(uint32 size, bool makePrivate,
int port);
extern bool PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2);
+extern void PGSharedMemoryDetach(void);
#endif /* PG_SHMEM_H */