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 CreateSharedMemoryAndSemaphores (bool makePrivate, int port)
 

Variables

PGDLLIMPORT bool proc_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:377
void * arg

Definition at line 52 of file ipc.h.

Referenced by autoprewarm_dump_now(), btbulkdelete(), createdb(), do_pg_start_backup(), do_pg_stop_backup(), movedb(), and perform_base_backup().

◆ PG_ENSURE_ERROR_CLEANUP

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

Definition at line 47 of file ipc.h.

Referenced by autoprewarm_dump_now(), btbulkdelete(), createdb(), do_pg_start_backup(), do_pg_stop_backup(), movedb(), and perform_base_backup().

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 320 of file ipc.c.

References ONEXIT::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(), Exec_ListenPreCommit(), InitPostgres(), LockGXact(), logicalrep_worker_attach(), MarkAsPreparing(), and pg_start_backup().

321 {
323  ereport(FATAL,
324  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
325  errmsg_internal("out of before_shmem_exit slots")));
326 
329 
331 
333  {
334  atexit(atexit_callback);
335  atexit_callback_setup = true;
336  }
337 }
#define MAX_ON_EXITS
Definition: ipc.c:66
int errcode(int sqlerrcode)
Definition: elog.c:575
pg_on_exit_callback function
Definition: ipc.c:70
static bool atexit_callback_setup
Definition: ipc.c:46
#define FATAL
Definition: elog.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
static int before_shmem_exit_index
Definition: ipc.c:78
Datum arg
Definition: ipc.c:71
void * arg
static struct ONEXIT before_shmem_exit_list[MAX_ON_EXITS]
Definition: ipc.c:76
static void atexit_callback(void)
Definition: ipc.c:277

◆ cancel_before_shmem_exit()

void cancel_before_shmem_exit ( pg_on_exit_callback  function,
Datum  arg 
)

Definition at line 377 of file ipc.c.

References before_shmem_exit_index, and before_shmem_exit_list.

Referenced by pg_stop_backup_v2().

378 {
379  if (before_shmem_exit_index > 0 &&
381  == function &&
384 }
static int before_shmem_exit_index
Definition: ipc.c:78
void * arg
static struct ONEXIT before_shmem_exit_list[MAX_ON_EXITS]
Definition: ipc.c:76

◆ CreateSharedMemoryAndSemaphores()

void CreateSharedMemoryAndSemaphores ( bool  makePrivate,
int  port 
)

Definition at line 96 of file ipci.c.

References add_size(), addin_request_allowed, ApplyLauncherShmemInit(), ApplyLauncherShmemSize(), Assert, AsyncShmemInit(), AsyncShmemSize(), AutoVacuumShmemInit(), AutoVacuumShmemSize(), BackendRandomShmemInit(), BackendRandomShmemSize(), BackendStatusShmemSize(), BackgroundWorkerShmemInit(), BackgroundWorkerShmemSize(), BTreeShmemInit(), BTreeShmemSize(), BufferShmemSize(), CheckpointerShmemInit(), CheckpointerShmemSize(), CLOGShmemInit(), CLOGShmemSize(), CommitTsShmemInit(), CommitTsShmemSize(), CreateLWLocks(), CreateSharedBackendStatus(), CreateSharedInvalidationState(), CreateSharedProcArray(), DEBUG3, dsm_postmaster_startup(), elog, hash_estimate_size(), InitBufferPool(), InitLocks(), InitPredicateLocks(), InitProcGlobal(), InitShmemAccess(), InitShmemAllocation(), InitShmemIndex(), IsUnderPostmaster, LockShmemSize(), LWLockShmemSize(), MultiXactShmemInit(), MultiXactShmemSize(), PANIC, PGReserveSemaphores(), PGSemaphoreShmemSize(), PGSharedMemoryCreate(), PMSignalShmemInit(), PMSignalShmemSize(), PredicateLockShmemSize(), ProcArrayShmemSize(), ProcGlobalSemas(), ProcGlobalShmemSize(), ProcSignalShmemInit(), ProcSignalShmemSize(), ReplicationOriginShmemInit(), ReplicationOriginShmemSize(), ReplicationSlotsShmemInit(), ReplicationSlotsShmemSize(), SHMEM_INDEX_SIZE, shmem_startup_hook, SInvalShmemSize(), SnapMgrInit(), SnapMgrShmemSize(), SpinlockSemaInit(), SpinlockSemas(), SpinlockSemaSize(), SUBTRANSShmemInit(), SUBTRANSShmemSize(), SyncScanShmemInit(), SyncScanShmemSize(), total_addin_request, TwoPhaseShmemInit(), TwoPhaseShmemSize(), WalRcvShmemInit(), WalRcvShmemSize(), WalSndShmemInit(), WalSndShmemSize(), XLOGShmemInit(), and XLOGShmemSize().

Referenced by BackendRun(), InitCommunication(), and reset_shared().

97 {
98  PGShmemHeader *shim = NULL;
99 
100  if (!IsUnderPostmaster)
101  {
102  PGShmemHeader *seghdr;
103  Size size;
104  int numSemas;
105 
106  /* Compute number of semaphores we'll need */
107  numSemas = ProcGlobalSemas();
108  numSemas += SpinlockSemas();
109 
110  /*
111  * Size of the Postgres shared-memory block is estimated via
112  * moderately-accurate estimates for the big hogs, plus 100K for the
113  * stuff that's too small to bother with estimating.
114  *
115  * We take some care during this phase to ensure that the total size
116  * request doesn't overflow size_t. If this gets through, we don't
117  * need to be so careful during the actual allocation phase.
118  */
119  size = 100000;
120  size = add_size(size, PGSemaphoreShmemSize(numSemas));
121  size = add_size(size, SpinlockSemaSize());
123  sizeof(ShmemIndexEnt)));
124  size = add_size(size, BufferShmemSize());
125  size = add_size(size, LockShmemSize());
126  size = add_size(size, PredicateLockShmemSize());
127  size = add_size(size, ProcGlobalShmemSize());
128  size = add_size(size, XLOGShmemSize());
129  size = add_size(size, CLOGShmemSize());
130  size = add_size(size, CommitTsShmemSize());
131  size = add_size(size, SUBTRANSShmemSize());
132  size = add_size(size, TwoPhaseShmemSize());
133  size = add_size(size, BackgroundWorkerShmemSize());
134  size = add_size(size, MultiXactShmemSize());
135  size = add_size(size, LWLockShmemSize());
136  size = add_size(size, ProcArrayShmemSize());
137  size = add_size(size, BackendStatusShmemSize());
138  size = add_size(size, SInvalShmemSize());
139  size = add_size(size, PMSignalShmemSize());
140  size = add_size(size, ProcSignalShmemSize());
141  size = add_size(size, CheckpointerShmemSize());
142  size = add_size(size, AutoVacuumShmemSize());
143  size = add_size(size, ReplicationSlotsShmemSize());
144  size = add_size(size, ReplicationOriginShmemSize());
145  size = add_size(size, WalSndShmemSize());
146  size = add_size(size, WalRcvShmemSize());
147  size = add_size(size, ApplyLauncherShmemSize());
148  size = add_size(size, SnapMgrShmemSize());
149  size = add_size(size, BTreeShmemSize());
150  size = add_size(size, SyncScanShmemSize());
151  size = add_size(size, AsyncShmemSize());
152  size = add_size(size, BackendRandomShmemSize());
153 #ifdef EXEC_BACKEND
154  size = add_size(size, ShmemBackendArraySize());
155 #endif
156 
157  /* freeze the addin request size and include it */
158  addin_request_allowed = false;
159  size = add_size(size, total_addin_request);
160 
161  /* might as well round it off to a multiple of a typical page size */
162  size = add_size(size, 8192 - (size % 8192));
163 
164  elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
165 
166  /*
167  * Create the shmem segment
168  */
169  seghdr = PGSharedMemoryCreate(size, makePrivate, port, &shim);
170 
171  InitShmemAccess(seghdr);
172 
173  /*
174  * Create semaphores
175  */
176  PGReserveSemaphores(numSemas, port);
177 
178  /*
179  * If spinlocks are disabled, initialize emulation layer (which
180  * depends on semaphores, so the order is important here).
181  */
182 #ifndef HAVE_SPINLOCKS
184 #endif
185  }
186  else
187  {
188  /*
189  * We are reattaching to an existing shared memory segment. This
190  * should only be reached in the EXEC_BACKEND case, and even then only
191  * with makePrivate == false.
192  */
193 #ifdef EXEC_BACKEND
194  Assert(!makePrivate);
195 #else
196  elog(PANIC, "should be attached to shared memory already");
197 #endif
198  }
199 
200  /*
201  * Set up shared memory allocation mechanism
202  */
203  if (!IsUnderPostmaster)
205 
206  /*
207  * Now initialize LWLocks, which do shared memory allocation and are
208  * needed for InitShmemIndex.
209  */
210  CreateLWLocks();
211 
212  /*
213  * Set up shmem.c index hashtable
214  */
215  InitShmemIndex();
216 
217  /*
218  * Set up xlog, clog, and buffers
219  */
220  XLOGShmemInit();
221  CLOGShmemInit();
225  InitBufferPool();
226 
227  /*
228  * Set up lock manager
229  */
230  InitLocks();
231 
232  /*
233  * Set up predicate lock manager
234  */
236 
237  /*
238  * Set up process table
239  */
240  if (!IsUnderPostmaster)
241  InitProcGlobal();
246 
247  /*
248  * Set up shared-inval messaging
249  */
251 
252  /*
253  * Set up interprocess signaling mechanisms
254  */
261  WalSndShmemInit();
262  WalRcvShmemInit();
264 
265  /*
266  * Set up other modules that need some shared memory space
267  */
268  SnapMgrInit();
269  BTreeShmemInit();
271  AsyncShmemInit();
273 
274 #ifdef EXEC_BACKEND
275 
276  /*
277  * Alloc the win32 shared backend array
278  */
279  if (!IsUnderPostmaster)
280  ShmemBackendArrayAllocation();
281 #endif
282 
283  /* Initialize dynamic shared memory facilities. */
284  if (!IsUnderPostmaster)
286 
287  /*
288  * Now give loadable modules a chance to set up their shmem allocations
289  */
290  if (shmem_startup_hook)
292 }
void dsm_postmaster_startup(PGShmemHeader *shim)
Definition: dsm.c:145
Size SInvalShmemSize(void)
Definition: sinvaladt.c:204
void BTreeShmemInit(void)
Definition: nbtutils.c:2014
Size PMSignalShmemSize(void)
Definition: pmsignal.c:80
Size ReplicationSlotsShmemSize(void)
Definition: slot.c:114
Size BackendRandomShmemSize(void)
#define SHMEM_INDEX_SIZE
Definition: shmem.h:55
void BackendRandomShmemInit(void)
void SUBTRANSShmemInit(void)
Definition: subtrans.c:191
#define DEBUG3
Definition: elog.h:23
Size SpinlockSemaSize(void)
Definition: spin.c:39
void InitShmemAccess(void *seghdr)
Definition: shmem.c:97
void AsyncShmemInit(void)
Definition: async.c:443
void CreateSharedInvalidationState(void)
Definition: sinvaladt.c:219
Size SyncScanShmemSize(void)
Definition: syncscan.c:126
void PGReserveSemaphores(int maxSemas, int port)
Definition: posix_sema.c:193
Size WalSndShmemSize(void)
Definition: walsender.c:2960
void CheckpointerShmemInit(void)
Definition: checkpointer.c:926
Size PredicateLockShmemSize(void)
Definition: predicate.c:1311
Size TwoPhaseShmemSize(void)
Definition: twophase.c:238
void ReplicationOriginShmemInit(void)
Definition: origin.c:478
void WalRcvShmemInit(void)
#define PANIC
Definition: elog.h:53
void XLOGShmemInit(void)
Definition: xlog.c:4890
Size BackgroundWorkerShmemSize(void)
Definition: bgworker.c:143
void ReplicationSlotsShmemInit(void)
Definition: slot.c:132
Size LWLockShmemSize(void)
Definition: lwlock.c:349
Size BTreeShmemSize(void)
Definition: nbtutils.c:2001
void InitProcGlobal(void)
Definition: proc.c:162
void BackgroundWorkerShmemInit(void)
Definition: bgworker.c:159
void MultiXactShmemInit(void)
Definition: multixact.c:1820
void PMSignalShmemInit(void)
Definition: pmsignal.c:95
Size XLOGShmemSize(void)
Definition: xlog.c:4849
void SpinlockSemaInit(void)
Definition: spin.c:76
bool IsUnderPostmaster
Definition: globals.c:101
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:745
void InitBufferPool(void)
Definition: buf_init.c:68
void SnapMgrInit(void)
Definition: snapmgr.c:267
void InitLocks(void)
Definition: lock.c:377
Size ProcGlobalShmemSize(void)
Definition: proc.c:102
static int port
Definition: pg_regress.c:90
static bool addin_request_allowed
Definition: ipci.c:54
Size ProcSignalShmemSize(void)
Definition: procsignal.c:74
shmem_startup_hook_type shmem_startup_hook
Definition: ipci.c:51
Size hash_estimate_size(long num_entries, Size entrysize)
Definition: dynahash.c:728
void TwoPhaseShmemInit(void)
Definition: twophase.c:254
int SpinlockSemas(void)
Definition: spin.c:65
static Size total_addin_request
Definition: ipci.c:53
Size BackendStatusShmemSize(void)
Definition: pgstat.c:2604
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
Size AutoVacuumShmemSize(void)
Definition: autovacuum.c:3267
Size ReplicationOriginShmemSize(void)
Definition: origin.c:458
Size CheckpointerShmemSize(void)
Definition: checkpointer.c:907
Size CLOGShmemSize(void)
Definition: clog.c:692
void CreateSharedBackendStatus(void)
Definition: pgstat.c:2632
Size WalRcvShmemSize(void)
void CreateLWLocks(void)
Definition: lwlock.c:381
#define Assert(condition)
Definition: c.h:670
void SyncScanShmemInit(void)
Definition: syncscan.c:135
void CreateSharedProcArray(void)
Definition: procarray.c:221
Size AsyncShmemSize(void)
Definition: async.c:426
size_t Size
Definition: c.h:404
PGShmemHeader * PGSharedMemoryCreate(Size size, bool makePrivate, int port, PGShmemHeader **shim)
Definition: sysv_shmem.c:558
Size CommitTsShmemSize(void)
Definition: commit_ts.c:479
void ApplyLauncherShmemInit(void)
Definition: launcher.c:793
Size MultiXactShmemSize(void)
Definition: multixact.c:1803
Size LockShmemSize(void)
Definition: lock.c:3361
Size SUBTRANSShmemSize(void)
Definition: subtrans.c:185
Size BufferShmemSize(void)
Definition: buf_init.c:161
void WalSndShmemInit(void)
Definition: walsender.c:2972
void CLOGShmemInit(void)
Definition: clog.c:698
Size PGSemaphoreShmemSize(int maxSemas)
Definition: posix_sema.c:158
void ProcSignalShmemInit(void)
Definition: procsignal.c:84
void InitShmemIndex(void)
Definition: shmem.c:272
#define elog
Definition: elog.h:219
void InitPredicateLocks(void)
Definition: predicate.c:1108
void AutoVacuumShmemInit(void)
Definition: autovacuum.c:3286
Size SnapMgrShmemSize(void)
Definition: snapmgr.c:251
Size ProcArrayShmemSize(void)
Definition: procarray.c:179
int ProcGlobalSemas(void)
Definition: proc.c:128
void InitShmemAllocation(void)
Definition: shmem.c:112
void CommitTsShmemInit(void)
Definition: commit_ts.c:490

◆ on_exit_reset()

void on_exit_reset ( void  )

Definition at line 396 of file ipc.c.

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

Referenced by bg_quickdie(), bgworker_quickdie(), chkpt_quickdie(), InitPostmasterChild(), quickdie(), startupproc_quickdie(), wal_quickdie(), and WalRcvQuickDieHandler().

397 {
400  on_proc_exit_index = 0;
402 }
void reset_on_dsm_detach(void)
Definition: dsm.c:1075
static int on_shmem_exit_index
Definition: ipc.c:78
static int on_proc_exit_index
Definition: ipc.c:78
static int before_shmem_exit_index
Definition: ipc.c:78

◆ on_proc_exit()

void on_proc_exit ( pg_on_exit_callback  function,
Datum  arg 
)

Definition at line 292 of file ipc.c.

References ONEXIT::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(), InitFileAccess(), PostgresMain(), PostmasterMain(), pq_init(), select_perl_context(), sepgsql_avc_init(), and smgrinit().

293 {
295  ereport(FATAL,
296  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
297  errmsg_internal("out of on_proc_exit slots")));
298 
301 
303 
305  {
306  atexit(atexit_callback);
307  atexit_callback_setup = true;
308  }
309 }
static struct ONEXIT on_proc_exit_list[MAX_ON_EXITS]
Definition: ipc.c:74
#define MAX_ON_EXITS
Definition: ipc.c:66
int errcode(int sqlerrcode)
Definition: elog.c:575
pg_on_exit_callback function
Definition: ipc.c:70
static bool atexit_callback_setup
Definition: ipc.c:46
#define FATAL
Definition: elog.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
static int on_proc_exit_index
Definition: ipc.c:78
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
Datum arg
Definition: ipc.c:71
void * arg
static void atexit_callback(void)
Definition: ipc.c:277

◆ on_shmem_exit()

void on_shmem_exit ( pg_on_exit_callback  function,
Datum  arg 
)

Definition at line 348 of file ipc.c.

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

Referenced by autoprewarm_main(), AutoVacWorkerMain(), dsm_postmaster_startup(), InitAuxiliaryProcess(), InitBufferPoolBackend(), InitPostgres(), InitProcess(), InitProcessPhase2(), InitWalSenderSlot(), InternalIpcMemoryCreate(), PGReserveSemaphores(), PGSharedMemoryCreate(), pgss_shmem_startup(), pgstat_initialize(), ProcSignalInit(), replorigin_session_setup(), SharedInvalBackendInit(), and WalReceiverMain().

349 {
351  ereport(FATAL,
352  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
353  errmsg_internal("out of on_shmem_exit slots")));
354 
357 
359 
361  {
362  atexit(atexit_callback);
363  atexit_callback_setup = true;
364  }
365 }
#define MAX_ON_EXITS
Definition: ipc.c:66
int errcode(int sqlerrcode)
Definition: elog.c:575
static int on_shmem_exit_index
Definition: ipc.c:78
pg_on_exit_callback function
Definition: ipc.c:70
static bool atexit_callback_setup
Definition: ipc.c:46
#define FATAL
Definition: elog.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
static struct ONEXIT on_shmem_exit_list[MAX_ON_EXITS]
Definition: ipc.c:75
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
Datum arg
Definition: ipc.c:71
void * arg
static void atexit_callback(void)
Definition: ipc.c:277

◆ proc_exit()

void proc_exit ( int  code)

Definition at line 99 of file ipc.c.

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

Referenced by ApplyLauncherMain(), ApplyWorkerMain(), auth_failed(), autoprewarm_main(), AutoVacLauncherMain(), AutoVacWorkerMain(), AuxiliaryProcessMain(), BackendInitialize(), BackgroundWriterMain(), BootstrapModeMain(), CheckerModeMain(), CheckpointerMain(), copy_read_data(), errfinish(), ExitPostmaster(), HandleStartupProcInterrupts(), initialize_worker_spi(), logicalrep_worker_stop(), LogicalRepApplyLoop(), maybe_reread_subscription(), pg_attribute_noreturn(), pgstat_bestart(), PostgresMain(), PreRestoreCommand(), ProcessInterrupts(), ProcessRepliesIfAny(), ProcessStandbyMessage(), RestoreArchivedFile(), StartBackgroundWorker(), StartLogicalReplication(), StartReplication(), startup_die(), StartupPacketTimeoutHandler(), StartupProcessMain(), StartupProcShutdownHandler(), StartupXLOG(), SysLoggerMain(), test_shm_mq_main(), wait_for_relation_state_change(), wait_for_worker_state_change(), WaitForReplicationWorkerAttach(), WalRcvWaitForStartPosition(), WalReceiverMain(), WalSndDone(), WalSndErrorCleanup(), and WalWriterMain().

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

◆ shmem_exit()

void shmem_exit ( int  code)

Definition at line 215 of file ipc.c.

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

Referenced by PostmasterStateMachine(), and proc_exit_prepare().

216 {
217  /*
218  * Call before_shmem_exit callbacks.
219  *
220  * These should be things that need most of the system to still be up and
221  * working, such as cleanup of temp relations, which requires catalog
222  * access; or things that need to be completed because later cleanup steps
223  * depend on them, such as releasing lwlocks.
224  */
225  elog(DEBUG3, "shmem_exit(%d): %d before_shmem_exit callbacks to make",
227  while (--before_shmem_exit_index >= 0)
231 
232  /*
233  * Call dynamic shared memory callbacks.
234  *
235  * These serve the same purpose as late callbacks, but for dynamic shared
236  * memory segments rather than the main shared memory segment.
237  * dsm_backend_shutdown() has the same kind of progressive logic we use
238  * for the main shared memory segment; namely, it unregisters each
239  * callback before invoking it, so that we don't get stuck in an infinite
240  * loop if one of those callbacks itself throws an ERROR or FATAL.
241  *
242  * Note that explicitly calling this function here is quite different from
243  * registering it as an on_shmem_exit callback for precisely this reason:
244  * if one dynamic shared memory callback errors out, the remaining
245  * callbacks will still be invoked. Thus, hard-coding this call puts it
246  * equal footing with callbacks for the main shared memory segment.
247  */
249 
250  /*
251  * Call on_shmem_exit callbacks.
252  *
253  * These are generally releasing low-level shared memory resources. In
254  * some cases, this is a backstop against the possibility that the early
255  * callbacks might themselves fail, leading to re-entry to this routine;
256  * in other cases, it's cleanup that only happens at process exit.
257  */
258  elog(DEBUG3, "shmem_exit(%d): %d on_shmem_exit callbacks to make",
259  code, on_shmem_exit_index);
260  while (--on_shmem_exit_index >= 0)
264 }
#define DEBUG3
Definition: elog.h:23
static int on_shmem_exit_index
Definition: ipc.c:78
pg_on_exit_callback function
Definition: ipc.c:70
void dsm_backend_shutdown(void)
Definition: dsm.c:648
static struct ONEXIT on_shmem_exit_list[MAX_ON_EXITS]
Definition: ipc.c:75
static int before_shmem_exit_index
Definition: ipc.c:78
void * arg
static struct ONEXIT before_shmem_exit_list[MAX_ON_EXITS]
Definition: ipc.c:76
#define elog
Definition: elog.h:219

Variable Documentation

◆ proc_exit_inprogress

◆ shmem_startup_hook

Definition at line 51 of file ipci.c.

Referenced by _PG_fini(), _PG_init(), and CreateSharedMemoryAndSemaphores().