PostgreSQL Source Code git master
dsm_impl.c File Reference
#include "postgres.h"
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include "common/file_perm.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "portability/mem.h"
#include "postmaster/postmaster.h"
#include "storage/dsm_impl.h"
#include "storage/fd.h"
#include "utils/guc.h"
#include "utils/memutils.h"
Include dependency graph for dsm_impl.c:

Go to the source code of this file.

Macros

#define ZBUFFER_SIZE   8192
 
#define SEGMENT_NAME_PREFIX   "Global/PostgreSQL"
 

Functions

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 int dsm_impl_posix_resize (int fd, off_t size)
 
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)
 
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)
 
static int errcode_for_dynamic_shared_memory (void)
 
bool dsm_impl_op (dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
 
void dsm_impl_pin_segment (dsm_handle handle, void *impl_private, void **impl_private_pm_handle)
 
void dsm_impl_unpin_segment (dsm_handle handle, void **impl_private)
 

Variables

const struct config_enum_entry dynamic_shared_memory_options []
 
int dynamic_shared_memory_type = DEFAULT_DYNAMIC_SHARED_MEMORY_TYPE
 
int min_dynamic_shared_memory
 

Macro Definition Documentation

◆ SEGMENT_NAME_PREFIX

#define SEGMENT_NAME_PREFIX   "Global/PostgreSQL"

Definition at line 120 of file dsm_impl.c.

◆ ZBUFFER_SIZE

#define ZBUFFER_SIZE   8192

Definition at line 118 of file dsm_impl.c.

Function Documentation

◆ dsm_impl_mmap()

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 
)
static

Definition at line 792 of file dsm_impl.c.

795{
796 char name[64];
797 int flags;
798 int fd;
799 char *address;
800
802 handle);
803
804 /* Handle teardown cases. */
805 if (op == DSM_OP_DETACH || op == DSM_OP_DESTROY)
806 {
807 if (*mapped_address != NULL
808 && munmap(*mapped_address, *mapped_size) != 0)
809 {
810 ereport(elevel,
812 errmsg("could not unmap shared memory segment \"%s\": %m",
813 name)));
814 return false;
815 }
816 *mapped_address = NULL;
817 *mapped_size = 0;
818 if (op == DSM_OP_DESTROY && unlink(name) != 0)
819 {
820 ereport(elevel,
822 errmsg("could not remove shared memory segment \"%s\": %m",
823 name)));
824 return false;
825 }
826 return true;
827 }
828
829 /* Create new segment or open an existing one for attach. */
830 flags = O_RDWR | (op == DSM_OP_CREATE ? O_CREAT | O_EXCL : 0);
831 if ((fd = OpenTransientFile(name, flags)) == -1)
832 {
833 if (op == DSM_OP_ATTACH || errno != EEXIST)
834 ereport(elevel,
836 errmsg("could not open shared memory segment \"%s\": %m",
837 name)));
838 return false;
839 }
840
841 /*
842 * If we're attaching the segment, determine the current size; if we are
843 * creating the segment, set the size to the requested value.
844 */
845 if (op == DSM_OP_ATTACH)
846 {
847 struct stat st;
848
849 if (fstat(fd, &st) != 0)
850 {
851 int save_errno;
852
853 /* Back out what's already been done. */
854 save_errno = errno;
856 errno = save_errno;
857
858 ereport(elevel,
860 errmsg("could not stat shared memory segment \"%s\": %m",
861 name)));
862 return false;
863 }
864 request_size = st.st_size;
865 }
866 else
867 {
868 /*
869 * Allocate a buffer full of zeros.
870 *
871 * Note: palloc zbuffer, instead of just using a local char array, to
872 * ensure it is reasonably well-aligned; this may save a few cycles
873 * transferring data to the kernel.
874 */
875 char *zbuffer = (char *) palloc0(ZBUFFER_SIZE);
876 Size remaining = request_size;
877 bool success = true;
878
879 /*
880 * Zero-fill the file. We have to do this the hard way to ensure that
881 * all the file space has really been allocated, so that we don't
882 * later seg fault when accessing the memory mapping. This is pretty
883 * pessimal.
884 */
885 while (success && remaining > 0)
886 {
887 Size goal = remaining;
888
889 if (goal > ZBUFFER_SIZE)
890 goal = ZBUFFER_SIZE;
891 pgstat_report_wait_start(WAIT_EVENT_DSM_FILL_ZERO_WRITE);
892 if (write(fd, zbuffer, goal) == goal)
893 remaining -= goal;
894 else
895 success = false;
897 }
898
899 if (!success)
900 {
901 int save_errno;
902
903 /* Back out what's already been done. */
904 save_errno = errno;
906 unlink(name);
907 errno = save_errno ? save_errno : ENOSPC;
908
909 ereport(elevel,
911 errmsg("could not resize shared memory segment \"%s\" to %zu bytes: %m",
912 name, request_size)));
913 return false;
914 }
915 }
916
917 /* Map it. */
918 address = mmap(NULL, request_size, PROT_READ | PROT_WRITE,
919 MAP_SHARED | MAP_HASSEMAPHORE | MAP_NOSYNC, fd, 0);
920 if (address == MAP_FAILED)
921 {
922 int save_errno;
923
924 /* Back out what's already been done. */
925 save_errno = errno;
927 if (op == DSM_OP_CREATE)
928 unlink(name);
929 errno = save_errno;
930
931 ereport(elevel,
933 errmsg("could not map shared memory segment \"%s\": %m",
934 name)));
935 return false;
936 }
937 *mapped_address = address;
938 *mapped_size = request_size;
939
940 if (CloseTransientFile(fd) != 0)
941 {
942 ereport(elevel,
944 errmsg("could not close shared memory segment \"%s\": %m",
945 name)));
946 return false;
947 }
948
949 return true;
950}
size_t Size
Definition: c.h:562
static int errcode_for_dynamic_shared_memory(void)
Definition: dsm_impl.c:1047
#define ZBUFFER_SIZE
Definition: dsm_impl.c:118
@ DSM_OP_DETACH
Definition: dsm_impl.h:65
@ DSM_OP_CREATE
Definition: dsm_impl.h:63
@ DSM_OP_DESTROY
Definition: dsm_impl.h:66
@ DSM_OP_ATTACH
Definition: dsm_impl.h:64
#define PG_DYNSHMEM_MMAP_FILE_PREFIX
Definition: dsm_impl.h:52
#define PG_DYNSHMEM_DIR
Definition: dsm_impl.h:51
int errcode_for_file_access(void)
Definition: elog.c:876
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereport(elevel,...)
Definition: elog.h:149
int CloseTransientFile(int fd)
Definition: fd.c:2831
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2655
int remaining
Definition: informix.c:692
static bool success
Definition: initdb.c:186
#define write(a, b, c)
Definition: win32.h:14
void * palloc0(Size size)
Definition: mcxt.c:1347
#define MAP_FAILED
Definition: mem.h:45
#define MAP_HASSEMAPHORE
Definition: mem.h:30
#define MAP_NOSYNC
Definition: mem.h:38
#define snprintf
Definition: port.h:238
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:85
static void pgstat_report_wait_end(void)
Definition: wait_event.h:101
const char * name
#define fstat
Definition: win32_port.h:281

References CloseTransientFile(), DSM_OP_ATTACH, DSM_OP_CREATE, DSM_OP_DESTROY, DSM_OP_DETACH, ereport, errcode_for_dynamic_shared_memory(), errcode_for_file_access(), errmsg(), fd(), fstat, MAP_FAILED, MAP_HASSEMAPHORE, MAP_NOSYNC, name, OpenTransientFile(), palloc0(), PG_DYNSHMEM_DIR, PG_DYNSHMEM_MMAP_FILE_PREFIX, pgstat_report_wait_end(), pgstat_report_wait_start(), remaining, snprintf, stat::st_size, success, write, and ZBUFFER_SIZE.

Referenced by dsm_impl_op().

◆ dsm_impl_op()

bool dsm_impl_op ( dsm_op  op,
dsm_handle  handle,
Size  request_size,
void **  impl_private,
void **  mapped_address,
Size mapped_size,
int  elevel 
)

Definition at line 159 of file dsm_impl.c.

162{
163 Assert(op == DSM_OP_CREATE || request_size == 0);
164 Assert((op != DSM_OP_CREATE && op != DSM_OP_ATTACH) ||
165 (*mapped_address == NULL && *mapped_size == 0));
166
168 {
169#ifdef USE_DSM_POSIX
170 case DSM_IMPL_POSIX:
171 return dsm_impl_posix(op, handle, request_size, impl_private,
172 mapped_address, mapped_size, elevel);
173#endif
174#ifdef USE_DSM_SYSV
175 case DSM_IMPL_SYSV:
176 return dsm_impl_sysv(op, handle, request_size, impl_private,
177 mapped_address, mapped_size, elevel);
178#endif
179#ifdef USE_DSM_WINDOWS
180 case DSM_IMPL_WINDOWS:
181 return dsm_impl_windows(op, handle, request_size, impl_private,
182 mapped_address, mapped_size, elevel);
183#endif
184#ifdef USE_DSM_MMAP
185 case DSM_IMPL_MMAP:
186 return dsm_impl_mmap(op, handle, request_size, impl_private,
187 mapped_address, mapped_size, elevel);
188#endif
189 default:
190 elog(ERROR, "unexpected dynamic shared memory type: %d",
192 return false;
193 }
194}
#define Assert(condition)
Definition: c.h:815
int dynamic_shared_memory_type
Definition: dsm_impl.c:112
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)
Definition: dsm_impl.c:423
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)
Definition: dsm_impl.c:212
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)
Definition: dsm_impl.c:792
#define DSM_IMPL_WINDOWS
Definition: dsm_impl.h:19
#define DSM_IMPL_POSIX
Definition: dsm_impl.h:17
#define DSM_IMPL_SYSV
Definition: dsm_impl.h:18
#define DSM_IMPL_MMAP
Definition: dsm_impl.h:20
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225

References Assert, dsm_impl_mmap(), DSM_IMPL_MMAP, dsm_impl_posix(), DSM_IMPL_POSIX, dsm_impl_sysv(), DSM_IMPL_SYSV, DSM_IMPL_WINDOWS, DSM_OP_ATTACH, DSM_OP_CREATE, dynamic_shared_memory_type, elog, and ERROR.

Referenced by dsm_attach(), dsm_backend_startup(), dsm_cleanup_using_control_segment(), dsm_create(), dsm_detach(), dsm_detach_all(), dsm_postmaster_shutdown(), dsm_postmaster_startup(), and dsm_unpin_segment().

◆ dsm_impl_pin_segment()

void dsm_impl_pin_segment ( dsm_handle  handle,
void *  impl_private,
void **  impl_private_pm_handle 
)

Definition at line 963 of file dsm_impl.c.

965{
967 {
968#ifdef USE_DSM_WINDOWS
969 case DSM_IMPL_WINDOWS:
971 {
972 HANDLE hmap;
973
974 if (!DuplicateHandle(GetCurrentProcess(), impl_private,
975 PostmasterHandle, &hmap, 0, FALSE,
976 DUPLICATE_SAME_ACCESS))
977 {
978 char name[64];
979
980 snprintf(name, 64, "%s.%u", SEGMENT_NAME_PREFIX, handle);
981 _dosmaperr(GetLastError());
984 errmsg("could not duplicate handle for \"%s\": %m",
985 name)));
986 }
987
988 /*
989 * Here, we remember the handle that we created in the
990 * postmaster process. This handle isn't actually usable in
991 * any process other than the postmaster, but that doesn't
992 * matter. We're just holding onto it so that, if the segment
993 * is unpinned, dsm_impl_unpin_segment can close it.
994 */
995 *impl_private_pm_handle = hmap;
996 }
997 break;
998#endif
999 default:
1000 break;
1001 }
1002}
#define SEGMENT_NAME_PREFIX
Definition: dsm_impl.c:120
bool IsUnderPostmaster
Definition: globals.c:119
void _dosmaperr(unsigned long)
Definition: win32error.c:177

References _dosmaperr(), DSM_IMPL_WINDOWS, dynamic_shared_memory_type, ereport, errcode_for_dynamic_shared_memory(), errmsg(), ERROR, IsUnderPostmaster, name, SEGMENT_NAME_PREFIX, and snprintf.

Referenced by dsm_pin_segment().

◆ dsm_impl_posix()

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

Definition at line 212 of file dsm_impl.c.

215{
216 char name[64];
217 int flags;
218 int fd;
219 char *address;
220
221 snprintf(name, 64, "/PostgreSQL.%u", handle);
222
223 /* Handle teardown cases. */
224 if (op == DSM_OP_DETACH || op == DSM_OP_DESTROY)
225 {
226 if (*mapped_address != NULL
227 && munmap(*mapped_address, *mapped_size) != 0)
228 {
229 ereport(elevel,
231 errmsg("could not unmap shared memory segment \"%s\": %m",
232 name)));
233 return false;
234 }
235 *mapped_address = NULL;
236 *mapped_size = 0;
237 if (op == DSM_OP_DESTROY && shm_unlink(name) != 0)
238 {
239 ereport(elevel,
241 errmsg("could not remove shared memory segment \"%s\": %m",
242 name)));
243 return false;
244 }
245 return true;
246 }
247
248 /*
249 * Create new segment or open an existing one for attach.
250 *
251 * Even though we will close the FD before returning, it seems desirable
252 * to use Reserve/ReleaseExternalFD, to reduce the probability of EMFILE
253 * failure. The fact that we won't hold the FD open long justifies using
254 * ReserveExternalFD rather than AcquireExternalFD, though.
255 */
257
258 flags = O_RDWR | (op == DSM_OP_CREATE ? O_CREAT | O_EXCL : 0);
259 if ((fd = shm_open(name, flags, PG_FILE_MODE_OWNER)) == -1)
260 {
262 if (op == DSM_OP_ATTACH || errno != EEXIST)
263 ereport(elevel,
265 errmsg("could not open shared memory segment \"%s\": %m",
266 name)));
267 return false;
268 }
269
270 /*
271 * If we're attaching the segment, determine the current size; if we are
272 * creating the segment, set the size to the requested value.
273 */
274 if (op == DSM_OP_ATTACH)
275 {
276 struct stat st;
277
278 if (fstat(fd, &st) != 0)
279 {
280 int save_errno;
281
282 /* Back out what's already been done. */
283 save_errno = errno;
284 close(fd);
286 errno = save_errno;
287
288 ereport(elevel,
290 errmsg("could not stat shared memory segment \"%s\": %m",
291 name)));
292 return false;
293 }
294 request_size = st.st_size;
295 }
296 else if (dsm_impl_posix_resize(fd, request_size) != 0)
297 {
298 int save_errno;
299
300 /* Back out what's already been done. */
301 save_errno = errno;
302 close(fd);
304 shm_unlink(name);
305 errno = save_errno;
306
307 ereport(elevel,
309 errmsg("could not resize shared memory segment \"%s\" to %zu bytes: %m",
310 name, request_size)));
311 return false;
312 }
313
314 /* Map it. */
315 address = mmap(NULL, request_size, PROT_READ | PROT_WRITE,
316 MAP_SHARED | MAP_HASSEMAPHORE | MAP_NOSYNC, fd, 0);
317 if (address == MAP_FAILED)
318 {
319 int save_errno;
320
321 /* Back out what's already been done. */
322 save_errno = errno;
323 close(fd);
325 if (op == DSM_OP_CREATE)
326 shm_unlink(name);
327 errno = save_errno;
328
329 ereport(elevel,
331 errmsg("could not map shared memory segment \"%s\": %m",
332 name)));
333 return false;
334 }
335 *mapped_address = address;
336 *mapped_size = request_size;
337 close(fd);
339
340 return true;
341}
static int dsm_impl_posix_resize(int fd, off_t size)
Definition: dsm_impl.c:351
void ReleaseExternalFD(void)
Definition: fd.c:1238
void ReserveExternalFD(void)
Definition: fd.c:1220
#define PG_FILE_MODE_OWNER
Definition: file_perm.h:38
#define close(a)
Definition: win32.h:12

References close, dsm_impl_posix_resize(), DSM_OP_ATTACH, DSM_OP_CREATE, DSM_OP_DESTROY, DSM_OP_DETACH, ereport, errcode_for_dynamic_shared_memory(), errmsg(), fd(), fstat, MAP_FAILED, MAP_HASSEMAPHORE, MAP_NOSYNC, name, PG_FILE_MODE_OWNER, ReleaseExternalFD(), ReserveExternalFD(), snprintf, and stat::st_size.

Referenced by dsm_impl_op().

◆ dsm_impl_posix_resize()

static int dsm_impl_posix_resize ( int  fd,
off_t  size 
)
static

Definition at line 351 of file dsm_impl.c.

352{
353 int rc;
354 int save_errno;
355 sigset_t save_sigmask;
356
357 /*
358 * Block all blockable signals, except SIGQUIT. posix_fallocate() can run
359 * for quite a long time, and is an all-or-nothing operation. If we
360 * allowed SIGUSR1 to interrupt us repeatedly (for example, due to
361 * recovery conflicts), the retry loop might never succeed.
362 */
364 sigprocmask(SIG_SETMASK, &BlockSig, &save_sigmask);
365
366 pgstat_report_wait_start(WAIT_EVENT_DSM_ALLOCATE);
367#if defined(HAVE_POSIX_FALLOCATE) && defined(__linux__)
368
369 /*
370 * On Linux, a shm_open fd is backed by a tmpfs file. If we were to use
371 * ftruncate, the file would contain a hole. Accessing memory backed by a
372 * hole causes tmpfs to allocate pages, which fails with SIGBUS if there
373 * is no more tmpfs space available. So we ask tmpfs to allocate pages
374 * here, so we can fail gracefully with ENOSPC now rather than risking
375 * SIGBUS later.
376 *
377 * We still use a traditional EINTR retry loop to handle SIGCONT.
378 * posix_fallocate() doesn't restart automatically, and we don't want this
379 * to fail if you attach a debugger.
380 */
381 do
382 {
383 rc = posix_fallocate(fd, 0, size);
384 } while (rc == EINTR);
385
386 /*
387 * The caller expects errno to be set, but posix_fallocate() doesn't set
388 * it. Instead it returns error numbers directly. So set errno, even
389 * though we'll also return rc to indicate success or failure.
390 */
391 errno = rc;
392#else
393 /* Extend the file to the requested size. */
394 do
395 {
396 rc = ftruncate(fd, size);
397 } while (rc < 0 && errno == EINTR);
398#endif
400
402 {
403 save_errno = errno;
404 sigprocmask(SIG_SETMASK, &save_sigmask, NULL);
405 errno = save_errno;
406 }
407
408 return rc;
409}
sigset_t BlockSig
Definition: pqsignal.c:23
static pg_noinline void Size size
Definition: slab.c:607
#define EINTR
Definition: win32_port.h:372

References BlockSig, EINTR, fd(), IsUnderPostmaster, pgstat_report_wait_end(), pgstat_report_wait_start(), and size.

Referenced by dsm_impl_posix().

◆ dsm_impl_sysv()

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 
)
static

Definition at line 423 of file dsm_impl.c.

426{
427 key_t key;
428 int ident;
429 char *address;
430 char name[64];
431 int *ident_cache;
432
433 /*
434 * POSIX shared memory and mmap-based shared memory identify segments with
435 * names. To avoid needless error message variation, we use the handle as
436 * the name.
437 */
438 snprintf(name, 64, "%u", handle);
439
440 /*
441 * The System V shared memory namespace is very restricted; names are of
442 * type key_t, which is expected to be some sort of integer data type, but
443 * not necessarily the same one as dsm_handle. Since we use dsm_handle to
444 * identify shared memory segments across processes, this might seem like
445 * a problem, but it's really not. If dsm_handle is bigger than key_t,
446 * the cast below might truncate away some bits from the handle the
447 * user-provided, but it'll truncate exactly the same bits away in exactly
448 * the same fashion every time we use that handle, which is all that
449 * really matters. Conversely, if dsm_handle is smaller than key_t, we
450 * won't use the full range of available key space, but that's no big deal
451 * either.
452 *
453 * We do make sure that the key isn't negative, because that might not be
454 * portable.
455 */
456 key = (key_t) handle;
457 if (key < 1) /* avoid compiler warning if type is unsigned */
458 key = -key;
459
460 /*
461 * There's one special key, IPC_PRIVATE, which can't be used. If we end
462 * up with that value by chance during a create operation, just pretend it
463 * already exists, so that caller will retry. If we run into it anywhere
464 * else, the caller has passed a handle that doesn't correspond to
465 * anything we ever created, which should not happen.
466 */
467 if (key == IPC_PRIVATE)
468 {
469 if (op != DSM_OP_CREATE)
470 elog(DEBUG4, "System V shared memory key may not be IPC_PRIVATE");
471 errno = EEXIST;
472 return false;
473 }
474
475 /*
476 * Before we can do anything with a shared memory segment, we have to map
477 * the shared memory key to a shared memory identifier using shmget(). To
478 * avoid repeated lookups, we store the key using impl_private.
479 */
480 if (*impl_private != NULL)
481 {
482 ident_cache = *impl_private;
483 ident = *ident_cache;
484 }
485 else
486 {
487 int flags = IPCProtection;
488 size_t segsize;
489
490 /*
491 * Allocate the memory BEFORE acquiring the resource, so that we don't
492 * leak the resource if memory allocation fails.
493 */
494 ident_cache = MemoryContextAlloc(TopMemoryContext, sizeof(int));
495
496 /*
497 * When using shmget to find an existing segment, we must pass the
498 * size as 0. Passing a non-zero size which is greater than the
499 * actual size will result in EINVAL.
500 */
501 segsize = 0;
502
503 if (op == DSM_OP_CREATE)
504 {
505 flags |= IPC_CREAT | IPC_EXCL;
506 segsize = request_size;
507 }
508
509 if ((ident = shmget(key, segsize, flags)) == -1)
510 {
511 if (op == DSM_OP_ATTACH || errno != EEXIST)
512 {
513 int save_errno = errno;
514
515 pfree(ident_cache);
516 errno = save_errno;
517 ereport(elevel,
519 errmsg("could not get shared memory segment: %m")));
520 }
521 return false;
522 }
523
524 *ident_cache = ident;
525 *impl_private = ident_cache;
526 }
527
528 /* Handle teardown cases. */
529 if (op == DSM_OP_DETACH || op == DSM_OP_DESTROY)
530 {
531 pfree(ident_cache);
532 *impl_private = NULL;
533 if (*mapped_address != NULL && shmdt(*mapped_address) != 0)
534 {
535 ereport(elevel,
537 errmsg("could not unmap shared memory segment \"%s\": %m",
538 name)));
539 return false;
540 }
541 *mapped_address = NULL;
542 *mapped_size = 0;
543 if (op == DSM_OP_DESTROY && shmctl(ident, IPC_RMID, NULL) < 0)
544 {
545 ereport(elevel,
547 errmsg("could not remove shared memory segment \"%s\": %m",
548 name)));
549 return false;
550 }
551 return true;
552 }
553
554 /* If we're attaching it, we must use IPC_STAT to determine the size. */
555 if (op == DSM_OP_ATTACH)
556 {
557 struct shmid_ds shm;
558
559 if (shmctl(ident, IPC_STAT, &shm) != 0)
560 {
561 ereport(elevel,
563 errmsg("could not stat shared memory segment \"%s\": %m",
564 name)));
565 return false;
566 }
567 request_size = shm.shm_segsz;
568 }
569
570 /* Map it. */
571 address = shmat(ident, NULL, PG_SHMAT_FLAGS);
572 if (address == (void *) -1)
573 {
574 int save_errno;
575
576 /* Back out what's already been done. */
577 save_errno = errno;
578 if (op == DSM_OP_CREATE)
579 shmctl(ident, IPC_RMID, NULL);
580 errno = save_errno;
581
582 ereport(elevel,
584 errmsg("could not map shared memory segment \"%s\": %m",
585 name)));
586 return false;
587 }
588 *mapped_address = address;
589 *mapped_size = request_size;
590
591 return true;
592}
#define DEBUG4
Definition: elog.h:27
#define ident
Definition: indent_codes.h:47
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1181
void pfree(void *pointer)
Definition: mcxt.c:1521
MemoryContext TopMemoryContext
Definition: mcxt.c:149
#define PG_SHMAT_FLAGS
Definition: mem.h:20
#define IPCProtection
Definition: posix_sema.c:59
#define IPC_STAT
Definition: win32_port.h:98
#define IPC_RMID
Definition: win32_port.h:93
long key_t
Definition: win32_port.h:245
#define IPC_EXCL
Definition: win32_port.h:95
#define IPC_CREAT
Definition: win32_port.h:94
#define IPC_PRIVATE
Definition: win32_port.h:96

References DEBUG4, DSM_OP_ATTACH, DSM_OP_CREATE, DSM_OP_DESTROY, DSM_OP_DETACH, elog, ereport, errcode_for_dynamic_shared_memory(), errmsg(), ident, IPC_CREAT, IPC_EXCL, IPC_PRIVATE, IPC_RMID, IPC_STAT, IPCProtection, sort-test::key, MemoryContextAlloc(), name, pfree(), PG_SHMAT_FLAGS, snprintf, and TopMemoryContext.

Referenced by dsm_impl_op().

◆ dsm_impl_unpin_segment()

void dsm_impl_unpin_segment ( dsm_handle  handle,
void **  impl_private 
)

Definition at line 1014 of file dsm_impl.c.

1015{
1017 {
1018#ifdef USE_DSM_WINDOWS
1019 case DSM_IMPL_WINDOWS:
1021 {
1022 if (*impl_private &&
1023 !DuplicateHandle(PostmasterHandle, *impl_private,
1024 NULL, NULL, 0, FALSE,
1025 DUPLICATE_CLOSE_SOURCE))
1026 {
1027 char name[64];
1028
1029 snprintf(name, 64, "%s.%u", SEGMENT_NAME_PREFIX, handle);
1030 _dosmaperr(GetLastError());
1031 ereport(ERROR,
1033 errmsg("could not duplicate handle for \"%s\": %m",
1034 name)));
1035 }
1036
1037 *impl_private = NULL;
1038 }
1039 break;
1040#endif
1041 default:
1042 break;
1043 }
1044}

References _dosmaperr(), DSM_IMPL_WINDOWS, dynamic_shared_memory_type, ereport, errcode_for_dynamic_shared_memory(), errmsg(), ERROR, IsUnderPostmaster, name, SEGMENT_NAME_PREFIX, and snprintf.

Referenced by dsm_unpin_segment().

◆ errcode_for_dynamic_shared_memory()

static int errcode_for_dynamic_shared_memory ( void  )
static

Definition at line 1047 of file dsm_impl.c.

1048{
1049 if (errno == EFBIG || errno == ENOMEM)
1050 return errcode(ERRCODE_OUT_OF_MEMORY);
1051 else
1052 return errcode_for_file_access();
1053}
int errcode(int sqlerrcode)
Definition: elog.c:853

References errcode(), and errcode_for_file_access().

Referenced by dsm_impl_mmap(), dsm_impl_pin_segment(), dsm_impl_posix(), dsm_impl_sysv(), and dsm_impl_unpin_segment().

Variable Documentation

◆ dynamic_shared_memory_options

const struct config_enum_entry dynamic_shared_memory_options[]
Initial value:
= {
{"posix", DSM_IMPL_POSIX, false},
{"sysv", DSM_IMPL_SYSV, false},
{"mmap", DSM_IMPL_MMAP, false},
{NULL, 0, false}
}

Definition at line 95 of file dsm_impl.c.

◆ dynamic_shared_memory_type

int dynamic_shared_memory_type = DEFAULT_DYNAMIC_SHARED_MEMORY_TYPE

◆ min_dynamic_shared_memory

int min_dynamic_shared_memory

Definition at line 115 of file dsm_impl.c.

Referenced by dsm_estimate_size().