-
Notifications
You must be signed in to change notification settings - Fork 1
/
pg_querylog.c
378 lines (325 loc) · 8.87 KB
/
pg_querylog.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* pg_querylog.c
* PostgreSQL running queries viewer
*/
#include "postgres.h"
#include "access/xact.h"
#include "fmgr.h"
#include "libpq/libpq-be.h"
#include "miscadmin.h"
#include "postmaster/autovacuum.h"
#include "storage/dsm.h"
#include "storage/ipc.h"
#include "storage/shm_mq.h"
#include "storage/shm_toc.h"
#include "string.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/ps_status.h"
#include "executor/executor.h"
#include "catalog/pg_type.h"
#include "lib/stringinfo.h"
#include "utils/lsyscache.h"
#include "pg_querylog.h"
PG_MODULE_MAGIC;
void _PG_init(void);
void _PG_fini(void);
/* global variables */
static int buffer_size_setting = 0;
static shm_toc *toc = NULL;
BacklogDataHdr *pgl_shared_hdr = NULL;
CollectedQuery *pgl_shared_queries = NULL;
char *pgl_shared_buffer = NULL; /* the whole buffer */
static bool shmem_initialized = false;
/* local */
static CollectedQuery *backend_query = NULL;
static bool using_dsm = false;
static dsm_segment *seg = NULL;
static ExecutorStart_hook_type pg_querylog_executor_start_hook_next = NULL;
static ExecutorEnd_hook_type pg_querylog_executor_end_hook_next = NULL;
#define safe_strlen(s) ((s) ? strlen(s) : 0)
/* handles error cases */
static void
xact_finish_callback(XactEvent event, void *arg)
{
if (backend_query && backend_query->running)
backend_query->running = false;
}
static void
setup_gucs(bool basic)
{
if (basic)
{
DefineCustomIntVariable(
"pg_querylog.buffer_size",
"Buffer size for each query", NULL,
&buffer_size_setting,
10, /* 10KB */
10,
INT_MAX,
PGC_SUSET,
GUC_UNIT_KB,
NULL, NULL, NULL
);
}
else
{
DefineCustomBoolVariable(
"pg_querylog.enabled",
"Enable logging queries", NULL,
&pgl_shared_hdr->enabled,
true,
PGC_SUSET,
0, NULL, NULL, NULL
);
}
}
/* copied from pg code */
static void
print_literal(StringInfo s, Oid typid, char *outputstr)
{
const char *valptr;
switch (typid)
{
case INT2OID:
case INT4OID:
case INT8OID:
case OIDOID:
case FLOAT4OID:
case FLOAT8OID:
case NUMERICOID:
/* NB: We don't care about Inf, NaN et al. */
appendStringInfoString(s, outputstr);
break;
case BITOID:
case VARBITOID:
appendStringInfo(s, "B'%s'", outputstr);
break;
case BOOLOID:
if (strcmp(outputstr, "t") == 0)
appendStringInfoString(s, "true");
else
appendStringInfoString(s, "false");
break;
default:
appendStringInfoChar(s, '\'');
for (valptr = outputstr; *valptr; valptr++)
{
char ch = *valptr;
if (SQL_STR_DOUBLE(ch, false))
appendStringInfoChar(s, ch);
appendStringInfoChar(s, ch);
}
appendStringInfoChar(s, '\'');
break;
}
}
static void
pg_querylog_executor_start_hook(QueryDesc *queryDesc, int eflags)
{
if (pgl_shared_hdr && pgl_shared_hdr->enabled)
{
char *itembuf;
if (!backend_query && MyBackendId)
{
backend_query = &pgl_shared_queries[MyBackendId - 1];
backend_query->magic = PG_QUERYLOG_ITEM_MAGIC;
backend_query->running = false;
backend_query->gen = 0;
pg_atomic_init_flag(&backend_query->is_free);
backend_query->pid = MyProcPid;
itembuf = QUERYBUF(pgl_shared_hdr, MyBackendId - 1);
memset(itembuf, 0, pgl_shared_hdr->bufsize);
pg_write_barrier();
}
if (backend_query)
{
StringInfoData data;
int i;
Assert(backend_query->magic = PG_QUERYLOG_ITEM_MAGIC);
initStringInfo(&data);
// mark is not free,
// we don't check for result here, since there could some
// error before clearing flag, and we don't want to depend
// on that.
pg_atomic_test_set_flag(&backend_query->is_free);
backend_query->gen++;
backend_query->running = true;
backend_query->start = GetCurrentTimestamp();
backend_query->end = 0;
appendStringInfoString(&data, queryDesc->sourceText);
backend_query->querylen = data.len;
itembuf = QUERYBUF(pgl_shared_hdr, MyBackendId - 1);
// collect params
if (queryDesc->params && data.len < pgl_shared_hdr->bufsize)
{
backend_query->params_offset = data.len;
for (i = 0; i < queryDesc->params->numParams; i++)
{
ParamExternData *param = &queryDesc->params->params[i];
if (param->isnull)
{
if (i > 0)
appendStringInfoChar(&data, ',');
appendStringInfoString(&data, "NULL");
}
else if (param->ptype != InvalidOid)
{
Oid typoutput; /* output function */
bool typisvarlena;
char *val;
getTypeOutputInfo(param->ptype, &typoutput, &typisvarlena);
val = OidOutputFunctionCall(typoutput, param->value);
if (i > 0)
appendStringInfoChar(&data, ',');
print_literal(&data, param->ptype, val);
pfree(val);
}
}
}
else
backend_query->params_offset = 0;
backend_query->datalen = data.len;
backend_query->overflow = (data.len >= pgl_shared_hdr->bufsize);
memcpy(itembuf, data.data, backend_query->overflow ?
pgl_shared_hdr->bufsize - 1: data.len + 1);
itembuf[pgl_shared_hdr->bufsize - 1] = '\0';
resetStringInfo(&data);
pg_atomic_clear_flag(&backend_query->is_free);
}
}
if (pg_querylog_executor_start_hook_next)
pg_querylog_executor_start_hook_next(queryDesc, eflags);
else
standard_ExecutorStart(queryDesc, eflags);
}
static void
pg_querylog_executor_end_hook(QueryDesc *queryDesc)
{
if (pgl_shared_hdr->enabled && backend_query)
{
pg_atomic_test_set_flag(&backend_query->is_free);
backend_query->gen++;
backend_query->end = GetCurrentTimestamp();
backend_query->running = false;
pg_atomic_clear_flag(&backend_query->is_free);
}
if (pg_querylog_executor_end_hook_next)
pg_querylog_executor_end_hook_next(queryDesc);
else
standard_ExecutorEnd(queryDesc);
}
static void
install_hooks()
{
pg_querylog_executor_start_hook_next = ExecutorStart_hook;
ExecutorStart_hook = pg_querylog_executor_start_hook;
pg_querylog_executor_end_hook_next = ExecutorEnd_hook;
ExecutorEnd_hook = pg_querylog_executor_end_hook;
}
static void
uninstall_hooks(void)
{
ExecutorStart_hook = pg_querylog_executor_start_hook_next;
ExecutorEnd_hook = pg_querylog_executor_end_hook_next;
}
static Size
calculate_shmem_size(int bufsize)
{
shm_toc_estimator e;
Size size;
Assert(bufsize != 0);
shm_toc_initialize_estimator(&e);
shm_toc_estimate_chunk(&e, sizeof(BacklogDataHdr));
shm_toc_estimate_chunk(&e, sizeof(CollectedQuery) * MaxBackends);
shm_toc_estimate_chunk(&e, bufsize * MaxBackends);
shm_toc_estimate_keys(&e, 3);
size = shm_toc_estimate(&e);
return size;
}
static void
setup_buffers(Size segsize, Size bufsize, void *addr)
{
toc = shm_toc_create(PG_QUERYLOG_MAGIC, addr, segsize);
pgl_shared_hdr = shm_toc_allocate(toc, sizeof(BacklogDataHdr));
pgl_shared_hdr->count = MaxBackends;
pgl_shared_hdr->bufsize = bufsize;
pgl_shared_queries = shm_toc_allocate(toc, sizeof(CollectedQuery) * pgl_shared_hdr->count);
pgl_shared_buffer = shm_toc_allocate(toc, bufsize * pgl_shared_hdr->count);
shm_toc_insert(toc, 0, pgl_shared_hdr);
shm_toc_insert(toc, 1, pgl_shared_queries);
shm_toc_insert(toc, 2, pgl_shared_buffer);
memset(pgl_shared_queries, 0, sizeof(CollectedQuery) * pgl_shared_hdr->count);
memset(pgl_shared_buffer, 0, bufsize * pgl_shared_hdr->count);
}
/*
* Module load callback
*/
void
_PG_init(void)
{
void *addr;
bool found;
Size bufsize,
segsize;
setup_gucs(true);
bufsize = MAXALIGN(buffer_size_setting * 1024);
segsize = calculate_shmem_size(bufsize);
if (MyProcPort != NULL)
{
// we're going different way, using DSM to store our data, and saving
// dsm pointer in shmem.
// that's not a good way but we know that shared memory has some
// space at the end which we can use here
LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
addr = ShmemInitStruct("pg_querylog dsm", sizeof(dsm_handle), &found);
if (found)
{
seg = dsm_attach(*((dsm_handle *) addr));
if (seg == NULL)
{
elog(LOG, "pg_querylog: could not find dsm segment");
return;
}
dsm_pin_mapping(seg);
addr = dsm_segment_address(seg);
toc = shm_toc_attach(PG_QUERYLOG_MAGIC, addr);
pgl_shared_hdr = shm_toc_lookup(toc, 0, false);
pgl_shared_queries = shm_toc_lookup(toc, 1, false);
pgl_shared_buffer = shm_toc_lookup(toc, 2, false);
} else {
seg = dsm_create(segsize, DSM_CREATE_NULL_IF_MAXSEGMENTS);
if (seg == NULL)
{
elog(LOG, "pg_querylog: could not create dsm segment");
return;
}
*((dsm_handle *) addr) = dsm_segment_handle(seg);
addr = dsm_segment_address(seg);
dsm_pin_segment(seg);
dsm_pin_mapping(seg);
setup_buffers(segsize, bufsize, addr);
elog(LOG, "pg_querylog initialized");
}
LWLockRelease(AddinShmemInitLock);
using_dsm = true;
shmem_initialized = true;
install_hooks(false);
setup_gucs(false);
}
RegisterXactCallback(xact_finish_callback, NULL);
}
/*
* Module unload callback
*/
void
_PG_fini(void)
{
if (shmem_initialized)
{
uninstall_hooks();
if (seg)
dsm_detach(seg);
}
}