PostgreSQL Source Code  git master
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

void proc_exit (int code) pg_attribute_noreturn()
 
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 (int *num_semaphores)
 
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:
cancel_before_shmem_exit(cleanup_function, arg); \
PG_CATCH(); \
{ \
cancel_before_shmem_exit(cleanup_function, arg); \
cleanup_function (0, arg); \
PG_RE_THROW(); \
} \
PG_END_TRY(); \
} while (0)
void cancel_before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:390
void * arg

Definition at line 52 of file ipc.h.

◆ PG_ENSURE_ERROR_CLEANUP

#define PG_ENSURE_ERROR_CLEANUP (   cleanup_function,
  arg 
)
Value:
do { \
before_shmem_exit(cleanup_function, arg); \
PG_TRY()

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 
)

Definition at line 333 of file ipc.c.

334 {
336  ereport(FATAL,
337  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
338  errmsg_internal("out of before_shmem_exit slots")));
339 
342 
344 
346  {
347  atexit(atexit_callback);
348  atexit_callback_setup = true;
349  }
350 }
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1156
int errcode(int sqlerrcode)
Definition: elog.c:858
#define FATAL
Definition: elog.h:41
#define ereport(elevel,...)
Definition: elog.h:149
static bool atexit_callback_setup
Definition: ipc.c:51
static void atexit_callback(void)
Definition: ipc.c:290
#define MAX_ON_EXITS
Definition: ipc.c:71
static int before_shmem_exit_index
Definition: ipc.c:85
static struct ONEXIT before_shmem_exit_list[MAX_ON_EXITS]
Definition: ipc.c:81
Datum arg
Definition: ipc.c:76
pg_on_exit_callback function
Definition: ipc.c:75

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

Referenced by ApplyLauncherMain(), AtEOXact_Namespace(), AuxiliaryProcessMain(), CheckpointerMain(), Exec_ListenPreCommit(), InitPostgres(), InitTemporaryFileAccess(), LoadArchiveLibrary(), LockGXact(), logicalrep_worker_attach(), MarkAsPreparing(), ParallelApplyWorkerMain(), ParallelWorkerMain(), pgstat_initialize(), register_persistent_abort_backup_handler(), and ReplicationSlotInitialize().

◆ CalculateShmemSize()

Size CalculateShmemSize ( int *  num_semaphores)

Definition at line 86 of file ipci.c.

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

References add_size(), 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(), SpinlockSemas(), SpinlockSemaSize(), StatsShmemSize(), SUBTRANSShmemSize(), SyncScanShmemSize(), total_addin_request, TwoPhaseShmemSize(), WaitEventExtensionShmemSize(), WalRcvShmemSize(), WalSndShmemSize(), 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 
)

Definition at line 390 of file ipc.c.

391 {
392  if (before_shmem_exit_index > 0 &&
394  == function &&
397  else
398  elog(ERROR, "before_shmem_exit callback (%p,0x%llx) is not the latest entry",
399  function, (long long) arg);
400 }
#define ERROR
Definition: elog.h:39

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

◆ check_on_shmem_exit_lists_are_empty()

void check_on_shmem_exit_lists_are_empty ( void  )

Definition at line 428 of file ipc.c.

429 {
431  elog(FATAL, "before_shmem_exit has been called prematurely");
433  elog(FATAL, "on_shmem_exit has been called prematurely");
434  /* Checking DSM detach state seems unnecessary given the above */
435 }
static int on_shmem_exit_index
Definition: ipc.c:84

References before_shmem_exit_index, elog(), FATAL, and on_shmem_exit_index.

Referenced by BackendInitialize().

◆ CreateSharedMemoryAndSemaphores()

void CreateSharedMemoryAndSemaphores ( void  )

Definition at line 175 of file ipci.c.

176 {
177  PGShmemHeader *shim = NULL;
178 
179  if (!IsUnderPostmaster)
180  {
181  PGShmemHeader *seghdr;
182  Size size;
183  int numSemas;
184 
185  /* Compute the size of the shared-memory block */
186  size = CalculateShmemSize(&numSemas);
187  elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
188 
189  /*
190  * Create the shmem segment
191  */
192  seghdr = PGSharedMemoryCreate(size, &shim);
193 
194  /*
195  * Make sure that huge pages are never reported as "unknown" while the
196  * server is running.
197  */
198  Assert(strcmp("unknown",
199  GetConfigOption("huge_pages_status", false, false)) != 0);
200 
201  InitShmemAccess(seghdr);
202 
203  /*
204  * Create semaphores
205  */
206  PGReserveSemaphores(numSemas);
207 
208  /*
209  * If spinlocks are disabled, initialize emulation layer (which
210  * depends on semaphores, so the order is important here).
211  */
212 #ifndef HAVE_SPINLOCKS
214 #endif
215  }
216  else
217  {
218  /*
219  * We are reattaching to an existing shared memory segment. This
220  * should only be reached in the EXEC_BACKEND case.
221  */
222 #ifndef EXEC_BACKEND
223  elog(PANIC, "should be attached to shared memory already");
224 #endif
225  }
226 
227  /*
228  * Set up shared memory allocation mechanism
229  */
230  if (!IsUnderPostmaster)
232 
233  /*
234  * Now initialize LWLocks, which do shared memory allocation and are
235  * needed for InitShmemIndex.
236  */
237  CreateLWLocks();
238 
239  /*
240  * Set up shmem.c index hashtable
241  */
242  InitShmemIndex();
243 
244  dsm_shmem_init();
245 
246  /*
247  * Set up xlog, clog, and buffers
248  */
249  XLOGShmemInit();
252  CLOGShmemInit();
256  InitBufferPool();
257 
258  /*
259  * Set up lock manager
260  */
261  InitLocks();
262 
263  /*
264  * Set up predicate lock manager
265  */
267 
268  /*
269  * Set up process table
270  */
271  if (!IsUnderPostmaster)
272  InitProcGlobal();
277 
278  /*
279  * Set up shared-inval messaging
280  */
282 
283  /*
284  * Set up interprocess signaling mechanisms
285  */
292  WalSndShmemInit();
293  WalRcvShmemInit();
294  PgArchShmemInit();
296 
297  /*
298  * Set up other modules that need some shared memory space
299  */
300  BTreeShmemInit();
302  AsyncShmemInit();
303  StatsShmemInit();
305 
306 #ifdef EXEC_BACKEND
307 
308  /*
309  * Alloc the win32 shared backend array
310  */
311  if (!IsUnderPostmaster)
312  ShmemBackendArrayAllocation();
313 #endif
314 
315  /* Initialize dynamic shared memory facilities. */
316  if (!IsUnderPostmaster)
318 
319  /*
320  * Now give loadable modules a chance to set up their shmem allocations
321  */
322  if (shmem_startup_hook)
324 }
void AsyncShmemInit(void)
Definition: async.c:533
void AutoVacuumShmemInit(void)
Definition: autovacuum.c:3427
void CreateSharedBackendStatus(void)
void BackgroundWorkerShmemInit(void)
Definition: bgworker.c:164
void InitBufferPool(void)
Definition: buf_init.c:68
void CheckpointerShmemInit(void)
Definition: checkpointer.c:887
void CLOGShmemInit(void)
Definition: clog.c:695
void CommitTsShmemInit(void)
Definition: commit_ts.c:518
void dsm_postmaster_startup(PGShmemHeader *shim)
Definition: dsm.c:150
void dsm_shmem_init(void)
Definition: dsm.c:452
#define DEBUG3
Definition: elog.h:28
#define PANIC
Definition: elog.h:42
bool IsUnderPostmaster
Definition: globals.c:113
const char * GetConfigOption(const char *name, bool missing_ok, bool restrict_privileged)
Definition: guc.c:4200
shmem_startup_hook_type shmem_startup_hook
Definition: ipci.c:57
Size CalculateShmemSize(int *num_semaphores)
Definition: ipci.c:86
void ApplyLauncherShmemInit(void)
Definition: launcher.c:953
Assert(fmt[strlen(fmt) - 1] !='\n')
void InitLocks(void)
Definition: lock.c:393
void CreateLWLocks(void)
Definition: lwlock.c:474
void MultiXactShmemInit(void)
Definition: multixact.c:1844
void BTreeShmemInit(void)
Definition: nbtutils.c:2083
void ReplicationOriginShmemInit(void)
Definition: origin.c:526
void PgArchShmemInit(void)
Definition: pgarch.c:163
void StatsShmemInit(void)
Definition: pgstat_shmem.c:140
void PMSignalShmemInit(void)
Definition: pmsignal.c:144
void PGReserveSemaphores(int maxSemas)
Definition: posix_sema.c:196
void InitPredicateLocks(void)
Definition: predicate.c:1089
void CreateSharedProcArray(void)
Definition: procarray.c:419
void ProcSignalShmemInit(void)
Definition: procsignal.c:126
void InitShmemIndex(void)
Definition: shmem.c:292
void InitShmemAllocation(void)
Definition: shmem.c:116
void InitShmemAccess(void *seghdr)
Definition: shmem.c:101
void CreateSharedInvalidationState(void)
Definition: sinvaladt.c:227
void ReplicationSlotsShmemInit(void)
Definition: slot.c:136
void SpinlockSemaInit(void)
Definition: spin.c:77
void InitProcGlobal(void)
Definition: proc.c:157
void SUBTRANSShmemInit(void)
Definition: subtrans.c:191
void SyncScanShmemInit(void)
Definition: syncscan.c:135
PGShmemHeader * PGSharedMemoryCreate(Size size, PGShmemHeader **shim)
Definition: sysv_shmem.c:699
void TwoPhaseShmemInit(void)
Definition: twophase.c:257
void WaitEventExtensionShmemInit(void)
Definition: wait_event.c:122
void WalRcvShmemInit(void)
void WalSndShmemInit(void)
Definition: walsender.c:3289
void XLOGShmemInit(void)
Definition: xlog.c:4560
void XLogPrefetchShmemInit(void)
void XLogRecoveryShmemInit(void)
Definition: xlogrecovery.c:458

References ApplyLauncherShmemInit(), Assert(), AsyncShmemInit(), AutoVacuumShmemInit(), BackgroundWorkerShmemInit(), BTreeShmemInit(), CalculateShmemSize(), CheckpointerShmemInit(), CLOGShmemInit(), CommitTsShmemInit(), CreateLWLocks(), CreateSharedBackendStatus(), CreateSharedInvalidationState(), CreateSharedProcArray(), DEBUG3, dsm_postmaster_startup(), dsm_shmem_init(), elog(), GetConfigOption(), InitBufferPool(), InitLocks(), InitPredicateLocks(), InitProcGlobal(), InitShmemAccess(), InitShmemAllocation(), InitShmemIndex(), IsUnderPostmaster, MultiXactShmemInit(), PANIC, PgArchShmemInit(), PGReserveSemaphores(), PGSharedMemoryCreate(), PMSignalShmemInit(), ProcSignalShmemInit(), ReplicationOriginShmemInit(), ReplicationSlotsShmemInit(), shmem_startup_hook, SpinlockSemaInit(), StatsShmemInit(), SUBTRANSShmemInit(), SyncScanShmemInit(), TwoPhaseShmemInit(), WaitEventExtensionShmemInit(), WalRcvShmemInit(), WalSndShmemInit(), XLogPrefetchShmemInit(), XLogRecoveryShmemInit(), and XLOGShmemInit().

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

◆ InitializeShmemGUCs()

void InitializeShmemGUCs ( void  )

Definition at line 333 of file ipci.c.

334 {
335  char buf[64];
336  Size size_b;
337  Size size_mb;
338  Size hp_size;
339 
340  /*
341  * Calculate the shared memory size and round up to the nearest megabyte.
342  */
343  size_b = CalculateShmemSize(NULL);
344  size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
345  sprintf(buf, "%zu", size_mb);
346  SetConfigOption("shared_memory_size", buf,
348 
349  /*
350  * Calculate the number of huge pages required.
351  */
352  GetHugePageSize(&hp_size, NULL);
353  if (hp_size != 0)
354  {
355  Size hp_required;
356 
357  hp_required = add_size(size_b / hp_size, 1);
358  sprintf(buf, "%zu", hp_required);
359  SetConfigOption("shared_memory_size_in_huge_pages", buf,
361  }
362 }
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:4176
@ PGC_S_DYNAMIC_DEFAULT
Definition: guc.h:110
@ PGC_INTERNAL
Definition: guc.h:69
static char * buf
Definition: pg_test_fsync.c:67
#define sprintf
Definition: port.h:240
void GetHugePageSize(Size *hugepagesize, int *mmap_flags)
Definition: sysv_shmem.c:478

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

Referenced by PostgresSingleUserMain(), and PostmasterMain().

◆ on_exit_reset()

void on_exit_reset ( void  )

Definition at line 412 of file ipc.c.

413 {
416  on_proc_exit_index = 0;
418 }
void reset_on_dsm_detach(void)
Definition: dsm.c:1141
static int on_proc_exit_index
Definition: ipc.c:83

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

Referenced by InitPostmasterChild().

◆ on_proc_exit()

void on_proc_exit ( pg_on_exit_callback  function,
Datum  arg 
)

Definition at line 305 of file ipc.c.

306 {
308  ereport(FATAL,
309  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
310  errmsg_internal("out of on_proc_exit slots")));
311 
314 
316 
318  {
319  atexit(atexit_callback);
320  atexit_callback_setup = true;
321  }
322 }
static struct ONEXIT on_proc_exit_list[MAX_ON_EXITS]
Definition: ipc.c:79

References ONEXIT::arg, arg, atexit_callback(), atexit_callback_setup, ereport, errcode(), errmsg_internal(), FATAL, ONEXIT::function, MAX_ON_EXITS, on_proc_exit_index, and on_proc_exit_list.

Referenced by CreateLockFile(), InitCatCache(), llvm_session_initialize(), PostgresMain(), PostmasterMain(), pq_init(), select_perl_context(), sepgsql_avc_init(), and smgrinit().

◆ on_shmem_exit()

◆ proc_exit()

void proc_exit ( int  code)

Definition at line 104 of file ipc.c.

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

References DEBUG3, elog(), exit(), IsAutoVacuumWorkerProcess(), mkdir, proc_exit_prepare(), S_IRWXG, S_IRWXO, S_IRWXU, and snprintf.

Referenced by apply_worker_exit(), ApplyWorkerMain(), auth_failed(), AutoVacLauncherMain(), AutoVacWorkerMain(), AuxiliaryProcessMain(), BackendInitialize(), BootstrapModeMain(), CheckerModeMain(), DisableSubscriptionAndExit(), errfinish(), ExitPostmaster(), HandleCheckpointerInterrupts(), HandleMainLoopInterrupts(), HandlePgArchInterrupts(), HandleStartupProcInterrupts(), HandleWalWriterInterrupts(), InitializeLogRepWorker(), maybe_reread_subscription(), PerformWalRecovery(), pg_attribute_noreturn(), PgArchiverMain(), PostgresMain(), PostgresSingleUserMain(), PreRestoreCommand(), process_syncing_tables_for_apply(), ProcessInterrupts(), ProcessParallelApplyInterrupts(), ProcessRepliesIfAny(), ProcessStandbyMessage(), RestoreArchivedFile(), StartBackgroundWorker(), StartLogicalReplication(), StartReplication(), StartupProcessMain(), StartupProcShutdownHandler(), SysLoggerMain(), test_shm_mq_main(), WaitEventSetWaitBlock(), WalRcvWaitForStartPosition(), WalReceiverMain(), WalSndDone(), WalSndErrorCleanup(), and WalSndWait().

◆ shmem_exit()

void shmem_exit ( int  code)

Definition at line 224 of file ipc.c.

225 {
226  shmem_exit_inprogress = true;
227 
228  /*
229  * Call before_shmem_exit callbacks.
230  *
231  * These should be things that need most of the system to still be up and
232  * working, such as cleanup of temp relations, which requires catalog
233  * access; or things that need to be completed because later cleanup steps
234  * depend on them, such as releasing lwlocks.
235  */
236  elog(DEBUG3, "shmem_exit(%d): %d before_shmem_exit callbacks to make",
238  while (--before_shmem_exit_index >= 0)
242 
243  /*
244  * Call dynamic shared memory callbacks.
245  *
246  * These serve the same purpose as late callbacks, but for dynamic shared
247  * memory segments rather than the main shared memory segment.
248  * dsm_backend_shutdown() has the same kind of progressive logic we use
249  * for the main shared memory segment; namely, it unregisters each
250  * callback before invoking it, so that we don't get stuck in an infinite
251  * loop if one of those callbacks itself throws an ERROR or FATAL.
252  *
253  * Note that explicitly calling this function here is quite different from
254  * registering it as an on_shmem_exit callback for precisely this reason:
255  * if one dynamic shared memory callback errors out, the remaining
256  * callbacks will still be invoked. Thus, hard-coding this call puts it
257  * equal footing with callbacks for the main shared memory segment.
258  */
260 
261  /*
262  * Call on_shmem_exit callbacks.
263  *
264  * These are generally releasing low-level shared memory resources. In
265  * some cases, this is a backstop against the possibility that the early
266  * callbacks might themselves fail, leading to re-entry to this routine;
267  * in other cases, it's cleanup that only happens at process exit.
268  */
269  elog(DEBUG3, "shmem_exit(%d): %d on_shmem_exit callbacks to make",
270  code, on_shmem_exit_index);
271  while (--on_shmem_exit_index >= 0)
275 
276  shmem_exit_inprogress = false;
277 }
void dsm_backend_shutdown(void)
Definition: dsm.c:730
bool shmem_exit_inprogress
Definition: ipc.c:45

References arg, before_shmem_exit_index, before_shmem_exit_list, DEBUG3, dsm_backend_shutdown(), elog(), ONEXIT::function, 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 45 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 57 of file ipci.c.

Referenced by _PG_init(), and CreateSharedMemoryAndSemaphores().