/* OK, should be able to attach to the segment */
#ifdef SHM_SHARE_MMU
- /* use intimate shared memory on SPARC Solaris */
+ /* use intimate shared memory on Solaris */
memAddress = shmat(shmid, 0, SHM_SHARE_MMU);
#else
memAddress = shmat(shmid, 0, 0);
/* Room for a header? */
Assert(size > MAXALIGN(sizeof(PGShmemHeader)));
- /* Just attach and return the pointer */
+ /* If Exec case, just attach and return the pointer */
if (ExecBackend && UsedShmemSegAddr != NULL && !makePrivate)
{
if ((hdr = PGSharedMemoryAttach(UsedShmemSegID, &shmid)) == NULL)
return hdr;
}
+ /* Make sure PGSharedMemoryAttach doesn't fail without need */
+ UsedShmemSegAddr = NULL;
+
/* Loop till we find a free IPC key */
NextShmemSegID = port * 1000;
hdr->totalsize = size;
hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
-
- if (ExecBackend && UsedShmemSegAddr == NULL && !makePrivate)
- {
- UsedShmemSegAddr = memAddress;
- UsedShmemSegID = NextShmemSegID;
- }
+ /* 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;
+ }
+}
/*
* Attach to shared memory and make sure it has a Postgres header
#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"
/* 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);