PostgreSQL Source Code  git master
ipci.c File Reference
#include "postgres.h"
#include "access/clog.h"
#include "access/commit_ts.h"
#include "access/heapam.h"
#include "access/multixact.h"
#include "access/nbtree.h"
#include "access/subtrans.h"
#include "access/twophase.h"
#include "commands/async.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/autovacuum.h"
#include "postmaster/bgworker_internals.h"
#include "postmaster/bgwriter.h"
#include "postmaster/postmaster.h"
#include "replication/logicallauncher.h"
#include "replication/slot.h"
#include "replication/walreceiver.h"
#include "replication/walsender.h"
#include "replication/origin.h"
#include "storage/bufmgr.h"
#include "storage/dsm.h"
#include "storage/ipc.h"
#include "storage/pg_shmem.h"
#include "storage/pmsignal.h"
#include "storage/predicate.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "storage/procsignal.h"
#include "storage/sinvaladt.h"
#include "storage/spin.h"
#include "utils/snapmgr.h"
Include dependency graph for ipci.c:

Go to the source code of this file.

Functions

void RequestAddinShmemSpace (Size size)
 
void CreateSharedMemoryAndSemaphores (int port)
 

Variables

int shared_memory_type = DEFAULT_SHARED_MEMORY_TYPE
 
shmem_startup_hook_type shmem_startup_hook = NULL
 
static Size total_addin_request = 0
 
static bool addin_request_allowed = true
 

Function Documentation

◆ CreateSharedMemoryAndSemaphores()

void CreateSharedMemoryAndSemaphores ( int  port)

Definition at line 94 of file ipci.c.

References add_size(), addin_request_allowed, ApplyLauncherShmemInit(), ApplyLauncherShmemSize(), AsyncShmemInit(), AsyncShmemSize(), AutoVacuumShmemInit(), AutoVacuumShmemSize(), BackendStatusShmemSize(), BackgroundWorkerShmemInit(), BackgroundWorkerShmemSize(), BTreeShmemInit(), BTreeShmemSize(), BufferShmemSize(), CheckpointerShmemInit(), CheckpointerShmemSize(), CLOGShmemInit(), CLOGShmemSize(), CommitTsShmemInit(), CommitTsShmemSize(), CreateLWLocks(), CreateSharedBackendStatus(), CreateSharedInvalidationState(), CreateSharedProcArray(), DEBUG3, dsm_postmaster_startup(), elog, hash_estimate_size(), InitBufferPool(), InitLocks(), InitPredicateLocks(), InitProcGlobal(), InitShmemAccess(), InitShmemAllocation(), InitShmemIndex(), IsUnderPostmaster, LockShmemSize(), LWLockShmemSize(), MultiXactShmemInit(), MultiXactShmemSize(), PANIC, PGReserveSemaphores(), PGSemaphoreShmemSize(), PGSharedMemoryCreate(), PMSignalShmemInit(), PMSignalShmemSize(), PredicateLockShmemSize(), ProcArrayShmemSize(), ProcGlobalSemas(), ProcGlobalShmemSize(), ProcSignalShmemInit(), ProcSignalShmemSize(), ReplicationOriginShmemInit(), ReplicationOriginShmemSize(), ReplicationSlotsShmemInit(), ReplicationSlotsShmemSize(), SHMEM_INDEX_SIZE, shmem_startup_hook, SInvalShmemSize(), SnapMgrInit(), SnapMgrShmemSize(), SpinlockSemaInit(), SpinlockSemas(), SpinlockSemaSize(), SUBTRANSShmemInit(), SUBTRANSShmemSize(), SyncScanShmemInit(), SyncScanShmemSize(), total_addin_request, TwoPhaseShmemInit(), TwoPhaseShmemSize(), WalRcvShmemInit(), WalRcvShmemSize(), WalSndShmemInit(), WalSndShmemSize(), XLOGShmemInit(), and XLOGShmemSize().

Referenced by BackendRun(), InitCommunication(), and reset_shared().

95 {
96  PGShmemHeader *shim = NULL;
97 
98  if (!IsUnderPostmaster)
99  {
100  PGShmemHeader *seghdr;
101  Size size;
102  int numSemas;
103 
104  /* Compute number of semaphores we'll need */
105  numSemas = ProcGlobalSemas();
106  numSemas += SpinlockSemas();
107 
108  /*
109  * Size of the Postgres shared-memory block is estimated via
110  * moderately-accurate estimates for the big hogs, plus 100K for the
111  * stuff that's too small to bother with estimating.
112  *
113  * We take some care during this phase to ensure that the total size
114  * request doesn't overflow size_t. If this gets through, we don't
115  * need to be so careful during the actual allocation phase.
116  */
117  size = 100000;
118  size = add_size(size, PGSemaphoreShmemSize(numSemas));
119  size = add_size(size, SpinlockSemaSize());
121  sizeof(ShmemIndexEnt)));
122  size = add_size(size, BufferShmemSize());
123  size = add_size(size, LockShmemSize());
124  size = add_size(size, PredicateLockShmemSize());
125  size = add_size(size, ProcGlobalShmemSize());
126  size = add_size(size, XLOGShmemSize());
127  size = add_size(size, CLOGShmemSize());
128  size = add_size(size, CommitTsShmemSize());
129  size = add_size(size, SUBTRANSShmemSize());
130  size = add_size(size, TwoPhaseShmemSize());
131  size = add_size(size, BackgroundWorkerShmemSize());
132  size = add_size(size, MultiXactShmemSize());
133  size = add_size(size, LWLockShmemSize());
134  size = add_size(size, ProcArrayShmemSize());
135  size = add_size(size, BackendStatusShmemSize());
136  size = add_size(size, SInvalShmemSize());
137  size = add_size(size, PMSignalShmemSize());
138  size = add_size(size, ProcSignalShmemSize());
139  size = add_size(size, CheckpointerShmemSize());
140  size = add_size(size, AutoVacuumShmemSize());
141  size = add_size(size, ReplicationSlotsShmemSize());
142  size = add_size(size, ReplicationOriginShmemSize());
143  size = add_size(size, WalSndShmemSize());
144  size = add_size(size, WalRcvShmemSize());
145  size = add_size(size, ApplyLauncherShmemSize());
146  size = add_size(size, SnapMgrShmemSize());
147  size = add_size(size, BTreeShmemSize());
148  size = add_size(size, SyncScanShmemSize());
149  size = add_size(size, AsyncShmemSize());
150 #ifdef EXEC_BACKEND
151  size = add_size(size, ShmemBackendArraySize());
152 #endif
153 
154  /* freeze the addin request size and include it */
155  addin_request_allowed = false;
156  size = add_size(size, total_addin_request);
157 
158  /* might as well round it off to a multiple of a typical page size */
159  size = add_size(size, 8192 - (size % 8192));
160 
161  elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
162 
163  /*
164  * Create the shmem segment
165  */
166  seghdr = PGSharedMemoryCreate(size, port, &shim);
167 
168  InitShmemAccess(seghdr);
169 
170  /*
171  * Create semaphores
172  */
173  PGReserveSemaphores(numSemas, port);
174 
175  /*
176  * If spinlocks are disabled, initialize emulation layer (which
177  * depends on semaphores, so the order is important here).
178  */
179 #ifndef HAVE_SPINLOCKS
181 #endif
182  }
183  else
184  {
185  /*
186  * We are reattaching to an existing shared memory segment. This
187  * should only be reached in the EXEC_BACKEND case.
188  */
189 #ifndef EXEC_BACKEND
190  elog(PANIC, "should be attached to shared memory already");
191 #endif
192  }
193 
194  /*
195  * Set up shared memory allocation mechanism
196  */
197  if (!IsUnderPostmaster)
199 
200  /*
201  * Now initialize LWLocks, which do shared memory allocation and are
202  * needed for InitShmemIndex.
203  */
204  CreateLWLocks();
205 
206  /*
207  * Set up shmem.c index hashtable
208  */
209  InitShmemIndex();
210 
211  /*
212  * Set up xlog, clog, and buffers
213  */
214  XLOGShmemInit();
215  CLOGShmemInit();
219  InitBufferPool();
220 
221  /*
222  * Set up lock manager
223  */
224  InitLocks();
225 
226  /*
227  * Set up predicate lock manager
228  */
230 
231  /*
232  * Set up process table
233  */
234  if (!IsUnderPostmaster)
235  InitProcGlobal();
240 
241  /*
242  * Set up shared-inval messaging
243  */
245 
246  /*
247  * Set up interprocess signaling mechanisms
248  */
255  WalSndShmemInit();
256  WalRcvShmemInit();
258 
259  /*
260  * Set up other modules that need some shared memory space
261  */
262  SnapMgrInit();
263  BTreeShmemInit();
265  AsyncShmemInit();
266 
267 #ifdef EXEC_BACKEND
268 
269  /*
270  * Alloc the win32 shared backend array
271  */
272  if (!IsUnderPostmaster)
273  ShmemBackendArrayAllocation();
274 #endif
275 
276  /* Initialize dynamic shared memory facilities. */
277  if (!IsUnderPostmaster)
279 
280  /*
281  * Now give loadable modules a chance to set up their shmem allocations
282  */
283  if (shmem_startup_hook)
285 }
void dsm_postmaster_startup(PGShmemHeader *shim)
Definition: dsm.c:145
Size SInvalShmemSize(void)
Definition: sinvaladt.c:204
void BTreeShmemInit(void)
Definition: nbtutils.c:2000
Size PMSignalShmemSize(void)
Definition: pmsignal.c:113
Size ReplicationSlotsShmemSize(void)
Definition: slot.c:114
#define SHMEM_INDEX_SIZE
Definition: shmem.h:55
void SUBTRANSShmemInit(void)
Definition: subtrans.c:191
#define DEBUG3
Definition: elog.h:23
Size SpinlockSemaSize(void)
Definition: spin.c:39
void InitShmemAccess(void *seghdr)
Definition: shmem.c:97
void AsyncShmemInit(void)
Definition: async.c:472
void CreateSharedInvalidationState(void)
Definition: sinvaladt.c:219
Size SyncScanShmemSize(void)
Definition: syncscan.c:126
void PGReserveSemaphores(int maxSemas, int port)
Definition: posix_sema.c:199
Size WalSndShmemSize(void)
Definition: walsender.c:3040
PGShmemHeader * PGSharedMemoryCreate(Size size, int port, PGShmemHeader **shim)
Definition: sysv_shmem.c:625
void CheckpointerShmemInit(void)
Definition: checkpointer.c:909
Size PredicateLockShmemSize(void)
Definition: predicate.c:1288
Size TwoPhaseShmemSize(void)
Definition: twophase.c:237
void ReplicationOriginShmemInit(void)
Definition: origin.c:496
void WalRcvShmemInit(void)
#define PANIC
Definition: elog.h:53
void XLOGShmemInit(void)
Definition: xlog.c:4974
Size BackgroundWorkerShmemSize(void)
Definition: bgworker.c:143
void ReplicationSlotsShmemInit(void)
Definition: slot.c:132
Size LWLockShmemSize(void)
Definition: lwlock.c:349
Size BTreeShmemSize(void)
Definition: nbtutils.c:1987
void InitProcGlobal(void)
Definition: proc.c:164
void BackgroundWorkerShmemInit(void)
Definition: bgworker.c:159
void MultiXactShmemInit(void)
Definition: multixact.c:1822
void PMSignalShmemInit(void)
Definition: pmsignal.c:128
Size XLOGShmemSize(void)
Definition: xlog.c:4933
void SpinlockSemaInit(void)
Definition: spin.c:76
bool IsUnderPostmaster
Definition: globals.c:109
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:770
void InitBufferPool(void)
Definition: buf_init.c:68
void SnapMgrInit(void)
Definition: snapmgr.c:269
void InitLocks(void)
Definition: lock.c:377
Size ProcGlobalShmemSize(void)
Definition: proc.c:103
static int port
Definition: pg_regress.c:92
static bool addin_request_allowed
Definition: ipci.c:55
Size ProcSignalShmemSize(void)
Definition: procsignal.c:74
shmem_startup_hook_type shmem_startup_hook
Definition: ipci.c:52
Size hash_estimate_size(long num_entries, Size entrysize)
Definition: dynahash.c:732
void TwoPhaseShmemInit(void)
Definition: twophase.c:253
int SpinlockSemas(void)
Definition: spin.c:65
static Size total_addin_request
Definition: ipci.c:54
Size BackendStatusShmemSize(void)
Definition: pgstat.c:2652
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
Size AutoVacuumShmemSize(void)
Definition: autovacuum.c:3289
Size ReplicationOriginShmemSize(void)
Definition: origin.c:476
Size CheckpointerShmemSize(void)
Definition: checkpointer.c:890
Size CLOGShmemSize(void)
Definition: clog.c:692
void CreateSharedBackendStatus(void)
Definition: pgstat.c:2680
Size WalRcvShmemSize(void)
void CreateLWLocks(void)
Definition: lwlock.c:381
void SyncScanShmemInit(void)
Definition: syncscan.c:135
void CreateSharedProcArray(void)
Definition: procarray.c:222
Size AsyncShmemSize(void)
Definition: async.c:455
size_t Size
Definition: c.h:466
Size CommitTsShmemSize(void)
Definition: commit_ts.c:479
void ApplyLauncherShmemInit(void)
Definition: launcher.c:818
Size MultiXactShmemSize(void)
Definition: multixact.c:1805
Size LockShmemSize(void)
Definition: lock.c:3441
Size SUBTRANSShmemSize(void)
Definition: subtrans.c:185
Size BufferShmemSize(void)
Definition: buf_init.c:161
#define elog(elevel,...)
Definition: elog.h:226
void WalSndShmemInit(void)
Definition: walsender.c:3052
void CLOGShmemInit(void)
Definition: clog.c:698
Size PGSemaphoreShmemSize(int maxSemas)
Definition: posix_sema.c:164
void ProcSignalShmemInit(void)
Definition: procsignal.c:84
void InitShmemIndex(void)
Definition: shmem.c:272
void InitPredicateLocks(void)
Definition: predicate.c:1083
void AutoVacuumShmemInit(void)
Definition: autovacuum.c:3308
Size SnapMgrShmemSize(void)
Definition: snapmgr.c:253
Size ProcArrayShmemSize(void)
Definition: procarray.c:180
int ProcGlobalSemas(void)
Definition: proc.c:129
void InitShmemAllocation(void)
Definition: shmem.c:112
void CommitTsShmemInit(void)
Definition: commit_ts.c:490

◆ RequestAddinShmemSpace()

void RequestAddinShmemSpace ( Size  size)

Definition at line 70 of file ipci.c.

References add_size(), addin_request_allowed, IsUnderPostmaster, and total_addin_request.

Referenced by _PG_init().

71 {
73  return; /* too late */
75 }
bool IsUnderPostmaster
Definition: globals.c:109
static bool addin_request_allowed
Definition: ipci.c:55
static Size total_addin_request
Definition: ipci.c:54
Size add_size(Size s1, Size s2)
Definition: shmem.c:475

Variable Documentation

◆ addin_request_allowed

bool addin_request_allowed = true
static

Definition at line 55 of file ipci.c.

Referenced by CreateSharedMemoryAndSemaphores(), and RequestAddinShmemSpace().

◆ shared_memory_type

int shared_memory_type = DEFAULT_SHARED_MEMORY_TYPE

Definition at line 50 of file ipci.c.

Referenced by PGSharedMemoryCreate().

◆ shmem_startup_hook

shmem_startup_hook_type shmem_startup_hook = NULL

Definition at line 52 of file ipci.c.

Referenced by _PG_fini(), _PG_init(), and CreateSharedMemoryAndSemaphores().

◆ total_addin_request

Size total_addin_request = 0
static

Definition at line 54 of file ipci.c.

Referenced by CreateSharedMemoryAndSemaphores(), and RequestAddinShmemSpace().