pg共享内存


 TL;DR
PostgreSQL 共享内存:PGShmemHeader 的结构,以及共享内存段的创建与切分。
难度 中等

基本数据结构

  • PGShmemHeader
typedef struct PGShmemHeader	/* standard header for all Postgres shmem */
{
  int32		magic;			/* magic # to identify Postgres segments */
#define PGShmemMagic  679834894
  pid_t		creatorPID;		/* PID of creating process (set but unread) */
  Size		totalsize;		/* total size of segment */
  Size		freeoffset;		/* offset to first free space */
  dsm_handle	dsm_control;	/* ID of dynamic shared memory control seg */
  void	   *index;			/* pointer to ShmemIndex table */
#ifndef WIN32					/* Windows doesn't have useful inode#s */
  dev_t		device;			/* device data directory is on */
  ino_t		inode;			/* inode number of data directory */
#endif
} PGShmemHeader;
  • ShmemIndexEnt
/* this is a hash bucket in the shmem index table */
typedef struct
{
  char		key[SHMEM_INDEX_KEYSIZE];	/* string name */
  void	   *location;		/* location in shared mem */
  Size		size;			/* # bytes requested for the structure */
  Size		allocated_size; /* # bytes actually allocated */
} ShmemIndexEnt;

初始化

  • 全局变量
static HTAB *ShmemIndex = NULL; /* primary index hashtable for shmem */
graph TB CreateSharedMemoryAndSemaphores-->PGSharedMemoryCreate-->shmget/shmat

处理逻辑

graph TB ShmemInitStruct-->ShmemAlloc-->ShmemAllocRaw

对外接口


文章作者: growdu
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 growdu !
这篇还发到了
今日头条 drafted
  目录