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/syncscan.h"
#include "access/twophase.h"
#include "access/xlogprefetch.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/origin.h"
#include "replication/slot.h"
#include "replication/walreceiver.h"
#include "replication/walsender.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 (void)
 

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 ( void  )

Definition at line 96 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_estimate_size(), dsm_postmaster_startup(), dsm_shmem_init(), elog, hash_estimate_size(), InitBufferPool(), InitLocks(), InitPredicateLocks(), InitProcGlobal(), InitShmemAccess(), InitShmemAllocation(), InitShmemIndex(), IsUnderPostmaster, LockShmemSize(), LWLockShmemSize(), MultiXactShmemInit(), MultiXactShmemSize(), PANIC, PgArchShmemInit(), PgArchShmemSize(), 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(), XLogPrefetchShmemInit(), XLogPrefetchShmemSize(), XLOGShmemInit(), and XLOGShmemSize().

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

97 {
98  PGShmemHeader *shim = NULL;
99 
100  if (!IsUnderPostmaster)
101  {
102  PGShmemHeader *seghdr;
103  Size size;
104  int numSemas;
105 
106  /* Compute number of semaphores we'll need */
107  numSemas = ProcGlobalSemas();
108  numSemas += SpinlockSemas();
109 
110  /*
111  * Size of the Postgres shared-memory block is estimated via
112  * moderately-accurate estimates for the big hogs, plus 100K for the
113  * stuff that's too small to bother with estimating.
114  *
115  * We take some care during this phase to ensure that the total size
116  * request doesn't overflow size_t. If this gets through, we don't
117  * need to be so careful during the actual allocation phase.
118  */
119  size = 100000;
120  size = add_size(size, PGSemaphoreShmemSize(numSemas));
121  size = add_size(size, SpinlockSemaSize());
123  sizeof(ShmemIndexEnt)));
124  size = add_size(size, dsm_estimate_size());
125  size = add_size(size, BufferShmemSize());
126  size = add_size(size, LockShmemSize());
127  size = add_size(size, PredicateLockShmemSize());
128  size = add_size(size, ProcGlobalShmemSize());
129  size = add_size(size, XLOGShmemSize());
130  size = add_size(size, XLogPrefetchShmemSize());
131  size = add_size(size, CLOGShmemSize());
132  size = add_size(size, CommitTsShmemSize());
133  size = add_size(size, SUBTRANSShmemSize());
134  size = add_size(size, TwoPhaseShmemSize());
135  size = add_size(size, BackgroundWorkerShmemSize());
136  size = add_size(size, MultiXactShmemSize());
137  size = add_size(size, LWLockShmemSize());
138  size = add_size(size, ProcArrayShmemSize());
139  size = add_size(size, BackendStatusShmemSize());
140  size = add_size(size, SInvalShmemSize());
141  size = add_size(size, PMSignalShmemSize());
142  size = add_size(size, ProcSignalShmemSize());
143  size = add_size(size, CheckpointerShmemSize());
144  size = add_size(size, AutoVacuumShmemSize());
145  size = add_size(size, ReplicationSlotsShmemSize());
146  size = add_size(size, ReplicationOriginShmemSize());
147  size = add_size(size, WalSndShmemSize());
148  size = add_size(size, WalRcvShmemSize());
149  size = add_size(size, PgArchShmemSize());
150  size = add_size(size, ApplyLauncherShmemSize());
151  size = add_size(size, SnapMgrShmemSize());
152  size = add_size(size, BTreeShmemSize());
153  size = add_size(size, SyncScanShmemSize());
154  size = add_size(size, AsyncShmemSize());
155 #ifdef EXEC_BACKEND
156  size = add_size(size, ShmemBackendArraySize());
157 #endif
158 
159  /* freeze the addin request size and include it */
160  addin_request_allowed = false;
161  size = add_size(size, total_addin_request);
162 
163  /* might as well round it off to a multiple of a typical page size */
164  size = add_size(size, 8192 - (size % 8192));
165 
166  elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
167 
168  /*
169  * Create the shmem segment
170  */
171  seghdr = PGSharedMemoryCreate(size, &shim);
172 
173  InitShmemAccess(seghdr);
174 
175  /*
176  * Create semaphores
177  */
178  PGReserveSemaphores(numSemas);
179 
180  /*
181  * If spinlocks are disabled, initialize emulation layer (which
182  * depends on semaphores, so the order is important here).
183  */
184 #ifndef HAVE_SPINLOCKS
186 #endif
187  }
188  else
189  {
190  /*
191  * We are reattaching to an existing shared memory segment. This
192  * should only be reached in the EXEC_BACKEND case.
193  */
194 #ifndef EXEC_BACKEND
195  elog(PANIC, "should be attached to shared memory already");
196 #endif
197  }
198 
199  /*
200  * Set up shared memory allocation mechanism
201  */
202  if (!IsUnderPostmaster)
204 
205  /*
206  * Now initialize LWLocks, which do shared memory allocation and are
207  * needed for InitShmemIndex.
208  */
209  CreateLWLocks();
210 
211  /*
212  * Set up shmem.c index hashtable
213  */
214  InitShmemIndex();
215 
216  dsm_shmem_init();
217 
218  /*
219  * Set up xlog, clog, and buffers
220  */
221  XLOGShmemInit();
223  CLOGShmemInit();
227  InitBufferPool();
228 
229  /*
230  * Set up lock manager
231  */
232  InitLocks();
233 
234  /*
235  * Set up predicate lock manager
236  */
238 
239  /*
240  * Set up process table
241  */
242  if (!IsUnderPostmaster)
243  InitProcGlobal();
248 
249  /*
250  * Set up shared-inval messaging
251  */
253 
254  /*
255  * Set up interprocess signaling mechanisms
256  */
263  WalSndShmemInit();
264  WalRcvShmemInit();
265  PgArchShmemInit();
267 
268  /*
269  * Set up other modules that need some shared memory space
270  */
271  SnapMgrInit();
272  BTreeShmemInit();
274  AsyncShmemInit();
275 
276 #ifdef EXEC_BACKEND
277 
278  /*
279  * Alloc the win32 shared backend array
280  */
281  if (!IsUnderPostmaster)
282  ShmemBackendArrayAllocation();
283 #endif
284 
285  /* Initialize dynamic shared memory facilities. */
286  if (!IsUnderPostmaster)
288 
289  /*
290  * Now give loadable modules a chance to set up their shmem allocations
291  */
292  if (shmem_startup_hook)
294 }
void dsm_postmaster_startup(PGShmemHeader *shim)
Definition: dsm.c:149
Size SInvalShmemSize(void)
Definition: sinvaladt.c:203
Size PgArchShmemSize(void)
Definition: pgarch.c:108
PGShmemHeader * PGSharedMemoryCreate(Size size, PGShmemHeader **shim)
Definition: sysv_shmem.c:651
void BTreeShmemInit(void)
Definition: nbtutils.c:2076
Size PMSignalShmemSize(void)
Definition: pmsignal.c:119
Size ReplicationSlotsShmemSize(void)
Definition: slot.c:116
#define SHMEM_INDEX_SIZE
Definition: shmem.h:55
void dsm_shmem_init(void)
Definition: dsm.c:450
void SUBTRANSShmemInit(void)
Definition: subtrans.c:191
#define DEBUG3
Definition: elog.h:23
Size SpinlockSemaSize(void)
Definition: spin.c:55
size_t XLogPrefetchShmemSize(void)
Definition: xlogprefetch.c:172
void InitShmemAccess(void *seghdr)
Definition: shmem.c:101
void AsyncShmemInit(void)
Definition: async.c:526
void CreateSharedInvalidationState(void)
Definition: sinvaladt.c:218
Size SyncScanShmemSize(void)
Definition: syncscan.c:126
Size WalSndShmemSize(void)
Definition: walsender.c:3057
void PgArchShmemInit(void)
Definition: pgarch.c:119
void CheckpointerShmemInit(void)
Definition: checkpointer.c:884
Size PredicateLockShmemSize(void)
Definition: predicate.c:1355
Size TwoPhaseShmemSize(void)
Definition: twophase.c:236
void ReplicationOriginShmemInit(void)
Definition: origin.c:510
Size BackendStatusShmemSize(void)
void WalRcvShmemInit(void)
#define PANIC
Definition: elog.h:50
void XLOGShmemInit(void)
Definition: xlog.c:5137
Size BackgroundWorkerShmemSize(void)
Definition: bgworker.c:142
void ReplicationSlotsShmemInit(void)
Definition: slot.c:134
Size LWLockShmemSize(void)
Definition: lwlock.c:432
Size BTreeShmemSize(void)
Definition: nbtutils.c:2063
void InitProcGlobal(void)
Definition: proc.c:159
void BackgroundWorkerShmemInit(void)
Definition: bgworker.c:158
void MultiXactShmemInit(void)
Definition: multixact.c:1841
void PMSignalShmemInit(void)
Definition: pmsignal.c:134
Size XLOGShmemSize(void)
Definition: xlog.c:5096
void SpinlockSemaInit(void)
Definition: spin.c:77
size_t dsm_estimate_size(void)
Definition: dsm.c:441
bool IsUnderPostmaster
Definition: globals.c:112
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:689
void InitBufferPool(void)
Definition: buf_init.c:67
void SnapMgrInit(void)
Definition: snapmgr.c:213
void InitLocks(void)
Definition: lock.c:404
Size ProcGlobalShmemSize(void)
Definition: proc.c:102
static bool addin_request_allowed
Definition: ipci.c:57
Size ProcSignalShmemSize(void)
Definition: procsignal.c:111
shmem_startup_hook_type shmem_startup_hook
Definition: ipci.c:54
Size hash_estimate_size(long num_entries, Size entrysize)
Definition: dynahash.c:780
void TwoPhaseShmemInit(void)
Definition: twophase.c:252
int SpinlockSemas(void)
Definition: spin.c:64
static Size total_addin_request
Definition: ipci.c:56
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
Size AutoVacuumShmemSize(void)
Definition: autovacuum.c:3459
Size ReplicationOriginShmemSize(void)
Definition: origin.c:490
Size CheckpointerShmemSize(void)
Definition: checkpointer.c:865
Size CLOGShmemSize(void)
Definition: clog.c:685
void PGReserveSemaphores(int maxSemas)
Definition: posix_sema.c:196
Size WalRcvShmemSize(void)
void CreateLWLocks(void)
Definition: lwlock.c:465
void SyncScanShmemInit(void)
Definition: syncscan.c:135
void CreateSharedProcArray(void)
Definition: procarray.c:394
Size AsyncShmemSize(void)
Definition: async.c:509
size_t Size
Definition: c.h:540
Size CommitTsShmemSize(void)
Definition: commit_ts.c:523
void ApplyLauncherShmemInit(void)
Definition: launcher.c:737
Size MultiXactShmemSize(void)
Definition: multixact.c:1824
void XLogPrefetchShmemInit(void)
Definition: xlogprefetch.c:191
void CreateSharedBackendStatus(void)
Size LockShmemSize(void)
Definition: lock.c:3523
Size SUBTRANSShmemSize(void)
Definition: subtrans.c:185
Size BufferShmemSize(void)
Definition: buf_init.c:156
#define elog(elevel,...)
Definition: elog.h:232
void WalSndShmemInit(void)
Definition: walsender.c:3069
void CLOGShmemInit(void)
Definition: clog.c:691
Size PGSemaphoreShmemSize(int maxSemas)
Definition: posix_sema.c:165
void ProcSignalShmemInit(void)
Definition: procsignal.c:125
void InitShmemIndex(void)
Definition: shmem.c:292
void InitPredicateLocks(void)
Definition: predicate.c:1153
void AutoVacuumShmemInit(void)
Definition: autovacuum.c:3478
Size SnapMgrShmemSize(void)
Definition: snapmgr.c:197
Size ProcArrayShmemSize(void)
Definition: procarray.c:352
int ProcGlobalSemas(void)
Definition: proc.c:124
void InitShmemAllocation(void)
Definition: shmem.c:116
void CommitTsShmemInit(void)
Definition: commit_ts.c:534

◆ RequestAddinShmemSpace()

void RequestAddinShmemSpace ( Size  size)

Definition at line 72 of file ipci.c.

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

Referenced by _PG_init().

73 {
75  return; /* too late */
77 }
bool IsUnderPostmaster
Definition: globals.c:112
static bool addin_request_allowed
Definition: ipci.c:57
static Size total_addin_request
Definition: ipci.c:56
Size add_size(Size s1, Size s2)
Definition: shmem.c:502

Variable Documentation

◆ addin_request_allowed

bool addin_request_allowed = true
static

Definition at line 57 of file ipci.c.

Referenced by CreateSharedMemoryAndSemaphores(), and RequestAddinShmemSpace().

◆ shared_memory_type

int shared_memory_type = DEFAULT_SHARED_MEMORY_TYPE

Definition at line 52 of file ipci.c.

Referenced by PGSharedMemoryCreate().

◆ shmem_startup_hook

shmem_startup_hook_type shmem_startup_hook = NULL

Definition at line 54 of file ipci.c.

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

◆ total_addin_request

Size total_addin_request = 0
static

Definition at line 56 of file ipci.c.

Referenced by CreateSharedMemoryAndSemaphores(), and RequestAddinShmemSpace().