PostgreSQL Source Code git master
ipc.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * ipc.h
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-2025, PostgreSQL Global Development Group
12 * Portions Copyright (c) 1994, Regents of the University of California
13 *
14 * src/include/storage/ipc.h
15 *
16 *-------------------------------------------------------------------------
17 */
18#ifndef IPC_H
19#define IPC_H
20
21typedef void (*pg_on_exit_callback) (int code, Datum arg);
22typedef void (*shmem_startup_hook_type) (void);
23
24/*----------
25 * API for handling cleanup that must occur during either ereport(ERROR)
26 * or ereport(FATAL) exits from a block of code. (Typical examples are
27 * undoing transient changes to shared-memory state.)
28 *
29 * PG_ENSURE_ERROR_CLEANUP(cleanup_function, arg);
30 * {
31 * ... code that might throw ereport(ERROR) or ereport(FATAL) ...
32 * }
33 * PG_END_ENSURE_ERROR_CLEANUP(cleanup_function, arg);
34 *
35 * where the cleanup code is in a function declared per pg_on_exit_callback.
36 * The Datum value "arg" can carry any information the cleanup function
37 * needs.
38 *
39 * This construct ensures that cleanup_function() will be called during
40 * either ERROR or FATAL exits. It will not be called on successful
41 * exit from the controlled code. (If you want it to happen then too,
42 * call the function yourself from just after the construct.)
43 *
44 * Note: the macro arguments are multiply evaluated, so avoid side-effects.
45 *----------
46 */
47#define PG_ENSURE_ERROR_CLEANUP(cleanup_function, arg) \
48 do { \
49 before_shmem_exit(cleanup_function, arg); \
50 PG_TRY()
51
52#define PG_END_ENSURE_ERROR_CLEANUP(cleanup_function, arg) \
53 cancel_before_shmem_exit(cleanup_function, arg); \
54 PG_CATCH(); \
55 { \
56 cancel_before_shmem_exit(cleanup_function, arg); \
57 cleanup_function (0, arg); \
58 PG_RE_THROW(); \
59 } \
60 PG_END_TRY(); \
61 } while (0)
62
63
64/* ipc.c */
67
68extern void proc_exit(int code) pg_attribute_noreturn();
69extern void shmem_exit(int code);
74extern void on_exit_reset(void);
76
77/* ipci.c */
79
80extern Size CalculateShmemSize(int *num_semaphores);
81extern void CreateSharedMemoryAndSemaphores(void);
82#ifdef EXEC_BACKEND
83extern void AttachSharedMemoryStructs(void);
84#endif
85extern void InitializeShmemGUCs(void);
86
87#endif /* IPC_H */
#define PGDLLIMPORT
Definition: c.h:1277
#define pg_attribute_noreturn()
Definition: c.h:239
size_t Size
Definition: c.h:562
void check_on_shmem_exit_lists_are_empty(void)
Definition: ipc.c:432
PGDLLIMPORT bool shmem_exit_inprogress
Definition: ipc.c:45
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:365
void on_proc_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:309
void cancel_before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:394
void(* shmem_startup_hook_type)(void)
Definition: ipc.h:22
void(* pg_on_exit_callback)(int code, Datum arg)
Definition: ipc.h:21
void shmem_exit(int code)
Definition: ipc.c:228
PGDLLIMPORT shmem_startup_hook_type shmem_startup_hook
Definition: ipci.c:57
void proc_exit(int code) pg_attribute_noreturn()
Definition: ipc.c:104
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:337
Size CalculateShmemSize(int *num_semaphores)
Definition: ipci.c:88
void InitializeShmemGUCs(void)
Definition: ipci.c:352
void on_exit_reset(void)
Definition: ipc.c:416
void CreateSharedMemoryAndSemaphores(void)
Definition: ipci.c:198
PGDLLIMPORT bool proc_exit_inprogress
Definition: ipc.c:40
on_exit_nicely_callback function
void * arg
uintptr_t Datum
Definition: postgres.h:69