-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpg_get_queryid.c
More file actions
263 lines (227 loc) · 6.49 KB
/
pg_get_queryid.c
File metadata and controls
263 lines (227 loc) · 6.49 KB
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
/*-------------------------------------------------------------------------
*
* pg_get_queryid.c
* get the last queryid generated/used by pg_stat_statements for a backend pid
*
* This program is open source, licensed under the PostgreSQL license.
* For license terms, see the LICENSE file.
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "storage/ipc.h"
#include "storage/proc.h"
#include "miscadmin.h"
#include "access/twophase.h"
#include "parser/analyze.h"
#include "parser/scansup.h"
#include "access/hash.h"
#include "utils/guc.h"
#include "executor/executor.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(pg_get_queryid);
/* Entry point of library loading */
void _PG_init(void);
void _PG_fini(void);
/* Saved hook values in case of unload */
static shmem_startup_hook_type pgqi_prev_shmem_startup_hook = NULL;
static post_parse_analyze_hook_type prev_post_parse_analyze_hook = NULL;
static ExecutorStart_hook_type prev_ExecutorStart = NULL;
/* Our hooks */
static void pgqi_shmem_startup(void);
static void pgqi_post_parse_analyze(ParseState *pstate, Query *query);
static void pgqi_ExecutorStart(QueryDesc *queryDesc, int eflags);
static uint64 *QueryIdArray = NULL;
static int get_max_procs_count(void);
#if PG_VERSION_NUM >= 100000
static int get_ql_for_utility(const char *query_text, int query_len, int query_location);
#endif
/* to create queryid in case of utility statements*/
#if PG_VERSION_NUM >= 110000
static uint64 getparsedinfo_hash64_string(const char *str, int len);
#else
static uint32 getparsedinfo_hash32_string(const char *str, int len);
#endif
static bool pgqi_track_utility; /* whether to track utility commands */
/* get max procs */
static int
get_max_procs_count(void)
{
int count = 0;
count += MaxBackends;
count += NUM_AUXILIARY_PROCS;
count += max_prepared_xacts;
return count;
}
/*
* get query length to create queryid for utility statements
* see pg_stat_statements.c for details
*/
#if PG_VERSION_NUM >= 100000
static int
get_ql_for_utility(const char *querytext, int query_len, int query_location)
{
if (query_location >= 0)
{
Assert(query_location <= strlen(querytext));
querytext += query_location;
/* Length of 0 (or -1) means "rest of string" */
if (query_len <= 0)
query_len = strlen(querytext);
else
Assert(query_len <= strlen(querytext));
}
else
{
/* If query location is unknown, distrust query_len as well */
query_location = 0;
query_len = strlen(querytext);
}
/*
* Discard leading and trailing whitespace, too. Use scanner_isspace()
* not libc's isspace(), because we want to match the lexer's behavior.
*/
while (query_len > 0 && scanner_isspace(querytext[0]))
querytext++, query_location++, query_len--;
while (query_len > 0 && scanner_isspace(querytext[query_len - 1]))
query_len--;
return query_len;
}
#endif
/* to create queryid in case of utility statements*/
#if PG_VERSION_NUM >= 110000
static uint64
getparsedinfo_hash64_string(const char *str, int len)
{
return DatumGetUInt64(hash_any_extended((const unsigned char *) str,
len, 0));
}
#else
static uint32
getparsedinfo_hash32_string(const char *str, int len)
{
return hash_any((const unsigned char *) str, len);
}
#endif
static void
pgqi_ExecutorStart(QueryDesc *queryDesc, int eflags)
{
if (prev_ExecutorStart)
prev_ExecutorStart(queryDesc, eflags);
else
standard_ExecutorStart(queryDesc, eflags);
if (MyProc)
{
int i = MyProc - ProcGlobal->allProcs;
QueryIdArray[i] = queryDesc->plannedstmt->queryId;
}
}
static void
pgqi_post_parse_analyze(ParseState *pstate, Query *query)
{
if (prev_post_parse_analyze_hook)
prev_post_parse_analyze_hook(pstate, query);
if (MyProc)
{
int i = MyProc - ProcGlobal->allProcs;
#if PG_VERSION_NUM >= 110000
if (query->queryId != UINT64CONST(0)) {
QueryIdArray[i] = query->queryId;
#else
if (query->queryId != 0) {
QueryIdArray[i] = query->queryId;
#endif
} else if (query->commandType == CMD_UTILITY && pgqi_track_utility) {
const char *querytext = pstate->p_sourcetext;
uint64 queryid_utility;
int query_len = 0;
#if PG_VERSION_NUM >= 100000
int query_location = query->stmt_location;
query_len = query->stmt_len;
query_len = get_ql_for_utility(querytext, query_len, query_location);
#else
query_len = strlen(querytext);
#endif
#if PG_VERSION_NUM >= 110000
queryid_utility = getparsedinfo_hash64_string(querytext, query_len);
#if PG_VERSION_NUM >= 120000
/*
* If we are unlucky enough to get a hash of zero(invalid), use
* queryID as 2 instead, queryID 1 is already in use for normal
* statements.
*/
if (queryid_utility == UINT64CONST(0))
queryid_utility = UINT64CONST(2);
#endif
#else
queryid_utility = getparsedinfo_hash32_string(querytext, query_len);
#endif
QueryIdArray[i] = queryid_utility;
}
else
QueryIdArray[i] = 0;
}
}
static void
pgqi_shmem_startup(void)
{
int size;
bool found;
if (pgqi_prev_shmem_startup_hook)
pgqi_prev_shmem_startup_hook();
LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
size = mul_size(sizeof(uint64), get_max_procs_count());
QueryIdArray = (uint64 *) ShmemInitStruct("pg_get_queryid proc entry array", size, &found);
if (!found)
MemSet(QueryIdArray, 0, size);
LWLockRelease(AddinShmemInitLock);
}
void
_PG_init(void)
{
if (!process_shared_preload_libraries_in_progress)
return;
/*
* Define (or redefine) custom GUC variables.
*/
DefineCustomBoolVariable("pg_get_queryid.track_utility",
"Selects whether utility commands are reported by pg_get_queryid.",
NULL,
&pgqi_track_utility,
true,
PGC_SUSET,
0,
NULL,
NULL,
NULL);
pgqi_prev_shmem_startup_hook = shmem_startup_hook;
shmem_startup_hook = pgqi_shmem_startup;
prev_post_parse_analyze_hook = post_parse_analyze_hook;
post_parse_analyze_hook = pgqi_post_parse_analyze;
prev_ExecutorStart = ExecutorStart_hook;
ExecutorStart_hook = pgqi_ExecutorStart;
}
Datum
pg_get_queryid(PG_FUNCTION_ARGS)
{
int i;
LWLockAcquire(ProcArrayLock, LW_SHARED);
for (i = 0; i < ProcGlobal->allProcCount; i++)
{
PGPROC *proc = &ProcGlobal->allProcs[i];
if (proc != NULL && proc->pid != 0 && proc->pid == PG_GETARG_INT32(0))
{
LWLockRelease(ProcArrayLock);
return QueryIdArray[i];
}
}
LWLockRelease(ProcArrayLock);
return 0;
}
void
_PG_fini(void)
{
shmem_startup_hook = pgqi_prev_shmem_startup_hook;
post_parse_analyze_hook = prev_post_parse_analyze_hook;
ExecutorStart_hook = prev_ExecutorStart;
}