PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 "storage/lwlock.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)
 
void check_on_shmem_exit_lists_are_empty (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 72 of file ipc.c.

Function Documentation

◆ atexit_callback()

static void atexit_callback ( void  )
static

Definition at line 301 of file ipc.c.

302{
303 /* Clean up everything that must be cleaned up */
304 /* ... too bad we don't know the real exit code ... */
306}
static void proc_exit_prepare(int code)
Definition ipc.c:166

References proc_exit_prepare().

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

◆ before_shmem_exit()

void before_shmem_exit ( pg_on_exit_callback  function,
Datum  arg 
)

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 errmsg_internal(const char *fmt,...)
Definition elog.c:1170
int errcode(int sqlerrcode)
Definition elog.c:863
#define FATAL
Definition elog.h:41
#define ereport(elevel,...)
Definition elog.h:150
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
void * arg
static int fb(int x)
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(), pgaio_init_backend(), pgstat_initialize(), register_persistent_abort_backup_handler(), ReplicationSlotInitialize(), and ReplSlotSyncWorkerMain().

◆ cancel_before_shmem_exit()

void cancel_before_shmem_exit ( pg_on_exit_callback  function,
Datum  arg 
)

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

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  )

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().

◆ on_exit_reset()

void on_exit_reset ( void  )

Definition at line 423 of file ipc.c.

424{
429}
void reset_on_dsm_detach(void)
Definition dsm.c:1170
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()

void proc_exit ( int  code)

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 DEBUG3
Definition elog.h:28
#define PANIC
Definition elog.h:42
int MyProcPid
Definition globals.c:47
#define AmAutoVacuumWorkerProcess()
Definition miscadmin.h:383
#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(), CheckerModeMain(), CheckpointerMain(), DisableSubscriptionAndExit(), errfinish(), ExitPostmaster(), FinishSyncWorker(), InitializeLogRepWorker(), IoWorkerMain(), maybe_reread_subscription(), PerformWalRecovery(), PgArchiverMain(), PostgresMain(), PostgresSingleUserMain(), PreRestoreCommand(), ProcessInterrupts(), ProcessMainLoopInterrupts(), ProcessParallelApplyInterrupts(), ProcessPgArchInterrupts(), ProcessRepliesIfAny(), ProcessSlotSyncInterrupts(), ProcessStandbyMessage(), ProcessStartupProcInterrupts(), ProcessSyncingTablesForApply(), ProcessWalSummarizerInterrupts(), ReplSlotSyncWorkerMain(), RestoreArchivedFile(), slotsync_reread_config(), StartLogicalReplication(), StartReplication(), StartupProcessMain(), StartupProcShutdownHandler(), SysLoggerMain(), test_shm_mq_main(), WaitEventSetWaitBlock(), WalRcvWaitForStartPosition(), WalReceiverMain(), WalSndDone(), WalSndErrorCleanup(), WalSndShutdown(), WalSndWait(), and WalSummarizerMain().

◆ proc_exit_prepare()

static void proc_exit_prepare ( int  code)
static

Definition at line 166 of file ipc.c.

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

References arg, CritSectionCount, DEBUG3, debug_query_string, elog, error_context_stack, fb(), 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().

◆ shmem_exit()

void shmem_exit ( int  code)

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

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

◆ atexit_callback_setup

bool atexit_callback_setup = false
static

Definition at line 52 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

◆ before_shmem_exit_list

struct ONEXIT before_shmem_exit_list[MAX_ON_EXITS]
static

Definition at line 82 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 84 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 80 of file ipc.c.

Referenced by on_proc_exit(), and proc_exit_prepare().

◆ on_shmem_exit_index

int on_shmem_exit_index
static

◆ on_shmem_exit_list

struct ONEXIT on_shmem_exit_list[MAX_ON_EXITS]
static

Definition at line 81 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 46 of file ipc.c.

Referenced by AtAbort_Portals(), and shmem_exit().