forked from man-brain/repl_mon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl_mon.c
349 lines (306 loc) · 10 KB
/
repl_mon.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*-------------------------------------------------------------------------
*
* repl_mon.c
* Store replication related information of a Postgres instance
* once in a while.
*
* Copyright (c) 1996-2022, PostgreSQL Global Development Group
*
* IDENTIFICATION
* repl_mon/repl_mon.c
*
*-------------------------------------------------------------------------
*/
/* Some general headers for custom bgworker facility */
#include "postgres.h"
#include "fmgr.h"
#include "access/xact.h"
#include "lib/stringinfo.h"
#include "pgstat.h"
#include "executor/spi.h"
#include "postmaster/bgworker.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/proc.h"
#include "utils/guc.h"
#include "utils/snapmgr.h"
/* Needed for getting hostname of the host */
#include "unistd.h"
/* Some OS lacks HOST_NAME_MAX */
#ifndef HOST_NAME_MAX
# if defined(_POSIX_HOST_NAME_MAX)
# define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
# elif defined(MAXHOSTNAMELEN)
# define HOST_NAME_MAX MAXHOSTNAMELEN
# endif
#endif /* HOST_NAME_MAX */
/* Allow load of this module in shared libs */
PG_MODULE_MAGIC;
/* Entry point of library loading */
void _PG_init(void);
#if PG_VERSION_NUM >= 100000
void repl_mon_main(Datum) pg_attribute_noreturn();
#endif
/* Signal handling */
static volatile sig_atomic_t got_sigterm = false;
static volatile sig_atomic_t got_sighup = false;
/* GUC variables */
static int interval = 1000;
static char *tablename = "repl_mon";
/* Worker name */
static char *worker_name = "repl_mon";
static char hostname[HOST_NAME_MAX];
#if PG_VERSION_NUM >= 100000
static char *get_current_lsn = "pg_current_wal_lsn()";
#else
static char *get_current_lsn = "pg_current_xlog_location()";
#endif
static void
repl_mon_sigterm(SIGNAL_ARGS)
{
int save_errno = errno;
got_sigterm = true;
if (MyProc)
SetLatch(&MyProc->procLatch);
errno = save_errno;
}
static void
repl_mon_sighup(SIGNAL_ARGS)
{
int save_errno = errno;
got_sighup = true;
if (MyProc)
SetLatch(&MyProc->procLatch);
errno = save_errno;
}
static void
repl_mon_prepare_queries()
{
SetCurrentStatementStartTimestamp();
StartTransactionCommand();
SPI_connect();
PushActiveSnapshot(GetTransactionSnapshot());
SetCurrentStatementStartTimestamp();
}
static void
repl_mon_finish_queries()
{
SPI_finish();
PopActiveSnapshot();
CommitTransactionCommand();
pgstat_report_activity(STATE_IDLE, NULL);
/*
* Send statistic about updated table to stats collector
* to prevent bloating table. If we don't do this, we
* will not know about number of dead tuples in table.
* Consequently, autovacuum will not come.
*/
pgstat_report_stat(false);
}
static void
repl_mon_init()
{
int ret;
StringInfoData buf;
if (gethostname(hostname, sizeof hostname) != 0)
elog(FATAL, "Error while trying to get hostname");
repl_mon_prepare_queries();
/* Creating table if it does not exist */
initStringInfo(&buf);
appendStringInfo(&buf, "SELECT * FROM pg_catalog.pg_tables "
"WHERE schemaname = 'public' AND tablename = '%s'", tablename);
pgstat_report_activity(STATE_RUNNING, buf.data);
ret = SPI_execute(buf.data, true, 1);
if (ret != SPI_OK_SELECT)
elog(FATAL, "Error while trying to get info about table");
if (SPI_processed == 0)
{
initStringInfo(&buf);
appendStringInfo(&buf, "CREATE TABLE public.%s ("
"ts timestamp with time zone,"
"location text, replics int,"
"master text);", tablename);
pgstat_report_activity(STATE_RUNNING, buf.data);
ret = SPI_execute(buf.data, false, 0);
if (ret != SPI_OK_UTILITY)
elog(FATAL, "Error while creating table");
}
/* Make repl_mon settings visible on replicas */
initStringInfo(&buf);
appendStringInfo(&buf, "CREATE TABLE IF NOT EXISTS public.%s_settings ("
"key TEXT PRIMARY KEY,"
"value TEXT NOT NULL)", tablename);
pgstat_report_activity(STATE_RUNNING, buf.data);
ret = SPI_execute(buf.data, false, 0);
if (ret != SPI_OK_UTILITY)
elog(FATAL, "Error while creating setting table");
initStringInfo(&buf);
appendStringInfo(&buf, "UPDATE public.%s_settings SET value = '%d' WHERE key = '%s'",
tablename, interval, "interval");
pgstat_report_activity(STATE_RUNNING, buf.data);
ret = SPI_execute(buf.data, false, 0);
if (ret != SPI_OK_UPDATE)
elog(FATAL, "Error while updating settings table");
if (SPI_processed == 0)
{
initStringInfo(&buf);
appendStringInfo(&buf, "INSERT INTO public.%s_settings VALUES ('%s', '%d')",
tablename, "interval", interval);
pgstat_report_activity(STATE_RUNNING, buf.data);
ret = SPI_execute(buf.data, false, 0);
if (ret != SPI_OK_INSERT)
elog(FATAL, "Error while inserting into settings table");
}
repl_mon_finish_queries();
}
static void
repl_mon_update_data()
{
int ret;
StringInfoData buf;
repl_mon_prepare_queries();
initStringInfo(&buf);
appendStringInfo(&buf, "WITH repl AS ("
"SELECT count(*) AS cnt FROM pg_catalog.pg_stat_replication "
"WHERE state='streaming') UPDATE public.%s "
"SET ts = current_timestamp, location = %s, "
"replics = repl.cnt, master = '%s' FROM repl", tablename, get_current_lsn, hostname);
pgstat_report_activity(STATE_RUNNING, buf.data);
ret = SPI_execute(buf.data, false, 1);
if (ret != SPI_OK_UPDATE)
elog(FATAL, "Error while updating timestamp");
if (SPI_processed == 0)
{
initStringInfo(&buf);
appendStringInfo(&buf, "WITH repl AS ("
"SELECT count(*) AS cnt FROM pg_catalog.pg_stat_replication "
"WHERE state='streaming') INSERT INTO public.%s "
"SELECT current_timestamp, %s, "
"repl.cnt, '%s' FROM repl", tablename, get_current_lsn, hostname);
pgstat_report_activity(STATE_RUNNING, buf.data);
ret = SPI_execute(buf.data, false, 0);
if (ret != SPI_OK_INSERT)
elog(FATAL, "Error while inserting timestamp");
}
repl_mon_finish_queries();
}
#if PG_VERSION_NUM < 100000
static void
#else
void
#endif
repl_mon_main(Datum main_arg)
{
/* Register functions for SIGTERM/SIGHUP management */
pqsignal(SIGHUP, repl_mon_sighup);
pqsignal(SIGTERM, repl_mon_sigterm);
/* We're now ready to receive signals */
BackgroundWorkerUnblockSignals();
/* Connect to a database */
#if PG_VERSION_NUM < 110000
BackgroundWorkerInitializeConnection("postgres", NULL);
#else
BackgroundWorkerInitializeConnection("postgres", NULL, 0);
#endif
/* Creating table if it does not exist */
repl_mon_init();
while (!got_sigterm)
{
int rc;
int wait_interval = interval > 0 ? interval: 1000;
/* Wait necessary amount of time */
rc = WaitLatch(&MyProc->procLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
#if PG_VERSION_NUM < 100000
wait_interval);
#else
wait_interval, PG_WAIT_EXTENSION);
#endif
ResetLatch(&MyProc->procLatch);
/* Emergency bailout if postmaster has died */
if (rc & WL_POSTMASTER_DEATH)
proc_exit(1);
/* Process signals */
if (got_sighup)
{
/* Process config file */
ProcessConfigFile(PGC_SIGHUP);
got_sighup = false;
ereport(DEBUG1, (errmsg("bgworker repl_mon signal: processed SIGHUP")));
/* Recreate table if needed */
repl_mon_init();
}
if (got_sigterm)
{
/* Simply exit */
ereport(DEBUG1, (errmsg("bgworker repl_mon signal: processed SIGTERM")));
proc_exit(0);
}
/* Main work happens here */
if (interval > 0)
repl_mon_update_data();
}
/* No problems, so clean exit */
proc_exit(0);
}
static void
repl_mon_load_params(void)
{
DefineCustomIntVariable("repl_mon.interval",
"Time between writing timestamp (ms).",
"Default of 1s, max of 300s",
&interval,
1000,
0,
300000,
PGC_SIGHUP,
GUC_UNIT_MS,
NULL,
NULL,
NULL);
DefineCustomStringVariable("repl_mon.table",
"Name of the table (in schema public).",
"Default is repl_mon",
&tablename,
"repl_mon",
PGC_SIGHUP,
0,
NULL,
NULL,
NULL);
}
/*
* Entry point for worker loading
*/
void
_PG_init(void)
{
BackgroundWorker worker;
/* Add parameters */
repl_mon_load_params();
/* Worker parameter and registration */
MemSet(&worker, 0, sizeof(BackgroundWorker));
worker.bgw_flags = BGWORKER_SHMEM_ACCESS |
BGWORKER_BACKEND_DATABASE_CONNECTION;
/* Start only on master hosts after finishing crash recovery */
worker.bgw_start_time = BgWorkerStart_RecoveryFinished;
#if PG_VERSION_NUM < 100000
worker.bgw_main = repl_mon_main;
#endif
snprintf(worker.bgw_name, BGW_MAXLEN, "%s", worker_name);
#if PG_VERSION_NUM >= 100000
sprintf(worker.bgw_library_name, "repl_mon");
sprintf(worker.bgw_function_name, "repl_mon_main");
#endif
/* Wait 10 seconds for restart after crash */
worker.bgw_restart_time = 10;
worker.bgw_main_arg = (Datum) 0;
#if PG_VERSION_NUM >= 90400
/*
* Notify PID is present since 9.4. If this is not initialized
* a static background worker cannot start properly.
*/
worker.bgw_notify_pid = 0;
#endif
RegisterBackgroundWorker(&worker);
}