PostgreSQL Source Code git master
Loading...
Searching...
No Matches
ipc.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define PG_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
 
#define PG_END_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
 

Typedefs

typedef void(* pg_on_exit_callback) (int code, Datum arg)
 
typedef void(* shmem_startup_hook_type) (void)
 

Functions

pg_noreturn void proc_exit (int code)
 
void shmem_exit (int code)
 
void on_proc_exit (pg_on_exit_callback function, Datum arg)
 
void on_shmem_exit (pg_on_exit_callback function, Datum arg)
 
void before_shmem_exit (pg_on_exit_callback function, Datum arg)
 
void cancel_before_shmem_exit (pg_on_exit_callback function, Datum arg)
 
void on_exit_reset (void)
 
void check_on_shmem_exit_lists_are_empty (void)
 
Size CalculateShmemSize (void)
 
void CreateSharedMemoryAndSemaphores (void)
 
void InitializeShmemGUCs (void)
 

Variables

PGDLLIMPORT bool proc_exit_inprogress
 
PGDLLIMPORT bool shmem_exit_inprogress
 
PGDLLIMPORT shmem_startup_hook_type shmem_startup_hook
 

Macro Definition Documentation

◆ PG_END_ENSURE_ERROR_CLEANUP

#define PG_END_ENSURE_ERROR_CLEANUP (   cleanup_function,
  arg 
)
Value:
{ \
} \
} while (0)
void cancel_before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition ipc.c:401
void * arg
static int fb(int x)

Definition at line 52 of file ipc.h.

55 { \
58 PG_RE_THROW(); \
59 } \
60 PG_END_TRY(); \
61 } while (0)

◆ PG_ENSURE_ERROR_CLEANUP

#define PG_ENSURE_ERROR_CLEANUP (   cleanup_function,
  arg 
)
Value:

Definition at line 47 of file ipc.h.

Typedef Documentation

◆ pg_on_exit_callback

typedef void(* pg_on_exit_callback) (int code, Datum arg)

Definition at line 21 of file ipc.h.

◆ shmem_startup_hook_type

typedef void(* shmem_startup_hook_type) (void)

Definition at line 22 of file ipc.h.

Function Documentation

◆ before_shmem_exit()

void before_shmem_exit ( pg_on_exit_callback  function,
Datum  arg 
)
extern

Definition at line 344 of file ipc.c.

345{
349 errmsg_internal("out of before_shmem_exit slots")));
350
353
355
357 {
360 }
361}
int errmsg_internal(const char *fmt,...)
Definition elog.c:1170
int errcode(int sqlerrcode)
Definition elog.c:863
#define FATAL
Definition elog.h:41
#define ereport(elevel,...)
Definition elog.h:150
static bool atexit_callback_setup
Definition ipc.c:52
static void atexit_callback(void)
Definition ipc.c:301
#define MAX_ON_EXITS
Definition ipc.c:72
static int before_shmem_exit_index
Definition ipc.c:86
static struct ONEXIT before_shmem_exit_list[MAX_ON_EXITS]
Definition ipc.c:82
on_exit_nicely_callback function
Datum arg
Definition ipc.c:77
pg_on_exit_callback function
Definition ipc.c:76

References ONEXIT::arg, arg, atexit_callback(), atexit_callback_setup, before_shmem_exit_index, before_shmem_exit_list, ereport, errcode(), errmsg_internal(), FATAL, fb(), ONEXIT::function, function, and MAX_ON_EXITS.

Referenced by ApplyLauncherMain(), AtEOXact_Namespace(), autoprewarm_main(), AuxiliaryProcessMainCommon(), BecomeRegisteredListener(), CheckpointerMain(), InitializeLogRepWorker(), InitPostgres(), InitTemporaryFileAccess(), injection_points_set_local(), LoadArchiveLibrary(), LockGXact(), logicalrep_worker_attach(), MarkAsPreparing(), ParallelApplyWorkerMain(), ParallelWorkerMain(), pgaio_init_backend(), pgstat_initialize(), register_persistent_abort_backup_handler(), ReplicationSlotInitialize(), and ReplSlotSyncWorkerMain().

◆ CalculateShmemSize()

Size CalculateShmemSize ( void  )
extern

Definition at line 87 of file ipci.c.

88{
89 Size size;
90
91 /*
92 * Size of the Postgres shared-memory block is estimated via moderately-
93 * accurate estimates for the big hogs, plus 100K for the stuff that's too
94 * small to bother with estimating.
95 *
96 * We take some care to ensure that the total size request doesn't
97 * overflow size_t. If this gets through, we don't need to be so careful
98 * during the actual allocation phase.
99 */
100 size = 100000;
102 sizeof(ShmemIndexEnt)));
103 size = add_size(size, dsm_estimate_size());
104 size = add_size(size, DSMRegistryShmemSize());
105 size = add_size(size, BufferManagerShmemSize());
106 size = add_size(size, LockManagerShmemSize());
107 size = add_size(size, PredicateLockShmemSize());
108 size = add_size(size, ProcGlobalShmemSize());
109 size = add_size(size, XLogPrefetchShmemSize());
110 size = add_size(size, VarsupShmemSize());
111 size = add_size(size, XLOGShmemSize());
112 size = add_size(size, XLogRecoveryShmemSize());
113 size = add_size(size, CLOGShmemSize());
114 size = add_size(size, CommitTsShmemSize());
115 size = add_size(size, SUBTRANSShmemSize());
116 size = add_size(size, TwoPhaseShmemSize());
117 size = add_size(size, BackgroundWorkerShmemSize());
118 size = add_size(size, MultiXactShmemSize());
119 size = add_size(size, LWLockShmemSize());
120 size = add_size(size, ProcArrayShmemSize());
121 size = add_size(size, BackendStatusShmemSize());
122 size = add_size(size, SharedInvalShmemSize());
123 size = add_size(size, PMSignalShmemSize());
124 size = add_size(size, ProcSignalShmemSize());
125 size = add_size(size, CheckpointerShmemSize());
126 size = add_size(size, AutoVacuumShmemSize());
127 size = add_size(size, ReplicationSlotsShmemSize());
128 size = add_size(size, ReplicationOriginShmemSize());
129 size = add_size(size, WalSndShmemSize());
130 size = add_size(size, WalRcvShmemSize());
131 size = add_size(size, WalSummarizerShmemSize());
132 size = add_size(size, PgArchShmemSize());
133 size = add_size(size, ApplyLauncherShmemSize());
134 size = add_size(size, BTreeShmemSize());
135 size = add_size(size, SyncScanShmemSize());
136 size = add_size(size, AsyncShmemSize());
137 size = add_size(size, StatsShmemSize());
138 size = add_size(size, WaitEventCustomShmemSize());
139 size = add_size(size, InjectionPointShmemSize());
140 size = add_size(size, SlotSyncShmemSize());
141 size = add_size(size, AioShmemSize());
142 size = add_size(size, WaitLSNShmemSize());
144
145 /* include additional requested shmem from preload libraries */
146 size = add_size(size, total_addin_request);
147
148 /* might as well round it off to a multiple of a typical page size */
149 size = add_size(size, 8192 - (size % 8192));
150
151 return size;
152}
Size AioShmemSize(void)
Definition aio_init.c:113
Size AsyncShmemSize(void)
Definition async.c:775
Size AutoVacuumShmemSize(void)
Size BackendStatusShmemSize(void)
Size BackgroundWorkerShmemSize(void)
Definition bgworker.c:150
Size BufferManagerShmemSize(void)
Definition buf_init.c:152
size_t Size
Definition c.h:629
Size CheckpointerShmemSize(void)
Size CLOGShmemSize(void)
Definition clog.c:780
Size CommitTsShmemSize(void)
Definition commit_ts.c:517
size_t dsm_estimate_size(void)
Definition dsm.c:470
Size DSMRegistryShmemSize(void)
Size hash_estimate_size(int64 num_entries, Size entrysize)
Definition dynahash.c:783
Size InjectionPointShmemSize(void)
static Size total_addin_request
Definition ipci.c:61
Size ApplyLauncherShmemSize(void)
Definition launcher.c:978
Size LockManagerShmemSize(void)
Definition lock.c:3756
Size LogicalDecodingCtlShmemSize(void)
Definition logicalctl.c:123
Size LWLockShmemSize(void)
Definition lwlock.c:397
Size MultiXactShmemSize(void)
Definition multixact.c:1707
Size BTreeShmemSize(void)
Definition nbtutils.c:563
Size ReplicationOriginShmemSize(void)
Definition origin.c:540
Size PgArchShmemSize(void)
Definition pgarch.c:156
Size StatsShmemSize(void)
Size PMSignalShmemSize(void)
Definition pmsignal.c:130
Size PredicateLockShmemSize(void)
Definition predicate.c:1357
Size ProcArrayShmemSize(void)
Definition procarray.c:378
Size ProcSignalShmemSize(void)
Definition procsignal.c:117
Size add_size(Size s1, Size s2)
Definition shmem.c:495
#define SHMEM_INDEX_SIZE
Definition shmem.h:53
Size SharedInvalShmemSize(void)
Definition sinvaladt.c:218
Size ReplicationSlotsShmemSize(void)
Definition slot.c:188
Size SlotSyncShmemSize(void)
Definition slotsync.c:1891
Size ProcGlobalShmemSize(void)
Definition proc.c:140
Size SUBTRANSShmemSize(void)
Definition subtrans.c:213
Size SyncScanShmemSize(void)
Definition syncscan.c:126
Size TwoPhaseShmemSize(void)
Definition twophase.c:239
Size VarsupShmemSize(void)
Definition varsup.c:41
Size WaitEventCustomShmemSize(void)
Definition wait_event.c:103
Size WalRcvShmemSize(void)
Size WalSndShmemSize(void)
Definition walsender.c:3743
Size WalSummarizerShmemSize(void)
Size XLOGShmemSize(void)
Definition xlog.c:4962
size_t XLogPrefetchShmemSize(void)
Size XLogRecoveryShmemSize(void)
Size WaitLSNShmemSize(void)
Definition xlogwait.c:113

References add_size(), AioShmemSize(), ApplyLauncherShmemSize(), AsyncShmemSize(), AutoVacuumShmemSize(), BackendStatusShmemSize(), BackgroundWorkerShmemSize(), BTreeShmemSize(), BufferManagerShmemSize(), CheckpointerShmemSize(), CLOGShmemSize(), CommitTsShmemSize(), dsm_estimate_size(), DSMRegistryShmemSize(), hash_estimate_size(), InjectionPointShmemSize(), LockManagerShmemSize(), LogicalDecodingCtlShmemSize(), LWLockShmemSize(), MultiXactShmemSize(), PgArchShmemSize(), PMSignalShmemSize(), PredicateLockShmemSize(), ProcArrayShmemSize(), ProcGlobalShmemSize(), ProcSignalShmemSize(), ReplicationOriginShmemSize(), ReplicationSlotsShmemSize(), SharedInvalShmemSize(), SHMEM_INDEX_SIZE, SlotSyncShmemSize(), StatsShmemSize(), SUBTRANSShmemSize(), SyncScanShmemSize(), total_addin_request, TwoPhaseShmemSize(), VarsupShmemSize(), WaitEventCustomShmemSize(), WaitLSNShmemSize(), WalRcvShmemSize(), WalSndShmemSize(), WalSummarizerShmemSize(), XLogPrefetchShmemSize(), XLogRecoveryShmemSize(), and XLOGShmemSize().

Referenced by CreateSharedMemoryAndSemaphores(), and InitializeShmemGUCs().

◆ cancel_before_shmem_exit()

void cancel_before_shmem_exit ( pg_on_exit_callback  function,
Datum  arg 
)
extern

Definition at line 401 of file ipc.c.

402{
403 if (before_shmem_exit_index > 0 &&
405 == function &&
408 else
409 elog(ERROR, "before_shmem_exit callback (%p,0x%" PRIx64 ") is not the latest entry",
410 function, arg);
411}
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226

References arg, before_shmem_exit_index, before_shmem_exit_list, elog, ERROR, fb(), and function.

◆ check_on_shmem_exit_lists_are_empty()

void check_on_shmem_exit_lists_are_empty ( void  )
extern

Definition at line 439 of file ipc.c.

440{
442 elog(FATAL, "before_shmem_exit has been called prematurely");
444 elog(FATAL, "on_shmem_exit has been called prematurely");
445 /* Checking DSM detach state seems unnecessary given the above */
446}
static int on_shmem_exit_index
Definition ipc.c:85

References before_shmem_exit_index, elog, FATAL, and on_shmem_exit_index.

Referenced by BackendInitialize().

◆ CreateSharedMemoryAndSemaphores()

void CreateSharedMemoryAndSemaphores ( void  )
extern

Definition at line 191 of file ipci.c.

192{
195 Size size;
196
198
199 /* Compute the size of the shared-memory block */
200 size = CalculateShmemSize();
201 elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
202
203 /*
204 * Create the shmem segment
205 */
207
208 /*
209 * Make sure that huge pages are never reported as "unknown" while the
210 * server is running.
211 */
212 Assert(strcmp("unknown",
213 GetConfigOption("huge_pages_status", false, false)) != 0);
214
216
217 /*
218 * Set up shared memory allocation mechanism
219 */
221
222 /* Initialize subsystems */
224
225 /* Initialize dynamic shared memory facilities. */
227
228 /*
229 * Now give loadable modules a chance to set up their shmem allocations
230 */
233}
#define Assert(condition)
Definition c.h:883
void dsm_postmaster_startup(PGShmemHeader *shim)
Definition dsm.c:177
#define DEBUG3
Definition elog.h:28
bool IsUnderPostmaster
Definition globals.c:120
const char * GetConfigOption(const char *name, bool missing_ok, bool restrict_privileged)
Definition guc.c:4219
shmem_startup_hook_type shmem_startup_hook
Definition ipci.c:59
Size CalculateShmemSize(void)
Definition ipci.c:87
static void CreateOrAttachShmemStructs(void)
Definition ipci.c:251
void InitShmemAccess(PGShmemHeader *seghdr)
Definition shmem.c:104
void InitShmemAllocation(void)
Definition shmem.c:117
PGShmemHeader * PGSharedMemoryCreate(Size size, PGShmemHeader **shim)
Definition sysv_shmem.c:701

References Assert, CalculateShmemSize(), CreateOrAttachShmemStructs(), DEBUG3, dsm_postmaster_startup(), elog, fb(), GetConfigOption(), InitShmemAccess(), InitShmemAllocation(), IsUnderPostmaster, PGSharedMemoryCreate(), and shmem_startup_hook.

Referenced by BootstrapModeMain(), PostgresSingleUserMain(), PostmasterMain(), and PostmasterStateMachine().

◆ InitializeShmemGUCs()

void InitializeShmemGUCs ( void  )
extern

Definition at line 342 of file ipci.c.

343{
344 char buf[64];
345 Size size_b;
348
349 /*
350 * Calculate the shared memory size and round up to the nearest megabyte.
351 */
353 size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
354 sprintf(buf, "%zu", size_mb);
355 SetConfigOption("shared_memory_size", buf,
357
358 /*
359 * Calculate the number of huge pages required.
360 */
362 if (hp_size != 0)
363 {
365
367 if (size_b % hp_size != 0)
369 sprintf(buf, "%zu", hp_required);
370 SetConfigOption("shared_memory_size_in_huge_pages", buf,
372 }
373
374 sprintf(buf, "%d", ProcGlobalSemas());
376}
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition guc.c:4196
@ PGC_S_DYNAMIC_DEFAULT
Definition guc.h:114
@ PGC_INTERNAL
Definition guc.h:73
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define sprintf
Definition port.h:262
int ProcGlobalSemas(void)
Definition proc.c:159
void GetHugePageSize(Size *hugepagesize, int *mmap_flags)
Definition sysv_shmem.c:480

References add_size(), buf, CalculateShmemSize(), fb(), GetHugePageSize(), PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT, ProcGlobalSemas(), SetConfigOption(), and sprintf.

Referenced by PostgresSingleUserMain(), and PostmasterMain().

◆ on_exit_reset()

void on_exit_reset ( void  )
extern

Definition at line 423 of file ipc.c.

424{
429}
void reset_on_dsm_detach(void)
Definition dsm.c:1170
static int on_proc_exit_index
Definition ipc.c:84

References before_shmem_exit_index, on_proc_exit_index, on_shmem_exit_index, and reset_on_dsm_detach().

Referenced by InitPostmasterChild().

◆ on_proc_exit()

◆ on_shmem_exit()

◆ proc_exit()

pg_noreturn void proc_exit ( int  code)
extern

Definition at line 105 of file ipc.c.

106{
107 /* not safe if forked by system(), etc. */
108 if (MyProcPid != (int) getpid())
109 elog(PANIC, "proc_exit() called in child process");
110
111 /* Clean up everything that must be cleaned up */
112 proc_exit_prepare(code);
113
114#ifdef PROFILE_PID_DIR
115 {
116 /*
117 * If we are profiling ourself then gprof's mcleanup() is about to
118 * write out a profile to ./gmon.out. Since mcleanup() always uses a
119 * fixed file name, each backend will overwrite earlier profiles. To
120 * fix that, we create a separate subdirectory for each backend
121 * (./gprof/pid) and 'cd' to that subdirectory before we exit() - that
122 * forces mcleanup() to write each profile into its own directory. We
123 * end up with something like: $PGDATA/gprof/8829/gmon.out
124 * $PGDATA/gprof/8845/gmon.out ...
125 *
126 * To avoid undesirable disk space bloat, autovacuum workers are
127 * discriminated against: all their gmon.out files go into the same
128 * subdirectory. Without this, an installation that is "just sitting
129 * there" nonetheless eats megabytes of disk space every few seconds.
130 *
131 * Note that we do this here instead of in an on_proc_exit() callback
132 * because we want to ensure that this code executes last - we don't
133 * want to interfere with any other on_proc_exit() callback. For the
134 * same reason, we do not include it in proc_exit_prepare ... so if
135 * you are exiting in the "wrong way" you won't drop your profile in a
136 * nice place.
137 */
138 char gprofDirName[32];
139
141 snprintf(gprofDirName, 32, "gprof/avworker");
142 else
143 snprintf(gprofDirName, 32, "gprof/%d", (int) getpid());
144
145 /*
146 * Use mkdir() instead of MakePGDirectory() since we aren't making a
147 * PG directory here.
148 */
149 mkdir("gprof", S_IRWXU | S_IRWXG | S_IRWXO);
152 }
153#endif
154
155 elog(DEBUG3, "exit(%d)", code);
156
157 exit(code);
158}
#define PANIC
Definition elog.h:42
int MyProcPid
Definition globals.c:47
static void proc_exit_prepare(int code)
Definition ipc.c:166
#define AmAutoVacuumWorkerProcess()
Definition miscadmin.h:383
#define snprintf
Definition port.h:260
#define S_IRWXG
Definition win32_port.h:300
#define S_IRWXO
Definition win32_port.h:312
#define mkdir(a, b)
Definition win32_port.h:80
#define S_IRWXU
Definition win32_port.h:288

References AmAutoVacuumWorkerProcess, DEBUG3, elog, fb(), mkdir, MyProcPid, PANIC, proc_exit_prepare(), S_IRWXG, S_IRWXO, S_IRWXU, and snprintf.

Referenced by apply_worker_exit(), ApplyWorkerMain(), auth_failed(), AutoVacLauncherMain(), AutoVacLauncherShutdown(), AutoVacWorkerMain(), BackendInitialize(), BackgroundWorkerMain(), BootstrapModeMain(), CheckerModeMain(), CheckpointerMain(), DisableSubscriptionAndExit(), errfinish(), ExitPostmaster(), FinishSyncWorker(), InitializeLogRepWorker(), IoWorkerMain(), maybe_reread_subscription(), PerformWalRecovery(), PgArchiverMain(), PostgresMain(), PostgresSingleUserMain(), PreRestoreCommand(), ProcessInterrupts(), ProcessMainLoopInterrupts(), ProcessParallelApplyInterrupts(), ProcessPgArchInterrupts(), ProcessRepliesIfAny(), ProcessSlotSyncInterrupts(), ProcessStandbyMessage(), ProcessStartupProcInterrupts(), ProcessSyncingTablesForApply(), ProcessWalSummarizerInterrupts(), ReplSlotSyncWorkerMain(), RestoreArchivedFile(), slotsync_reread_config(), StartLogicalReplication(), StartReplication(), StartupProcessMain(), StartupProcShutdownHandler(), SysLoggerMain(), test_shm_mq_main(), WaitEventSetWaitBlock(), WalRcvWaitForStartPosition(), WalReceiverMain(), WalSndDone(), WalSndErrorCleanup(), WalSndShutdown(), WalSndWait(), and WalSummarizerMain().

◆ shmem_exit()

void shmem_exit ( int  code)
extern

Definition at line 229 of file ipc.c.

230{
232
233 /*
234 * Release any LWLocks we might be holding before callbacks run. This
235 * prevents accessing locks in detached DSM segments and allows callbacks
236 * to acquire new locks.
237 */
239
240 /*
241 * Call before_shmem_exit callbacks.
242 *
243 * These should be things that need most of the system to still be up and
244 * working, such as cleanup of temp relations, which requires catalog
245 * access.
246 */
247 elog(DEBUG3, "shmem_exit(%d): %d before_shmem_exit callbacks to make",
249 while (--before_shmem_exit_index >= 0)
253
254 /*
255 * Call dynamic shared memory callbacks.
256 *
257 * These serve the same purpose as late callbacks, but for dynamic shared
258 * memory segments rather than the main shared memory segment.
259 * dsm_backend_shutdown() has the same kind of progressive logic we use
260 * for the main shared memory segment; namely, it unregisters each
261 * callback before invoking it, so that we don't get stuck in an infinite
262 * loop if one of those callbacks itself throws an ERROR or FATAL.
263 *
264 * Note that explicitly calling this function here is quite different from
265 * registering it as an on_shmem_exit callback for precisely this reason:
266 * if one dynamic shared memory callback errors out, the remaining
267 * callbacks will still be invoked. Thus, hard-coding this call puts it
268 * equal footing with callbacks for the main shared memory segment.
269 */
271
272 /*
273 * Call on_shmem_exit callbacks.
274 *
275 * These are generally releasing low-level shared memory resources. In
276 * some cases, this is a backstop against the possibility that the early
277 * callbacks might themselves fail, leading to re-entry to this routine;
278 * in other cases, it's cleanup that only happens at process exit.
279 */
280 elog(DEBUG3, "shmem_exit(%d): %d on_shmem_exit callbacks to make",
281 code, on_shmem_exit_index);
282 while (--on_shmem_exit_index >= 0)
286
287 shmem_exit_inprogress = false;
288}
void dsm_backend_shutdown(void)
Definition dsm.c:757
bool shmem_exit_inprogress
Definition ipc.c:46
void LWLockReleaseAll(void)
Definition lwlock.c:1892

References arg, before_shmem_exit_index, before_shmem_exit_list, DEBUG3, dsm_backend_shutdown(), elog, ONEXIT::function, LWLockReleaseAll(), on_shmem_exit_index, on_shmem_exit_list, and shmem_exit_inprogress.

Referenced by PostmasterStateMachine(), and proc_exit_prepare().

Variable Documentation

◆ proc_exit_inprogress

◆ shmem_exit_inprogress

PGDLLIMPORT bool shmem_exit_inprogress
extern

Definition at line 46 of file ipc.c.

Referenced by AtAbort_Portals(), and shmem_exit().

◆ shmem_startup_hook

PGDLLIMPORT shmem_startup_hook_type shmem_startup_hook
extern

Definition at line 59 of file ipci.c.

Referenced by _PG_init(), and CreateSharedMemoryAndSemaphores().