| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* ipci.c |
| 4 |
* POSTGRES inter-process communication initialization code. |
| 5 |
* |
| 6 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 7 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 8 |
* |
| 9 |
* |
| 10 |
* IDENTIFICATION |
| 11 |
* src/backend/storage/ipc/ipci.c |
| 12 |
* |
| 13 |
*------------------------------------------------------------------------- |
| 14 |
*/ |
| 15 |
#include "postgres.h" |
| 16 |
|
| 17 |
#include "access/clog.h" |
| 18 |
#include "access/commit_ts.h" |
| 19 |
#include "access/multixact.h" |
| 20 |
#include "access/nbtree.h" |
| 21 |
#include "access/subtrans.h" |
| 22 |
#include "access/syncscan.h" |
| 23 |
#include "access/transam.h" |
| 24 |
#include "access/twophase.h" |
| 25 |
#include "access/xlogprefetcher.h" |
| 26 |
#include "access/xlogrecovery.h" |
| 27 |
#include "commands/async.h" |
| 28 |
#include "miscadmin.h" |
| 29 |
#include "pgstat.h" |
| 30 |
#include "postmaster/autovacuum.h" |
| 31 |
#include "postmaster/bgworker_internals.h" |
| 32 |
#include "postmaster/bgwriter.h" |
| 33 |
#include "postmaster/walsummarizer.h" |
| 34 |
#include "replication/logicallauncher.h" |
| 35 |
#include "replication/origin.h" |
| 36 |
#include "replication/slot.h" |
| 37 |
#include "replication/slotsync.h" |
| 38 |
#include "replication/walreceiver.h" |
| 39 |
#include "replication/walsender.h" |
| 40 |
#include "storage/aio_subsys.h" |
| 41 |
#include "storage/bufmgr.h" |
| 42 |
#include "storage/dsm.h" |
| 43 |
#include "storage/dsm_registry.h" |
| 44 |
#include "storage/ipc.h" |
| 45 |
#include "storage/pg_shmem.h" |
| 46 |
#include "storage/pmsignal.h" |
| 47 |
#include "storage/predicate.h" |
| 48 |
#include "storage/proc.h" |
| 49 |
#include "storage/procarray.h" |
| 50 |
#include "storage/procsignal.h" |
| 51 |
#include "storage/sinvaladt.h" |
| 52 |
#include "utils/guc.h" |
| 53 |
#include "utils/injection_point.h" |
| 54 |
|
| 55 |
/* GUCs */ |
| 56 |
int shared_memory_type = DEFAULT_SHARED_MEMORY_TYPE; |
| 57 |
|
| 58 |
shmem_startup_hook_type shmem_startup_hook = NULL; |
| 59 |
|
| 60 |
static Size total_addin_request = 0; |
| 61 |
|
| 62 |
static void CreateOrAttachShmemStructs(void); |
| 63 |
|
| 64 |
/* |
| 65 |
* RequestAddinShmemSpace |
| 66 |
* Request that extra shmem space be allocated for use by |
| 67 |
* a loadable module. |
| 68 |
* |
| 69 |
* This may only be called via the shmem_request_hook of a library that is |
| 70 |
* loaded into the postmaster via shared_preload_libraries. Calls from |
| 71 |
* elsewhere will fail. |
| 72 |
*/ |
| 73 |
void |
| 74 |
RequestAddinShmemSpace(Size size) |
| 75 |
{ |
| 76 |
if (!process_shmem_requests_in_progress) |
| 77 |
elog(FATAL, "cannot request additional shared memory outside shmem_request_hook"); |
| 78 |
total_addin_request = add_size(total_addin_request, size); |
| 79 |
} |
| 80 |
|
| 81 |
/* |
| 82 |
* CalculateShmemSize |
| 83 |
* Calculates the amount of shared memory and number of semaphores needed. |
| 84 |
* |
| 85 |
* If num_semaphores is not NULL, it will be set to the number of semaphores |
| 86 |
* required. |
| 87 |
*/ |
| 88 |
Size |
| 89 |
CalculateShmemSize(int *num_semaphores) |
| 90 |
{ |
| 91 |
Size size; |
| 92 |
int numSemas; |
| 93 |
|
| 94 |
/* Compute number of semaphores we'll need */ |
| 95 |
numSemas = ProcGlobalSemas(); |
| 96 |
|
| 97 |
/* Return the number of semaphores if requested by the caller */ |
| 98 |
if (num_semaphores) |
| 99 |
*num_semaphores = numSemas; |
| 100 |
|
| 101 |
/* |
| 102 |
* Size of the Postgres shared-memory block is estimated via moderately- |
| 103 |
* accurate estimates for the big hogs, plus 100K for the stuff that's too |
| 104 |
* small to bother with estimating. |
| 105 |
* |
| 106 |
* We take some care to ensure that the total size request doesn't |
| 107 |
* overflow size_t. If this gets through, we don't need to be so careful |
| 108 |
* during the actual allocation phase. |
| 109 |
*/ |
| 110 |
size = 100000; |
| 111 |
size = add_size(size, PGSemaphoreShmemSize(numSemas)); |
| 112 |
size = add_size(size, hash_estimate_size(SHMEM_INDEX_SIZE, |
| 113 |
sizeof(ShmemIndexEnt))); |
| 114 |
size = add_size(size, dsm_estimate_size()); |
| 115 |
size = add_size(size, DSMRegistryShmemSize()); |
| 116 |
size = add_size(size, BufferManagerShmemSize()); |
| 117 |
size = add_size(size, LockManagerShmemSize()); |
| 118 |
size = add_size(size, PredicateLockShmemSize()); |
| 119 |
size = add_size(size, ProcGlobalShmemSize()); |
| 120 |
size = add_size(size, XLogPrefetchShmemSize()); |
| 121 |
size = add_size(size, VarsupShmemSize()); |
| 122 |
size = add_size(size, XLOGShmemSize()); |
| 123 |
size = add_size(size, XLogRecoveryShmemSize()); |
| 124 |
size = add_size(size, CLOGShmemSize()); |
| 125 |
size = add_size(size, CommitTsShmemSize()); |
| 126 |
size = add_size(size, SUBTRANSShmemSize()); |
| 127 |
size = add_size(size, TwoPhaseShmemSize()); |
| 128 |
size = add_size(size, BackgroundWorkerShmemSize()); |
| 129 |
size = add_size(size, MultiXactShmemSize()); |
| 130 |
size = add_size(size, LWLockShmemSize()); |
| 131 |
size = add_size(size, ProcArrayShmemSize()); |
| 132 |
size = add_size(size, BackendStatusShmemSize()); |
| 133 |
size = add_size(size, SharedInvalShmemSize()); |
| 134 |
size = add_size(size, PMSignalShmemSize()); |
| 135 |
size = add_size(size, ProcSignalShmemSize()); |
| 136 |
size = add_size(size, CheckpointerShmemSize()); |
| 137 |
size = add_size(size, AutoVacuumShmemSize()); |
| 138 |
size = add_size(size, ReplicationSlotsShmemSize()); |
| 139 |
size = add_size(size, ReplicationOriginShmemSize()); |
| 140 |
size = add_size(size, WalSndShmemSize()); |
| 141 |
size = add_size(size, WalRcvShmemSize()); |
| 142 |
size = add_size(size, WalSummarizerShmemSize()); |
| 143 |
size = add_size(size, PgArchShmemSize()); |
| 144 |
size = add_size(size, ApplyLauncherShmemSize()); |
| 145 |
size = add_size(size, BTreeShmemSize()); |
| 146 |
size = add_size(size, SyncScanShmemSize()); |
| 147 |
size = add_size(size, AsyncShmemSize()); |
| 148 |
size = add_size(size, StatsShmemSize()); |
| 149 |
size = add_size(size, WaitEventCustomShmemSize()); |
| 150 |
size = add_size(size, InjectionPointShmemSize()); |
| 151 |
size = add_size(size, SlotSyncShmemSize()); |
| 152 |
size = add_size(size, AioShmemSize()); |
| 153 |
|
| 154 |
/* include additional requested shmem from preload libraries */ |
| 155 |
size = add_size(size, total_addin_request); |
| 156 |
|
| 157 |
/* might as well round it off to a multiple of a typical page size */ |
| 158 |
size = add_size(size, 8192 - (size % 8192)); |
| 159 |
|
| 160 |
return size; |
| 161 |
} |
| 162 |
|
| 163 |
#ifdef EXEC_BACKEND |
| 164 |
/* |
| 165 |
* AttachSharedMemoryStructs |
| 166 |
* Initialize a postmaster child process's access to shared memory |
| 167 |
* structures. |
| 168 |
* |
| 169 |
* In !EXEC_BACKEND mode, we inherit everything through the fork, and this |
| 170 |
* isn't needed. |
| 171 |
*/ |
| 172 |
void |
| 173 |
AttachSharedMemoryStructs(void) |
| 174 |
{ |
| 175 |
/* InitProcess must've been called already */ |
| 176 |
Assert(MyProc != NULL); |
| 177 |
Assert(IsUnderPostmaster); |
| 178 |
|
| 179 |
/* |
| 180 |
* In EXEC_BACKEND mode, backends don't inherit the number of fast-path |
| 181 |
* groups we calculated before setting the shmem up, so recalculate it. |
| 182 |
*/ |
| 183 |
InitializeFastPathLocks(); |
| 184 |
|
| 185 |
CreateOrAttachShmemStructs(); |
| 186 |
|
| 187 |
/* |
| 188 |
* Now give loadable modules a chance to set up their shmem allocations |
| 189 |
*/ |
| 190 |
if (shmem_startup_hook) |
| 191 |
shmem_startup_hook(); |
| 192 |
} |
| 193 |
#endif |
| 194 |
|
| 195 |
/* |
| 196 |
* CreateSharedMemoryAndSemaphores |
| 197 |
* Creates and initializes shared memory and semaphores. |
| 198 |
*/ |
| 199 |
void |
| 200 |
CreateSharedMemoryAndSemaphores(void) |
| 201 |
{ |
| 202 |
PGShmemHeader *shim; |
| 203 |
PGShmemHeader *seghdr; |
| 204 |
Size size; |
| 205 |
int numSemas; |
| 206 |
|
| 207 |
Assert(!IsUnderPostmaster); |
| 208 |
|
| 209 |
/* Compute the size of the shared-memory block */ |
| 210 |
size = CalculateShmemSize(&numSemas); |
| 211 |
elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size); |
| 212 |
|
| 213 |
/* |
| 214 |
* Create the shmem segment |
| 215 |
*/ |
| 216 |
seghdr = PGSharedMemoryCreate(size, &shim); |
| 217 |
|
| 218 |
/* |
| 219 |
* Make sure that huge pages are never reported as "unknown" while the |
| 220 |
* server is running. |
| 221 |
*/ |
| 222 |
Assert(strcmp("unknown", |
| 223 |
GetConfigOption("huge_pages_status", false, false)) != 0); |
| 224 |
|
| 225 |
InitShmemAccess(seghdr); |
| 226 |
|
| 227 |
/* |
| 228 |
* Create semaphores. (This is done here for historical reasons. We used |
| 229 |
* to support emulating spinlocks with semaphores, which required |
| 230 |
* initializing semaphores early.) |
| 231 |
*/ |
| 232 |
PGReserveSemaphores(numSemas); |
| 233 |
|
| 234 |
/* |
| 235 |
* Set up shared memory allocation mechanism |
| 236 |
*/ |
| 237 |
InitShmemAllocation(); |
| 238 |
|
| 239 |
/* Initialize subsystems */ |
| 240 |
CreateOrAttachShmemStructs(); |
| 241 |
|
| 242 |
/* Initialize dynamic shared memory facilities. */ |
| 243 |
dsm_postmaster_startup(shim); |
| 244 |
|
| 245 |
/* |
| 246 |
* Now give loadable modules a chance to set up their shmem allocations |
| 247 |
*/ |
| 248 |
if (shmem_startup_hook) |
| 249 |
shmem_startup_hook(); |
| 250 |
} |
| 251 |
|
| 252 |
/* |
| 253 |
* Initialize various subsystems, setting up their data structures in |
| 254 |
* shared memory. |
| 255 |
* |
| 256 |
* This is called by the postmaster or by a standalone backend. |
| 257 |
* It is also called by a backend forked from the postmaster in the |
| 258 |
* EXEC_BACKEND case. In the latter case, the shared memory segment |
| 259 |
* already exists and has been physically attached to, but we have to |
| 260 |
* initialize pointers in local memory that reference the shared structures, |
| 261 |
* because we didn't inherit the correct pointer values from the postmaster |
| 262 |
* as we do in the fork() scenario. The easiest way to do that is to run |
| 263 |
* through the same code as before. (Note that the called routines mostly |
| 264 |
* check IsUnderPostmaster, rather than EXEC_BACKEND, to detect this case. |
| 265 |
* This is a bit code-wasteful and could be cleaned up.) |
| 266 |
*/ |
| 267 |
static void |
| 268 |
CreateOrAttachShmemStructs(void) |
| 269 |
{ |
| 270 |
/* |
| 271 |
* Now initialize LWLocks, which do shared memory allocation and are |
| 272 |
* needed for InitShmemIndex. |
| 273 |
*/ |
| 274 |
CreateLWLocks(); |
| 275 |
|
| 276 |
/* |
| 277 |
* Set up shmem.c index hashtable |
| 278 |
*/ |
| 279 |
InitShmemIndex(); |
| 280 |
|
| 281 |
dsm_shmem_init(); |
| 282 |
DSMRegistryShmemInit(); |
| 283 |
|
| 284 |
/* |
| 285 |
* Set up xlog, clog, and buffers |
| 286 |
*/ |
| 287 |
VarsupShmemInit(); |
| 288 |
XLOGShmemInit(); |
| 289 |
XLogPrefetchShmemInit(); |
| 290 |
XLogRecoveryShmemInit(); |
| 291 |
CLOGShmemInit(); |
| 292 |
CommitTsShmemInit(); |
| 293 |
SUBTRANSShmemInit(); |
| 294 |
MultiXactShmemInit(); |
| 295 |
BufferManagerShmemInit(); |
| 296 |
|
| 297 |
/* |
| 298 |
* Set up lock manager |
| 299 |
*/ |
| 300 |
LockManagerShmemInit(); |
| 301 |
|
| 302 |
/* |
| 303 |
* Set up predicate lock manager |
| 304 |
*/ |
| 305 |
PredicateLockShmemInit(); |
| 306 |
|
| 307 |
/* |
| 308 |
* Set up process table |
| 309 |
*/ |
| 310 |
if (!IsUnderPostmaster) |
| 311 |
InitProcGlobal(); |
| 312 |
ProcArrayShmemInit(); |
| 313 |
BackendStatusShmemInit(); |
| 314 |
TwoPhaseShmemInit(); |
| 315 |
BackgroundWorkerShmemInit(); |
| 316 |
|
| 317 |
/* |
| 318 |
* Set up shared-inval messaging |
| 319 |
*/ |
| 320 |
SharedInvalShmemInit(); |
| 321 |
|
| 322 |
/* |
| 323 |
* Set up interprocess signaling mechanisms |
| 324 |
*/ |
| 325 |
PMSignalShmemInit(); |
| 326 |
ProcSignalShmemInit(); |
| 327 |
CheckpointerShmemInit(); |
| 328 |
AutoVacuumShmemInit(); |
| 329 |
ReplicationSlotsShmemInit(); |
| 330 |
ReplicationOriginShmemInit(); |
| 331 |
WalSndShmemInit(); |
| 332 |
WalRcvShmemInit(); |
| 333 |
WalSummarizerShmemInit(); |
| 334 |
PgArchShmemInit(); |
| 335 |
ApplyLauncherShmemInit(); |
| 336 |
SlotSyncShmemInit(); |
| 337 |
|
| 338 |
/* |
| 339 |
* Set up other modules that need some shared memory space |
| 340 |
*/ |
| 341 |
BTreeShmemInit(); |
| 342 |
SyncScanShmemInit(); |
| 343 |
AsyncShmemInit(); |
| 344 |
StatsShmemInit(); |
| 345 |
WaitEventCustomShmemInit(); |
| 346 |
InjectionPointShmemInit(); |
| 347 |
AioShmemInit(); |
| 348 |
} |
| 349 |
|
| 350 |
/* |
| 351 |
* InitializeShmemGUCs |
| 352 |
* |
| 353 |
* This function initializes runtime-computed GUCs related to the amount of |
| 354 |
* shared memory required for the current configuration. |
| 355 |
*/ |
| 356 |
void |
| 357 |
InitializeShmemGUCs(void) |
| 358 |
{ |
| 359 |
char buf[64]; |
| 360 |
Size size_b; |
| 361 |
Size size_mb; |
| 362 |
Size hp_size; |
| 363 |
int num_semas; |
| 364 |
|
| 365 |
/* |
| 366 |
* Calculate the shared memory size and round up to the nearest megabyte. |
| 367 |
*/ |
| 368 |
size_b = CalculateShmemSize(&num_semas); |
| 369 |
size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024); |
| 370 |
sprintf(buf, "%zu", size_mb); |
| 371 |
SetConfigOption("shared_memory_size", buf, |
| 372 |
PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT); |
| 373 |
|
| 374 |
/* |
| 375 |
* Calculate the number of huge pages required. |
| 376 |
*/ |
| 377 |
GetHugePageSize(&hp_size, NULL); |
| 378 |
if (hp_size != 0) |
| 379 |
{ |
| 380 |
Size hp_required; |
| 381 |
|
| 382 |
hp_required = add_size(size_b / hp_size, 1); |
| 383 |
sprintf(buf, "%zu", hp_required); |
| 384 |
SetConfigOption("shared_memory_size_in_huge_pages", buf, |
| 385 |
PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT); |
| 386 |
} |
| 387 |
|
| 388 |
sprintf(buf, "%d", num_semas); |
| 389 |
SetConfigOption("num_os_semaphores", buf, PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT); |
| 390 |
} |