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/xlogprefetcher.h"
#include "access/xlogrecovery.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)
 
Size CalculateShmemSize (int *num_semaphores)
 
void CreateSharedMemoryAndSemaphores (void)
 
void InitializeShmemGUCs (void)
 

Variables

int shared_memory_type = DEFAULT_SHARED_MEMORY_TYPE
 
shmem_startup_hook_type shmem_startup_hook = NULL
 
static Size total_addin_request = 0
 

Function Documentation

◆ CalculateShmemSize()

Size CalculateShmemSize ( int *  num_semaphores)

Definition at line 84 of file ipci.c.

85 {
86  Size size;
87  int numSemas;
88 
89  /* Compute number of semaphores we'll need */
90  numSemas = ProcGlobalSemas();
91  numSemas += SpinlockSemas();
92 
93  /* Return the number of semaphores if requested by the caller */
94  if (num_semaphores)
95  *num_semaphores = numSemas;
96 
97  /*
98  * Size of the Postgres shared-memory block is estimated via moderately-
99  * accurate estimates for the big hogs, plus 100K for the stuff that's too
100  * small to bother with estimating.
101  *
102  * We take some care to ensure that the total size request doesn't
103  * overflow size_t. If this gets through, we don't need to be so careful
104  * during the actual allocation phase.
105  */
106  size = 100000;
107  size = add_size(size, PGSemaphoreShmemSize(numSemas));
108  size = add_size(size, SpinlockSemaSize());
110  sizeof(ShmemIndexEnt)));
111  size = add_size(size, dsm_estimate_size());
112  size = add_size(size, BufferShmemSize());
113  size = add_size(size, LockShmemSize());
114  size = add_size(size, PredicateLockShmemSize());
115  size = add_size(size, ProcGlobalShmemSize());
116  size = add_size(size, XLogPrefetchShmemSize());
117  size = add_size(size, XLOGShmemSize());
118  size = add_size(size, XLogRecoveryShmemSize());
119  size = add_size(size, CLOGShmemSize());
120  size = add_size(size, CommitTsShmemSize());
121  size = add_size(size, SUBTRANSShmemSize());
122  size = add_size(size, TwoPhaseShmemSize());
123  size = add_size(size, BackgroundWorkerShmemSize());
124  size = add_size(size, MultiXactShmemSize());
125  size = add_size(size, LWLockShmemSize());
126  size = add_size(size, ProcArrayShmemSize());
127  size = add_size(size, BackendStatusShmemSize());
128  size = add_size(size, SInvalShmemSize());
129  size = add_size(size, PMSignalShmemSize());
130  size = add_size(size, ProcSignalShmemSize());
131  size = add_size(size, CheckpointerShmemSize());
132  size = add_size(size, AutoVacuumShmemSize());
133  size = add_size(size, ReplicationSlotsShmemSize());
134  size = add_size(size, ReplicationOriginShmemSize());
135  size = add_size(size, WalSndShmemSize());
136  size = add_size(size, WalRcvShmemSize());
137  size = add_size(size, PgArchShmemSize());
138  size = add_size(size, ApplyLauncherShmemSize());
139  size = add_size(size, SnapMgrShmemSize());
140  size = add_size(size, BTreeShmemSize());
141  size = add_size(size, SyncScanShmemSize());
142  size = add_size(size, AsyncShmemSize());
143  size = add_size(size, StatsShmemSize());
144 #ifdef EXEC_BACKEND
145  size = add_size(size, ShmemBackendArraySize());
146 #endif
147 
148  /* include additional requested shmem from preload libraries */
149  size = add_size(size, total_addin_request);
150 
151  /* might as well round it off to a multiple of a typical page size */
152  size = add_size(size, 8192 - (size % 8192));
153 
154  return size;
155 }
Size AsyncShmemSize(void)
Definition: async.c:516
Size AutoVacuumShmemSize(void)
Definition: autovacuum.c:3320
Size BackendStatusShmemSize(void)
Size BackgroundWorkerShmemSize(void)
Definition: bgworker.c:142
Size BufferShmemSize(void)
Definition: buf_init.c:157
size_t Size
Definition: c.h:540
Size CheckpointerShmemSize(void)
Definition: checkpointer.c:869
Size CLOGShmemSize(void)
Definition: clog.c:691
Size CommitTsShmemSize(void)
Definition: commit_ts.c:528
size_t dsm_estimate_size(void)
Definition: dsm.c:444
Size hash_estimate_size(long num_entries, Size entrysize)
Definition: dynahash.c:780
static Size total_addin_request
Definition: ipci.c:57
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:687
Size LockShmemSize(void)
Definition: lock.c:3609
Size LWLockShmemSize(void)
Definition: lwlock.c:437
Size MultiXactShmemSize(void)
Definition: multixact.c:1827
Size BTreeShmemSize(void)
Definition: nbtutils.c:2070
Size ReplicationOriginShmemSize(void)
Definition: origin.c:490
Size PgArchShmemSize(void)
Definition: pgarch.c:149
Size StatsShmemSize(void)
Definition: pgstat_shmem.c:126
Size PMSignalShmemSize(void)
Definition: pmsignal.c:119
Size PGSemaphoreShmemSize(int maxSemas)
Definition: posix_sema.c:165
Size PredicateLockShmemSize(void)
Definition: predicate.c:1356
Size ProcArrayShmemSize(void)
Definition: procarray.c:362
Size ProcSignalShmemSize(void)
Definition: procsignal.c:111
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
#define SHMEM_INDEX_SIZE
Definition: shmem.h:55
Size SInvalShmemSize(void)
Definition: sinvaladt.c:203
Size ReplicationSlotsShmemSize(void)
Definition: slot.c:116
Size SnapMgrShmemSize(void)
Definition: snapmgr.c:197
Size SpinlockSemaSize(void)
Definition: spin.c:55
int SpinlockSemas(void)
Definition: spin.c:64
Size ProcGlobalShmemSize(void)
Definition: proc.c:102
int ProcGlobalSemas(void)
Definition: proc.c:124
Size SUBTRANSShmemSize(void)
Definition: subtrans.c:203
Size SyncScanShmemSize(void)
Definition: syncscan.c:126
Size TwoPhaseShmemSize(void)
Definition: twophase.c:240
Size WalRcvShmemSize(void)
Size WalSndShmemSize(void)
Definition: walsender.c:3265
Size XLOGShmemSize(void)
Definition: xlog.c:4337
size_t XLogPrefetchShmemSize(void)
Size XLogRecoveryShmemSize(void)
Definition: xlogrecovery.c:443

References add_size(), ApplyLauncherShmemSize(), AsyncShmemSize(), AutoVacuumShmemSize(), BackendStatusShmemSize(), BackgroundWorkerShmemSize(), BTreeShmemSize(), BufferShmemSize(), CheckpointerShmemSize(), CLOGShmemSize(), CommitTsShmemSize(), dsm_estimate_size(), hash_estimate_size(), LockShmemSize(), LWLockShmemSize(), MultiXactShmemSize(), PgArchShmemSize(), PGSemaphoreShmemSize(), PMSignalShmemSize(), PredicateLockShmemSize(), ProcArrayShmemSize(), ProcGlobalSemas(), ProcGlobalShmemSize(), ProcSignalShmemSize(), ReplicationOriginShmemSize(), ReplicationSlotsShmemSize(), SHMEM_INDEX_SIZE, SInvalShmemSize(), SnapMgrShmemSize(), SpinlockSemas(), SpinlockSemaSize(), StatsShmemSize(), SUBTRANSShmemSize(), SyncScanShmemSize(), total_addin_request, TwoPhaseShmemSize(), WalRcvShmemSize(), WalSndShmemSize(), XLogPrefetchShmemSize(), XLogRecoveryShmemSize(), and XLOGShmemSize().

Referenced by CreateSharedMemoryAndSemaphores(), and InitializeShmemGUCs().

◆ CreateSharedMemoryAndSemaphores()

void CreateSharedMemoryAndSemaphores ( void  )

Definition at line 173 of file ipci.c.

174 {
175  PGShmemHeader *shim = NULL;
176 
177  if (!IsUnderPostmaster)
178  {
179  PGShmemHeader *seghdr;
180  Size size;
181  int numSemas;
182 
183  /* Compute the size of the shared-memory block */
184  size = CalculateShmemSize(&numSemas);
185  elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
186 
187  /*
188  * Create the shmem segment
189  */
190  seghdr = PGSharedMemoryCreate(size, &shim);
191 
192  InitShmemAccess(seghdr);
193 
194  /*
195  * Create semaphores
196  */
197  PGReserveSemaphores(numSemas);
198 
199  /*
200  * If spinlocks are disabled, initialize emulation layer (which
201  * depends on semaphores, so the order is important here).
202  */
203 #ifndef HAVE_SPINLOCKS
205 #endif
206  }
207  else
208  {
209  /*
210  * We are reattaching to an existing shared memory segment. This
211  * should only be reached in the EXEC_BACKEND case.
212  */
213 #ifndef EXEC_BACKEND
214  elog(PANIC, "should be attached to shared memory already");
215 #endif
216  }
217 
218  /*
219  * Set up shared memory allocation mechanism
220  */
221  if (!IsUnderPostmaster)
223 
224  /*
225  * Now initialize LWLocks, which do shared memory allocation and are
226  * needed for InitShmemIndex.
227  */
228  CreateLWLocks();
229 
230  /*
231  * Set up shmem.c index hashtable
232  */
233  InitShmemIndex();
234 
235  dsm_shmem_init();
236 
237  /*
238  * Set up xlog, clog, and buffers
239  */
240  XLOGShmemInit();
243  CLOGShmemInit();
247  InitBufferPool();
248 
249  /*
250  * Set up lock manager
251  */
252  InitLocks();
253 
254  /*
255  * Set up predicate lock manager
256  */
258 
259  /*
260  * Set up process table
261  */
262  if (!IsUnderPostmaster)
263  InitProcGlobal();
268 
269  /*
270  * Set up shared-inval messaging
271  */
273 
274  /*
275  * Set up interprocess signaling mechanisms
276  */
283  WalSndShmemInit();
284  WalRcvShmemInit();
285  PgArchShmemInit();
287 
288  /*
289  * Set up other modules that need some shared memory space
290  */
291  SnapMgrInit();
292  BTreeShmemInit();
294  AsyncShmemInit();
295  StatsShmemInit();
296 
297 #ifdef EXEC_BACKEND
298 
299  /*
300  * Alloc the win32 shared backend array
301  */
302  if (!IsUnderPostmaster)
303  ShmemBackendArrayAllocation();
304 #endif
305 
306  /* Initialize dynamic shared memory facilities. */
307  if (!IsUnderPostmaster)
309 
310  /*
311  * Now give loadable modules a chance to set up their shmem allocations
312  */
313  if (shmem_startup_hook)
315 }
void AsyncShmemInit(void)
Definition: async.c:533
void AutoVacuumShmemInit(void)
Definition: autovacuum.c:3339
void CreateSharedBackendStatus(void)
void BackgroundWorkerShmemInit(void)
Definition: bgworker.c:158
void InitBufferPool(void)
Definition: buf_init.c:68
void CheckpointerShmemInit(void)
Definition: checkpointer.c:888
void CLOGShmemInit(void)
Definition: clog.c:697
void CommitTsShmemInit(void)
Definition: commit_ts.c:539
void dsm_postmaster_startup(PGShmemHeader *shim)
Definition: dsm.c:150
void dsm_shmem_init(void)
Definition: dsm.c:453
#define DEBUG3
Definition: elog.h:22
#define PANIC
Definition: elog.h:36
#define elog(elevel,...)
Definition: elog.h:218
bool IsUnderPostmaster
Definition: globals.c:113
shmem_startup_hook_type shmem_startup_hook
Definition: ipci.c:55
Size CalculateShmemSize(int *num_semaphores)
Definition: ipci.c:84
void ApplyLauncherShmemInit(void)
Definition: launcher.c:735
void InitLocks(void)
Definition: lock.c:405
void CreateLWLocks(void)
Definition: lwlock.c:467
void MultiXactShmemInit(void)
Definition: multixact.c:1844
void BTreeShmemInit(void)
Definition: nbtutils.c:2083
void ReplicationOriginShmemInit(void)
Definition: origin.c:510
void PgArchShmemInit(void)
Definition: pgarch.c:160
void StatsShmemInit(void)
Definition: pgstat_shmem.c:140
void PMSignalShmemInit(void)
Definition: pmsignal.c:134
void PGReserveSemaphores(int maxSemas)
Definition: posix_sema.c:196
void InitPredicateLocks(void)
Definition: predicate.c:1153
void CreateSharedProcArray(void)
Definition: procarray.c:404
void ProcSignalShmemInit(void)
Definition: procsignal.c:125
void InitShmemIndex(void)
Definition: shmem.c:292
void InitShmemAllocation(void)
Definition: shmem.c:116
void InitShmemAccess(void *seghdr)
Definition: shmem.c:101
void CreateSharedInvalidationState(void)
Definition: sinvaladt.c:227
void ReplicationSlotsShmemInit(void)
Definition: slot.c:134
void SnapMgrInit(void)
Definition: snapmgr.c:213
void SpinlockSemaInit(void)
Definition: spin.c:77
void InitProcGlobal(void)
Definition: proc.c:159
void SUBTRANSShmemInit(void)
Definition: subtrans.c:209
void SyncScanShmemInit(void)
Definition: syncscan.c:135
PGShmemHeader * PGSharedMemoryCreate(Size size, PGShmemHeader **shim)
Definition: sysv_shmem.c:678
void TwoPhaseShmemInit(void)
Definition: twophase.c:256
void WalRcvShmemInit(void)
void WalSndShmemInit(void)
Definition: walsender.c:3277
void XLOGShmemInit(void)
Definition: xlog.c:4378
void XLogPrefetchShmemInit(void)
void XLogRecoveryShmemInit(void)
Definition: xlogrecovery.c:454

References ApplyLauncherShmemInit(), AsyncShmemInit(), AutoVacuumShmemInit(), BackgroundWorkerShmemInit(), BTreeShmemInit(), CalculateShmemSize(), CheckpointerShmemInit(), CLOGShmemInit(), CommitTsShmemInit(), CreateLWLocks(), CreateSharedBackendStatus(), CreateSharedInvalidationState(), CreateSharedProcArray(), DEBUG3, dsm_postmaster_startup(), dsm_shmem_init(), elog, InitBufferPool(), InitLocks(), InitPredicateLocks(), InitProcGlobal(), InitShmemAccess(), InitShmemAllocation(), InitShmemIndex(), IsUnderPostmaster, MultiXactShmemInit(), PANIC, PgArchShmemInit(), PGReserveSemaphores(), PGSharedMemoryCreate(), PMSignalShmemInit(), ProcSignalShmemInit(), ReplicationOriginShmemInit(), ReplicationSlotsShmemInit(), shmem_startup_hook, SnapMgrInit(), SpinlockSemaInit(), StatsShmemInit(), SUBTRANSShmemInit(), SyncScanShmemInit(), TwoPhaseShmemInit(), WalRcvShmemInit(), WalSndShmemInit(), XLogPrefetchShmemInit(), XLogRecoveryShmemInit(), and XLOGShmemInit().

Referenced by BootstrapModeMain(), PostgresSingleUserMain(), and reset_shared().

◆ InitializeShmemGUCs()

void InitializeShmemGUCs ( void  )

Definition at line 324 of file ipci.c.

325 {
326  char buf[64];
327  Size size_b;
328  Size size_mb;
329  Size hp_size;
330 
331  /*
332  * Calculate the shared memory size and round up to the nearest megabyte.
333  */
334  size_b = CalculateShmemSize(NULL);
335  size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
336  sprintf(buf, "%zu", size_mb);
337  SetConfigOption("shared_memory_size", buf, PGC_INTERNAL, PGC_S_OVERRIDE);
338 
339  /*
340  * Calculate the number of huge pages required.
341  */
342  GetHugePageSize(&hp_size, NULL);
343  if (hp_size != 0)
344  {
345  Size hp_required;
346 
347  hp_required = add_size(size_b / hp_size, 1);
348  sprintf(buf, "%zu", hp_required);
349  SetConfigOption("shared_memory_size_in_huge_pages", buf, PGC_INTERNAL, PGC_S_OVERRIDE);
350  }
351 }
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:8370
@ PGC_S_OVERRIDE
Definition: guc.h:116
@ PGC_INTERNAL
Definition: guc.h:70
static char * buf
Definition: pg_test_fsync.c:67
#define sprintf
Definition: port.h:227
void GetHugePageSize(Size *hugepagesize, int *mmap_flags)
Definition: sysv_shmem.c:482

References add_size(), buf, CalculateShmemSize(), GetHugePageSize(), PGC_INTERNAL, PGC_S_OVERRIDE, SetConfigOption(), and sprintf.

Referenced by PostmasterMain().

◆ RequestAddinShmemSpace()

void RequestAddinShmemSpace ( Size  size)

Definition at line 69 of file ipci.c.

70 {
72  elog(FATAL, "cannot request additional shared memory outside shmem_request_hook");
74 }
#define FATAL
Definition: elog.h:35
bool process_shmem_requests_in_progress
Definition: miscinit.c:1622

References add_size(), elog, FATAL, process_shmem_requests_in_progress, and total_addin_request.

Referenced by autoprewarm_shmem_request(), and pgss_shmem_request().

Variable Documentation

◆ shared_memory_type

int shared_memory_type = DEFAULT_SHARED_MEMORY_TYPE

Definition at line 53 of file ipci.c.

Referenced by PGSharedMemoryCreate().

◆ shmem_startup_hook

shmem_startup_hook_type shmem_startup_hook = NULL

Definition at line 55 of file ipci.c.

Referenced by _PG_init(), and CreateSharedMemoryAndSemaphores().

◆ total_addin_request

Size total_addin_request = 0
static

Definition at line 57 of file ipci.c.

Referenced by CalculateShmemSize(), and RequestAddinShmemSpace().