@@ -89,6 +89,8 @@ static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rt
89
89
static List * accumulate_append_subpath (List * subpaths ,Path * path );
90
90
static void set_pathkeys (PlannerInfo * root ,RelOptInfo * childrel ,Path * path );
91
91
92
+ static void handle_delete_query (Query * parse );
93
+
92
94
/*
93
95
* Compare two Datums with the given comarison function
94
96
*
@@ -194,7 +196,20 @@ pathman_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams)
194
196
}
195
197
196
198
inheritance_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
+ }
198
213
199
214
/* Invoke original hook */
200
215
if (planner_hook_original )
@@ -217,9 +232,6 @@ disable_inheritance(Query *parse)
217
232
PartRelationInfo * prel ;
218
233
bool found ;
219
234
220
- if (parse -> commandType != CMD_SELECT )
221
- return ;
222
-
223
235
foreach (lc ,parse -> rtable )
224
236
{
225
237
rte = (RangeTblEntry * )lfirst (lc );
@@ -253,6 +265,54 @@ disable_inheritance(Query *parse)
253
265
}
254
266
}
255
267
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
+
256
316
/*
257
317
* Shared memory startup hook
258
318
*/