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 "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 118 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 116 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 208 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, snprintf(), and stat.

209 {
211  {
212  case DSM_IMPL_NONE:
213  return false;
214  case DSM_IMPL_POSIX:
215  return true;
216  case DSM_IMPL_SYSV:
217  return false;
218  case DSM_IMPL_WINDOWS:
219  return false;
220  case DSM_IMPL_MMAP:
221  return true;
222  default:
223  return false; /* should not happen */
224  }
225 }
#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:113
#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 846 of file dsm_impl.c.

References close, 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().

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

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

1058 {
1060  {
1061 #ifdef USE_DSM_WINDOWS
1062  case DSM_IMPL_WINDOWS:
1063  {
1064  HANDLE hmap;
1065 
1066  if (!DuplicateHandle(GetCurrentProcess(), impl_private,
1067  PostmasterHandle, &hmap, 0, FALSE,
1068  DUPLICATE_SAME_ACCESS))
1069  {
1070  char name[64];
1071 
1072  snprintf(name, 64, "%s.%u", SEGMENT_NAME_PREFIX, handle);
1073  _dosmaperr(GetLastError());
1074  ereport(ERROR,
1076  errmsg("could not duplicate handle for \"%s\": %m",
1077  name)));
1078  }
1079 
1080  /*
1081  * Here, we remember the handle that we created in the
1082  * postmaster process. This handle isn't actually usable in
1083  * any process other than the postmaster, but that doesn't
1084  * matter. We're just holding onto it so that, if the segment
1085  * is unpinned, dsm_impl_unpin_segment can close it.
1086  */
1087  *impl_private_pm_handle = hmap;
1088  break;
1089  }
1090 #endif
1091  default:
1092  break;
1093  }
1094 }
#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:1138
#define ERROR
Definition: elog.h:43
#define SEGMENT_NAME_PREFIX
Definition: dsm_impl.c:118
int dynamic_shared_memory_type
Definition: dsm_impl.c:113
#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 455 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().

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

1107 {
1109  {
1110 #ifdef USE_DSM_WINDOWS
1111  case DSM_IMPL_WINDOWS:
1112  {
1113  if (*impl_private &&
1114  !DuplicateHandle(PostmasterHandle, *impl_private,
1115  NULL, NULL, 0, FALSE,
1116  DUPLICATE_CLOSE_SOURCE))
1117  {
1118  char name[64];
1119 
1120  snprintf(name, 64, "%s.%u", SEGMENT_NAME_PREFIX, handle);
1121  _dosmaperr(GetLastError());
1122  ereport(ERROR,
1124  errmsg("could not duplicate handle for \"%s\": %m",
1125  name)));
1126  }
1127 
1128  *impl_private = NULL;
1129  break;
1130  }
1131 #endif
1132  default:
1133  break;
1134  }
1135 }
#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:1138
#define ERROR
Definition: elog.h:43
#define SEGMENT_NAME_PREFIX
Definition: dsm_impl.c:118
int dynamic_shared_memory_type
Definition: dsm_impl.c:113
#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 1138 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().

1139 {
1140  if (errno == EFBIG || errno == ENOMEM)
1141  return errcode(ERRCODE_OUT_OF_MEMORY);
1142  else
1143  return errcode_for_file_access();
1144 }
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 95 of file dsm_impl.c.

◆ dynamic_shared_memory_type