参考資料 http://ikubo.x0.com/PostgreSQL/pdf/IK08_ipc_041117.pdf
上記の資料から引用
PostgreSQLでは、バックエンドプロセス間でデータを共有 するために、共有メモリを使用している。
共有メモリは、postmaster 起動時にまとめて1つの領域として確保される。
そして、この1つの共有メモリの領域をセグメントに切り分けて、様々な目的に使用する。
この共有メモリの管理を行うのが shmem.c である。
CreateSharedMemoryAndSemaphores(bool makePrivate, int port)で、各モジュールに必要な共有メモリのサイズを計算(add_size())して、領域を確保する。
XLOGモジュールに必要な共有メモリのサイズは
size = add_size(size, XLOGShmemSize());
として計算される。
/*
* Initialization of shared memory for XLOG
*/
Size
XLOGShmemSize(void)
{
Size size;
/*
* If the value of wal_buffers is -1, use the preferred auto-tune value.
* This isn't an amazingly clean place to do this, but we must wait till
* NBuffers has received its final value, and must do it before using the
* value of XLOGbuffers to do anything important.
*/
if (XLOGbuffers == -1)
{
char buf[32];
snprintf(buf, sizeof(buf), "%d", XLOGChooseNumBuffers());
SetConfigOption("wal_buffers", buf, PGC_POSTMASTER, PGC_S_OVERRIDE);
}
Assert(XLOGbuffers > 0);
/* XLogCtl */
size = sizeof(XLogCtlData);
/* WAL insertion locks, plus alignment */
size = add_size(size, mul_size(sizeof(WALInsertLockPadded), NUM_XLOGINSERT_LOCKS + 1));
/* xlblocks array */
size = add_size(size, mul_size(sizeof(XLogRecPtr), XLOGbuffers));
/* extra alignment padding for XLOG I/O buffers */
size = add_size(size, XLOG_BLCKSZ);
/* and the buffers themselves */
size = add_size(size, mul_size(XLOG_BLCKSZ, XLOGbuffers));
/*
* Note: we don't count ControlFileData, it comes out of the "slop factor"
* added by CreateSharedMemoryAndSemaphores. This lets us use this
* routine again below to compute the actual allocation size.
*/
return size;
}