41 #define PROTECTIVE_REGION_SIZE (10 * WIN32_STACK_RLIMIT)
74 elog(
FATAL,
"could not get size for full pathname of datadir %s: error code %lu",
79 elog(
FATAL,
"could not allocate memory for shared memory name");
81 strcpy(retptr,
"Global\\PostgreSQL:");
84 elog(
FATAL,
"could not generate full pathname for datadir %s: error code %lu",
93 for (cp = retptr; *cp; cp++)
120 hmap = OpenFileMapping(FILE_MAP_READ, FALSE, szShareMem);
143 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
146 (
errmsg(
"could not enable user right \"%s\": error code %lu",
152 _(
"Lock pages in memory"),
154 errdetail(
"Failed system call was %s.",
"OpenProcessToken")));
158 if (!LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &luid))
161 (
errmsg(
"could not enable user right \"%s\": error code %lu",
_(
"Lock pages in memory"), GetLastError()),
162 errdetail(
"Failed system call was %s.",
"LookupPrivilegeValue")));
166 tp.PrivilegeCount = 1;
167 tp.Privileges[0].Luid = luid;
168 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
170 if (!AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL))
173 (
errmsg(
"could not enable user right \"%s\": error code %lu",
_(
"Lock pages in memory"), GetLastError()),
174 errdetail(
"Failed system call was %s.",
"AdjustTokenPrivileges")));
179 if (GetLastError() != ERROR_SUCCESS)
181 if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
183 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
184 errmsg(
"could not enable user right \"%s\"",
_(
"Lock pages in memory")),
185 errhint(
"Assign user right \"%s\" to the Windows user account which runs PostgreSQL.",
186 _(
"Lock pages in memory"))));
189 (
errmsg(
"could not enable user right \"%s\": error code %lu",
_(
"Lock pages in memory"), GetLastError()),
190 errdetail(
"Failed system call was %s.",
"AdjustTokenPrivileges")));
218 SIZE_T largePageSize = 0;
220 DWORD flProtect = PAGE_READWRITE;
224 MEM_RESERVE, PAGE_NOACCESS);
226 elog(
FATAL,
"could not reserve memory region: error code %lu",
239 largePageSize = GetLargePageMinimum();
240 if (largePageSize == 0)
243 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
244 errmsg(
"the processor does not support large pages")));
256 flProtect = PAGE_READWRITE | SEC_COMMIT | SEC_LARGE_PAGES;
259 if (
size % largePageSize != 0)
260 size += largePageSize - (
size % largePageSize);
266 size_high =
size >> 32;
270 size_low = (DWORD)
size;
278 for (
i = 0;
i < 10;
i++)
286 hmap = CreateFileMapping(INVALID_HANDLE_VALUE,
295 if (GetLastError() == ERROR_NO_SYSTEM_RESOURCES &&
297 (flProtect & SEC_LARGE_PAGES) != 0)
299 elog(
DEBUG1,
"CreateFileMapping(%zu) with SEC_LARGE_PAGES failed, "
300 "huge pages disabled",
308 flProtect = PAGE_READWRITE;
313 (
errmsg(
"could not create shared memory segment: error code %lu", GetLastError()),
314 errdetail(
"Failed system call was CreateFileMapping(size=%zu, name=%s).",
322 if (GetLastError() == ERROR_ALREADY_EXISTS)
339 (
errmsg(
"pre-existing shared memory block is still in use"),
340 errhint(
"Check if there are any old server processes still running, and terminate them.")));
347 if (!DuplicateHandle(GetCurrentProcess(), hmap, GetCurrentProcess(), &hmap2, 0, TRUE, DUPLICATE_SAME_ACCESS))
349 (
errmsg(
"could not create shared memory segment: error code %lu", GetLastError()),
350 errdetail(
"Failed system call was DuplicateHandle.")));
356 if (!CloseHandle(hmap))
357 elog(
LOG,
"could not close handle to shared memory: error code %lu", GetLastError());
359 desiredAccess = FILE_MAP_WRITE | FILE_MAP_READ;
361 #ifdef FILE_MAP_LARGE_PAGES
363 if ((flProtect & SEC_LARGE_PAGES) != 0)
364 desiredAccess |= FILE_MAP_LARGE_PAGES;
371 memAddress = MapViewOfFileEx(hmap2, desiredAccess, 0, 0, 0, NULL);
374 (
errmsg(
"could not create shared memory segment: error code %lu", GetLastError()),
375 errdetail(
"Failed system call was MapViewOfFileEx.")));
437 elog(
FATAL,
"failed to release reserved memory region (addr=%p): error code %lu",
440 elog(
FATAL,
"failed to release reserved memory region (addr=%p): error code %lu",
445 elog(
FATAL,
"could not reattach to shared memory (key=%p, addr=%p): error code %lu",
447 if (hdr != origUsedShmemSegAddr)
448 elog(
FATAL,
"reattaching to shared memory returned unexpected address (got %p, expected %p)",
449 hdr, origUsedShmemSegAddr);
451 elog(
FATAL,
"reattaching to shared memory returned non-PostgreSQL memory");
514 elog(
LOG,
"failed to release reserved memory region (addr=%p): error code %lu",
524 elog(
LOG,
"could not unmap view of shared memory: error code %lu",
534 elog(
LOG,
"could not close handle to shared memory: error code %lu",
584 MEM_RESERVE, PAGE_NOACCESS);
588 elog(
LOG,
"could not reserve shared memory region (addr=%p) for child %p: error code %lu",
600 elog(
LOG,
"reserved shared memory region got incorrect address %p, expected %p",
607 MEM_RESERVE, PAGE_READWRITE);
610 elog(
LOG,
"could not reserve shared memory region (addr=%p) for child %p: error code %lu",
616 elog(
LOG,
"reserved shared memory region got incorrect address %p, expected %p",
#define Assert(condition)
int errmsg_internal(const char *fmt,...)
int errdetail(const char *fmt,...)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
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)
static rewind_source * source
static Datum PointerGetDatum(const void *X)
static Pointer DatumGetPointer(Datum X)
static pg_noinline void Size size
#define PROTECTIVE_REGION_SIZE
PGShmemHeader * PGSharedMemoryCreate(Size size, PGShmemHeader **shim)
void PGSharedMemoryDetach(void)
void PGSharedMemoryReAttach(void)
bool check_huge_page_size(int *newval, void **extra, GucSource source)
void * ShmemProtectiveRegion
void GetHugePageSize(Size *hugepagesize, int *mmap_flags)
int pgwin32_ReserveSharedMemoryRegion(HANDLE hChild)
static bool EnableLockPagesPrivilege(int elevel)
bool PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2)
static void pgwin32_SharedMemoryDelete(int status, Datum shmId)
static Size UsedShmemSegSize
void PGSharedMemoryNoReAttach(void)
static char * GetSharedMemName(void)