PostgreSQL Source Code  git master
ipc.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * ipc.c
4  * POSTGRES inter-process communication definitions.
5  *
6  * This file is misnamed, as it no longer has much of anything directly
7  * to do with IPC. The functionality here is concerned with managing
8  * exit-time cleanup for either a postmaster or a backend.
9  *
10  *
11  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
12  * Portions Copyright (c) 1994, Regents of the University of California
13  *
14  *
15  * IDENTIFICATION
16  * src/backend/storage/ipc/ipc.c
17  *
18  *-------------------------------------------------------------------------
19  */
20 #include "postgres.h"
21 
22 #include <signal.h>
23 #include <unistd.h>
24 #include <sys/stat.h>
25 
26 #include "miscadmin.h"
27 #ifdef PROFILE_PID_DIR
28 #include "postmaster/autovacuum.h"
29 #endif
30 #include "storage/dsm.h"
31 #include "storage/ipc.h"
32 #include "tcop/tcopprot.h"
33 
34 
35 /*
36  * This flag is set during proc_exit() to change ereport()'s behavior,
37  * so that an ereport() from an on_proc_exit routine cannot get us out
38  * of the exit procedure. We do NOT want to go back to the idle loop...
39  */
40 bool proc_exit_inprogress = false;
41 
42 /*
43  * Set when shmem_exit() is in progress.
44  */
45 bool shmem_exit_inprogress = false;
46 
47 /*
48  * This flag tracks whether we've called atexit() in the current process
49  * (or in the parent postmaster).
50  */
51 static bool atexit_callback_setup = false;
52 
53 /* local functions */
54 static void proc_exit_prepare(int code);
55 
56 
57 /* ----------------------------------------------------------------
58  * exit() handling stuff
59  *
60  * These functions are in generally the same spirit as atexit(),
61  * but provide some additional features we need --- in particular,
62  * we want to register callbacks to invoke when we are disconnecting
63  * from a broken shared-memory context but not exiting the postmaster.
64  *
65  * Callback functions can take zero, one, or two args: the first passed
66  * arg is the integer exitcode, the second is the Datum supplied when
67  * the callback was registered.
68  * ----------------------------------------------------------------
69  */
70 
71 #define MAX_ON_EXITS 20
72 
73 struct ONEXIT
74 {
77 };
78 
82 
83 static int on_proc_exit_index,
86 
87 
88 /* ----------------------------------------------------------------
89  * proc_exit
90  *
91  * this function calls all the callbacks registered
92  * for it (to free resources) and then calls exit.
93  *
94  * This should be the only function to call exit().
95  * -cim 2/6/90
96  *
97  * Unfortunately, we can't really guarantee that add-on code
98  * obeys the rule of not calling exit() directly. So, while
99  * this is the preferred way out of the system, we also register
100  * an atexit callback that will make sure cleanup happens.
101  * ----------------------------------------------------------------
102  */
103 void
104 proc_exit(int code)
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  mkdir("gprof", S_IRWXU | S_IRWXG | S_IRWXO);
141  mkdir(gprofDirName, S_IRWXU | S_IRWXG | S_IRWXO);
142  chdir(gprofDirName);
143  }
144 #endif
145 
146  elog(DEBUG3, "exit(%d)", code);
147 
148  exit(code);
149 }
150 
151 /*
152  * Code shared between proc_exit and the atexit handler. Note that in
153  * normal exit through proc_exit, this will actually be called twice ...
154  * but the second call will have nothing to do.
155  */
156 static void
158 {
159  /*
160  * Once we set this flag, we are committed to exit. Any ereport() will
161  * NOT send control back to the main loop, but right back here.
162  */
163  proc_exit_inprogress = true;
164 
165  /*
166  * Forget any pending cancel or die requests; we're doing our best to
167  * close up shop already. Note that the signal handlers will not set
168  * these flags again, now that proc_exit_inprogress is set.
169  */
170  InterruptPending = false;
171  ProcDiePending = false;
172  QueryCancelPending = false;
174  CritSectionCount = 0;
175 
176  /*
177  * Also clear the error context stack, to prevent error callbacks from
178  * being invoked by any elog/ereport calls made during proc_exit. Whatever
179  * context they might want to offer is probably not relevant, and in any
180  * case they are likely to fail outright after we've done things like
181  * aborting any open transaction. (In normal exit scenarios the context
182  * stack should be empty anyway, but it might not be in the case of
183  * elog(FATAL) for example.)
184  */
185  error_context_stack = NULL;
186  /* For the same reason, reset debug_query_string before it's clobbered */
187  debug_query_string = NULL;
188 
189  /* do our shared memory exits first */
190  shmem_exit(code);
191 
192  elog(DEBUG3, "proc_exit(%d): %d callbacks to make",
193  code, on_proc_exit_index);
194 
195  /*
196  * call all the registered callbacks.
197  *
198  * Note that since we decrement on_proc_exit_index each time, if a
199  * callback calls ereport(ERROR) or ereport(FATAL) then it won't be
200  * invoked again when control comes back here (nor will the
201  * previously-completed callbacks). So, an infinite loop should not be
202  * possible.
203  */
204  while (--on_proc_exit_index >= 0)
207 
208  on_proc_exit_index = 0;
209 }
210 
211 /* ------------------
212  * Run all of the on_shmem_exit routines --- but don't actually exit.
213  * This is used by the postmaster to re-initialize shared memory and
214  * semaphores after a backend dies horribly. As with proc_exit(), we
215  * remove each callback from the list before calling it, to avoid
216  * infinite loop in case of error.
217  * ------------------
218  */
219 void
220 shmem_exit(int code)
221 {
222  shmem_exit_inprogress = true;
223 
224  /*
225  * Call before_shmem_exit callbacks.
226  *
227  * These should be things that need most of the system to still be up and
228  * working, such as cleanup of temp relations, which requires catalog
229  * access; or things that need to be completed because later cleanup steps
230  * depend on them, such as releasing lwlocks.
231  */
232  elog(DEBUG3, "shmem_exit(%d): %d before_shmem_exit callbacks to make",
234  while (--before_shmem_exit_index >= 0)
238 
239  /*
240  * Call dynamic shared memory callbacks.
241  *
242  * These serve the same purpose as late callbacks, but for dynamic shared
243  * memory segments rather than the main shared memory segment.
244  * dsm_backend_shutdown() has the same kind of progressive logic we use
245  * for the main shared memory segment; namely, it unregisters each
246  * callback before invoking it, so that we don't get stuck in an infinite
247  * loop if one of those callbacks itself throws an ERROR or FATAL.
248  *
249  * Note that explicitly calling this function here is quite different from
250  * registering it as an on_shmem_exit callback for precisely this reason:
251  * if one dynamic shared memory callback errors out, the remaining
252  * callbacks will still be invoked. Thus, hard-coding this call puts it
253  * equal footing with callbacks for the main shared memory segment.
254  */
256 
257  /*
258  * Call on_shmem_exit callbacks.
259  *
260  * These are generally releasing low-level shared memory resources. In
261  * some cases, this is a backstop against the possibility that the early
262  * callbacks might themselves fail, leading to re-entry to this routine;
263  * in other cases, it's cleanup that only happens at process exit.
264  */
265  elog(DEBUG3, "shmem_exit(%d): %d on_shmem_exit callbacks to make",
266  code, on_shmem_exit_index);
267  while (--on_shmem_exit_index >= 0)
271 }
272 
273 /* ----------------------------------------------------------------
274  * atexit_callback
275  *
276  * Backstop to ensure that direct calls of exit() don't mess us up.
277  *
278  * Somebody who was being really uncooperative could call _exit(),
279  * but for that case we have a "dead man switch" that will make the
280  * postmaster treat it as a crash --- see pmsignal.c.
281  * ----------------------------------------------------------------
282  */
283 static void
285 {
286  /* Clean up everything that must be cleaned up */
287  /* ... too bad we don't know the real exit code ... */
288  proc_exit_prepare(-1);
289 }
290 
291 /* ----------------------------------------------------------------
292  * on_proc_exit
293  *
294  * this function adds a callback function to the list of
295  * functions invoked by proc_exit(). -cim 2/6/90
296  * ----------------------------------------------------------------
297  */
298 void
300 {
302  ereport(FATAL,
303  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
304  errmsg_internal("out of on_proc_exit slots")));
305 
308 
310 
312  {
313  atexit(atexit_callback);
314  atexit_callback_setup = true;
315  }
316 }
317 
318 /* ----------------------------------------------------------------
319  * before_shmem_exit
320  *
321  * Register early callback to perform user-level cleanup,
322  * e.g. transaction abort, before we begin shutting down
323  * low-level subsystems.
324  * ----------------------------------------------------------------
325  */
326 void
328 {
330  ereport(FATAL,
331  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
332  errmsg_internal("out of before_shmem_exit slots")));
333 
336 
338 
340  {
341  atexit(atexit_callback);
342  atexit_callback_setup = true;
343  }
344 }
345 
346 /* ----------------------------------------------------------------
347  * on_shmem_exit
348  *
349  * Register ordinary callback to perform low-level shutdown
350  * (e.g. releasing our PGPROC); run after before_shmem_exit
351  * callbacks and before on_proc_exit callbacks.
352  * ----------------------------------------------------------------
353  */
354 void
356 {
358  ereport(FATAL,
359  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
360  errmsg_internal("out of on_shmem_exit slots")));
361 
364 
366 
368  {
369  atexit(atexit_callback);
370  atexit_callback_setup = true;
371  }
372 }
373 
374 /* ----------------------------------------------------------------
375  * cancel_before_shmem_exit
376  *
377  * this function removes a previously-registed before_shmem_exit
378  * callback. For simplicity, only the latest entry can be
379  * removed. (We could work harder but there is no need for
380  * current uses.)
381  * ----------------------------------------------------------------
382  */
383 void
385 {
386  if (before_shmem_exit_index > 0 &&
388  == function &&
391 }
392 
393 /* ----------------------------------------------------------------
394  * on_exit_reset
395  *
396  * this function clears all on_proc_exit() and on_shmem_exit()
397  * registered functions. This is used just after forking a backend,
398  * so that the backend doesn't believe it should call the postmaster's
399  * on-exit routines when it exits...
400  * ----------------------------------------------------------------
401  */
402 void
404 {
407  on_proc_exit_index = 0;
409 }
volatile uint32 InterruptHoldoffCount
Definition: globals.c:35
void reset_on_dsm_detach(void)
Definition: dsm.c:1062
static struct ONEXIT on_proc_exit_list[MAX_ON_EXITS]
Definition: ipc.c:79
#define DEBUG3
Definition: elog.h:23
void on_proc_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:299
#define MAX_ON_EXITS
Definition: ipc.c:71
static void proc_exit_prepare(int code)
Definition: ipc.c:157
void proc_exit(int code)
Definition: ipc.c:104
int errcode(int sqlerrcode)
Definition: elog.c:575
static int on_shmem_exit_index
Definition: ipc.c:83
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void shmem_exit(int code)
Definition: ipc.c:220
volatile bool QueryCancelPending
Definition: globals.c:30
ErrorContextCallback * error_context_stack
Definition: elog.c:88
pg_on_exit_callback function
Definition: ipc.c:75
static bool atexit_callback_setup
Definition: ipc.c:51
void on_exit_reset(void)
Definition: ipc.c:403
#define FATAL
Definition: elog.h:52
void cancel_before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:384
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:355
#define S_IRWXG
Definition: win32_port.h:292
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:327
volatile uint32 CritSectionCount
Definition: globals.c:37
Definition: ipc.c:73
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3255
#define ereport(elevel, rest)
Definition: elog.h:122
void dsm_backend_shutdown(void)
Definition: dsm.c:635
const char * debug_query_string
Definition: postgres.c:85
static struct ONEXIT on_shmem_exit_list[MAX_ON_EXITS]
Definition: ipc.c:80
uintptr_t Datum
Definition: postgres.h:365
static int on_proc_exit_index
Definition: ipc.c:83
volatile bool InterruptPending
Definition: globals.c:29
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
bool proc_exit_inprogress
Definition: ipc.c:40
static int before_shmem_exit_index
Definition: ipc.c:83
void(* pg_on_exit_callback)(int code, Datum arg)
Definition: ipc.h:21
Datum arg
Definition: ipc.c:76
volatile bool ProcDiePending
Definition: globals.c:31
bool shmem_exit_inprogress
Definition: ipc.c:45
#define S_IRWXU
Definition: win32_port.h:280
static struct ONEXIT before_shmem_exit_list[MAX_ON_EXITS]
Definition: ipc.c:81
#define elog
Definition: elog.h:219
#define mkdir(a, b)
Definition: win32_port.h:58
static void atexit_callback(void)
Definition: ipc.c:284
#define S_IRWXO
Definition: win32_port.h:304