Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f80e13f

Browse files
committed
[refer #PGPRO-3213] Add shared_memory_type=posix
1 parent 0ea4d9b commit f80e13f

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

src/backend/port/sysv_shmem.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ unsigned long UsedShmemSegID = 0;
9898
void *UsedShmemSegAddr = NULL;
9999

100100
void *AnonymousShmem = NULL;
101+
int AnonymousShmemFile = -1;
101102
static Size AnonymousShmemSize;
102103

103104
static void *InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size);
@@ -540,26 +541,31 @@ CreateAnonymousSegment(Size *size)
540541
void *hint = NULL;
541542
int mmap_flags = PG_MMAP_FLAGS;
542543

543-
if (OnlineUpgradePath)
544+
if (shared_memory_type == SHMEM_TYPE_POSIX)
544545
{
545-
int shm_flags = O_RDWR;
546-
char path[64];
547546
mmap_flags &= ~MAP_ANONYMOUS;
548-
sprintf(path, "/%d", MyProcPid);
549-
550547
if (IsOnlineUpgrade)
551548
{
549+
Assert(AnonymousShmemFile >= 0);
550+
fd = AnonymousShmemFile;
552551
mmap_flags |= MAP_FIXED;
553552
hint = AnonymousShmem;
554553
}
555554
else
556555
{
557-
shm_flags |= O_CREAT|O_TRUNC;
556+
int flags;
557+
char path[64];
558+
sprintf(path, "/%d", MyProcPid);
559+
fd = shm_open(path, O_CREAT|O_TRUNC|O_RDWR, pg_file_create_mode);
560+
if (fd < 0)
561+
ereport(FATAL,
562+
(errmsg("could not create shared memory object: %m"), 0));
563+
AnonymousShmemFile = fd;
564+
shm_unlink(path);
565+
flags = fcntl(fd, F_GETFD);
566+
flags &= ~FD_CLOEXEC;
567+
fcntl(fd, F_SETFD, flags);
558568
}
559-
fd = shm_open(path, shm_flags, pg_file_create_mode);
560-
if (fd < 0)
561-
ereport(FATAL,
562-
(errmsg("could not create shared memory object: %m"), 0));
563569
}
564570

565571
#ifndef MAP_HUGETLB
@@ -692,7 +698,7 @@ PGSharedMemoryCreate(Size size,
692698
/* Room for a header? */
693699
Assert(size > MAXALIGN(sizeof(PGShmemHeader)));
694700

695-
if (shared_memory_type == SHMEM_TYPE_MMAP)
701+
if (shared_memory_type != SHMEM_TYPE_SYSV)
696702
{
697703
AnonymousShmem = CreateAnonymousSegment(&size);
698704
AnonymousShmemSize = size;

src/backend/postmaster/postmaster.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ typedef struct
575575
#endif
576576
void *UsedShmemSegAddr;
577577
void *AnonymousShmem;
578+
int AnonymousShmemFile;
578579
slock_t *ShmemLock;
579580
VariableCache ShmemVariableCache;
580581
#ifndef HAVE_SPINLOCKS
@@ -624,6 +625,7 @@ static bool SavePostmasterParameters(void)
624625
param.UsedShmemSegID = UsedShmemSegID;
625626
param.UsedShmemSegAddr = UsedShmemSegAddr;
626627
param.AnonymousShmem = AnonymousShmem;
628+
param.AnonymousShmemFile = AnonymousShmemFile;
627629
param.ShmemLock = ShmemLock;
628630
param.ShmemVariableCache = ShmemVariableCache;
629631

@@ -724,6 +726,7 @@ RestorePostmasterParameters(void)
724726
UsedShmemSegID = param.UsedShmemSegID;
725727
UsedShmemSegAddr = param.UsedShmemSegAddr;
726728
AnonymousShmem = param.AnonymousShmem;
729+
AnonymousShmemFile = param.AnonymousShmemFile;
727730

728731
ShmemLock = param.ShmemLock;
729732
ShmemVariableCache = param.ShmemVariableCache;

src/backend/utils/misc/guc.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ static bool check_wal_consistency_checking(char **newval, void **extra,
160160
GucSource source);
161161
static void assign_wal_consistency_checking(const char *newval, void *extra);
162162

163+
static bool check_online_update_support(char **newval, void **extra, GucSource source);
164+
163165
#ifdef HAVE_SYSLOG
164166
static int syslog_facility = LOG_LOCAL0;
165167
#else
@@ -460,6 +462,7 @@ const struct config_enum_entry ssl_protocol_versions_info[] = {
460462
static struct config_enum_entry shared_memory_options[] = {
461463
#ifndef WIN32
462464
{"sysv", SHMEM_TYPE_SYSV, false},
465+
{"posix", SHMEM_TYPE_POSIX, false},
463466
#endif
464467
#ifndef EXEC_BACKEND
465468
{"mmap", SHMEM_TYPE_MMAP, false},
@@ -3675,8 +3678,8 @@ static struct config_string ConfigureNamesString[] =
36753678
GUC_SUPERUSER_ONLY
36763679
},
36773680
&OnlineUpgradePath,
3678-
"",
3679-
NULL, NULL, NULL
3681+
NULL,
3682+
check_online_update_support, NULL, NULL
36803683
},
36813684

36823685
{
@@ -11726,4 +11729,19 @@ check_default_with_oids(bool *newval, void **extra, GucSource source)
1172611729
return true;
1172711730
}
1172811731

11732+
static bool
11733+
check_online_update_support(char **newval, void **extra, GucSource source)
11734+
{
11735+
if (*newval && **newval != '\0')
11736+
{
11737+
if (shared_memory_type == SHMEM_TYPE_MMAP)
11738+
{
11739+
GUC_check_errcode(ERRCODE_FEATURE_NOT_SUPPORTED);
11740+
GUC_check_errmsg("Online upgrade is not possible with shared_memory_type=mmap, please use sysv or posix");
11741+
return false;
11742+
}
11743+
}
11744+
return true;
11745+
}
11746+
1172911747
#include "guc-file.c"

src/include/storage/pg_shmem.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ typedef enum
5858
{
5959
SHMEM_TYPE_WINDOWS,
6060
SHMEM_TYPE_SYSV,
61-
SHMEM_TYPE_MMAP
61+
SHMEM_TYPE_MMAP,
62+
SHMEM_TYPE_POSIX
6263
} PGShmemType;
6364

6465
#ifndef WIN32
@@ -69,6 +70,7 @@ extern void *ShmemProtectiveRegion;
6970
#endif
7071
extern void *UsedShmemSegAddr;
7172
extern void *AnonymousShmem;
73+
extern int AnonymousShmemFile;
7274

7375
#if !defined(WIN32) && !defined(EXEC_BACKEND)
7476
#define DEFAULT_SHARED_MEMORY_TYPE SHMEM_TYPE_MMAP

0 commit comments

Comments
 (0)