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 ;
@@ -152,6 +156,17 @@ _PG_init(void)
152156post_parse_analyze_hook = pathman_post_parse_analysis_hook ;
153157planner_hook_original = planner_hook ;
154158planner_hook = pathman_planner_hook ;
159+
160+ DefineCustomBoolVariable ("pg_pathman.enable" ,
161+ "Enables pg_pathman's optimizations during the planner stage" ,
162+ NULL ,
163+ & pg_pathman_enable ,
164+ true,
165+ PGC_USERSET ,
166+ 0 ,
167+ NULL ,
168+ NULL ,
169+ NULL );
155170}
156171
157172void
@@ -227,27 +242,30 @@ pathman_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams)
227242PlannedStmt * result ;
228243ListCell * lc ;
229244
230- inheritance_disabled = false;
231- switch (parse -> commandType )
245+ if (pg_pathman_enable )
232246{
233- case CMD_SELECT :
234- disable_inheritance (parse );
235- break ;
236- case CMD_UPDATE :
237- case CMD_DELETE :
238- handle_modification_query (parse );
239- break ;
240- default :
241- break ;
242- }
247+ inheritance_disabled = false;
248+ switch (parse -> commandType )
249+ {
250+ case CMD_SELECT :
251+ disable_inheritance (parse );
252+ break ;
253+ case CMD_UPDATE :
254+ case CMD_DELETE :
255+ handle_modification_query (parse );
256+ break ;
257+ default :
258+ break ;
259+ }
243260
244- /* If query contains CTE (WITH statement) then handle subqueries too */
245- foreach (lc ,parse -> cteList )
246- {
247- CommonTableExpr * cte = (CommonTableExpr * )lfirst (lc );
261+ /* If query contains CTE (WITH statement) then handle subqueries too */
262+ foreach (lc ,parse -> cteList )
263+ {
264+ CommonTableExpr * cte = (CommonTableExpr * )lfirst (lc );
248265
249- if (IsA (cte -> ctequery ,Query ))
250- disable_inheritance ((Query * )cte -> ctequery );
266+ if (IsA (cte -> ctequery ,Query ))
267+ disable_inheritance ((Query * )cte -> ctequery );
268+ }
251269}
252270
253271/* Invoke original hook */
@@ -382,6 +400,9 @@ pathman_set_rel_pathlist_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, Ran
382400bool found ;
383401int first_child_relid = 0 ;
384402
403+ if (!pg_pathman_enable )
404+ return ;
405+
385406/* This works only for SELECT queries */
386407if (root -> parse -> commandType != CMD_SELECT || !inheritance_disabled )
387408return ;
@@ -1017,29 +1038,29 @@ handle_binary_opexpr(const PartRelationInfo *prel, WrapperNode *result,
10171038
10181039if ((cmp_min < 0 &&
10191040 (strategy == BTLessEqualStrategyNumber ||
1020- strategy == BTEqualStrategyNumber ))||
1041+ strategy == BTEqualStrategyNumber ))||
10211042(cmp_min <=0 && strategy == BTLessStrategyNumber ))
10221043{
10231044result -> rangeset = NIL ;
10241045return ;
10251046}
10261047
1027- if (cmp_max >=0 && (strategy == BTGreaterEqualStrategyNumber ||
1048+ if (cmp_max >=0 && (strategy == BTGreaterEqualStrategyNumber ||
10281049strategy == BTGreaterStrategyNumber ||
10291050strategy == BTEqualStrategyNumber ))
10301051{
10311052result -> rangeset = NIL ;
10321053return ;
10331054}
10341055
1035- if ((cmp_min < 0 && strategy == BTGreaterStrategyNumber )||
1056+ if ((cmp_min < 0 && strategy == BTGreaterStrategyNumber )||
10361057(cmp_min <=0 && strategy == BTGreaterEqualStrategyNumber ))
10371058{
10381059result -> rangeset = list_make1_irange (make_irange (startidx ,endidx , false));
10391060return ;
10401061}
10411062
1042- if (cmp_max >=0 && (strategy == BTLessEqualStrategyNumber ||
1063+ if (cmp_max >=0 && (strategy == BTLessEqualStrategyNumber ||
10431064strategy == BTLessStrategyNumber ))
10441065{
10451066result -> rangeset = list_make1_irange (make_irange (startidx ,endidx , false));