PostgreSQL Source Code git master
Loading...
Searching...
No Matches
ipci.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * ipci.c
4 * POSTGRES inter-process communication initialization code.
5 *
6 * Portions Copyright (c) 1996-2026, 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 "miscadmin.h"
18#include "pgstat.h"
19#include "storage/dsm.h"
20#include "storage/ipc.h"
21#include "storage/lock.h"
22#include "storage/pg_shmem.h"
23#include "storage/proc.h"
25#include "storage/subsystems.h"
26#include "utils/guc.h"
27
28/* GUCs */
30
32
34
35/*
36 * RequestAddinShmemSpace
37 * Request that extra shmem space be allocated for use by
38 * a loadable module.
39 *
40 * This may only be called via the shmem_request_hook of a library that is
41 * loaded into the postmaster via shared_preload_libraries. Calls from
42 * elsewhere will fail.
43 */
44void
46{
48 elog(FATAL, "cannot request additional shared memory outside shmem_request_hook");
50}
51
52/*
53 * CalculateShmemSize
54 * Calculates the amount of shared memory needed.
55 */
56Size
58{
59 Size size;
60
61 /*
62 * Size of the Postgres shared-memory block is estimated via moderately-
63 * accurate estimates for the big hogs, plus 100K for the stuff that's too
64 * small to bother with estimating.
65 *
66 * We take some care to ensure that the total size request doesn't
67 * overflow size_t. If this gets through, we don't need to be so careful
68 * during the actual allocation phase.
69 */
70 size = 100000;
71 size = add_size(size, ShmemGetRequestedSize());
72
73 /* include additional requested shmem from preload libraries */
74 size = add_size(size, total_addin_request);
75
76 /* might as well round it off to a multiple of a typical page size */
77 size = add_size(size, 8192 - (size % 8192));
78
79 return size;
80}
81
82#ifdef EXEC_BACKEND
83/*
84 * AttachSharedMemoryStructs
85 * Initialize a postmaster child process's access to shared memory
86 * structures.
87 *
88 * In !EXEC_BACKEND mode, we inherit everything through the fork, and this
89 * isn't needed.
90 */
91void
93{
94 /* InitProcess must've been called already */
95 Assert(MyProc != NULL);
97
98 /*
99 * In EXEC_BACKEND mode, backends don't inherit the number of fast-path
100 * groups we calculated before setting the shmem up, so recalculate it.
101 */
103
104 /* Establish pointers to all shared memory areas in this backend */
106
107 /*
108 * Now give loadable modules a chance to set up their shmem allocations
109 */
112}
113#endif
114
115/*
116 * CreateSharedMemoryAndSemaphores
117 * Creates and initializes shared memory and semaphores.
118 */
119void
121{
124 Size size;
125
127
128 /* Compute the size of the shared-memory block */
129 size = CalculateShmemSize();
130 elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
131
132 /*
133 * Create the shmem segment
134 */
136
137 /*
138 * Make sure that huge pages are never reported as "unknown" while the
139 * server is running.
140 */
141 Assert(strcmp("unknown",
142 GetConfigOption("huge_pages_status", false, false)) != 0);
143
144 /*
145 * Set up shared memory allocation mechanism
146 */
148
149 /* Initialize all shmem areas */
151
152 /* Initialize dynamic shared memory facilities. */
154
155 /*
156 * Now give loadable modules a chance to set up their shmem allocations
157 */
160}
161
162/*
163 * Early initialization of various subsystems, giving them a chance to
164 * register their shared memory needs before the shared memory segment is
165 * allocated.
166 */
167void
169{
170 /*
171 * Call RegisterShmemCallbacks(...) on each subsystem listed in
172 * subsystemslist.h
173 */
174#define PG_SHMEM_SUBSYSTEM(subsystem_callbacks) \
175 RegisterShmemCallbacks(&(subsystem_callbacks));
176
178
179#undef PG_SHMEM_SUBSYSTEM
180}
181
182/*
183 * InitializeShmemGUCs
184 *
185 * This function initializes runtime-computed GUCs related to the amount of
186 * shared memory required for the current configuration.
187 */
188void
190{
191 char buf[64];
192 Size size_b;
195
196 /*
197 * Calculate the shared memory size and round up to the nearest megabyte.
198 */
200 size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
201 sprintf(buf, "%zu", size_mb);
202 SetConfigOption("shared_memory_size", buf,
204
205 /*
206 * Calculate the number of huge pages required.
207 */
209 if (hp_size != 0)
210 {
212
214 if (size_b % hp_size != 0)
216 sprintf(buf, "%zu", hp_required);
217 SetConfigOption("shared_memory_size_in_huge_pages", buf,
219 }
220
221 sprintf(buf, "%d", ProcGlobalSemas());
223}
#define Assert(condition)
Definition c.h:943
size_t Size
Definition c.h:689
void dsm_postmaster_startup(PGShmemHeader *shim)
Definition dsm.c:187
#define DEBUG3
Definition elog.h:29
#define FATAL
Definition elog.h:42
#define elog(elevel,...)
Definition elog.h:228
bool IsUnderPostmaster
Definition globals.c:122
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition guc.c:4234
const char * GetConfigOption(const char *name, bool missing_ok, bool restrict_privileged)
Definition guc.c:4257
@ PGC_S_DYNAMIC_DEFAULT
Definition guc.h:114
@ PGC_INTERNAL
Definition guc.h:73
void(* shmem_startup_hook_type)(void)
Definition ipc.h:22
static Size total_addin_request
Definition ipci.c:33
int shared_memory_type
Definition ipci.c:29
shmem_startup_hook_type shmem_startup_hook
Definition ipci.c:31
void RegisterBuiltinShmemCallbacks(void)
Definition ipci.c:168
void RequestAddinShmemSpace(Size size)
Definition ipci.c:45
Size CalculateShmemSize(void)
Definition ipci.c:57
void InitializeShmemGUCs(void)
Definition ipci.c:189
void CreateSharedMemoryAndSemaphores(void)
Definition ipci.c:120
bool process_shmem_requests_in_progress
Definition miscinit.c:1792
#define DEFAULT_SHARED_MEMORY_TYPE
Definition pg_shmem.h:76
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define sprintf
Definition port.h:262
void InitializeFastPathLocks(void)
Definition postinit.c:584
static int fb(int x)
void InitShmemAllocator(PGShmemHeader *seghdr)
Definition shmem.c:638
Size add_size(Size s1, Size s2)
Definition shmem.c:1048
void ShmemInitRequested(void)
Definition shmem.c:425
size_t ShmemGetRequestedSize(void)
Definition shmem.c:392
PGPROC * MyProc
Definition proc.c:71
int ProcGlobalSemas(void)
Definition proc.c:130
PGShmemHeader * PGSharedMemoryCreate(Size size, PGShmemHeader **shim)
Definition sysv_shmem.c:702
void GetHugePageSize(Size *hugepagesize, int *mmap_flags)
Definition sysv_shmem.c:480