Skip to content

Commit c63c9a1

Browse files
committed
PartitionFilter now transforms INSERT statements within subplans
1 parent e6b0997 commit c63c9a1

File tree

3 files changed

+75
-22
lines changed

3 files changed

+75
-22
lines changed

src/partition_filter.c

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ init_partition_filter_static_data(void)
4040
}
4141

4242
Plan *
43-
make_partition_filter_plan(Plan *subplan, Oid partitioned_table,
43+
make_partition_filter(Plan *subplan, Oid partitioned_table,
4444
OnConflictAction conflict_action)
4545
{
4646
CustomScan *cscan = makeNode(CustomScan);
@@ -265,27 +265,73 @@ pfilter_build_tlist(List *tlist)
265265
return result_tlist;
266266
}
267267

268-
/* Add proxy PartitionFilter nodes to subplans of ModifyTable node */
269268
void
270-
add_partition_filters(List *rtable, ModifyTable *modify_table)
269+
add_partition_filters(List *rtable, Plan *plan)
271270
{
272-
ListCell *lc1,
273-
*lc2;
271+
ListCell *l;
274272

275-
Assert(IsA(modify_table, ModifyTable));
276-
277-
if (!pg_pathman_enable_partition_filter)
273+
if (plan == NULL || !pg_pathman_enable_partition_filter)
278274
return;
279275

280-
forboth (lc1, modify_table->plans, lc2, modify_table->resultRelations)
276+
/* Plan-type-specific fixes*/
277+
switch (nodeTag(plan))
281278
{
282-
Index rindex = lfirst_int(lc2);
283-
Oid relid = getrelid(rindex, rtable);
284-
PartRelationInfo *prel = get_pathman_relation_info(relid, NULL);
285-
286-
if (prel)
287-
lfirst(lc1) = make_partition_filter_plan((Plan *) lfirst(lc1),
288-
relid,
289-
modify_table->onConflictAction);
279+
case T_SubqueryScan:
280+
add_partition_filters(rtable, ((SubqueryScan *) plan)->subplan);
281+
break;
282+
283+
case T_CustomScan:
284+
foreach(l, ((CustomScan *) plan)->custom_plans)
285+
add_partition_filters(rtable, (Plan *) lfirst(l));
286+
break;
287+
288+
/*
289+
* Add proxy PartitionFilter nodes
290+
* to subplans of ModifyTable node
291+
*/
292+
case T_ModifyTable:
293+
{
294+
ModifyTable *modify_table = ((ModifyTable *) plan);
295+
ListCell *lc1,
296+
*lc2;
297+
298+
forboth (lc1, modify_table->plans, lc2, modify_table->resultRelations)
299+
{
300+
Index rindex = lfirst_int(lc2);
301+
Oid relid = getrelid(rindex, rtable);
302+
PartRelationInfo *prel = get_pathman_relation_info(relid, NULL);
303+
304+
add_partition_filters(rtable, (Plan *) lfirst(lc1));
305+
306+
if (prel)
307+
lfirst(lc1) = make_partition_filter((Plan *) lfirst(lc1),
308+
relid,
309+
modify_table->onConflictAction);
310+
}
311+
}
312+
break;
313+
314+
/* Since they look alike */
315+
case T_MergeAppend:
316+
case T_Append:
317+
foreach(l, ((Append *) plan)->appendplans)
318+
add_partition_filters(rtable, (Plan *) lfirst(l));
319+
break;
320+
321+
case T_BitmapAnd:
322+
foreach(l, ((BitmapAnd *) plan)->bitmapplans)
323+
add_partition_filters(rtable, (Plan *) lfirst(l));
324+
break;
325+
326+
case T_BitmapOr:
327+
foreach(l, ((BitmapOr *) plan)->bitmapplans)
328+
add_partition_filters(rtable, (Plan *) lfirst(l));
329+
break;
330+
331+
default:
332+
break;
290333
}
334+
335+
add_partition_filters(rtable, plan->lefttree);
336+
add_partition_filters(rtable, plan->righttree);
291337
}

src/partition_filter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ extern CustomScanMethods partition_filter_plan_methods;
3737
extern CustomExecMethods partition_filter_exec_methods;
3838

3939

40-
void add_partition_filters(List *rtable, ModifyTable *modify_table);
40+
void add_partition_filters(List *rtable, Plan *plan);
4141

4242
void init_partition_filter_static_data(void);
4343

44-
Plan * make_partition_filter_plan(Plan *subplan, Oid partitioned_table,
45-
OnConflictAction conflict_action);
44+
Plan * make_partition_filter(Plan *subplan, Oid partitioned_table,
45+
OnConflictAction conflict_action);
4646

4747
Node * partition_filter_create_scan_state(CustomScan *node);
4848

src/pg_pathman.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,17 @@ pathman_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams)
254254
handle_modification_query(parse);
255255
break;
256256
case CMD_INSERT:
257+
{
258+
ListCell *lc;
259+
257260
result = standard_planner(parse, cursorOptions, boundParams);
258-
add_partition_filters(result->rtable,
259-
(ModifyTable *) result->planTree);
261+
262+
add_partition_filters(result->rtable, result->planTree);
263+
foreach (lc, result->subplans)
264+
add_partition_filters(result->rtable, (Plan *) lfirst(lc));
265+
260266
return result;
267+
}
261268
default:
262269
break;
263270
}

0 commit comments

Comments
 (0)