From 63ca350ef9f58d48ac89fd3c68416e319cac0a39 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Fri, 27 Apr 2018 21:59:58 -0400 Subject: [PATCH] Try to get some info about Windows can't-reattach-to-shared-memory errors. Add some debug printouts focused on the idea that MapViewOfFileEx might be rounding its virtual memory allocation up more than we expect (and, in particular, more than VirtualAllocEx does). Once we've seen what this reports in one of the failures on buildfarm members dory or jacana, we might revert this ... or perhaps just decrease the log level. Discussion: https://postgr.es/m/25495.1524517820@sss.pgh.pa.us --- src/backend/port/win32_shmem.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/backend/port/win32_shmem.c b/src/backend/port/win32_shmem.c index f8ca52e1af..d431e0d036 100644 --- a/src/backend/port/win32_shmem.c +++ b/src/backend/port/win32_shmem.c @@ -191,6 +191,7 @@ PGSharedMemoryCreate(Size size, bool makePrivate, int port, SIZE_T largePageSize = 0; Size orig_size = size; DWORD flProtect = PAGE_READWRITE; + MEMORY_BASIC_INFORMATION info; /* Room for a header? */ Assert(size > MAXALIGN(sizeof(PGShmemHeader))); @@ -359,6 +360,14 @@ retry: /* Register on-exit routine to delete the new segment */ on_shmem_exit(pgwin32_SharedMemoryDelete, PointerGetDatum(hmap2)); + /* Log information about the segment's virtual memory use */ + if (VirtualQuery(memAddress, &info, sizeof(info)) != 0) + elog(LOG, "mapped shared memory segment at %p, requested size %zu, mapped size %zu", + memAddress, size, info.RegionSize); + else + elog(LOG, "VirtualQuery(%p) failed: error code %lu", + memAddress, GetLastError()); + *shim = hdr; return hdr; } @@ -392,8 +401,21 @@ PGSharedMemoryReAttach(void) hdr = (PGShmemHeader *) MapViewOfFileEx(UsedShmemSegID, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0, UsedShmemSegAddr); if (!hdr) + { + DWORD maperr = GetLastError(); + MEMORY_BASIC_INFORMATION info; + + if (VirtualQuery(UsedShmemSegAddr, &info, sizeof(info)) != 0) + elog(LOG, "VirtualQuery(%p) reports region of size %zu, base %p, has state 0x%lx", + UsedShmemSegAddr, info.RegionSize, + info.AllocationBase, info.State); + else + elog(LOG, "VirtualQuery(%p) failed: error code %lu", + UsedShmemSegAddr, GetLastError()); + elog(FATAL, "could not reattach to shared memory (key=%p, addr=%p): error code %lu", - UsedShmemSegID, UsedShmemSegAddr, GetLastError()); + UsedShmemSegID, UsedShmemSegAddr, maperr); + } if (hdr != origUsedShmemSegAddr) elog(FATAL, "reattaching to shared memory returned unexpected address (got %p, expected %p)", hdr, origUsedShmemSegAddr);