@@ -81,6 +81,8 @@ static int append_child_relation(PlannerInfo *root, RelOptInfo *rel, Index rti,
8181RangeTblEntry * rte ,int index ,Oid childOID ,List * wrappers );
8282static Node * wrapper_make_expression (WrapperNode * wrap ,int index ,bool * alwaysTrue );
8383static void disable_inheritance (Query * parse );
84+ static void disable_inheritance_cte (Query * parse );
85+ static void disable_inheritance_subselect (Query * parse );
8486bool inheritance_disabled ;
8587
8688/* Expression tree handlers */
@@ -240,7 +242,6 @@ PlannedStmt *
240242pathman_planner_hook (Query * parse ,int cursorOptions ,ParamListInfo boundParams )
241243{
242244PlannedStmt * result ;
243- ListCell * lc ;
244245
245246if (pg_pathman_enable )
246247{
@@ -252,20 +253,13 @@ pathman_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams)
252253break ;
253254case CMD_UPDATE :
254255case CMD_DELETE :
256+ disable_inheritance_cte (parse );
257+ disable_inheritance_subselect (parse );
255258handle_modification_query (parse );
256259break ;
257260default :
258261break ;
259262}
260-
261- /* If query contains CTE (WITH statement) then handle subqueries too */
262- foreach (lc ,parse -> cteList )
263- {
264- CommonTableExpr * cte = (CommonTableExpr * )lfirst (lc );
265-
266- if (IsA (cte -> ctequery ,Query ))
267- disable_inheritance ((Query * )cte -> ctequery );
268- }
269263}
270264
271265/* Invoke original hook */
@@ -284,10 +278,16 @@ pathman_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams)
284278static void
285279disable_inheritance (Query * parse )
286280{
287- RangeTblEntry * rte ;
288- ListCell * lc ;
281+ ListCell * lc ;
282+ RangeTblEntry * rte ;
289283PartRelationInfo * prel ;
290- bool found ;
284+ bool found ;
285+
286+ /* If query contains CTE (WITH statement) then handle subqueries too */
287+ disable_inheritance_cte (parse );
288+
289+ /* If query contains subselects */
290+ disable_inheritance_subselect (parse );
291291
292292foreach (lc ,parse -> rtable )
293293{
@@ -322,6 +322,35 @@ disable_inheritance(Query *parse)
322322}
323323}
324324
325+ static void
326+ disable_inheritance_cte (Query * parse )
327+ {
328+ ListCell * lc ;
329+
330+ foreach (lc ,parse -> cteList )
331+ {
332+ CommonTableExpr * cte = (CommonTableExpr * )lfirst (lc );
333+
334+ if (IsA (cte -> ctequery ,Query ))
335+ disable_inheritance ((Query * )cte -> ctequery );
336+ }
337+ }
338+
339+ static void
340+ disable_inheritance_subselect (Query * parse )
341+ {
342+ SubLink * sublink ;
343+
344+ if (!parse -> jointree || !parse -> jointree -> quals )
345+ return ;
346+
347+ sublink = (SubLink * )parse -> jointree -> quals ;
348+ if (!IsA (sublink -> subselect ,Query ))
349+ return ;
350+
351+ disable_inheritance ((Query * )sublink -> subselect );
352+ }
353+
325354/*
326355 * Checks if query is affects only one partition. If true then substitute
327356 */