@@ -89,6 +89,8 @@ static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rt
8989static List * accumulate_append_subpath (List * subpaths ,Path * path );
9090static void set_pathkeys (PlannerInfo * root ,RelOptInfo * childrel ,Path * path );
9191
92+ static void handle_delete_query (Query * parse );
93+
9294/*
9395 * Compare two Datums with the given comarison function
9496 *
@@ -194,7 +196,20 @@ pathman_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams)
194196}
195197
196198inheritance_disabled = false;
197- disable_inheritance (parse );
199+
200+ // if (parse->commandType != CMD_SELECT)
201+
202+ switch (parse -> commandType )
203+ {
204+ case CMD_SELECT :
205+ disable_inheritance (parse );
206+ break ;
207+ case CMD_DELETE :
208+ handle_delete_query (parse );
209+ break ;
210+ default :
211+ break ;
212+ }
198213
199214/* Invoke original hook */
200215if (planner_hook_original )
@@ -217,9 +232,6 @@ disable_inheritance(Query *parse)
217232PartRelationInfo * prel ;
218233bool found ;
219234
220- if (parse -> commandType != CMD_SELECT )
221- return ;
222-
223235foreach (lc ,parse -> rtable )
224236{
225237rte = (RangeTblEntry * )lfirst (lc );
@@ -253,6 +265,54 @@ disable_inheritance(Query *parse)
253265}
254266}
255267
268+ /*
269+ * Checks if query is affects only one partition. If true then substitute
270+ */
271+ static void
272+ handle_delete_query (Query * parse )
273+ {
274+ PartRelationInfo * prel ;
275+ List * ranges ,
276+ * wrappers = NIL ;
277+ // ListCell *lc;
278+ RangeTblEntry * rte ;
279+ WrapperNode * wrap ;
280+ bool found ;
281+
282+ if (list_length (parse -> rtable )!= 1 )
283+ return ;
284+
285+ rte = (RangeTblEntry * )linitial (parse -> rtable );
286+ prel = get_pathman_relation_info (rte -> relid ,& found );
287+
288+ // foreach(lc, parse->jointree->quals)
289+ // {
290+ // WrapperNode *wrap;
291+ // Expr *expr = (Expr *) lfirst(lc);
292+
293+ // wrap = walk_expr_tree(expr, prel);
294+ ranges = list_make1_int (make_irange (0 ,prel -> children_count - 1 , false));
295+ wrap = walk_expr_tree ( (Expr * )parse -> jointree -> quals ,prel );
296+ wrappers = lappend (wrappers ,wrap );
297+ ranges = irange_list_intersect (ranges ,wrap -> rangeset );
298+ // }
299+
300+ /* If only one partition is affected then substitute parent table with partition */
301+ if (irange_list_length (ranges ))
302+ {
303+ IndexRange irange = (IndexRange )linitial_oid (ranges );
304+ elog (WARNING ,"lower: %d, upper: %d, lossy: %d" ,irange_lower (irange ),irange_upper (irange ),irange_is_lossy (irange ));
305+ if (irange_lower (irange )== irange_upper (irange ))
306+ {
307+ Oid * children = (Oid * )dsm_array_get_pointer (& prel -> children );
308+ rte -> relid = children [irange_lower (irange )];
309+ rte -> inh = false;
310+ }
311+ }
312+
313+ return ;
314+ }
315+
256316/*
257317 * Shared memory startup hook
258318 */