6262 * to a process after exec(). Since EXEC_BACKEND is intended only for
6363 * developer use, this shouldn't be a big problem. Because of this, we do
6464 * not worry about supporting anonymous shmem in the EXEC_BACKEND cases below.
65+ *
66+ * As of PostgreSQL 12, we regained the ability to use a large System V shared
67+ * memory region even in non-EXEC_BACKEND builds, if shared_memory_type is set
68+ * to sysv (though this is not the default).
6569 */
66- #ifndef EXEC_BACKEND
67- #define USE_ANONYMOUS_SHMEM
68- #endif
6970
7071
7172typedef key_t IpcMemoryKey ; /* shared memory key passed to shmget(2) */
@@ -75,10 +76,8 @@ typedef int IpcMemoryId; /* shared memory ID returned by shmget(2) */
7576unsigned long UsedShmemSegID = 0 ;
7677void * UsedShmemSegAddr = NULL ;
7778
78- #ifdef USE_ANONYMOUS_SHMEM
7979static Size AnonymousShmemSize ;
8080static void * AnonymousShmem = NULL ;
81- #endif
8281
8382static void * InternalIpcMemoryCreate (IpcMemoryKey memKey , Size size );
8483static void IpcMemoryDetach (int status , Datum shmaddr );
@@ -370,8 +369,6 @@ PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2)
370369 return true;
371370}
372371
373- #ifdef USE_ANONYMOUS_SHMEM
374-
375372#ifdef MAP_HUGETLB
376373
377374/*
@@ -534,8 +531,6 @@ AnonymousShmemDetach(int status, Datum arg)
534531 }
535532}
536533
537- #endif /* USE_ANONYMOUS_SHMEM */
538-
539534/*
540535 * PGSharedMemoryCreate
541536 *
@@ -566,7 +561,7 @@ PGSharedMemoryCreate(Size size, bool makePrivate, int port,
566561 Size sysvsize ;
567562
568563 /* Complain if hugepages demanded but we can't possibly support them */
569- #if !defined(USE_ANONYMOUS_SHMEM ) || !defined( MAP_HUGETLB )
564+ #if !defined(MAP_HUGETLB )
570565 if (huge_pages == HUGE_PAGES_ON )
571566 ereport (ERROR ,
572567 (errcode (ERRCODE_FEATURE_NOT_SUPPORTED ),
@@ -576,18 +571,19 @@ PGSharedMemoryCreate(Size size, bool makePrivate, int port,
576571 /* Room for a header? */
577572 Assert (size > MAXALIGN (sizeof (PGShmemHeader )));
578573
579- #ifdef USE_ANONYMOUS_SHMEM
580- AnonymousShmem = CreateAnonymousSegment (& size );
581- AnonymousShmemSize = size ;
574+ if (shared_memory_type == SHMEM_TYPE_MMAP )
575+ {
576+ AnonymousShmem = CreateAnonymousSegment (& size );
577+ AnonymousShmemSize = size ;
582578
583- /* Register on-exit routine to unmap the anonymous segment */
584- on_shmem_exit (AnonymousShmemDetach , (Datum ) 0 );
579+ /* Register on-exit routine to unmap the anonymous segment */
580+ on_shmem_exit (AnonymousShmemDetach , (Datum ) 0 );
585581
586- /* Now we need only allocate a minimal-sized SysV shmem block. */
587- sysvsize = sizeof (PGShmemHeader );
588- #else
589- sysvsize = size ;
590- #endif
582+ /* Now we need only allocate a minimal-sized SysV shmem block. */
583+ sysvsize = sizeof (PGShmemHeader );
584+ }
585+ else
586+ sysvsize = size ;
591587
592588 /* Make sure PGSharedMemoryAttach doesn't fail without need */
593589 UsedShmemSegAddr = NULL ;
@@ -687,14 +683,10 @@ PGSharedMemoryCreate(Size size, bool makePrivate, int port,
687683 * block. Otherwise, the System V shared memory block is only a shim, and
688684 * we must return a pointer to the real block.
689685 */
690- #ifdef USE_ANONYMOUS_SHMEM
691686 if (AnonymousShmem == NULL )
692687 return hdr ;
693688 memcpy (AnonymousShmem , hdr , sizeof (PGShmemHeader ));
694689 return (PGShmemHeader * ) AnonymousShmem ;
695- #else
696- return hdr ;
697- #endif
698690}
699691
700692#ifdef EXEC_BACKEND
@@ -801,15 +793,13 @@ PGSharedMemoryDetach(void)
801793 UsedShmemSegAddr = NULL ;
802794 }
803795
804- #ifdef USE_ANONYMOUS_SHMEM
805796 if (AnonymousShmem != NULL )
806797 {
807798 if (munmap (AnonymousShmem , AnonymousShmemSize ) < 0 )
808799 elog (LOG , "munmap(%p, %zu) failed: %m" ,
809800 AnonymousShmem , AnonymousShmemSize );
810801 AnonymousShmem = NULL ;
811802 }
812- #endif
813803}
814804
815805
0 commit comments