]> granicus.if.org Git - postgresql/blob - src/backend/storage/ipc/procsignal.c
Allow discovery of whether a dynamic background worker is running.
[postgresql] / src / backend / storage / ipc / procsignal.c
1 /*-------------------------------------------------------------------------
2  *
3  * procsignal.c
4  *        Routines for interprocess signalling
5  *
6  *
7  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * IDENTIFICATION
11  *        src/backend/storage/ipc/procsignal.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include <signal.h>
18 #include <unistd.h>
19
20 #include "commands/async.h"
21 #include "miscadmin.h"
22 #include "storage/latch.h"
23 #include "storage/ipc.h"
24 #include "storage/proc.h"
25 #include "storage/shmem.h"
26 #include "storage/sinval.h"
27 #include "tcop/tcopprot.h"
28
29
30 /*
31  * The SIGUSR1 signal is multiplexed to support signalling multiple event
32  * types. The specific reason is communicated via flags in shared memory.
33  * We keep a boolean flag for each possible "reason", so that different
34  * reasons can be signaled to a process concurrently.  (However, if the same
35  * reason is signaled more than once nearly simultaneously, the process may
36  * observe it only once.)
37  *
38  * Each process that wants to receive signals registers its process ID
39  * in the ProcSignalSlots array. The array is indexed by backend ID to make
40  * slot allocation simple, and to avoid having to search the array when you
41  * know the backend ID of the process you're signalling.  (We do support
42  * signalling without backend ID, but it's a bit less efficient.)
43  *
44  * The flags are actually declared as "volatile sig_atomic_t" for maximum
45  * portability.  This should ensure that loads and stores of the flag
46  * values are atomic, allowing us to dispense with any explicit locking.
47  */
48 typedef struct
49 {
50         pid_t           pss_pid;
51         sig_atomic_t pss_signalFlags[NUM_PROCSIGNALS];
52 } ProcSignalSlot;
53
54 /*
55  * We reserve a slot for each possible BackendId, plus one for each
56  * possible auxiliary process type.  (This scheme assumes there is not
57  * more than one of any auxiliary process type at a time.)
58  */
59 #define NumProcSignalSlots      (MaxBackends + NUM_AUXPROCTYPES)
60
61 /*
62  * If this flag is set, the process latch will be set whenever SIGUSR1
63  * is received.  This is useful when waiting for a signal from the postmaster.
64  * Spurious wakeups must be expected.  Make sure that the flag is cleared
65  * in the error path.
66  */
67 bool set_latch_on_sigusr1;
68
69 static ProcSignalSlot *ProcSignalSlots = NULL;
70 static volatile ProcSignalSlot *MyProcSignalSlot = NULL;
71
72 static bool CheckProcSignal(ProcSignalReason reason);
73 static void CleanupProcSignalState(int status, Datum arg);
74
75 /*
76  * ProcSignalShmemSize
77  *              Compute space needed for procsignal's shared memory
78  */
79 Size
80 ProcSignalShmemSize(void)
81 {
82         return NumProcSignalSlots * sizeof(ProcSignalSlot);
83 }
84
85 /*
86  * ProcSignalShmemInit
87  *              Allocate and initialize procsignal's shared memory
88  */
89 void
90 ProcSignalShmemInit(void)
91 {
92         Size            size = ProcSignalShmemSize();
93         bool            found;
94
95         ProcSignalSlots = (ProcSignalSlot *)
96                 ShmemInitStruct("ProcSignalSlots", size, &found);
97
98         /* If we're first, set everything to zeroes */
99         if (!found)
100                 MemSet(ProcSignalSlots, 0, size);
101 }
102
103 /*
104  * ProcSignalInit
105  *              Register the current process in the procsignal array
106  *
107  * The passed index should be my BackendId if the process has one,
108  * or MaxBackends + aux process type if not.
109  */
110 void
111 ProcSignalInit(int pss_idx)
112 {
113         volatile ProcSignalSlot *slot;
114
115         Assert(pss_idx >= 1 && pss_idx <= NumProcSignalSlots);
116
117         slot = &ProcSignalSlots[pss_idx - 1];
118
119         /* sanity check */
120         if (slot->pss_pid != 0)
121                 elog(LOG, "process %d taking over ProcSignal slot %d, but it's not empty",
122                          MyProcPid, pss_idx);
123
124         /* Clear out any leftover signal reasons */
125         MemSet(slot->pss_signalFlags, 0, NUM_PROCSIGNALS * sizeof(sig_atomic_t));
126
127         /* Mark slot with my PID */
128         slot->pss_pid = MyProcPid;
129
130         /* Remember slot location for CheckProcSignal */
131         MyProcSignalSlot = slot;
132
133         /* Set up to release the slot on process exit */
134         on_shmem_exit(CleanupProcSignalState, Int32GetDatum(pss_idx));
135 }
136
137 /*
138  * CleanupProcSignalState
139  *              Remove current process from ProcSignalSlots
140  *
141  * This function is called via on_shmem_exit() during backend shutdown.
142  */
143 static void
144 CleanupProcSignalState(int status, Datum arg)
145 {
146         int                     pss_idx = DatumGetInt32(arg);
147         volatile ProcSignalSlot *slot;
148
149         slot = &ProcSignalSlots[pss_idx - 1];
150         Assert(slot == MyProcSignalSlot);
151
152         /* sanity check */
153         if (slot->pss_pid != MyProcPid)
154         {
155                 /*
156                  * don't ERROR here. We're exiting anyway, and don't want to get into
157                  * infinite loop trying to exit
158                  */
159                 elog(LOG, "process %d releasing ProcSignal slot %d, but it contains %d",
160                          MyProcPid, pss_idx, (int) slot->pss_pid);
161                 return;                                 /* XXX better to zero the slot anyway? */
162         }
163
164         slot->pss_pid = 0;
165 }
166
167 /*
168  * SendProcSignal
169  *              Send a signal to a Postgres process
170  *
171  * Providing backendId is optional, but it will speed up the operation.
172  *
173  * On success (a signal was sent), zero is returned.
174  * On error, -1 is returned, and errno is set (typically to ESRCH or EPERM).
175  *
176  * Not to be confused with ProcSendSignal
177  */
178 int
179 SendProcSignal(pid_t pid, ProcSignalReason reason, BackendId backendId)
180 {
181         volatile ProcSignalSlot *slot;
182
183         if (backendId != InvalidBackendId)
184         {
185                 slot = &ProcSignalSlots[backendId - 1];
186
187                 /*
188                  * Note: Since there's no locking, it's possible that the target
189                  * process detaches from shared memory and exits right after this
190                  * test, before we set the flag and send signal. And the signal slot
191                  * might even be recycled by a new process, so it's remotely possible
192                  * that we set a flag for a wrong process. That's OK, all the signals
193                  * are such that no harm is done if they're mistakenly fired.
194                  */
195                 if (slot->pss_pid == pid)
196                 {
197                         /* Atomically set the proper flag */
198                         slot->pss_signalFlags[reason] = true;
199                         /* Send signal */
200                         return kill(pid, SIGUSR1);
201                 }
202         }
203         else
204         {
205                 /*
206                  * BackendId not provided, so search the array using pid.  We search
207                  * the array back to front so as to reduce search overhead.  Passing
208                  * InvalidBackendId means that the target is most likely an auxiliary
209                  * process, which will have a slot near the end of the array.
210                  */
211                 int                     i;
212
213                 for (i = NumProcSignalSlots - 1; i >= 0; i--)
214                 {
215                         slot = &ProcSignalSlots[i];
216
217                         if (slot->pss_pid == pid)
218                         {
219                                 /* the above note about race conditions applies here too */
220
221                                 /* Atomically set the proper flag */
222                                 slot->pss_signalFlags[reason] = true;
223                                 /* Send signal */
224                                 return kill(pid, SIGUSR1);
225                         }
226                 }
227         }
228
229         errno = ESRCH;
230         return -1;
231 }
232
233 /*
234  * CheckProcSignal - check to see if a particular reason has been
235  * signaled, and clear the signal flag.  Should be called after receiving
236  * SIGUSR1.
237  */
238 static bool
239 CheckProcSignal(ProcSignalReason reason)
240 {
241         volatile ProcSignalSlot *slot = MyProcSignalSlot;
242
243         if (slot != NULL)
244         {
245                 /* Careful here --- don't clear flag if we haven't seen it set */
246                 if (slot->pss_signalFlags[reason])
247                 {
248                         slot->pss_signalFlags[reason] = false;
249                         return true;
250                 }
251         }
252
253         return false;
254 }
255
256 /*
257  * procsignal_sigusr1_handler - handle SIGUSR1 signal.
258  */
259 void
260 procsignal_sigusr1_handler(SIGNAL_ARGS)
261 {
262         int                     save_errno = errno;
263
264         if (CheckProcSignal(PROCSIG_CATCHUP_INTERRUPT))
265                 HandleCatchupInterrupt();
266
267         if (CheckProcSignal(PROCSIG_NOTIFY_INTERRUPT))
268                 HandleNotifyInterrupt();
269
270         if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_DATABASE))
271                 RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_DATABASE);
272
273         if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_TABLESPACE))
274                 RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_TABLESPACE);
275
276         if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_LOCK))
277                 RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_LOCK);
278
279         if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_SNAPSHOT))
280                 RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_SNAPSHOT);
281
282         if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK))
283                 RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK);
284
285         if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN))
286                 RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN);
287
288         if (set_latch_on_sigusr1)
289                 SetLatch(&MyProc->procLatch);
290
291         latch_sigusr1_handler();
292
293         errno = save_errno;
294 }