3333#include "utils/date.h"
3434#include "utils/typcache.h"
3535#include "utils/lsyscache.h"
36+ #include "utils/guc.h"
3637#include "access/heapam.h"
3738#include "access/nbtree.h"
3839#include "storage/ipc.h"
@@ -55,6 +56,9 @@ typedef struct
5556List * rangeset ;
5657}WrapperNode ;
5758
59+ bool pg_pathman_enable ;
60+ PathmanState * pmstate ;
61+
5862/* Original hooks */
5963static set_rel_pathlist_hook_type set_rel_pathlist_hook_original = NULL ;
6064static shmem_startup_hook_type shmem_startup_hook_original = NULL ;
@@ -166,6 +170,17 @@ _PG_init(void)
166170post_parse_analyze_hook = pathman_post_parse_analysis_hook ;
167171planner_hook_original = planner_hook ;
168172planner_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 );
169184}
170185
171186void
@@ -241,27 +256,30 @@ pathman_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams)
241256PlannedStmt * result ;
242257ListCell * lc ;
243258
244- inheritance_disabled = false;
245- switch (parse -> commandType )
259+ if (pg_pathman_enable )
246260{
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+ }
257274
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 );
262279
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+ }
265283}
266284
267285/* Invoke original hook */
@@ -325,8 +343,7 @@ static void
325343handle_modification_query (Query * parse )
326344{
327345PartRelationInfo * prel ;
328- List * ranges ,
329- * wrappers = NIL ;
346+ List * ranges ;
330347RangeTblEntry * rte ;
331348WrapperNode * wrap ;
332349Expr * expr ;
@@ -350,7 +367,6 @@ handle_modification_query(Query *parse)
350367
351368/* Parse syntax tree and extract partition ranges */
352369wrap = walk_expr_tree (expr ,prel );
353- wrappers = lappend (wrappers ,wrap );
354370ranges = irange_list_intersect (ranges ,wrap -> rangeset );
355371
356372/* 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
398414bool found ;
399415int first_child_relid = 0 ;
400416
417+ if (!pg_pathman_enable )
418+ return ;
419+
401420/* This works only for SELECT queries */
402421if (root -> parse -> commandType != CMD_SELECT || !inheritance_disabled )
403422return ;
@@ -521,15 +540,6 @@ pathman_set_rel_pathlist_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, Ran
521540/* Clear old path list */
522541list_free (rel -> pathlist );
523542
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-
533543rel -> pathlist = NIL ;
534544set_append_rel_pathlist (root ,rel ,rti ,rte ,pathkeyAsc ,pathkeyDesc );
535545set_append_rel_size (root ,rel ,rti ,rte );
@@ -1033,29 +1043,29 @@ handle_binary_opexpr(const PartRelationInfo *prel, WrapperNode *result,
10331043
10341044if ((cmp_min < 0 &&
10351045 (strategy == BTLessEqualStrategyNumber ||
1036- strategy == BTEqualStrategyNumber ))||
1046+ strategy == BTEqualStrategyNumber ))||
10371047(cmp_min <=0 && strategy == BTLessStrategyNumber ))
10381048{
10391049result -> rangeset = NIL ;
10401050return ;
10411051}
10421052
1043- if (cmp_max >=0 && (strategy == BTGreaterEqualStrategyNumber ||
1053+ if (cmp_max >=0 && (strategy == BTGreaterEqualStrategyNumber ||
10441054strategy == BTGreaterStrategyNumber ||
10451055strategy == BTEqualStrategyNumber ))
10461056{
10471057result -> rangeset = NIL ;
10481058return ;
10491059}
10501060
1051- if ((cmp_min < 0 && strategy == BTGreaterStrategyNumber )||
1061+ if ((cmp_min < 0 && strategy == BTGreaterStrategyNumber )||
10521062(cmp_min <=0 && strategy == BTGreaterEqualStrategyNumber ))
10531063{
10541064result -> rangeset = list_make1_irange (make_irange (startidx ,endidx , false));
10551065return ;
10561066}
10571067
1058- if (cmp_max >=0 && (strategy == BTLessEqualStrategyNumber ||
1068+ if (cmp_max >=0 && (strategy == BTLessEqualStrategyNumber ||
10591069strategy == BTLessStrategyNumber ))
10601070{
10611071result -> rangeset = list_make1_irange (make_irange (startidx ,endidx , false));