33
33
#include "utils/date.h"
34
34
#include "utils/typcache.h"
35
35
#include "utils/lsyscache.h"
36
+ #include "utils/guc.h"
36
37
#include "access/heapam.h"
37
38
#include "access/nbtree.h"
38
39
#include "storage/ipc.h"
@@ -55,6 +56,9 @@ typedef struct
55
56
List * rangeset ;
56
57
}WrapperNode ;
57
58
59
+ bool pg_pathman_enable ;
60
+ PathmanState * pmstate ;
61
+
58
62
/* Original hooks */
59
63
static set_rel_pathlist_hook_type set_rel_pathlist_hook_original = NULL ;
60
64
static shmem_startup_hook_type shmem_startup_hook_original = NULL ;
@@ -166,6 +170,17 @@ _PG_init(void)
166
170
post_parse_analyze_hook = pathman_post_parse_analysis_hook ;
167
171
planner_hook_original = planner_hook ;
168
172
planner_hook = pathman_planner_hook ;
173
+
174
+ DefineCustomBoolVariable ("pg_pathman.enable" ,
175
+ "Enables pg_pathman's optimizations during the planner stage" ,
176
+ NULL ,
177
+ & pg_pathman_enable ,
178
+ true,
179
+ PGC_USERSET ,
180
+ 0 ,
181
+ NULL ,
182
+ NULL ,
183
+ NULL );
169
184
}
170
185
171
186
void
@@ -241,27 +256,30 @@ pathman_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams)
241
256
PlannedStmt * result ;
242
257
ListCell * lc ;
243
258
244
- inheritance_disabled = false;
245
- switch (parse -> commandType )
259
+ if (pg_pathman_enable )
246
260
{
247
- case CMD_SELECT :
248
- disable_inheritance (parse );
249
- break ;
250
- case CMD_UPDATE :
251
- case CMD_DELETE :
252
- handle_modification_query (parse );
253
- break ;
254
- default :
255
- break ;
256
- }
261
+ inheritance_disabled = false;
262
+ switch (parse -> commandType )
263
+ {
264
+ case CMD_SELECT :
265
+ disable_inheritance (parse );
266
+ break ;
267
+ case CMD_UPDATE :
268
+ case CMD_DELETE :
269
+ handle_modification_query (parse );
270
+ break ;
271
+ default :
272
+ break ;
273
+ }
257
274
258
- /* If query contains CTE (WITH statement) then handle subqueries too */
259
- foreach (lc ,parse -> cteList )
260
- {
261
- CommonTableExpr * cte = (CommonTableExpr * )lfirst (lc );
275
+ /* If query contains CTE (WITH statement) then handle subqueries too */
276
+ foreach (lc ,parse -> cteList )
277
+ {
278
+ CommonTableExpr * cte = (CommonTableExpr * )lfirst (lc );
262
279
263
- if (IsA (cte -> ctequery ,Query ))
264
- disable_inheritance ((Query * )cte -> ctequery );
280
+ if (IsA (cte -> ctequery ,Query ))
281
+ disable_inheritance ((Query * )cte -> ctequery );
282
+ }
265
283
}
266
284
267
285
/* Invoke original hook */
@@ -325,8 +343,7 @@ static void
325
343
handle_modification_query (Query * parse )
326
344
{
327
345
PartRelationInfo * prel ;
328
- List * ranges ,
329
- * wrappers = NIL ;
346
+ List * ranges ;
330
347
RangeTblEntry * rte ;
331
348
WrapperNode * wrap ;
332
349
Expr * expr ;
@@ -350,7 +367,6 @@ handle_modification_query(Query *parse)
350
367
351
368
/* Parse syntax tree and extract partition ranges */
352
369
wrap = walk_expr_tree (expr ,prel );
353
- wrappers = lappend (wrappers ,wrap );
354
370
ranges = irange_list_intersect (ranges ,wrap -> rangeset );
355
371
356
372
/* If only one partition is affected then substitute parent table with partition */
@@ -398,6 +414,9 @@ pathman_set_rel_pathlist_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, Ran
398
414
bool found ;
399
415
int first_child_relid = 0 ;
400
416
417
+ if (!pg_pathman_enable )
418
+ return ;
419
+
401
420
/* This works only for SELECT queries */
402
421
if (root -> parse -> commandType != CMD_SELECT || !inheritance_disabled )
403
422
return ;
@@ -521,15 +540,6 @@ pathman_set_rel_pathlist_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, Ran
521
540
/* Clear old path list */
522
541
list_free (rel -> pathlist );
523
542
524
- /* Set apropriate varnos */
525
- if (first_child_relid )
526
- {
527
- change_varnos ((Node * )root -> canon_pathkeys ,rti ,first_child_relid );
528
- change_varnos ((Node * )root -> eq_classes ,rti ,first_child_relid );
529
- change_varnos ((Node * )root -> parse -> targetList ,rti ,first_child_relid );
530
- change_varnos ((Node * )rel -> reltargetlist ,rti ,first_child_relid );
531
- }
532
-
533
543
rel -> pathlist = NIL ;
534
544
set_append_rel_pathlist (root ,rel ,rti ,rte ,pathkeyAsc ,pathkeyDesc );
535
545
set_append_rel_size (root ,rel ,rti ,rte );
@@ -1033,29 +1043,29 @@ handle_binary_opexpr(const PartRelationInfo *prel, WrapperNode *result,
1033
1043
1034
1044
if ((cmp_min < 0 &&
1035
1045
(strategy == BTLessEqualStrategyNumber ||
1036
- strategy == BTEqualStrategyNumber ))||
1046
+ strategy == BTEqualStrategyNumber ))||
1037
1047
(cmp_min <=0 && strategy == BTLessStrategyNumber ))
1038
1048
{
1039
1049
result -> rangeset = NIL ;
1040
1050
return ;
1041
1051
}
1042
1052
1043
- if (cmp_max >=0 && (strategy == BTGreaterEqualStrategyNumber ||
1053
+ if (cmp_max >=0 && (strategy == BTGreaterEqualStrategyNumber ||
1044
1054
strategy == BTGreaterStrategyNumber ||
1045
1055
strategy == BTEqualStrategyNumber ))
1046
1056
{
1047
1057
result -> rangeset = NIL ;
1048
1058
return ;
1049
1059
}
1050
1060
1051
- if ((cmp_min < 0 && strategy == BTGreaterStrategyNumber )||
1061
+ if ((cmp_min < 0 && strategy == BTGreaterStrategyNumber )||
1052
1062
(cmp_min <=0 && strategy == BTGreaterEqualStrategyNumber ))
1053
1063
{
1054
1064
result -> rangeset = list_make1_irange (make_irange (startidx ,endidx , false));
1055
1065
return ;
1056
1066
}
1057
1067
1058
- if (cmp_max >=0 && (strategy == BTLessEqualStrategyNumber ||
1068
+ if (cmp_max >=0 && (strategy == BTLessEqualStrategyNumber ||
1059
1069
strategy == BTLessStrategyNumber ))
1060
1070
{
1061
1071
result -> rangeset = list_make1_irange (make_irange (startidx ,endidx , false));