PostgreSQL Source Code  git master
dsm_impl.c File Reference
#include "postgres.h"
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include "common/file_perm.h"
#include "pgstat.h"
#include "portability/mem.h"
#include "storage/dsm_impl.h"
#include "storage/fd.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "postmaster/postmaster.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_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)
 
bool dsm_impl_can_resize (void)
 
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
 

Macro Definition Documentation

◆ SEGMENT_NAME_PREFIX

#define SEGMENT_NAME_PREFIX   "Global/PostgreSQL"

Definition at line 119 of file dsm_impl.c.

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

◆ ZBUFFER_SIZE

#define ZBUFFER_SIZE   8192

Definition at line 117 of file dsm_impl.c.

Referenced by dsm_impl_mmap().

Function Documentation

◆ dsm_impl_can_resize()

bool dsm_impl_can_resize ( void  )

Definition at line 209 of file dsm_impl.c.

References close, DSM_IMPL_MMAP, DSM_IMPL_NONE, DSM_IMPL_POSIX, DSM_IMPL_SYSV, DSM_IMPL_WINDOWS, DSM_OP_ATTACH, DSM_OP_CREATE, DSM_OP_DESTROY, DSM_OP_DETACH, dynamic_shared_memory_type, EINTR, elevel, ereport, errcode_for_dynamic_shared_memory(), errmsg(), fd(), ftruncate, MAP_FAILED, MAP_HASSEMAPHORE, MAP_NOSYNC, name, PG_FILE_MODE_OWNER, snprintf(), and stat.

210 {
212  {
213  case DSM_IMPL_NONE:
214  return false;
215  case DSM_IMPL_POSIX:
216  return true;
217  case DSM_IMPL_SYSV:
218  return false;
219  case DSM_IMPL_WINDOWS:
220  return false;
221  case DSM_IMPL_MMAP:
222  return true;
223  default:
224  return false; /* should not happen */
225  }
226 }
#define DSM_IMPL_MMAP
Definition: dsm_impl.h:21
#define DSM_IMPL_SYSV
Definition: dsm_impl.h:19
#define DSM_IMPL_WINDOWS
Definition: dsm_impl.h:20
#define DSM_IMPL_POSIX
Definition: dsm_impl.h:18
int dynamic_shared_memory_type
Definition: dsm_impl.c:114
#define DSM_IMPL_NONE
Definition: dsm_impl.h:17

◆ 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 847 of file dsm_impl.c.

References CloseTransientFile(), DSM_OP_ATTACH, DSM_OP_CREATE, DSM_OP_DESTROY, DSM_OP_DETACH, ereport, errcode_for_dynamic_shared_memory(), errmsg(), fd(), ftruncate, 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, success, WAIT_EVENT_DSM_FILL_ZERO_WRITE, write, and ZBUFFER_SIZE.

Referenced by dsm_impl_op().

850 {
851  char name[64];
852  int flags;
853  int fd;
854  char *address;
855 
857  handle);
858 
859  /* Handle teardown cases. */
860  if (op == DSM_OP_DETACH || op == DSM_OP_DESTROY)
861  {
862  if (*mapped_address != NULL
863  && munmap(*mapped_address, *mapped_size) != 0)
864  {
865  ereport(elevel,
867  errmsg("could not unmap shared memory segment \"%s\": %m",
868  name)));
869  return false;
870  }
871  *mapped_address = NULL;
872  *mapped_size = 0;
873  if (op == DSM_OP_DESTROY && unlink(name) != 0)
874  {
875  ereport(elevel,
877  errmsg("could not remove shared memory segment \"%s\": %m",
878  name)));
879  return false;
880  }
881  return true;
882  }
883 
884  /* Create new segment or open an existing one for attach or resize. */
885  flags = O_RDWR | (op == DSM_OP_CREATE ? O_CREAT | O_EXCL : 0);
886  if ((fd = OpenTransientFile(name, flags)) == -1)
887  {
888  if (errno != EEXIST)
889  ereport(elevel,
891  errmsg("could not open shared memory segment \"%s\": %m",
892  name)));
893  return false;
894  }
895 
896  /*
897  * If we're attaching the segment, determine the current size; if we are
898  * creating or resizing the segment, set the size to the requested value.
899  */
900  if (op == DSM_OP_ATTACH)
901  {
902  struct stat st;
903 
904  if (fstat(fd, &st) != 0)
905  {
906  int save_errno;
907 
908  /* Back out what's already been done. */
909  save_errno = errno;
910  CloseTransientFile(fd);
911  errno = save_errno;
912 
913  ereport(elevel,
915  errmsg("could not stat shared memory segment \"%s\": %m",
916  name)));
917  return false;
918  }
919  request_size = st.st_size;
920  }
921  else if (*mapped_size > request_size && ftruncate(fd, request_size))
922  {
923  int save_errno;
924 
925  /* Back out what's already been done. */
926  save_errno = errno;
927  CloseTransientFile(fd);
928  if (op == DSM_OP_CREATE)
929  unlink(name);
930  errno = save_errno;
931 
932  ereport(elevel,
934  errmsg("could not resize shared memory segment \"%s\" to %zu bytes: %m",
935  name, request_size)));
936  return false;
937  }
938  else if (*mapped_size < request_size)
939  {
940  /*
941  * Allocate a buffer full of zeros.
942  *
943  * Note: palloc zbuffer, instead of just using a local char array, to
944  * ensure it is reasonably well-aligned; this may save a few cycles
945  * transferring data to the kernel.
946  */
947  char *zbuffer = (char *) palloc0(ZBUFFER_SIZE);
948  uint32 remaining = request_size;
949  bool success = true;
950 
951  /*
952  * Zero-fill the file. We have to do this the hard way to ensure that
953  * all the file space has really been allocated, so that we don't
954  * later seg fault when accessing the memory mapping. This is pretty
955  * pessimal.
956  */
957  while (success && remaining > 0)
958  {
959  Size goal = remaining;
960 
961  if (goal > ZBUFFER_SIZE)
962  goal = ZBUFFER_SIZE;
964  if (write(fd, zbuffer, goal) == goal)
965  remaining -= goal;
966  else
967  success = false;
969  }
970 
971  if (!success)
972  {
973  int save_errno;
974 
975  /* Back out what's already been done. */
976  save_errno = errno;
977  CloseTransientFile(fd);
978  if (op == DSM_OP_CREATE)
979  unlink(name);
980  errno = save_errno ? save_errno : ENOSPC;
981 
982  ereport(elevel,
984  errmsg("could not resize shared memory segment \"%s\" to %zu bytes: %m",
985  name, request_size)));
986  return false;
987  }
988  }
989 
990  /*
991  * If we're reattaching or resizing, we must remove any existing mapping,
992  * unless we've already got the right thing mapped.
993  */
994  if (*mapped_address != NULL)
995  {
996  if (*mapped_size == request_size)
997  return true;
998  if (munmap(*mapped_address, *mapped_size) != 0)
999  {
1000  int save_errno;
1001 
1002  /* Back out what's already been done. */
1003  save_errno = errno;
1004  CloseTransientFile(fd);
1005  if (op == DSM_OP_CREATE)
1006  unlink(name);
1007  errno = save_errno;
1008 
1009  ereport(elevel,
1011  errmsg("could not unmap shared memory segment \"%s\": %m",
1012  name)));
1013  return false;
1014  }
1015  *mapped_address = NULL;
1016  *mapped_size = 0;
1017  }
1018 
1019  /* Map it. */
1020  address = mmap(NULL, request_size, PROT_READ | PROT_WRITE,
1021  MAP_SHARED | MAP_HASSEMAPHORE | MAP_NOSYNC, fd, 0);
1022  if (address == MAP_FAILED)
1023  {
1024  int save_errno;
1025 
1026  /* Back out what's already been done. */
1027  save_errno = errno;
1028  CloseTransientFile(fd);
1029  if (op == DSM_OP_CREATE)
1030  unlink(name);
1031  errno = save_errno;
1032 
1033  ereport(elevel,
1035  errmsg("could not map shared memory segment \"%s\": %m",
1036  name)));
1037  return false;
1038  }
1039  *mapped_address = address;
1040  *mapped_size = request_size;
1041  CloseTransientFile(fd);
1042 
1043  return true;
1044 }
int remaining
Definition: informix.c:692
#define MAP_HASSEMAPHORE
Definition: mem.h:30
#define MAP_FAILED
Definition: mem.h:45
#define PG_DYNSHMEM_DIR
Definition: dsm_impl.h:51
#define write(a, b, c)
Definition: win32.h:14
#define MAP_NOSYNC
Definition: mem.h:38
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static int errcode_for_dynamic_shared_memory(void)
Definition: dsm_impl.c:1139
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2396
static bool success
unsigned int uint32
Definition: c.h:325
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1260
#define ereport(elevel, rest)
Definition: elog.h:122
int CloseTransientFile(int fd)
Definition: fd.c:2566
#define stat(a, b)
Definition: win32_port.h:266
static int elevel
Definition: vacuumlazy.c:144
void * palloc0(Size size)
Definition: mcxt.c:955
size_t Size
Definition: c.h:433
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1236
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_DYNSHMEM_MMAP_FILE_PREFIX
Definition: dsm_impl.h:52
#define ZBUFFER_SIZE
Definition: dsm_impl.c:117
#define ftruncate(a, b)
Definition: win32_port.h:60

◆ 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 164 of file dsm_impl.c.

References Assert, DSM_IMPL_MMAP, dsm_impl_mmap(), DSM_IMPL_POSIX, DSM_IMPL_SYSV, dsm_impl_sysv(), DSM_IMPL_WINDOWS, DSM_OP_ATTACH, DSM_OP_CREATE, DSM_OP_RESIZE, 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(), dsm_remap(), dsm_resize(), and dsm_unpin_segment().

167 {
168  Assert(op == DSM_OP_CREATE || op == DSM_OP_RESIZE || request_size == 0);
169  Assert((op != DSM_OP_CREATE && op != DSM_OP_ATTACH) ||
170  (*mapped_address == NULL && *mapped_size == 0));
171 
173  {
174 #ifdef USE_DSM_POSIX
175  case DSM_IMPL_POSIX:
176  return dsm_impl_posix(op, handle, request_size, impl_private,
177  mapped_address, mapped_size, elevel);
178 #endif
179 #ifdef USE_DSM_SYSV
180  case DSM_IMPL_SYSV:
181  return dsm_impl_sysv(op, handle, request_size, impl_private,
182  mapped_address, mapped_size, elevel);
183 #endif
184 #ifdef USE_DSM_WINDOWS
185  case DSM_IMPL_WINDOWS:
186  return dsm_impl_windows(op, handle, request_size, impl_private,
187  mapped_address, mapped_size, elevel);
188 #endif
189 #ifdef USE_DSM_MMAP
190  case DSM_IMPL_MMAP:
191  return dsm_impl_mmap(op, handle, request_size, impl_private,
192  mapped_address, mapped_size, elevel);
193 #endif
194  default:
195  elog(ERROR, "unexpected dynamic shared memory type: %d",
197  return false;
198  }
199 }
#define DSM_IMPL_MMAP
Definition: dsm_impl.h:21
#define DSM_IMPL_SYSV
Definition: dsm_impl.h:19
#define DSM_IMPL_WINDOWS
Definition: dsm_impl.h:20
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:847
#define ERROR
Definition: elog.h:43
#define DSM_IMPL_POSIX
Definition: dsm_impl.h:18
int dynamic_shared_memory_type
Definition: dsm_impl.c:114
static int elevel
Definition: vacuumlazy.c:144
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:456
#define Assert(condition)
Definition: c.h:699
#define elog
Definition: elog.h:219

◆ dsm_impl_pin_segment()

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

Definition at line 1057 of file dsm_impl.c.

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

Referenced by dsm_pin_segment().

1059 {
1061  {
1062 #ifdef USE_DSM_WINDOWS
1063  case DSM_IMPL_WINDOWS:
1064  {
1065  HANDLE hmap;
1066 
1067  if (!DuplicateHandle(GetCurrentProcess(), impl_private,
1068  PostmasterHandle, &hmap, 0, FALSE,
1069  DUPLICATE_SAME_ACCESS))
1070  {
1071  char name[64];
1072 
1073  snprintf(name, 64, "%s.%u", SEGMENT_NAME_PREFIX, handle);
1074  _dosmaperr(GetLastError());
1075  ereport(ERROR,
1077  errmsg("could not duplicate handle for \"%s\": %m",
1078  name)));
1079  }
1080 
1081  /*
1082  * Here, we remember the handle that we created in the
1083  * postmaster process. This handle isn't actually usable in
1084  * any process other than the postmaster, but that doesn't
1085  * matter. We're just holding onto it so that, if the segment
1086  * is unpinned, dsm_impl_unpin_segment can close it.
1087  */
1088  *impl_private_pm_handle = hmap;
1089  break;
1090  }
1091 #endif
1092  default:
1093  break;
1094  }
1095 }
#define FALSE
Definition: ecpglib.h:39
void _dosmaperr(unsigned long)
Definition: win32error.c:171
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define DSM_IMPL_WINDOWS
Definition: dsm_impl.h:20
static int errcode_for_dynamic_shared_memory(void)
Definition: dsm_impl.c:1139
#define ERROR
Definition: elog.h:43
#define SEGMENT_NAME_PREFIX
Definition: dsm_impl.c:119
int dynamic_shared_memory_type
Definition: dsm_impl.c:114
#define ereport(elevel, rest)
Definition: elog.h:122
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ 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 456 of file dsm_impl.c.

References _dosmaperr(), DEBUG4, DSM_OP_ATTACH, DSM_OP_CREATE, DSM_OP_DESTROY, DSM_OP_DETACH, DSM_OP_RESIZE, elevel, elog, ereport, errcode(), errcode_for_dynamic_shared_memory(), errmsg(), FALSE, IPC_CREAT, IPC_EXCL, IPC_PRIVATE, IPC_RMID, IPC_STAT, IPCProtection, MemoryContextAlloc(), name, pfree(), PG_SHMAT_FLAGS, SEGMENT_NAME_PREFIX, snprintf(), and TopMemoryContext.

Referenced by dsm_impl_op().

459 {
460  key_t key;
461  int ident;
462  char *address;
463  char name[64];
464  int *ident_cache;
465 
466  /* Resize is not supported for System V shared memory. */
467  if (op == DSM_OP_RESIZE)
468  {
469  elog(elevel, "System V shared memory segments cannot be resized");
470  return false;
471  }
472 
473  /* Since resize isn't supported, reattach is a no-op. */
474  if (op == DSM_OP_ATTACH && *mapped_address != NULL)
475  return true;
476 
477  /*
478  * POSIX shared memory and mmap-based shared memory identify segments with
479  * names. To avoid needless error message variation, we use the handle as
480  * the name.
481  */
482  snprintf(name, 64, "%u", handle);
483 
484  /*
485  * The System V shared memory namespace is very restricted; names are of
486  * type key_t, which is expected to be some sort of integer data type, but
487  * not necessarily the same one as dsm_handle. Since we use dsm_handle to
488  * identify shared memory segments across processes, this might seem like
489  * a problem, but it's really not. If dsm_handle is bigger than key_t,
490  * the cast below might truncate away some bits from the handle the
491  * user-provided, but it'll truncate exactly the same bits away in exactly
492  * the same fashion every time we use that handle, which is all that
493  * really matters. Conversely, if dsm_handle is smaller than key_t, we
494  * won't use the full range of available key space, but that's no big deal
495  * either.
496  *
497  * We do make sure that the key isn't negative, because that might not be
498  * portable.
499  */
500  key = (key_t) handle;
501  if (key < 1) /* avoid compiler warning if type is unsigned */
502  key = -key;
503 
504  /*
505  * There's one special key, IPC_PRIVATE, which can't be used. If we end
506  * up with that value by chance during a create operation, just pretend it
507  * already exists, so that caller will retry. If we run into it anywhere
508  * else, the caller has passed a handle that doesn't correspond to
509  * anything we ever created, which should not happen.
510  */
511  if (key == IPC_PRIVATE)
512  {
513  if (op != DSM_OP_CREATE)
514  elog(DEBUG4, "System V shared memory key may not be IPC_PRIVATE");
515  errno = EEXIST;
516  return false;
517  }
518 
519  /*
520  * Before we can do anything with a shared memory segment, we have to map
521  * the shared memory key to a shared memory identifier using shmget(). To
522  * avoid repeated lookups, we store the key using impl_private.
523  */
524  if (*impl_private != NULL)
525  {
526  ident_cache = *impl_private;
527  ident = *ident_cache;
528  }
529  else
530  {
531  int flags = IPCProtection;
532  size_t segsize;
533 
534  /*
535  * Allocate the memory BEFORE acquiring the resource, so that we don't
536  * leak the resource if memory allocation fails.
537  */
538  ident_cache = MemoryContextAlloc(TopMemoryContext, sizeof(int));
539 
540  /*
541  * When using shmget to find an existing segment, we must pass the
542  * size as 0. Passing a non-zero size which is greater than the
543  * actual size will result in EINVAL.
544  */
545  segsize = 0;
546 
547  if (op == DSM_OP_CREATE)
548  {
549  flags |= IPC_CREAT | IPC_EXCL;
550  segsize = request_size;
551  }
552 
553  if ((ident = shmget(key, segsize, flags)) == -1)
554  {
555  if (errno != EEXIST)
556  {
557  int save_errno = errno;
558 
559  pfree(ident_cache);
560  errno = save_errno;
561  ereport(elevel,
563  errmsg("could not get shared memory segment: %m")));
564  }
565  return false;
566  }
567 
568  *ident_cache = ident;
569  *impl_private = ident_cache;
570  }
571 
572  /* Handle teardown cases. */
573  if (op == DSM_OP_DETACH || op == DSM_OP_DESTROY)
574  {
575  pfree(ident_cache);
576  *impl_private = NULL;
577  if (*mapped_address != NULL && shmdt(*mapped_address) != 0)
578  {
579  ereport(elevel,
581  errmsg("could not unmap shared memory segment \"%s\": %m",
582  name)));
583  return false;
584  }
585  *mapped_address = NULL;
586  *mapped_size = 0;
587  if (op == DSM_OP_DESTROY && shmctl(ident, IPC_RMID, NULL) < 0)
588  {
589  ereport(elevel,
591  errmsg("could not remove shared memory segment \"%s\": %m",
592  name)));
593  return false;
594  }
595  return true;
596  }
597 
598  /* If we're attaching it, we must use IPC_STAT to determine the size. */
599  if (op == DSM_OP_ATTACH)
600  {
601  struct shmid_ds shm;
602 
603  if (shmctl(ident, IPC_STAT, &shm) != 0)
604  {
605  ereport(elevel,
607  errmsg("could not stat shared memory segment \"%s\": %m",
608  name)));
609  return false;
610  }
611  request_size = shm.shm_segsz;
612  }
613 
614  /* Map it. */
615  address = shmat(ident, NULL, PG_SHMAT_FLAGS);
616  if (address == (void *) -1)
617  {
618  int save_errno;
619 
620  /* Back out what's already been done. */
621  save_errno = errno;
622  if (op == DSM_OP_CREATE)
623  shmctl(ident, IPC_RMID, NULL);
624  errno = save_errno;
625 
626  ereport(elevel,
628  errmsg("could not map shared memory segment \"%s\": %m",
629  name)));
630  return false;
631  }
632  *mapped_address = address;
633  *mapped_size = request_size;
634 
635  return true;
636 }
#define IPC_CREAT
Definition: win32_port.h:82
#define IPCProtection
Definition: posix_sema.c:52
#define PG_SHMAT_FLAGS
Definition: mem.h:20
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define DEBUG4
Definition: elog.h:22
static int errcode_for_dynamic_shared_memory(void)
Definition: dsm_impl.c:1139
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ereport(elevel, rest)
Definition: elog.h:122
#define IPC_PRIVATE
Definition: win32_port.h:84
MemoryContext TopMemoryContext
Definition: mcxt.c:44
static int elevel
Definition: vacuumlazy.c:144
#define IPC_RMID
Definition: win32_port.h:81
long key_t
Definition: win32_port.h:244
#define IPC_EXCL
Definition: win32_port.h:83
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:771
#define elog
Definition: elog.h:219
#define IPC_STAT
Definition: win32_port.h:86

◆ dsm_impl_unpin_segment()

void dsm_impl_unpin_segment ( dsm_handle  handle,
void **  impl_private 
)

Definition at line 1107 of file dsm_impl.c.

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

Referenced by dsm_unpin_segment().

1108 {
1110  {
1111 #ifdef USE_DSM_WINDOWS
1112  case DSM_IMPL_WINDOWS:
1113  {
1114  if (*impl_private &&
1115  !DuplicateHandle(PostmasterHandle, *impl_private,
1116  NULL, NULL, 0, FALSE,
1117  DUPLICATE_CLOSE_SOURCE))
1118  {
1119  char name[64];
1120 
1121  snprintf(name, 64, "%s.%u", SEGMENT_NAME_PREFIX, handle);
1122  _dosmaperr(GetLastError());
1123  ereport(ERROR,
1125  errmsg("could not duplicate handle for \"%s\": %m",
1126  name)));
1127  }
1128 
1129  *impl_private = NULL;
1130  break;
1131  }
1132 #endif
1133  default:
1134  break;
1135  }
1136 }
#define FALSE
Definition: ecpglib.h:39
void _dosmaperr(unsigned long)
Definition: win32error.c:171
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define DSM_IMPL_WINDOWS
Definition: dsm_impl.h:20
static int errcode_for_dynamic_shared_memory(void)
Definition: dsm_impl.c:1139
#define ERROR
Definition: elog.h:43
#define SEGMENT_NAME_PREFIX
Definition: dsm_impl.c:119
int dynamic_shared_memory_type
Definition: dsm_impl.c:114
#define ereport(elevel, rest)
Definition: elog.h:122
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ errcode_for_dynamic_shared_memory()

static int errcode_for_dynamic_shared_memory ( void  )
static

Definition at line 1139 of file dsm_impl.c.

References errcode(), and errcode_for_file_access().

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

1140 {
1141  if (errno == EFBIG || errno == ENOMEM)
1142  return errcode(ERRCODE_OUT_OF_MEMORY);
1143  else
1144  return errcode_for_file_access();
1145 }
int errcode(int sqlerrcode)
Definition: elog.c:575
int errcode_for_file_access(void)
Definition: elog.c:598

Variable Documentation

◆ dynamic_shared_memory_options

const struct config_enum_entry dynamic_shared_memory_options[]
Initial value:
= {
{"sysv", DSM_IMPL_SYSV, false},
{"mmap", DSM_IMPL_MMAP, false},
{"none", DSM_IMPL_NONE, false},
{NULL, 0, false}
}
#define DSM_IMPL_MMAP
Definition: dsm_impl.h:21
#define DSM_IMPL_SYSV
Definition: dsm_impl.h:19
#define DSM_IMPL_NONE
Definition: dsm_impl.h:17

Definition at line 96 of file dsm_impl.c.

◆ dynamic_shared_memory_type