124 void *requestedAddress = NULL;
139 char *pg_shmem_addr = getenv(
"PG_SHMEM_ADDR");
142 requestedAddress = (
void *) strtoul(pg_shmem_addr, NULL, 0);
145#if defined(__darwin__) && SIZEOF_VOID_P == 8
150 requestedAddress = (
void *) 0x80000000000;
160 int shmget_errno = errno;
168 if (shmget_errno == EEXIST || shmget_errno == EACCES
170 || shmget_errno ==
EIDRM
184 if (shmget_errno == EINVAL)
191 if (errno == EEXIST || errno == EACCES
207 if (shmctl(shmid,
IPC_RMID, NULL) < 0)
208 elog(
LOG,
"shmctl(%d, %d, 0) failed: %m",
222 errno = shmget_errno;
224 (
errmsg(
"could not create shared memory segment: %m"),
225 errdetail(
"Failed system call was shmget(key=%lu, size=%zu, 0%o).",
226 (
unsigned long) memKey,
size,
228 (shmget_errno == EINVAL) ?
229 errhint(
"This error usually means that PostgreSQL's request for a shared memory "
230 "segment exceeded your kernel's SHMMAX parameter, or possibly that "
232 "your kernel's SHMMIN parameter.\n"
233 "The PostgreSQL documentation contains more information about shared "
234 "memory configuration.") : 0,
235 (shmget_errno == ENOMEM) ?
236 errhint(
"This error usually means that PostgreSQL's request for a shared "
237 "memory segment exceeded your kernel's SHMALL parameter. You might need "
238 "to reconfigure the kernel with larger SHMALL.\n"
239 "The PostgreSQL documentation contains more information about shared "
240 "memory configuration.") : 0,
241 (shmget_errno == ENOSPC) ?
242 errhint(
"This error does *not* mean that you have run out of disk space. "
243 "It occurs either if all available shared memory IDs have been taken, "
244 "in which case you need to raise the SHMMNI parameter in your kernel, "
245 "or because the system's overall limit for shared memory has been "
247 "The PostgreSQL documentation contains more information about shared "
248 "memory configuration.") : 0));
257 if (memAddress == (
void *) -1)
258 elog(
FATAL,
"shmat(id=%d, addr=%p, flags=0x%x) failed: %m",
273 (
unsigned long) memKey, (
unsigned long) shmid);
301 elog(
LOG,
"shmctl(%d, %d, 0) failed: %m",
323 if (memAddress && shmdt(memAddress) < 0)
324 elog(
LOG,
"shmdt(%p) failed: %m", memAddress);
351 struct shmid_ds shmStat;
360 if (shmctl(shmId,
IPC_STAT, &shmStat) < 0)
385#ifdef HAVE_LINUX_EIDRM_BUG
428#ifdef HAVE_LINUX_EIDRM_BUG
438 hdr->device != statbuf.
st_dev ||
439 hdr->inode != statbuf.
st_ino)
483 Size default_hugepagesize = 0;
484 Size hugepagesize_local = 0;
485 int mmap_flags_local = 0;
503 while (fgets(
buf,
sizeof(
buf), fp))
505 if (sscanf(
buf,
"Hugepagesize: %u %c", &sz, &ch) == 2)
509 default_hugepagesize = sz * (
Size) 1024;
525 else if (default_hugepagesize != 0)
528 hugepagesize_local = default_hugepagesize;
540 hugepagesize_local = 2 * 1024 * 1024;
543 mmap_flags_local = MAP_HUGETLB;
549#if defined(MAP_HUGE_MASK) && defined(MAP_HUGE_SHIFT)
550 if (hugepagesize_local != default_hugepagesize)
554 mmap_flags_local |= (shift & MAP_HUGE_MASK) << MAP_HUGE_SHIFT;
560 *mmap_flags = mmap_flags_local;
562 *hugepagesize = hugepagesize_local;
580#if !(defined(MAP_HUGE_MASK) && defined(MAP_HUGE_SHIFT))
619 if (allocsize % hugepagesize != 0)
620 allocsize += hugepagesize - (allocsize % hugepagesize);
622 ptr = mmap(NULL, allocsize, PROT_READ | PROT_WRITE,
626 elog(
DEBUG1,
"mmap(%zu) with MAP_HUGETLB failed, huge pages disabled: %m",
646 ptr = mmap(NULL, allocsize, PROT_READ | PROT_WRITE,
655 (
errmsg(
"could not map anonymous shared memory: %m"),
656 (mmap_errno == ENOMEM) ?
657 errhint(
"This error usually means that PostgreSQL's request "
658 "for a shared memory segment exceeded available memory, "
659 "swap space, or huge pages. To reduce the request size "
660 "(currently %zu bytes), reduce PostgreSQL's shared "
661 "memory usage, perhaps by reducing \"shared_buffers\" or "
662 "\"max_connections\".",
681 elog(
LOG,
"munmap(%p, %zu) failed: %m",
717 errmsg(
"could not stat data directory \"%s\": %m",
721#if !defined(MAP_HUGETLB)
724 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
725 errmsg(
"huge pages not supported on this platform")));
731 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
732 errmsg(
"huge pages not supported with the current \"shared_memory_type\" setting")));
763 NextShmemSegID = statbuf.
st_ino;
797 (
errcode(ERRCODE_LOCK_FILE_EXISTS),
798 errmsg(
"pre-existing shared memory block (key %lu, ID %lu) is still in use",
799 (
unsigned long) NextShmemSegID,
800 (
unsigned long) shmid),
801 errhint(
"Terminate any old server processes associated with data directory \"%s\".",
812 "shared memory block (key %lu, ID %lu) deleted during startup",
813 (
unsigned long) NextShmemSegID,
814 (
unsigned long) shmid);
833 if (shmctl(shmid,
IPC_RMID, NULL) < 0)
838 if (oldhdr && shmdt(oldhdr) < 0)
839 elog(
LOG,
"shmdt(%p) failed: %m", oldhdr);
913 elog(
FATAL,
"could not reattach to shared memory (key=%d, addr=%p): %m",
915 if (hdr != origUsedShmemSegAddr)
916 elog(
FATAL,
"reattaching to shared memory returned unexpected address (got %p, expected %p)",
917 hdr, origUsedShmemSegAddr);
975#
if defined(EXEC_BACKEND) && defined(__CYGWIN__)
987 elog(
LOG,
"munmap(%p, %zu) failed: %m",
#define Assert(condition)
void dsm_cleanup_using_control_segment(dsm_handle old_control_handle)
int errcode_for_file_access(void)
int errdetail(const char *fmt,...)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
FILE * AllocateFile(const char *name, const char *mode)
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
#define GUC_check_errdetail
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
void AddToDataDirLockFile(int target_line, const char *str)
static uint64 pg_ceil_log2_64(uint64 num)
static rewind_source * source
struct PGShmemHeader PGShmemHeader
#define LOCK_FILE_LINE_SHMEM_KEY
static Datum PointerGetDatum(const void *X)
static Pointer DatumGetPointer(Datum X)
static Datum Int32GetDatum(int32 X)
static int32 DatumGetInt32(Datum X)
static pg_noinline void Size size
static void AnonymousShmemDetach(int status, Datum arg)
void PGSharedMemoryDetach(void)
@ SHMSTATE_ANALYSIS_FAILURE
PGShmemHeader * PGSharedMemoryCreate(Size size, PGShmemHeader **shim)
static Size AnonymousShmemSize
unsigned long UsedShmemSegID
bool check_huge_page_size(int *newval, void **extra, GucSource source)
static void * CreateAnonymousSegment(Size *size)
static void * InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size)
void GetHugePageSize(Size *hugepagesize, int *mmap_flags)
bool PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2)
static void IpcMemoryDetach(int status, Datum shmaddr)
static IpcMemoryState PGSharedMemoryAttach(IpcMemoryId shmId, void *attachAt, PGShmemHeader **addr)
static void * AnonymousShmem
static void IpcMemoryDelete(int status, Datum shmId)
void PGSharedMemoryReAttach(void)
void PGSharedMemoryNoReAttach(void)