76 void **impl_private,
void **mapped_address,
77 Size *mapped_size,
int elevel);
78 static int dsm_impl_posix_resize(
int fd, off_t size);
82 void **impl_private,
void **mapped_address,
83 Size *mapped_size,
int elevel);
85 #ifdef USE_DSM_WINDOWS
87 void **impl_private,
void **mapped_address,
88 Size *mapped_size,
int elevel);
92 void **impl_private,
void **mapped_address,
93 Size *mapped_size,
int elevel);
104 #ifdef USE_DSM_WINDOWS
120 #define ZBUFFER_SIZE 8192
122 #define SEGMENT_NAME_PREFIX "Global/PostgreSQL"
162 void **impl_private,
void **mapped_address,
Size *mapped_size,
167 (*mapped_address == NULL && *mapped_size == 0));
173 return dsm_impl_posix(op, handle, request_size, impl_private,
174 mapped_address, mapped_size, elevel);
179 mapped_address, mapped_size, elevel);
181 #ifdef USE_DSM_WINDOWS
183 return dsm_impl_windows(op, handle, request_size, impl_private,
184 mapped_address, mapped_size, elevel);
189 mapped_address, mapped_size, elevel);
192 elog(
ERROR,
"unexpected dynamic shared memory type: %d",
215 void **impl_private,
void **mapped_address,
Size *mapped_size,
228 if (*mapped_address != NULL
229 && munmap(*mapped_address, *mapped_size) != 0)
233 errmsg(
"could not unmap shared memory segment \"%s\": %m",
237 *mapped_address = NULL;
243 errmsg(
"could not remove shared memory segment \"%s\": %m",
260 flags = O_RDWR | (op ==
DSM_OP_CREATE ? O_CREAT | O_EXCL : 0);
267 errmsg(
"could not open shared memory segment \"%s\": %m",
292 errmsg(
"could not stat shared memory segment \"%s\": %m",
296 request_size = st.st_size;
298 else if (dsm_impl_posix_resize(
fd, request_size) != 0)
319 errmsg(
"could not resize shared memory segment \"%s\" to %zu bytes: %m",
320 name, request_size)));
325 address = mmap(NULL, request_size, PROT_READ | PROT_WRITE,
341 errmsg(
"could not map shared memory segment \"%s\": %m",
345 *mapped_address = address;
346 *mapped_size = request_size;
361 dsm_impl_posix_resize(
int fd, off_t size)
376 #if defined(HAVE_POSIX_FALLOCATE) && defined(__linux__)
387 rc = posix_fallocate(
fd, 0, size);
416 void **impl_private,
void **mapped_address,
Size *mapped_size,
462 elog(
DEBUG4,
"System V shared memory key may not be IPC_PRIVATE");
472 if (*impl_private != NULL)
474 ident_cache = *impl_private;
475 ident = *ident_cache;
498 segsize = request_size;
501 if ((ident = shmget(
key, segsize, flags)) == -1)
505 int save_errno = errno;
511 errmsg(
"could not get shared memory segment: %m")));
516 *ident_cache = ident;
517 *impl_private = ident_cache;
524 *impl_private = NULL;
525 if (*mapped_address != NULL && shmdt(*mapped_address) != 0)
529 errmsg(
"could not unmap shared memory segment \"%s\": %m",
533 *mapped_address = NULL;
539 errmsg(
"could not remove shared memory segment \"%s\": %m",
551 if (shmctl(ident,
IPC_STAT, &shm) != 0)
555 errmsg(
"could not stat shared memory segment \"%s\": %m",
559 request_size = shm.shm_segsz;
564 if (address == (
void *) -1)
576 errmsg(
"could not map shared memory segment \"%s\": %m",
580 *mapped_address = address;
581 *mapped_size = request_size;
587 #ifdef USE_DSM_WINDOWS
603 void **impl_private,
void **mapped_address,
604 Size *mapped_size,
int elevel)
609 MEMORY_BASIC_INFORMATION info;
627 if (*mapped_address != NULL
628 && UnmapViewOfFile(*mapped_address) == 0)
633 errmsg(
"could not unmap shared memory segment \"%s\": %m",
637 if (*impl_private != NULL
638 && CloseHandle(*impl_private) == 0)
643 errmsg(
"could not remove shared memory segment \"%s\": %m",
648 *impl_private = NULL;
649 *mapped_address = NULL;
663 size_high = request_size >> 32;
667 size_low = (DWORD) request_size;
672 hmap = CreateFileMapping(INVALID_HANDLE_VALUE,
680 if (
errcode == ERROR_ALREADY_EXISTS ||
errcode == ERROR_ACCESS_DENIED)
699 errmsg(
"could not create shared memory segment \"%s\": %m",
706 hmap = OpenFileMapping(FILE_MAP_WRITE | FILE_MAP_READ,
714 errmsg(
"could not open shared memory segment \"%s\": %m",
721 address = MapViewOfFile(hmap, FILE_MAP_WRITE | FILE_MAP_READ,
735 errmsg(
"could not map shared memory segment \"%s\": %m",
746 if (VirtualQuery(address, &info,
sizeof(info)) == 0)
753 UnmapViewOfFile(address);
759 errmsg(
"could not stat shared memory segment \"%s\": %m",
764 *mapped_address = address;
765 *mapped_size = info.RegionSize;
766 *impl_private = hmap;
785 void **impl_private,
void **mapped_address,
Size *mapped_size,
799 if (*mapped_address != NULL
800 && munmap(*mapped_address, *mapped_size) != 0)
804 errmsg(
"could not unmap shared memory segment \"%s\": %m",
808 *mapped_address = NULL;
814 errmsg(
"could not remove shared memory segment \"%s\": %m",
822 flags = O_RDWR | (op ==
DSM_OP_CREATE ? O_CREAT | O_EXCL : 0);
828 errmsg(
"could not open shared memory segment \"%s\": %m",
852 errmsg(
"could not stat shared memory segment \"%s\": %m",
884 if (
write(
fd, zbuffer, goal) == goal)
899 errno = save_errno ? save_errno : ENOSPC;
903 errmsg(
"could not resize shared memory segment \"%s\" to %zu bytes: %m",
904 name, request_size)));
910 address = mmap(NULL, request_size, PROT_READ | PROT_WRITE,
925 errmsg(
"could not map shared memory segment \"%s\": %m",
929 *mapped_address = address;
930 *mapped_size = request_size;
936 errmsg(
"could not close shared memory segment \"%s\": %m",
956 void **impl_private_pm_handle)
960 #ifdef USE_DSM_WINDOWS
966 if (!DuplicateHandle(GetCurrentProcess(), impl_private,
967 PostmasterHandle, &hmap, 0, FALSE,
968 DUPLICATE_SAME_ACCESS))
976 errmsg(
"could not duplicate handle for \"%s\": %m",
987 *impl_private_pm_handle = hmap;
1010 #ifdef USE_DSM_WINDOWS
1014 if (*impl_private &&
1015 !DuplicateHandle(PostmasterHandle, *impl_private,
1016 NULL, NULL, 0, FALSE,
1017 DUPLICATE_CLOSE_SOURCE))
1025 errmsg(
"could not duplicate handle for \"%s\": %m",
1029 *impl_private = NULL;
1041 if (errno == EFBIG || errno == ENOMEM)
1042 return errcode(ERRCODE_OUT_OF_MEMORY);
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)
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_mmap(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
#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
volatile sig_atomic_t QueryCancelPending
volatile sig_atomic_t ProcDiePending
Assert(fmt[strlen(fmt) - 1] !='\n')
void pfree(void *pointer)
MemoryContext TopMemoryContext
void * palloc0(Size size)
void * MemoryContextAlloc(MemoryContext context, Size size)
#define CHECK_FOR_INTERRUPTS()
static int fd(const char *x, int i)
@ WAIT_EVENT_DSM_FILL_ZERO_WRITE
static void pgstat_report_wait_start(uint32 wait_event_info)
static void pgstat_report_wait_end(void)
void _dosmaperr(unsigned long)