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)
 
void RegisterBuiltinShmemCallbacks (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)
Datum arg
Definition elog.c:1322
void cancel_before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition ipc.c:401
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 errcode(int sqlerrcode)
Definition elog.c:874
#define FATAL
Definition elog.h:42
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define ereport(elevel,...)
Definition elog.h:152
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(), pg_stash_advice_worker_main(), pgaio_init_backend(), pgstat_initialize(), register_persistent_abort_backup_handler(), RepackWorkerMain(), ReplicationSlotInitialize(), and ReplSlotSyncWorkerMain().

◆ CalculateShmemSize()

Size CalculateShmemSize ( void  )
extern

Definition at line 57 of file ipci.c.

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}
size_t Size
Definition c.h:689
static Size total_addin_request
Definition ipci.c:33
Size add_size(Size s1, Size s2)
Definition shmem.c:1048
size_t ShmemGetRequestedSize(void)
Definition shmem.c:392

References add_size(), ShmemGetRequestedSize(), and total_addin_request.

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:40
#define elog(elevel,...)
Definition elog.h:228

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 120 of file ipci.c.

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}
#define Assert(condition)
Definition c.h:943
void dsm_postmaster_startup(PGShmemHeader *shim)
Definition dsm.c:187
#define DEBUG3
Definition elog.h:29
bool IsUnderPostmaster
Definition globals.c:122
const char * GetConfigOption(const char *name, bool missing_ok, bool restrict_privileged)
Definition guc.c:4257
shmem_startup_hook_type shmem_startup_hook
Definition ipci.c:31
Size CalculateShmemSize(void)
Definition ipci.c:57
void InitShmemAllocator(PGShmemHeader *seghdr)
Definition shmem.c:638
void ShmemInitRequested(void)
Definition shmem.c:425
PGShmemHeader * PGSharedMemoryCreate(Size size, PGShmemHeader **shim)
Definition sysv_shmem.c:702

References Assert, CalculateShmemSize(), DEBUG3, dsm_postmaster_startup(), elog, fb(), GetConfigOption(), InitShmemAllocator(), IsUnderPostmaster, PGSharedMemoryCreate(), shmem_startup_hook, and ShmemInitRequested().

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

◆ InitializeShmemGUCs()

void InitializeShmemGUCs ( void  )
extern

Definition at line 189 of file ipci.c.

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}
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition guc.c:4234
@ 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:130
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:1178
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:44
int MyProcPid
Definition globals.c:49
static void proc_exit_prepare(int code)
Definition ipc.c:166
#define AmAutoVacuumWorkerProcess()
Definition miscadmin.h:398
#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(), check_and_set_sync_info(), CheckerModeMain(), CheckpointerMain(), DisableSubscriptionAndExit(), errfinish(), ExitPostmaster(), FinishSyncWorker(), InitializeLogRepWorker(), IoWorkerMain(), maybe_reread_subscription(), PerformWalRecovery(), PgArchiverMain(), PostgresMain(), PostgresSingleUserMain(), PreRestoreCommand(), ProcessInterrupts(), ProcessMainLoopInterrupts(), ProcessParallelApplyInterrupts(), ProcessPgArchInterrupts(), ProcessRepliesIfAny(), ProcessSlotSyncMessage(), ProcessStandbyMessage(), ProcessStartupProcInterrupts(), ProcessSyncingTablesForApply(), ProcessWalSummarizerInterrupts(), ReplSlotSyncWorkerMain(), RestoreArchivedFile(), slotsync_reread_config(), StartLogicalReplication(), StartReplication(), StartupProcessMain(), StartupProcShutdownHandler(), SysLoggerMain(), test_shm_mq_main(), WaitEventSetWaitBlock(), WalRcvWaitForStartPosition(), WalReceiverMain(), WalSndDone(), WalSndDoneImmediate(), WalSndErrorCleanup(), WalSndShutdown(), WalSndWait(), and WalSummarizerMain().

◆ RegisterBuiltinShmemCallbacks()

void RegisterBuiltinShmemCallbacks ( void  )
extern

Definition at line 168 of file ipci.c.

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}

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

◆ 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:765
bool shmem_exit_inprogress
Definition ipc.c:46
void LWLockReleaseAll(void)
Definition lwlock.c:1866

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 31 of file ipci.c.

Referenced by CreateSharedMemoryAndSemaphores().