123 void *requestedAddress = NULL;
138 char *pg_shmem_addr = getenv(
"PG_SHMEM_ADDR");
141 requestedAddress = (
void *) strtoul(pg_shmem_addr, NULL, 0);
144 #if defined(__darwin__) && SIZEOF_VOID_P == 8
149 requestedAddress = (
void *) 0x80000000000;
159 int shmget_errno = errno;
167 if (shmget_errno == EEXIST || shmget_errno == EACCES
169 || shmget_errno ==
EIDRM
183 if (shmget_errno == EINVAL)
190 if (errno == EEXIST || errno == EACCES
206 if (shmctl(shmid,
IPC_RMID, NULL) < 0)
207 elog(
LOG,
"shmctl(%d, %d, 0) failed: %m",
221 errno = shmget_errno;
223 (
errmsg(
"could not create shared memory segment: %m"),
224 errdetail(
"Failed system call was shmget(key=%lu, size=%zu, 0%o).",
225 (
unsigned long) memKey, size,
227 (shmget_errno == EINVAL) ?
228 errhint(
"This error usually means that PostgreSQL's request for a shared memory "
229 "segment exceeded your kernel's SHMMAX parameter, or possibly that "
231 "your kernel's SHMMIN parameter.\n"
232 "The PostgreSQL documentation contains more information about shared "
233 "memory configuration.") : 0,
234 (shmget_errno == ENOMEM) ?
235 errhint(
"This error usually means that PostgreSQL's request for a shared "
236 "memory segment exceeded your kernel's SHMALL parameter. You might need "
237 "to reconfigure the kernel with larger SHMALL.\n"
238 "The PostgreSQL documentation contains more information about shared "
239 "memory configuration.") : 0,
240 (shmget_errno == ENOSPC) ?
241 errhint(
"This error does *not* mean that you have run out of disk space. "
242 "It occurs either if all available shared memory IDs have been taken, "
243 "in which case you need to raise the SHMMNI parameter in your kernel, "
244 "or because the system's overall limit for shared memory has been "
246 "The PostgreSQL documentation contains more information about shared "
247 "memory configuration.") : 0));
256 if (memAddress == (
void *) -1)
257 elog(
FATAL,
"shmat(id=%d, addr=%p, flags=0x%x) failed: %m",
272 (
unsigned long) memKey, (
unsigned long) shmid);
300 elog(
LOG,
"shmctl(%d, %d, 0) failed: %m",
322 if (memAddress && shmdt((
void *) memAddress) < 0)
323 elog(
LOG,
"shmdt(%p) failed: %m", memAddress);
350 struct shmid_ds shmStat;
359 if (shmctl(shmId,
IPC_STAT, &shmStat) < 0)
384 #ifdef HAVE_LINUX_EIDRM_BUG
427 #ifdef HAVE_LINUX_EIDRM_BUG
437 hdr->device != statbuf.
st_dev ||
438 hdr->inode != statbuf.
st_ino)
482 Size default_hugepagesize = 0;
483 Size hugepagesize_local = 0;
484 int mmap_flags_local = 0;
502 while (fgets(
buf,
sizeof(
buf), fp))
504 if (sscanf(
buf,
"Hugepagesize: %u %c", &sz, &ch) == 2)
508 default_hugepagesize = sz * (
Size) 1024;
524 else if (default_hugepagesize != 0)
527 hugepagesize_local = default_hugepagesize;
539 hugepagesize_local = 2 * 1024 * 1024;
542 mmap_flags_local = MAP_HUGETLB;
548 #if defined(MAP_HUGE_MASK) && defined(MAP_HUGE_SHIFT)
549 if (hugepagesize_local != default_hugepagesize)
553 mmap_flags_local |= (shift & MAP_HUGE_MASK) << MAP_HUGE_SHIFT;
559 *mmap_flags = mmap_flags_local;
561 *hugepagesize = hugepagesize_local;
579 #if !(defined(MAP_HUGE_MASK) && defined(MAP_HUGE_SHIFT))
600 Size allocsize = *size;
618 if (allocsize % hugepagesize != 0)
619 allocsize += hugepagesize - (allocsize % hugepagesize);
621 ptr = mmap(NULL, allocsize, PROT_READ | PROT_WRITE,
625 elog(
DEBUG1,
"mmap(%zu) with MAP_HUGETLB failed, huge pages disabled: %m",
645 ptr = mmap(NULL, allocsize, PROT_READ | PROT_WRITE,
654 (
errmsg(
"could not map anonymous shared memory: %m"),
655 (mmap_errno == ENOMEM) ?
656 errhint(
"This error usually means that PostgreSQL's request "
657 "for a shared memory segment exceeded available memory, "
658 "swap space, or huge pages. To reduce the request size "
659 "(currently %zu bytes), reduce PostgreSQL's shared "
660 "memory usage, perhaps by reducing shared_buffers or "
680 elog(
LOG,
"munmap(%p, %zu) failed: %m",
716 errmsg(
"could not stat data directory \"%s\": %m",
720 #if !defined(MAP_HUGETLB)
723 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
724 errmsg(
"huge pages not supported on this platform")));
730 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
731 errmsg(
"huge pages not supported with the current shared_memory_type setting")));
762 NextShmemSegID = statbuf.
st_ino;
796 (
errcode(ERRCODE_LOCK_FILE_EXISTS),
797 errmsg(
"pre-existing shared memory block (key %lu, ID %lu) is still in use",
798 (
unsigned long) NextShmemSegID,
799 (
unsigned long) shmid),
800 errhint(
"Terminate any old server processes associated with data directory \"%s\".",
811 "shared memory block (key %lu, ID %lu) deleted during startup",
812 (
unsigned long) NextShmemSegID,
813 (
unsigned long) shmid);
832 if (shmctl(shmid,
IPC_RMID, NULL) < 0)
837 if (oldhdr && shmdt((
void *) oldhdr) < 0)
838 elog(
LOG,
"shmdt(%p) failed: %m", oldhdr);
912 elog(
FATAL,
"could not reattach to shared memory (key=%d, addr=%p): %m",
914 if (hdr != origUsedShmemSegAddr)
915 elog(
FATAL,
"reattaching to shared memory returned unexpected address (got %p, expected %p)",
916 hdr, origUsedShmemSegAddr);
974 #
if defined(EXEC_BACKEND) && defined(__CYGWIN__)
986 elog(
LOG,
"munmap(%p, %zu) failed: %m",
elog(ERROR, "%s: %s", p2, msg)
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)
Assert(fmt[strlen(fmt) - 1] !='\n')
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 void AnonymousShmemDetach(int status, Datum arg)
PGShmemHeader * PGSharedMemoryCreate(Size size, PGShmemHeader **shim)
void PGSharedMemoryDetach(void)
static void * CreateAnonymousSegment(Size *size)
@ SHMSTATE_ANALYSIS_FAILURE
static Size AnonymousShmemSize
unsigned long UsedShmemSegID
bool check_huge_page_size(int *newval, void **extra, GucSource source)
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)