-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunionreplacement.c
344 lines (305 loc) · 10.9 KB
/
unionreplacement.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
/*-------------------------------------------------------------------------
*
* unionreplacement is a PostgreSQL extension which replace OR with UNION
*
* This program is open source, licensed under the PostgreSQL license.
* For license terms, see the LICENSE file.
*
* Copyright (c) 2024 James Wang
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "executor/executor.h"
#include "executor/spi.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "access/xact.h"
#include "parser/parse_node.h"
#include "parser/analyze.h"
#include "parser/parser.h"
#include "tcop/tcopprot.h"
#include "tcop/utility.h"
#include "utils/guc.h"
#include "utils/snapmgr.h"
#include "utils/memutils.h"
#if PG_VERSION_NUM <= 90600
#include "storage/lwlock.h"
#endif
#include "pgstat.h"
#include "storage/ipc.h"
#include "storage/spin.h"
#include "miscadmin.h"
#if PG_VERSION_NUM >= 90600
#include "nodes/extensible.h"
#endif
#if PG_VERSION_NUM > 120000
#include "nodes/pathnodes.h"
#endif
#include "nodes/plannodes.h"
#include "utils/datum.h"
#include "utils/builtins.h"
#include "unistd.h"
#include "funcapi.h"
#include "catalog/pg_type.h"
#include "commands/dbcommands.h"
#include "ur.h"
PG_MODULE_MAGIC;
/*
* maximum number of rules processed
* by the extension defined as GUC
*/
static ParseState *new_static_pstate = NULL;
static Query *new_static_query = NULL;
/* Saved hook values in case of unload */
#if PG_VERSION_NUM >= 150000
static shmem_request_hook_type prev_shmem_request_hook = NULL;
#endif
static post_parse_analyze_hook_type prev_post_parse_analyze_hook = NULL;
static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
static ExecutorStart_hook_type prev_executor_start_hook = NULL;
static void ur_reanalyze(const char *new_query_string);
#if PG_VERSION_NUM < 140000
static void ur_analyze(ParseState *pstate, Query *query);
#else
static void ur_analyze(ParseState *pstate, Query *query, JumbleState *jstate);
#endif
/*
* Global shared state:
* SQL translation rules are stored in shared memory
*/
/*---- Function declarations ----*/
void _PG_init(void);
void _PG_fini(void);
static void ur_exec(QueryDesc *queryDesc, int eflags);
/*
* Module load callback
*/
void
_PG_init(void)
{
elog(NOTICE, "unionreplacement:_PG_init():entry"); //create extension would not call this
if (!process_shared_preload_libraries_in_progress)
return;
prev_post_parse_analyze_hook = post_parse_analyze_hook;
post_parse_analyze_hook = ur_analyze;
prev_executor_start_hook = ExecutorStart_hook;
ExecutorStart_hook = ur_exec;
elog(NOTICE, "unionreplacement:_PG_init():exit");
}
/*
* Module unload callback
*/
void
_PG_fini(void)
{
shmem_startup_hook = prev_shmem_startup_hook;
post_parse_analyze_hook = prev_post_parse_analyze_hook;
ExecutorStart_hook = prev_executor_start_hook;
}
/*
* ur_exec
*
*/
static void ur_exec(QueryDesc *queryDesc, int eflags)
{
#if PG_VERSION_NUM > 100000
int stmt_loc;
int stmt_len;
const char *src;
{
src = queryDesc->sourceText;
stmt_loc = queryDesc->plannedstmt->stmt_location;
stmt_len = queryDesc->plannedstmt->stmt_len;
elog(NOTICE, "unionreplacement: ur_exec: src=%s", src);
elog(NOTICE, "unionreplacement: ur_exec: stmt_loc=%d", stmt_loc);
elog(NOTICE, "unionreplacement: ur_exec: stmt_len=%d", stmt_len);
/*
* set stmt_location to -1 to avoid assertion failure in pgss_store:
* Assert(query_len <= strlen(query)
*/
queryDesc->plannedstmt->stmt_location = -1;
stmt_loc = queryDesc->plannedstmt->stmt_location;
elog(DEBUG1, "unionreplacement: ur_exec: stmt_loc=%d", stmt_loc);
}
#endif
elog(NOTICE, "unionreplacement: ur_exec: eflags=%d", eflags);
/*
* must always execute here whatever PG_VERSION_NUM
*/
if (prev_executor_start_hook)
(*prev_executor_start_hook)(queryDesc, eflags);
else standard_ExecutorStart(queryDesc, eflags);
}
static void ur_clone_Query(Query *source, Query *target)
{
target->type = source->type;
target->commandType = source->commandType;
target->querySource = source->querySource;
target->queryId = source->queryId;
target->canSetTag = source->canSetTag;
target->utilityStmt = source->utilityStmt;
target->resultRelation = source->resultRelation;
target->hasAggs = source->hasAggs;
target->hasWindowFuncs = source->hasWindowFuncs;
#if PG_VERSION_NUM > 100000
target->hasTargetSRFs = source->hasTargetSRFs;
#endif
target->hasSubLinks = source->hasSubLinks;
target->hasDistinctOn = source->hasDistinctOn;
target->hasRecursive = source->hasRecursive;
target->hasModifyingCTE = source->hasModifyingCTE;
target->hasForUpdate = source->hasForUpdate;
target->hasRowSecurity = source->hasRowSecurity;
target->cteList = source->cteList;
target->rtable = source->rtable;
target->jointree = source->jointree;
target->targetList = source->targetList;
#if PG_VERSION_NUM > 100000
target->override = source->override;
#endif
target->onConflict = source->onConflict;
target->returningList = source->returningList;
target->groupClause = source->groupClause;
target->groupingSets = source->groupingSets;
target->havingQual = source->havingQual;
target->windowClause = source->windowClause;
target->distinctClause= source->distinctClause;
target->sortClause= source->sortClause;
target->limitOffset= source->limitOffset;
target->limitCount= source->limitCount;
#if PG_VERSION_NUM > 130000
target->limitOption = source->limitOption;
#endif
target->rowMarks= source->rowMarks;
target->setOperations= source->setOperations;
target->constraintDeps= source->constraintDeps;
target->withCheckOptions = source->withCheckOptions;
#if PG_VERSION_NUM > 140000
target->limitOption = source->limitOption;
#endif
#if PG_VERSION_NUM > 100000
target->stmt_location=source->stmt_location;
target->stmt_len=source->stmt_len;
#endif
}
static void ur_clone_ParseState(ParseState *source, ParseState *target)
{
target->parentParseState = source->parentParseState;
target->p_sourcetext = source->p_sourcetext;
target->p_rtable= source->p_rtable;
target->p_joinexprs= source->p_joinexprs;
target->p_joinlist= source->p_joinlist;
target->p_namespace= source->p_namespace;
target->p_lateral_active= source->p_lateral_active;
target->p_ctenamespace= source->p_ctenamespace;
target->p_future_ctes= source->p_future_ctes;
target->p_parent_cte= source->p_parent_cte;
target->p_target_relation= source->p_target_relation;
target->p_is_insert= source->p_is_insert;
target->p_windowdefs= source->p_windowdefs;
target->p_expr_kind= source->p_expr_kind;
target->p_next_resno= source->p_next_resno;
target->p_multiassign_exprs= source->p_multiassign_exprs;
target->p_locking_clause= source->p_locking_clause;
target->p_locked_from_parent= source->p_locked_from_parent;
#if PG_VERSION_NUM > 100000
target->p_resolve_unknowns= source->p_resolve_unknowns;
target->p_queryEnv= source->p_queryEnv;
#endif
target->p_hasAggs = source->p_hasAggs;
target->p_hasWindowFuncs = source->p_hasWindowFuncs;
#if PG_VERSION_NUM > 100000
target->p_hasTargetSRFs= source->p_hasTargetSRFs;
#endif
target->p_hasSubLinks= source->p_hasSubLinks;
target->p_hasModifyingCTE= source->p_hasModifyingCTE;
#if PG_VERSION_NUM > 100000
target->p_last_srf = source->p_last_srf;
#endif
target->p_pre_columnref_hook = source->p_pre_columnref_hook;
target->p_post_columnref_hook = source->p_post_columnref_hook;
target->p_paramref_hook = source->p_paramref_hook;
target->p_coerce_param_hook = source->p_coerce_param_hook;
target->p_ref_hook_state = source->p_ref_hook_state;
}
static void ur_reanalyze(const char *new_query_string)
{
/*
* >>> FROM parse_analyze in src/backend/parser/analyze.c <<<
*/
ParseState *new_pstate = make_parsestate(NULL);
Query *new_query = (Query *)NULL;
#if PG_VERSION_NUM >= 100000
RawStmt *new_parsetree;
#else
Node *new_parsetree;
#endif
List *raw_parsetree_list;
ListCell *lc1;
elog(DEBUG1, "unionreplacement: ur_reanalyze: entry");
new_pstate->p_sourcetext = new_query_string;
/*
* missing data:
* 1. numParams
* 2. ParamTypes
* 3. queryEnv
*/
new_parsetree = NULL;
raw_parsetree_list = pg_parse_query(new_query_string);
/*
* we assume only one SQL statement
*/
foreach(lc1, raw_parsetree_list)
{
#if PG_VERSION_NUM >= 100000
new_parsetree = lfirst_node(RawStmt, lc1);
#else
new_parsetree = (Node *) lfirst(lc1);
#endif
}
new_query = transformTopLevelStmt(new_pstate, new_parsetree);
new_static_pstate = new_pstate;
new_static_query = new_query;
elog(DEBUG1, "unionreplacement: ur_reanalyze: exit");
}
#define MAX_QUERY_SIZE 32768 // 32M crashed server :( Adjust this according to your needs
#if PG_VERSION_NUM < 140000
static void ur_analyze(ParseState *pstate, Query *query)
#else
static void ur_analyze(ParseState *pstate, Query *query, JumbleState *js)
#endif
{
char *hint;
/*
** not possible to access parameters using pstate->p_ref_hook_state
** because no easy way to check FixedParamState vs VarParamState
** (p_ref_hook_state is generic pointer in both cases
** and p_param_ref_hook refer to a static function in parse_params.c).
*/
/* pstate->p_sourcetext is the current query text */
//_reanalyze("select 11;");//hard coded here
elog(WARNING,"unionreplacement: ur_analyze: query %s", pstate->p_sourcetext);
hint = extractUrhint(pstate->p_sourcetext); //elog(ERROR, in ur.c will ruin this and process will terminated here :(
if (strlen(hint) > 0) {
ur_reanalyze(workflow(pstate->p_sourcetext, hint));
// clone data
ur_clone_ParseState(new_static_pstate, pstate);
elog(WARNING,"unionreplacement: ur_analyze: hint %s rewrite=true pstate->p_source_text %s", hint, pstate->p_sourcetext);
ur_clone_Query(new_static_query, query);
} else {
ur_reanalyze(pstate->p_sourcetext);
}
/* no "standard_analyze" to call
* according to parse_analyze in analyze.c
*/
if (prev_post_parse_analyze_hook)
{
#if PG_VERSION_NUM < 140000
prev_post_parse_analyze_hook(pstate, query);
#else
prev_post_parse_analyze_hook(pstate, query, js);
#endif
}
elog(DEBUG1, "unionreplacement: ur_analyze: exit");
}