PostgreSQL Source Code  git master
ipc.c File Reference
#include "postgres.h"
#include <signal.h>
#include <unistd.h>
#include <sys/stat.h>
#include "miscadmin.h"
#include "storage/dsm.h"
#include "storage/ipc.h"
#include "tcop/tcopprot.h"
Include dependency graph for ipc.c:

Go to the source code of this file.

Data Structures

struct  ONEXIT
 

Macros

#define MAX_ON_EXITS   20
 

Functions

static void proc_exit_prepare (int code)
 
void proc_exit (int code)
 
void shmem_exit (int code)
 
static void atexit_callback (void)
 
void on_proc_exit (pg_on_exit_callback function, Datum arg)
 
void before_shmem_exit (pg_on_exit_callback function, Datum arg)
 
void on_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)
 

Variables

bool proc_exit_inprogress = false
 
bool shmem_exit_inprogress = false
 
static bool atexit_callback_setup = false
 
static struct ONEXIT on_proc_exit_list [MAX_ON_EXITS]
 
static struct ONEXIT on_shmem_exit_list [MAX_ON_EXITS]
 
static struct ONEXIT before_shmem_exit_list [MAX_ON_EXITS]
 
static int on_proc_exit_index
 
static int on_shmem_exit_index
 
static int before_shmem_exit_index
 

Macro Definition Documentation

◆ MAX_ON_EXITS

#define MAX_ON_EXITS   20

Definition at line 71 of file ipc.c.

Referenced by before_shmem_exit(), on_proc_exit(), and on_shmem_exit().

Function Documentation

◆ atexit_callback()

static void atexit_callback ( void  )
static

Definition at line 288 of file ipc.c.

References proc_exit_prepare().

Referenced by before_shmem_exit(), on_proc_exit(), and on_shmem_exit().

289 {
290  /* Clean up everything that must be cleaned up */
291  /* ... too bad we don't know the real exit code ... */
292  proc_exit_prepare(-1);
293 }
static void proc_exit_prepare(int code)
Definition: ipc.c:161

◆ before_shmem_exit()

void before_shmem_exit ( pg_on_exit_callback  function,
Datum  arg 
)

Definition at line 331 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(), llvm_session_initialize(), LockGXact(), logicalrep_worker_attach(), MarkAsPreparing(), and pg_start_backup().

332 {
334  ereport(FATAL,
335  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
336  errmsg_internal("out of before_shmem_exit slots")));
337 
340 
342 
344  {
345  atexit(atexit_callback);
346  atexit_callback_setup = true;
347  }
348 }
#define MAX_ON_EXITS
Definition: ipc.c:71
int errcode(int sqlerrcode)
Definition: elog.c:575
pg_on_exit_callback function
Definition: ipc.c:75
static bool atexit_callback_setup
Definition: ipc.c:51
#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:83
Datum arg
Definition: ipc.c:76
void * arg
static struct ONEXIT before_shmem_exit_list[MAX_ON_EXITS]
Definition: ipc.c:81
static void atexit_callback(void)
Definition: ipc.c:288

◆ cancel_before_shmem_exit()

void cancel_before_shmem_exit ( pg_on_exit_callback  function,
Datum  arg 
)

Definition at line 388 of file ipc.c.

References before_shmem_exit_index, and before_shmem_exit_list.

Referenced by pg_stop_backup_v2().

389 {
390  if (before_shmem_exit_index > 0 &&
392  == function &&
395 }
static int before_shmem_exit_index
Definition: ipc.c:83
void * arg
static struct ONEXIT before_shmem_exit_list[MAX_ON_EXITS]
Definition: ipc.c:81

◆ on_exit_reset()

void on_exit_reset ( void  )

Definition at line 407 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().

408 {
411  on_proc_exit_index = 0;
413 }
void reset_on_dsm_detach(void)
Definition: dsm.c:1062
static int on_shmem_exit_index
Definition: ipc.c:83
static int on_proc_exit_index
Definition: ipc.c:83
static int before_shmem_exit_index
Definition: ipc.c:83

◆ on_proc_exit()

void on_proc_exit ( pg_on_exit_callback  function,
Datum  arg 
)

Definition at line 303 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().

304 {
306  ereport(FATAL,
307  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
308  errmsg_internal("out of on_proc_exit slots")));
309 
312 
314 
316  {
317  atexit(atexit_callback);
318  atexit_callback_setup = true;
319  }
320 }
static struct ONEXIT on_proc_exit_list[MAX_ON_EXITS]
Definition: ipc.c:79
#define MAX_ON_EXITS
Definition: ipc.c:71
int errcode(int sqlerrcode)
Definition: elog.c:575
pg_on_exit_callback function
Definition: ipc.c:75
static bool atexit_callback_setup
Definition: ipc.c:51
#define FATAL
Definition: elog.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
static int on_proc_exit_index
Definition: ipc.c:83
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
Datum arg
Definition: ipc.c:76
void * arg
static void atexit_callback(void)
Definition: ipc.c:288

◆ on_shmem_exit()

void on_shmem_exit ( pg_on_exit_callback  function,
Datum  arg 
)

Definition at line 359 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(), ParallelWorkerMain(), PGReserveSemaphores(), PGSharedMemoryCreate(), pgss_shmem_startup(), pgstat_initialize(), ProcSignalInit(), replorigin_session_setup(), SharedInvalBackendInit(), and WalReceiverMain().

360 {
362  ereport(FATAL,
363  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
364  errmsg_internal("out of on_shmem_exit slots")));
365 
368 
370 
372  {
373  atexit(atexit_callback);
374  atexit_callback_setup = true;
375  }
376 }
#define MAX_ON_EXITS
Definition: ipc.c:71
int errcode(int sqlerrcode)
Definition: elog.c:575
static int on_shmem_exit_index
Definition: ipc.c:83
pg_on_exit_callback function
Definition: ipc.c:75
static bool atexit_callback_setup
Definition: ipc.c:51
#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:80
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
Datum arg
Definition: ipc.c:76
void * arg
static void atexit_callback(void)
Definition: ipc.c:288

◆ proc_exit()

void proc_exit ( int  code)

Definition at line 104 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(), WaitForParallelWorkersToAttach(), WaitForReplicationWorkerAttach(), WalRcvWaitForStartPosition(), WalReceiverMain(), WalSndDone(), WalSndErrorCleanup(), and WalWriterMain().

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 }
#define DEBUG3
Definition: elog.h:23
static void proc_exit_prepare(int code)
Definition: ipc.c:161
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:3295
#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

◆ proc_exit_prepare()

static void proc_exit_prepare ( int  code)
static

Definition at line 161 of file ipc.c.

References ONEXIT::arg, CritSectionCount, DEBUG3, debug_query_string, elog, error_context_stack, ONEXIT::function, InterruptHoldoffCount, InterruptPending, on_proc_exit_index, on_proc_exit_list, proc_exit_inprogress, ProcDiePending, QueryCancelPending, and shmem_exit().

Referenced by atexit_callback(), and proc_exit().

162 {
163  /*
164  * Once we set this flag, we are committed to exit. Any ereport() will
165  * NOT send control back to the main loop, but right back here.
166  */
167  proc_exit_inprogress = true;
168 
169  /*
170  * Forget any pending cancel or die requests; we're doing our best to
171  * close up shop already. Note that the signal handlers will not set
172  * these flags again, now that proc_exit_inprogress is set.
173  */
174  InterruptPending = false;
175  ProcDiePending = false;
176  QueryCancelPending = false;
178  CritSectionCount = 0;
179 
180  /*
181  * Also clear the error context stack, to prevent error callbacks from
182  * being invoked by any elog/ereport calls made during proc_exit. Whatever
183  * context they might want to offer is probably not relevant, and in any
184  * case they are likely to fail outright after we've done things like
185  * aborting any open transaction. (In normal exit scenarios the context
186  * stack should be empty anyway, but it might not be in the case of
187  * elog(FATAL) for example.)
188  */
189  error_context_stack = NULL;
190  /* For the same reason, reset debug_query_string before it's clobbered */
191  debug_query_string = NULL;
192 
193  /* do our shared memory exits first */
194  shmem_exit(code);
195 
196  elog(DEBUG3, "proc_exit(%d): %d callbacks to make",
197  code, on_proc_exit_index);
198 
199  /*
200  * call all the registered callbacks.
201  *
202  * Note that since we decrement on_proc_exit_index each time, if a
203  * callback calls ereport(ERROR) or ereport(FATAL) then it won't be
204  * invoked again when control comes back here (nor will the
205  * previously-completed callbacks). So, an infinite loop should not be
206  * possible.
207  */
208  while (--on_proc_exit_index >= 0)
211 
212  on_proc_exit_index = 0;
213 }
volatile uint32 InterruptHoldoffCount
Definition: globals.c:38
static struct ONEXIT on_proc_exit_list[MAX_ON_EXITS]
Definition: ipc.c:79
#define DEBUG3
Definition: elog.h:23
void shmem_exit(int code)
Definition: ipc.c:224
volatile bool QueryCancelPending
Definition: globals.c:33
ErrorContextCallback * error_context_stack
Definition: elog.c:88
pg_on_exit_callback function
Definition: ipc.c:75
volatile uint32 CritSectionCount
Definition: globals.c:40
const char * debug_query_string
Definition: postgres.c:86
static int on_proc_exit_index
Definition: ipc.c:83
volatile bool InterruptPending
Definition: globals.c:32
bool proc_exit_inprogress
Definition: ipc.c:40
volatile bool ProcDiePending
Definition: globals.c:34
void * arg
#define elog
Definition: elog.h:219

◆ shmem_exit()

void shmem_exit ( int  code)

Definition at line 224 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, on_shmem_exit_list, and shmem_exit_inprogress.

Referenced by PostmasterStateMachine(), and proc_exit_prepare().

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 }
#define DEBUG3
Definition: elog.h:23
static int on_shmem_exit_index
Definition: ipc.c:83
pg_on_exit_callback function
Definition: ipc.c:75
void dsm_backend_shutdown(void)
Definition: dsm.c:635
static struct ONEXIT on_shmem_exit_list[MAX_ON_EXITS]
Definition: ipc.c:80
static int before_shmem_exit_index
Definition: ipc.c:83
bool shmem_exit_inprogress
Definition: ipc.c:45
void * arg
static struct ONEXIT before_shmem_exit_list[MAX_ON_EXITS]
Definition: ipc.c:81
#define elog
Definition: elog.h:219

Variable Documentation

◆ atexit_callback_setup

bool atexit_callback_setup = false
static

Definition at line 51 of file ipc.c.

Referenced by before_shmem_exit(), on_proc_exit(), and on_shmem_exit().

◆ before_shmem_exit_index

int before_shmem_exit_index
static

Definition at line 83 of file ipc.c.

Referenced by before_shmem_exit(), cancel_before_shmem_exit(), on_exit_reset(), and shmem_exit().

◆ before_shmem_exit_list

struct ONEXIT before_shmem_exit_list[MAX_ON_EXITS]
static

Definition at line 81 of file ipc.c.

Referenced by before_shmem_exit(), cancel_before_shmem_exit(), and shmem_exit().

◆ on_proc_exit_index

int on_proc_exit_index
static

Definition at line 83 of file ipc.c.

Referenced by on_exit_reset(), on_proc_exit(), and proc_exit_prepare().

◆ on_proc_exit_list

struct ONEXIT on_proc_exit_list[MAX_ON_EXITS]
static

Definition at line 79 of file ipc.c.

Referenced by on_proc_exit(), and proc_exit_prepare().

◆ on_shmem_exit_index

int on_shmem_exit_index
static

Definition at line 83 of file ipc.c.

Referenced by on_exit_reset(), on_shmem_exit(), and shmem_exit().

◆ on_shmem_exit_list

struct ONEXIT on_shmem_exit_list[MAX_ON_EXITS]
static

Definition at line 80 of file ipc.c.

Referenced by on_shmem_exit(), and shmem_exit().

◆ proc_exit_inprogress

◆ shmem_exit_inprogress

bool shmem_exit_inprogress = false

Definition at line 45 of file ipc.c.

Referenced by AtAbort_Portals(), and shmem_exit().