74 void **impl_private,
void **mapped_address,
75 Size *mapped_size,
int elevel);
80 void **impl_private,
void **mapped_address,
81 Size *mapped_size,
int elevel);
83 #ifdef USE_DSM_WINDOWS
85 void **impl_private,
void **mapped_address,
86 Size *mapped_size,
int elevel);
90 void **impl_private,
void **mapped_address,
91 Size *mapped_size,
int elevel);
102 #ifdef USE_DSM_WINDOWS
118 #define ZBUFFER_SIZE 8192
120 #define SEGMENT_NAME_PREFIX "Global/PostgreSQL"
160 void **impl_private,
void **mapped_address,
Size *mapped_size,
165 (*mapped_address == NULL && *mapped_size == 0));
172 mapped_address, mapped_size, elevel);
177 mapped_address, mapped_size, elevel);
179 #ifdef USE_DSM_WINDOWS
181 return dsm_impl_windows(op, handle, request_size, impl_private,
182 mapped_address, mapped_size, elevel);
187 mapped_address, mapped_size, elevel);
190 elog(
ERROR,
"unexpected dynamic shared memory type: %d",
213 void **impl_private,
void **mapped_address,
Size *mapped_size,
226 if (*mapped_address != NULL
227 && munmap(*mapped_address, *mapped_size) != 0)
231 errmsg(
"could not unmap shared memory segment \"%s\": %m",
235 *mapped_address = NULL;
241 errmsg(
"could not remove shared memory segment \"%s\": %m",
258 flags = O_RDWR | (op ==
DSM_OP_CREATE ? O_CREAT | O_EXCL : 0);
265 errmsg(
"could not open shared memory segment \"%s\": %m",
290 errmsg(
"could not stat shared memory segment \"%s\": %m",
309 errmsg(
"could not resize shared memory segment \"%s\" to %zu bytes: %m",
310 name, request_size)));
315 address = mmap(NULL, request_size, PROT_READ | PROT_WRITE,
331 errmsg(
"could not map shared memory segment \"%s\": %m",
335 *mapped_address = address;
336 *mapped_size = request_size;
355 sigset_t save_sigmask;
364 sigprocmask(SIG_SETMASK, &
BlockSig, &save_sigmask);
367 #if defined(HAVE_POSIX_FALLOCATE) && defined(__linux__)
383 rc = posix_fallocate(
fd, 0,
size);
384 }
while (rc ==
EINTR);
397 }
while (rc < 0 && errno ==
EINTR);
404 sigprocmask(SIG_SETMASK, &save_sigmask, NULL);
424 void **impl_private,
void **mapped_address,
Size *mapped_size,
470 elog(
DEBUG4,
"System V shared memory key may not be IPC_PRIVATE");
480 if (*impl_private != NULL)
482 ident_cache = *impl_private;
483 ident = *ident_cache;
506 segsize = request_size;
509 if ((
ident = shmget(
key, segsize, flags)) == -1)
513 int save_errno = errno;
519 errmsg(
"could not get shared memory segment: %m")));
524 *ident_cache =
ident;
525 *impl_private = ident_cache;
532 *impl_private = NULL;
533 if (*mapped_address != NULL && shmdt(*mapped_address) != 0)
537 errmsg(
"could not unmap shared memory segment \"%s\": %m",
541 *mapped_address = NULL;
547 errmsg(
"could not remove shared memory segment \"%s\": %m",
563 errmsg(
"could not stat shared memory segment \"%s\": %m",
567 request_size = shm.shm_segsz;
572 if (address == (
void *) -1)
584 errmsg(
"could not map shared memory segment \"%s\": %m",
588 *mapped_address = address;
589 *mapped_size = request_size;
595 #ifdef USE_DSM_WINDOWS
611 void **impl_private,
void **mapped_address,
612 Size *mapped_size,
int elevel)
617 MEMORY_BASIC_INFORMATION info;
635 if (*mapped_address != NULL
636 && UnmapViewOfFile(*mapped_address) == 0)
641 errmsg(
"could not unmap shared memory segment \"%s\": %m",
645 if (*impl_private != NULL
646 && CloseHandle(*impl_private) == 0)
651 errmsg(
"could not remove shared memory segment \"%s\": %m",
656 *impl_private = NULL;
657 *mapped_address = NULL;
671 size_high = request_size >> 32;
675 size_low = (DWORD) request_size;
680 hmap = CreateFileMapping(INVALID_HANDLE_VALUE,
688 if (
errcode == ERROR_ALREADY_EXISTS ||
errcode == ERROR_ACCESS_DENIED)
707 errmsg(
"could not create shared memory segment \"%s\": %m",
714 hmap = OpenFileMapping(FILE_MAP_WRITE | FILE_MAP_READ,
722 errmsg(
"could not open shared memory segment \"%s\": %m",
729 address = MapViewOfFile(hmap, FILE_MAP_WRITE | FILE_MAP_READ,
743 errmsg(
"could not map shared memory segment \"%s\": %m",
754 if (VirtualQuery(address, &info,
sizeof(info)) == 0)
761 UnmapViewOfFile(address);
767 errmsg(
"could not stat shared memory segment \"%s\": %m",
772 *mapped_address = address;
773 *mapped_size = info.RegionSize;
774 *impl_private = hmap;
793 void **impl_private,
void **mapped_address,
Size *mapped_size,
807 if (*mapped_address != NULL
808 && munmap(*mapped_address, *mapped_size) != 0)
812 errmsg(
"could not unmap shared memory segment \"%s\": %m",
816 *mapped_address = NULL;
822 errmsg(
"could not remove shared memory segment \"%s\": %m",
830 flags = O_RDWR | (op ==
DSM_OP_CREATE ? O_CREAT | O_EXCL : 0);
836 errmsg(
"could not open shared memory segment \"%s\": %m",
860 errmsg(
"could not stat shared memory segment \"%s\": %m",
892 if (
write(
fd, zbuffer, goal) == goal)
907 errno = save_errno ? save_errno : ENOSPC;
911 errmsg(
"could not resize shared memory segment \"%s\" to %zu bytes: %m",
912 name, request_size)));
918 address = mmap(NULL, request_size, PROT_READ | PROT_WRITE,
933 errmsg(
"could not map shared memory segment \"%s\": %m",
937 *mapped_address = address;
938 *mapped_size = request_size;
944 errmsg(
"could not close shared memory segment \"%s\": %m",
964 void **impl_private_pm_handle)
968 #ifdef USE_DSM_WINDOWS
974 if (!DuplicateHandle(GetCurrentProcess(), impl_private,
975 PostmasterHandle, &hmap, 0, FALSE,
976 DUPLICATE_SAME_ACCESS))
984 errmsg(
"could not duplicate handle for \"%s\": %m",
995 *impl_private_pm_handle = hmap;
1018 #ifdef USE_DSM_WINDOWS
1022 if (*impl_private &&
1023 !DuplicateHandle(PostmasterHandle, *impl_private,
1024 NULL, NULL, 0, FALSE,
1025 DUPLICATE_CLOSE_SOURCE))
1033 errmsg(
"could not duplicate handle for \"%s\": %m",
1037 *impl_private = NULL;
1049 if (errno == EFBIG || errno == ENOMEM)
1050 return errcode(ERRCODE_OUT_OF_MEMORY);
#define Assert(condition)
void dsm_impl_pin_segment(dsm_handle handle, void *impl_private, void **impl_private_pm_handle)
int min_dynamic_shared_memory
static int errcode_for_dynamic_shared_memory(void)
#define SEGMENT_NAME_PREFIX
void dsm_impl_unpin_segment(dsm_handle handle, void **impl_private)
static int dsm_impl_posix_resize(int fd, off_t size)
bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
int dynamic_shared_memory_type
static bool dsm_impl_sysv(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
const struct config_enum_entry dynamic_shared_memory_options[]
static bool dsm_impl_posix(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
static bool dsm_impl_mmap(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
#define DEFAULT_DYNAMIC_SHARED_MEMORY_TYPE
#define PG_DYNSHMEM_MMAP_FILE_PREFIX
int errcode_for_file_access(void)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
int CloseTransientFile(int fd)
void ReleaseExternalFD(void)
void ReserveExternalFD(void)
int OpenTransientFile(const char *fileName, int fileFlags)
#define PG_FILE_MODE_OWNER
void pfree(void *pointer)
MemoryContext TopMemoryContext
void * palloc0(Size size)
void * MemoryContextAlloc(MemoryContext context, Size size)
static int fd(const char *x, int i)
static pg_noinline void Size size
static void pgstat_report_wait_start(uint32 wait_event_info)
static void pgstat_report_wait_end(void)
void _dosmaperr(unsigned long)