-
Notifications
You must be signed in to change notification settings - Fork 0
/
walker.c
292 lines (246 loc) · 6.38 KB
/
walker.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
/*-------------------------------------------------------------------------
*
* walker.c - WAL WALker, a simple and pluggable background worker
*
* Copyright (c) 2013-2017, PostgreSQL Global Development Group
*
* IDENTIFICATION
* walker/walker.c
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "walker.h"
/* These are always necessary for a bgworker */
#include "access/xlog.h"
#include "access/xlogreader.h"
#include "access/xlogrecord.h"
#include "access/xlog_internal.h"
#include "access/xlogutils.h"
#include "access/transam.h"
#include "access/heapam_xlog.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/bgworker.h"
#include "replication/syncrep.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/proc.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
#include "utils/palloc.h"
#include "utils/varlena.h"
PG_MODULE_MAGIC;
/* WALker state data */
typedef struct WALkerStateData
{
List *plugins; /* List of WALkerCallbacks */
} WALkerStateData;
void _PG_init(void);
void WALkerMain(Datum main_arg);
static void WALkerProcessRecord(XLogReaderState *record);
/* Pointer to state data */
static struct WALkerStateData *WALkerState;
/* GUC parameter */
static char *walker_plugins; /* comma-separated plugin list */
/* flags set by signal handlers */
sig_atomic_t got_sighup = false;
/*
* Signal handler for SIGHUP.
*/
static void
walker_sighup(SIGNAL_ARGS)
{
got_sighup = true;
if (MyProc)
SetLatch(&MyProc->procLatch);
}
/*
* Initialize.
*/
void
_PG_init(void)
{
BackgroundWorker worker;
if (!process_shared_preload_libraries_in_progress)
return;
DefineCustomStringVariable("walker.plugins",
"comma-separated plugin names",
NULL,
&walker_plugins,
NULL,
PGC_POSTMASTER,
0,
NULL,
NULL,
NULL);
worker.bgw_flags = BGWORKER_SHMEM_ACCESS |
BGWORKER_BACKEND_DATABASE_CONNECTION;
worker.bgw_start_time = BgWorkerStart_ConsistentState;
worker.bgw_restart_time = BGW_NEVER_RESTART;
strcpy(worker.bgw_library_name, "walker");
strcpy(worker.bgw_function_name, "WALkerMain");
worker.bgw_notify_pid = 0;
snprintf(worker.bgw_name, BGW_MAXLEN, "walker");
worker.bgw_main_arg = Int32GetDatum(1);
RegisterBackgroundWorker(&worker);
}
/*
* Initialize WALker's space. Also we load all given plugins here.
* If the loaded plugin support startup callback, we invoke it here.
*/
static void
WALkerInit(void)
{
MemoryContext ctx;
List *plugin_list;
ListCell *cell;
if (walker_plugins == NULL || walker_plugins[0] == '\0')
return;
if (!SplitIdentifierString(walker_plugins, ',', &plugin_list))
elog(ERROR, "plugin syntax is invalid");
ctx = MemoryContextSwitchTo(TopMemoryContext);
/* Initialize global variables */
WALkerState = (WALkerStateData *) palloc(sizeof(WALkerStateData));
WALkerState->plugins = NIL;
/* Iterate over all plugin names */
foreach (cell, plugin_list)
{
char *plugin_name = (char *) lfirst(cell);
WALkerPluginInit plugin_init;
WALkerCallbacks *callbacks;
callbacks = (WALkerCallbacks *) palloc(sizeof(WALkerCallbacks));
plugin_init = (WALkerPluginInit)
load_external_function(plugin_name, "_PG_walker_plugin_init", false, NULL);
if (plugin_init == NULL)
elog(ERROR, "output plugins have to declare the _PG_walker_plugin_init symbol");
/* Call plugin's init function for walker */
plugin_init(callbacks);
/*
* We don't check any existing check for callbacks because all callbacks
* are optional.
*/
/* Invoke startup callback if it's provided */
if (callbacks->startup_cb)
callbacks->startup_cb();
/* Add to plugin list */
WALkerState->plugins = lappend(WALkerState->plugins, callbacks);
}
MemoryContextSwitchTo(ctx);
}
/*
* Entry point of walker background worker process.
*/
void
WALkerMain(Datum main_arg)
{
XLogReaderState *xlogreader_state;
XLogRecPtr lsn = GetFlushRecPtr();
XLogRecord *record;
char *errmsg;
pqsignal(SIGHUP, walker_sighup);
pqsignal(SIGTERM, die);
/* Initialize walker plugins */
WALkerInit();
/* We're now ready to receive signals */
BackgroundWorkerUnblockSignals();
/* Connect to our database */
BackgroundWorkerInitializeConnection("postgres", NULL);
#if PG_VERSION_NUM >= 110000
xlogreader_state = XLogReaderAllocate(wal_segment_size, &read_local_xlog_page,
NULL);
#else
xlogreader_state = XLogReaderAllocate(&read_local_xlog_page, NULL);
#endif
if (!xlogreader_state)
elog(ERROR, "failed to allocate xlog reader");
/* Loop until get SIGTERM */
for (;;)
{
CHECK_FOR_INTERRUPTS();
/* Got SIGHUP, read configuration file */
if (got_sighup)
{
got_sighup = false;
ProcessConfigFile(PGC_SIGHUP);
}
/*
* Read a record. Wait for new record if it is not generated yet.
*/
record = XLogReadRecord(xlogreader_state, lsn, &errmsg);
lsn = InvalidXLogRecPtr;
if (record == NULL)
elog(ERROR, "could not read WAL at %X/%X",
(uint32) (lsn >> 32), (uint32) lsn);
WALkerProcessRecord(xlogreader_state);
}
}
/*
* Process read WAL record and invoke corresponding callbacks.
* All callbacks are optional.
*/
static void
WALkerProcessRecord(XLogReaderState *record)
{
ListCell *cell;
foreach (cell, WALkerState->plugins)
{
WALkerCallbacks *cb = (WALkerCallbacks *) lfirst(cell);
/* cast so we get a warning when new rmgrs are added */
switch ((RmgrIds) XLogRecGetRmid(record))
{
case RM_HEAP2_ID:
{
if (cb->heap2_cb)
cb->heap2_cb(record);
}
break;
case RM_HEAP_ID:
{
if (cb->heap_cb)
cb->heap_cb(record);
}
break;
case RM_XLOG_ID:
{
if (cb->xlog_cb)
cb->xlog_cb(record);
}
break;
case RM_XACT_ID:
{
if (cb->xact_cb)
cb->xact_cb(record);
}
break;
case RM_SMGR_ID:
{
if (cb->smgr_cb)
cb->smgr_cb(record);
}
break;
/* Not support yet */
case RM_STANDBY_ID:
case RM_LOGICALMSG_ID:
case RM_CLOG_ID:
case RM_DBASE_ID:
case RM_TBLSPC_ID:
case RM_MULTIXACT_ID:
case RM_RELMAP_ID:
case RM_BTREE_ID:
case RM_HASH_ID:
case RM_GIN_ID:
case RM_GIST_ID:
case RM_SEQ_ID:
case RM_SPGIST_ID:
case RM_BRIN_ID:
case RM_COMMIT_TS_ID:
case RM_REPLORIGIN_ID:
case RM_GENERIC_ID:
break;
case RM_NEXT_ID:
elog(ERROR, "unexpected RM_NEXT_ID rmgr_id: %u", (RmgrIds) XLogRecGetRmid(record));
}
}
}