Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "postgres.h"
00016
00017 #include <signal.h>
00018 #include <unistd.h>
00019
00020 #include "miscadmin.h"
00021 #include "postmaster/postmaster.h"
00022 #include "replication/walsender.h"
00023 #include "storage/pmsignal.h"
00024 #include "storage/shmem.h"
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 #define PM_CHILD_UNUSED 0
00057 #define PM_CHILD_ASSIGNED 1
00058 #define PM_CHILD_ACTIVE 2
00059 #define PM_CHILD_WALSENDER 3
00060
00061
00062 struct PMSignalData
00063 {
00064
00065 sig_atomic_t PMSignalFlags[NUM_PMSIGNALS];
00066
00067 int num_child_flags;
00068 int next_child_flag;
00069 sig_atomic_t PMChildFlags[1];
00070 };
00071
00072 NON_EXEC_STATIC volatile PMSignalData *PMSignalState = NULL;
00073
00074
00075
00076
00077
00078
00079 Size
00080 PMSignalShmemSize(void)
00081 {
00082 Size size;
00083
00084 size = offsetof(PMSignalData, PMChildFlags);
00085 size = add_size(size, mul_size(MaxLivePostmasterChildren(),
00086 sizeof(sig_atomic_t)));
00087
00088 return size;
00089 }
00090
00091
00092
00093
00094 void
00095 PMSignalShmemInit(void)
00096 {
00097 bool found;
00098
00099 PMSignalState = (PMSignalData *)
00100 ShmemInitStruct("PMSignalState", PMSignalShmemSize(), &found);
00101
00102 if (!found)
00103 {
00104 MemSet(PMSignalState, 0, PMSignalShmemSize());
00105 PMSignalState->num_child_flags = MaxLivePostmasterChildren();
00106 }
00107 }
00108
00109
00110
00111
00112 void
00113 SendPostmasterSignal(PMSignalReason reason)
00114 {
00115
00116 if (!IsUnderPostmaster)
00117 return;
00118
00119 PMSignalState->PMSignalFlags[reason] = true;
00120
00121 kill(PostmasterPid, SIGUSR1);
00122 }
00123
00124
00125
00126
00127
00128
00129 bool
00130 CheckPostmasterSignal(PMSignalReason reason)
00131 {
00132
00133 if (PMSignalState->PMSignalFlags[reason])
00134 {
00135 PMSignalState->PMSignalFlags[reason] = false;
00136 return true;
00137 }
00138 return false;
00139 }
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 int
00151 AssignPostmasterChildSlot(void)
00152 {
00153 int slot = PMSignalState->next_child_flag;
00154 int n;
00155
00156
00157
00158
00159
00160 for (n = PMSignalState->num_child_flags; n > 0; n--)
00161 {
00162 if (--slot < 0)
00163 slot = PMSignalState->num_child_flags - 1;
00164 if (PMSignalState->PMChildFlags[slot] == PM_CHILD_UNUSED)
00165 {
00166 PMSignalState->PMChildFlags[slot] = PM_CHILD_ASSIGNED;
00167 PMSignalState->next_child_flag = slot;
00168 return slot + 1;
00169 }
00170 }
00171
00172
00173 elog(FATAL, "no free slots in PMChildFlags array");
00174 return 0;
00175 }
00176
00177
00178
00179
00180
00181
00182
00183
00184 bool
00185 ReleasePostmasterChildSlot(int slot)
00186 {
00187 bool result;
00188
00189 Assert(slot > 0 && slot <= PMSignalState->num_child_flags);
00190 slot--;
00191
00192
00193
00194
00195
00196
00197 result = (PMSignalState->PMChildFlags[slot] == PM_CHILD_ASSIGNED);
00198 PMSignalState->PMChildFlags[slot] = PM_CHILD_UNUSED;
00199 return result;
00200 }
00201
00202
00203
00204
00205
00206 bool
00207 IsPostmasterChildWalSender(int slot)
00208 {
00209 Assert(slot > 0 && slot <= PMSignalState->num_child_flags);
00210 slot--;
00211
00212 if (PMSignalState->PMChildFlags[slot] == PM_CHILD_WALSENDER)
00213 return true;
00214 else
00215 return false;
00216 }
00217
00218
00219
00220
00221
00222 void
00223 MarkPostmasterChildActive(void)
00224 {
00225 int slot = MyPMChildSlot;
00226
00227 Assert(slot > 0 && slot <= PMSignalState->num_child_flags);
00228 slot--;
00229 Assert(PMSignalState->PMChildFlags[slot] == PM_CHILD_ASSIGNED);
00230 PMSignalState->PMChildFlags[slot] = PM_CHILD_ACTIVE;
00231 }
00232
00233
00234
00235
00236
00237
00238 void
00239 MarkPostmasterChildWalSender(void)
00240 {
00241 int slot = MyPMChildSlot;
00242
00243 Assert(am_walsender);
00244
00245 Assert(slot > 0 && slot <= PMSignalState->num_child_flags);
00246 slot--;
00247 Assert(PMSignalState->PMChildFlags[slot] == PM_CHILD_ACTIVE);
00248 PMSignalState->PMChildFlags[slot] = PM_CHILD_WALSENDER;
00249 }
00250
00251
00252
00253
00254
00255 void
00256 MarkPostmasterChildInactive(void)
00257 {
00258 int slot = MyPMChildSlot;
00259
00260 Assert(slot > 0 && slot <= PMSignalState->num_child_flags);
00261 slot--;
00262 Assert(PMSignalState->PMChildFlags[slot] == PM_CHILD_ACTIVE ||
00263 PMSignalState->PMChildFlags[slot] == PM_CHILD_WALSENDER);
00264 PMSignalState->PMChildFlags[slot] = PM_CHILD_ASSIGNED;
00265 }
00266
00267
00268
00269
00270
00271 bool
00272 PostmasterIsAlive(void)
00273 {
00274 #ifndef WIN32
00275 char c;
00276 ssize_t rc;
00277
00278 rc = read(postmaster_alive_fds[POSTMASTER_FD_WATCH], &c, 1);
00279 if (rc < 0)
00280 {
00281 if (errno == EAGAIN || errno == EWOULDBLOCK)
00282 return true;
00283 else
00284 elog(FATAL, "read on postmaster death monitoring pipe failed: %m");
00285 }
00286 else if (rc > 0)
00287 elog(FATAL, "unexpected data in postmaster death monitoring pipe");
00288
00289 return false;
00290 #else
00291 return (WaitForSingleObject(PostmasterHandle, 0) == WAIT_TIMEOUT);
00292 #endif
00293 }