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 "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
 
static bool addin_request_allowed = true
 

Function Documentation

◆ CalculateShmemSize()

Size CalculateShmemSize ( int *  num_semaphores)

Definition at line 89 of file ipci.c.

References add_size(), addin_request_allowed, 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(), SUBTRANSShmemSize(), SyncScanShmemSize(), total_addin_request, TwoPhaseShmemSize(), WalRcvShmemSize(), WalSndShmemSize(), and XLOGShmemSize().

Referenced by CreateSharedMemoryAndSemaphores(), and InitializeShmemGUCs().

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

◆ CreateSharedMemoryAndSemaphores()

void CreateSharedMemoryAndSemaphores ( void  )

Definition at line 176 of file ipci.c.

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(), SUBTRANSShmemInit(), SyncScanShmemInit(), TwoPhaseShmemInit(), WalRcvShmemInit(), WalSndShmemInit(), and XLOGShmemInit().

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

177 {
178  PGShmemHeader *shim = NULL;
179 
180  if (!IsUnderPostmaster)
181  {
182  PGShmemHeader *seghdr;
183  Size size;
184  int numSemas;
185 
186  /* Compute the size of the shared-memory block */
187  size = CalculateShmemSize(&numSemas);
188  elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
189 
190  /*
191  * Create the shmem segment
192  */
193  seghdr = PGSharedMemoryCreate(size, &shim);
194 
195  InitShmemAccess(seghdr);
196 
197  /*
198  * Create semaphores
199  */
200  PGReserveSemaphores(numSemas);
201 
202  /*
203  * If spinlocks are disabled, initialize emulation layer (which
204  * depends on semaphores, so the order is important here).
205  */
206 #ifndef HAVE_SPINLOCKS
208 #endif
209  }
210  else
211  {
212  /*
213  * We are reattaching to an existing shared memory segment. This
214  * should only be reached in the EXEC_BACKEND case.
215  */
216 #ifndef EXEC_BACKEND
217  elog(PANIC, "should be attached to shared memory already");
218 #endif
219  }
220 
221  /*
222  * Set up shared memory allocation mechanism
223  */
224  if (!IsUnderPostmaster)
226 
227  /*
228  * Now initialize LWLocks, which do shared memory allocation and are
229  * needed for InitShmemIndex.
230  */
231  CreateLWLocks();
232 
233  /*
234  * Set up shmem.c index hashtable
235  */
236  InitShmemIndex();
237 
238  dsm_shmem_init();
239 
240  /*
241  * Set up xlog, clog, and buffers
242  */
243  XLOGShmemInit();
244  CLOGShmemInit();
248  InitBufferPool();
249 
250  /*
251  * Set up lock manager
252  */
253  InitLocks();
254 
255  /*
256  * Set up predicate lock manager
257  */
259 
260  /*
261  * Set up process table
262  */
263  if (!IsUnderPostmaster)
264  InitProcGlobal();
269 
270  /*
271  * Set up shared-inval messaging
272  */
274 
275  /*
276  * Set up interprocess signaling mechanisms
277  */
284  WalSndShmemInit();
285  WalRcvShmemInit();
286  PgArchShmemInit();
288 
289  /*
290  * Set up other modules that need some shared memory space
291  */
292  SnapMgrInit();
293  BTreeShmemInit();
295  AsyncShmemInit();
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 dsm_postmaster_startup(PGShmemHeader *shim)
Definition: dsm.c:149
PGShmemHeader * PGSharedMemoryCreate(Size size, PGShmemHeader **shim)
Definition: sysv_shmem.c:678
void BTreeShmemInit(void)
Definition: nbtutils.c:2076
void dsm_shmem_init(void)
Definition: dsm.c:450
void SUBTRANSShmemInit(void)
Definition: subtrans.c:191
#define DEBUG3
Definition: elog.h:23
void InitShmemAccess(void *seghdr)
Definition: shmem.c:101
void AsyncShmemInit(void)
Definition: async.c:533
void CreateSharedInvalidationState(void)
Definition: sinvaladt.c:218
void PgArchShmemInit(void)
Definition: pgarch.c:120
void CheckpointerShmemInit(void)
Definition: checkpointer.c:877
void ReplicationOriginShmemInit(void)
Definition: origin.c:510
void WalRcvShmemInit(void)
#define PANIC
Definition: elog.h:50
void XLOGShmemInit(void)
Definition: xlog.c:5170
void ReplicationSlotsShmemInit(void)
Definition: slot.c:132
void InitProcGlobal(void)
Definition: proc.c:159
void BackgroundWorkerShmemInit(void)
Definition: bgworker.c:158
void MultiXactShmemInit(void)
Definition: multixact.c:1844
void PMSignalShmemInit(void)
Definition: pmsignal.c:134
void SpinlockSemaInit(void)
Definition: spin.c:77
bool IsUnderPostmaster
Definition: globals.c:112
Size CalculateShmemSize(int *num_semaphores)
Definition: ipci.c:89
void InitBufferPool(void)
Definition: buf_init.c:67
void SnapMgrInit(void)
Definition: snapmgr.c:213
void InitLocks(void)
Definition: lock.c:405
shmem_startup_hook_type shmem_startup_hook
Definition: ipci.c:53
void TwoPhaseShmemInit(void)
Definition: twophase.c:252
void PGReserveSemaphores(int maxSemas)
Definition: posix_sema.c:196
void CreateLWLocks(void)
Definition: lwlock.c:466
void SyncScanShmemInit(void)
Definition: syncscan.c:135
void CreateSharedProcArray(void)
Definition: procarray.c:405
size_t Size
Definition: c.h:540
void ApplyLauncherShmemInit(void)
Definition: launcher.c:736
void CreateSharedBackendStatus(void)
#define elog(elevel,...)
Definition: elog.h:232
void WalSndShmemInit(void)
Definition: walsender.c:3198
void CLOGShmemInit(void)
Definition: clog.c:696
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:3440
void InitShmemAllocation(void)
Definition: shmem.c:116
void CommitTsShmemInit(void)
Definition: commit_ts.c:535

◆ InitializeShmemGUCs()

void InitializeShmemGUCs ( void  )

Definition at line 324 of file ipci.c.

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

Referenced by PostmasterMain().

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 }
#define sprintf
Definition: port.h:219
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:8110
static char * buf
Definition: pg_test_fsync.c:68
Size CalculateShmemSize(int *num_semaphores)
Definition: ipci.c:89
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
size_t Size
Definition: c.h:540
void GetHugePageSize(Size *hugepagesize, int *mmap_flags)
Definition: sysv_shmem.c:482

◆ RequestAddinShmemSpace()

void RequestAddinShmemSpace ( Size  size)

Definition at line 71 of file ipci.c.

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

Referenced by _PG_init().

72 {
74  return; /* too late */
76 }
bool IsUnderPostmaster
Definition: globals.c:112
static bool addin_request_allowed
Definition: ipci.c:56
static Size total_addin_request
Definition: ipci.c:55
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 56 of file ipci.c.

Referenced by CalculateShmemSize(), and RequestAddinShmemSpace().

◆ shared_memory_type

int shared_memory_type = DEFAULT_SHARED_MEMORY_TYPE

Definition at line 51 of file ipci.c.

Referenced by PGSharedMemoryCreate().

◆ shmem_startup_hook

shmem_startup_hook_type shmem_startup_hook = NULL

Definition at line 53 of file ipci.c.

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

◆ total_addin_request

Size total_addin_request = 0
static

Definition at line 55 of file ipci.c.

Referenced by CalculateShmemSize(), and RequestAddinShmemSpace().