@@ -70,6 +70,8 @@ static PlannedStmt * pathman_planner_hook(Query *parse, int cursorOptions, Param
7070static void handle_modification_query (Query * parse );
7171static Node * wrapper_make_expression (WrapperNode * wrap ,int index ,bool * alwaysTrue );
7272static void disable_inheritance (Query * parse );
73+ static void disable_inheritance_cte (Query * parse );
74+ static void disable_inheritance_subselect (Query * parse );
7375
7476/* Expression tree handlers */
7577static int make_hash (const PartRelationInfo * prel ,int value );
@@ -252,7 +254,6 @@ PlannedStmt *
252254pathman_planner_hook (Query * parse ,int cursorOptions ,ParamListInfo boundParams )
253255{
254256PlannedStmt * result ;
255- ListCell * lc ;
256257
257258if (pg_pathman_enable )
258259{
@@ -264,20 +265,13 @@ pathman_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams)
264265break ;
265266case CMD_UPDATE :
266267case CMD_DELETE :
268+ disable_inheritance_cte (parse );
269+ disable_inheritance_subselect (parse );
267270handle_modification_query (parse );
268271break ;
269272default :
270273break ;
271274}
272-
273- /* If query contains CTE (WITH statement) then handle subqueries too */
274- foreach (lc ,parse -> cteList )
275- {
276- CommonTableExpr * cte = (CommonTableExpr * )lfirst (lc );
277-
278- if (IsA (cte -> ctequery ,Query ))
279- disable_inheritance ((Query * )cte -> ctequery );
280- }
281275}
282276
283277/* Invoke original hook */
@@ -296,10 +290,16 @@ pathman_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams)
296290static void
297291disable_inheritance (Query * parse )
298292{
299- RangeTblEntry * rte ;
300- ListCell * lc ;
293+ ListCell * lc ;
294+ RangeTblEntry * rte ;
301295PartRelationInfo * prel ;
302- bool found ;
296+ bool found ;
297+
298+ /* If query contains CTE (WITH statement) then handle subqueries too */
299+ disable_inheritance_cte (parse );
300+
301+ /* If query contains subselects */
302+ disable_inheritance_subselect (parse );
303303
304304foreach (lc ,parse -> rtable )
305305{
@@ -334,6 +334,38 @@ disable_inheritance(Query *parse)
334334}
335335}
336336
337+ static void
338+ disable_inheritance_cte (Query * parse )
339+ {
340+ ListCell * lc ;
341+
342+ foreach (lc ,parse -> cteList )
343+ {
344+ CommonTableExpr * cte = (CommonTableExpr * )lfirst (lc );
345+
346+ if (IsA (cte -> ctequery ,Query ))
347+ disable_inheritance ((Query * )cte -> ctequery );
348+ }
349+ }
350+
351+ static void
352+ disable_inheritance_subselect (Query * parse )
353+ {
354+ Node * quals ;
355+
356+ if (!parse -> jointree || !parse -> jointree -> quals )
357+ return ;
358+
359+ quals = parse -> jointree -> quals ;
360+ if (!IsA (quals ,SubLink ))
361+ return ;
362+
363+ if (!IsA (((SubLink * )quals )-> subselect ,Query ))
364+ return ;
365+
366+ disable_inheritance ((Query * ) (((SubLink * )quals )-> subselect ));
367+ }
368+
337369/*
338370 * Checks if query is affects only one partition. If true then substitute
339371 */