39 #define PROTECTIVE_REGION_SIZE (10 * WIN32_STACK_RLIMIT) 70 bufsize = GetFullPathName(
DataDir, 0, NULL, NULL);
72 elog(
FATAL,
"could not get size for full pathname of datadir %s: error code %lu",
75 retptr =
malloc(bufsize + 18);
77 elog(
FATAL,
"could not allocate memory for shared memory name");
79 strcpy(retptr,
"Global\\PostgreSQL:");
80 r = GetFullPathName(
DataDir, bufsize, retptr + 18, NULL);
81 if (r == 0 || r > bufsize)
82 elog(
FATAL,
"could not generate full pathname for datadir %s: error code %lu",
91 for (cp = retptr; *cp; cp++)
118 hmap = OpenFileMapping(FILE_MAP_READ, FALSE, szShareMem);
141 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
144 (
errmsg(
"could not enable Lock Pages in Memory user right: error code %lu", GetLastError()),
145 errdetail(
"Failed system call was %s.",
"OpenProcessToken")));
149 if (!LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &luid))
152 (
errmsg(
"could not enable Lock Pages in Memory user right: error code %lu", GetLastError()),
153 errdetail(
"Failed system call was %s.",
"LookupPrivilegeValue")));
157 tp.PrivilegeCount = 1;
158 tp.Privileges[0].Luid = luid;
159 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
161 if (!AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL))
164 (
errmsg(
"could not enable Lock Pages in Memory user right: error code %lu", GetLastError()),
165 errdetail(
"Failed system call was %s.",
"AdjustTokenPrivileges")));
170 if (GetLastError() != ERROR_SUCCESS)
172 if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
174 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
175 errmsg(
"could not enable Lock Pages in Memory user right"),
176 errhint(
"Assign Lock Pages in Memory user right to the Windows user account which runs PostgreSQL.")));
179 (
errmsg(
"could not enable Lock Pages in Memory user right: error code %lu", GetLastError()),
180 errdetail(
"Failed system call was %s.",
"AdjustTokenPrivileges")));
208 SIZE_T largePageSize = 0;
209 Size orig_size = size;
210 DWORD flProtect = PAGE_READWRITE;
213 MEM_RESERVE, PAGE_NOACCESS);
215 elog(
FATAL,
"could not reserve memory region: error code %lu",
228 largePageSize = GetLargePageMinimum();
229 if (largePageSize == 0)
232 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
233 errmsg(
"the processor does not support large pages")));
235 (
errmsg(
"disabling huge pages")));
240 (
errmsg(
"disabling huge pages")));
245 flProtect = PAGE_READWRITE | SEC_COMMIT | SEC_LARGE_PAGES;
248 if (size % largePageSize != 0)
249 size += largePageSize - (size % largePageSize);
255 size_high = size >> 32;
259 size_low = (DWORD) size;
267 for (i = 0; i < 10; i++)
275 hmap = CreateFileMapping(INVALID_HANDLE_VALUE,
284 if (GetLastError() == ERROR_NO_SYSTEM_RESOURCES &&
286 (flProtect & SEC_LARGE_PAGES) != 0)
288 elog(
DEBUG1,
"CreateFileMapping(%zu) with SEC_LARGE_PAGES failed, " 289 "huge pages disabled",
297 flProtect = PAGE_READWRITE;
302 (
errmsg(
"could not create shared memory segment: error code %lu", GetLastError()),
303 errdetail(
"Failed system call was CreateFileMapping(size=%zu, name=%s).",
311 if (GetLastError() == ERROR_ALREADY_EXISTS)
328 (
errmsg(
"pre-existing shared memory block is still in use"),
329 errhint(
"Check if there are any old server processes still running, and terminate them.")));
336 if (!DuplicateHandle(GetCurrentProcess(), hmap, GetCurrentProcess(), &hmap2, 0, TRUE, DUPLICATE_SAME_ACCESS))
338 (
errmsg(
"could not create shared memory segment: error code %lu", GetLastError()),
339 errdetail(
"Failed system call was DuplicateHandle.")));
345 if (!CloseHandle(hmap))
346 elog(
LOG,
"could not close handle to shared memory: error code %lu", GetLastError());
353 memAddress = MapViewOfFileEx(hmap2, FILE_MAP_WRITE | FILE_MAP_READ, 0, 0, 0, NULL);
356 (
errmsg(
"could not create shared memory segment: error code %lu", GetLastError()),
357 errdetail(
"Failed system call was MapViewOfFileEx.")));
414 elog(
FATAL,
"failed to release reserved memory region (addr=%p): error code %lu",
417 elog(
FATAL,
"failed to release reserved memory region (addr=%p): error code %lu",
422 elog(
FATAL,
"could not reattach to shared memory (key=%p, addr=%p): error code %lu",
424 if (hdr != origUsedShmemSegAddr)
425 elog(
FATAL,
"reattaching to shared memory returned unexpected address (got %p, expected %p)",
426 hdr, origUsedShmemSegAddr);
428 elog(
FATAL,
"reattaching to shared memory returned non-PostgreSQL memory");
491 elog(
LOG,
"failed to release reserved memory region (addr=%p): error code %lu",
501 elog(
LOG,
"could not unmap view of shared memory: error code %lu",
511 elog(
LOG,
"could not close handle to shared memory: error code %lu",
561 MEM_RESERVE, PAGE_NOACCESS);
565 elog(
LOG,
"could not reserve shared memory region (addr=%p) for child %p: error code %lu",
577 elog(
LOG,
"reserved shared memory region got incorrect address %p, expected %p",
584 MEM_RESERVE, PAGE_READWRITE);
587 elog(
LOG,
"could not reserve shared memory region (addr=%p) for child %p: error code %lu",
593 elog(
LOG,
"reserved shared memory region got incorrect address %p, expected %p",
void PGSharedMemoryDetach(void)
int errhint(const char *fmt,...)
PGShmemHeader * PGSharedMemoryCreate(Size size, PGShmemHeader **shim)
int pgwin32_ReserveSharedMemoryRegion(HANDLE hChild)
#define PROTECTIVE_REGION_SIZE
#define PointerGetDatum(X)
static bool EnableLockPagesPrivilege(int elevel)
void PGSharedMemoryNoReAttach(void)
int errcode(int sqlerrcode)
void * ShmemProtectiveRegion
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
int errdetail(const char *fmt,...)
void PGSharedMemoryReAttach(void)
static void pgwin32_SharedMemoryDelete(int status, Datum shmId)
static Size UsedShmemSegSize
static char * GetSharedMemName(void)
#define ereport(elevel,...)
#define Assert(condition)
#define DatumGetPointer(X)
bool PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2)
int errmsg(const char *fmt,...)
static void static void status(const char *fmt,...) pg_attribute_printf(1