Hello,

This patch using VirtualAlloc()/VirtualFree() to avoid failing in
reattach to shared memory.

Can this be added to CommitFest ?


Recent threads in pgsql-bugs are
  http://archives.postgresql.org/pgsql-bugs/2009-07/msg00036.php

This fix is almost same as previous patch. debug code is deleted.
  http://archives.postgresql.org/pgsql-bugs/2009-07/msg00078.php

Regards,

-- 
Tsutomu Yamada
SRA OSS, Inc. Japan

Index: src/backend/port/win32_shmem.c
===================================================================
RCS file: /mnt/prj/pg/cvsmirror/pg/pgsql/src/backend/port/win32_shmem.c,v
retrieving revision 1.11
diff -c -r1.11 win32_shmem.c
*** src/backend/port/win32_shmem.c      11 Jun 2009 14:49:00 -0000      1.11
--- src/backend/port/win32_shmem.c      14 Jul 2009 10:11:44 -0000
***************
*** 18,23 ****
--- 18,24 ----
  
  unsigned long UsedShmemSegID = 0;
  void     *UsedShmemSegAddr = NULL;
+ static Size UsedShmemSegSize = 0;
  
  static void pgwin32_SharedMemoryDelete(int status, Datum shmId);
  
***************
*** 233,238 ****
--- 234,240 ----
  
        /* Save info for possible future use */
        UsedShmemSegAddr = memAddress;
+       UsedShmemSegSize = size;
        UsedShmemSegID = (unsigned long) hmap2;
  
        return hdr;
***************
*** 257,262 ****
--- 259,273 ----
        Assert(UsedShmemSegAddr != NULL);
        Assert(IsUnderPostmaster);
  
+       /* release memory region
+        * that reserved by parant process
+        */
+       if (VirtualFree(UsedShmemSegAddr, 0, MEM_RELEASE) == 0)
+       {
+               elog(LOG, "failed to release reserved memory region (addr=%p): 
%lu",
+                        UsedShmemSegAddr, GetLastError());
+       }
+ 
        hdr = (PGShmemHeader *) MapViewOfFileEx((HANDLE) UsedShmemSegID, 
FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0, UsedShmemSegAddr);
        if (!hdr)
                elog(FATAL, "could not reattach to shared memory (key=%d, 
addr=%p): %lu",
***************
*** 302,304 ****
--- 313,335 ----
        if (!CloseHandle((HANDLE) DatumGetInt32(shmId)))
                elog(LOG, "could not close handle to shared memory: %lu", 
GetLastError());
  }
+ 
+ /*
+  * pgwin32_ReserveSharedMemory(HANDLE pChild)
+  * Reserve shared memory area,
+  * BEFORE child process allocates memory for DLL and/or others.
+  */
+ void
+ pgwin32_ReserveSharedMemory(HANDLE pChild)
+ {
+       void *memAddress;
+ 
+       Assert(UsedShmemSegAddr != NULL);
+       Assert(UsedShmemSegSize != 0);
+       memAddress = VirtualAllocEx(pChild, UsedShmemSegAddr, UsedShmemSegSize,
+                                                               MEM_RESERVE, 
PAGE_READWRITE);
+       if (memAddress == NULL) {
+               elog(LOG, "could not reserve shared memory region (addr=%p): 
%lu",
+                        UsedShmemSegAddr, GetLastError());
+       }
+ }
Index: src/backend/postmaster/postmaster.c
===================================================================
RCS file: /mnt/prj/pg/cvsmirror/pg/pgsql/src/backend/postmaster/postmaster.c,v
retrieving revision 1.584
diff -c -r1.584 postmaster.c
*** src/backend/postmaster/postmaster.c 8 Jul 2009 18:55:35 -0000       1.584
--- src/backend/postmaster/postmaster.c 13 Jul 2009 08:40:36 -0000
***************
*** 3643,3648 ****
--- 3643,3655 ----
                elog(LOG, "could not close handle to backend parameter file: 
error code %d",
                         (int) GetLastError());
  
+       {
+               /* reserve shared memory area before ResumeThread() */
+               /* XXX: if it fail ? */
+               extern void pgwin32_ReserveSharedMemory(HANDLE);
+               pgwin32_ReserveSharedMemory(pi.hProcess);
+       }
+ 
        /*
         * Now that the backend variables are written out, we start the child
         * thread so it can start initializing while we set up the rest of the
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to