forked from kaigai/pg_strom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrafter.c
More file actions
157 lines (139 loc) · 3.63 KB
/
grafter.c
File metadata and controls
157 lines (139 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* grafter.c
*
* Routines to modify plan tree once constructed.
* ----
* Copyright 2011-2014 (C) KaiGai Kohei <kaigai@kaigai.gr.jp>
* Copyright 2014 (C) The PG-Strom Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "postgres.h"
#include "optimizer/planner.h"
#include "pg_strom.h"
static planner_hook_type planner_hook_next;
static Plan *
grafter_try_replace_recurse(PlannedStmt *pstmt, Plan *plan)
{
Plan *newnode = plan;
Plan *temp;
List *newlist = NIL;
ListCell *lc;
if (!plan)
return NULL;
switch (nodeTag(plan))
{
case T_Agg:
/*
* Try to inject GpuPreAgg plan if cost of the aggregate plan
* is enough expensive to justify preprocess by GPU.
*/
pgstrom_try_insert_gpupreagg(pstmt, (Agg *) plan);
break;
case T_ModifyTable:
{
ModifyTable *mtplan = (ModifyTable *) newnode;
foreach (lc, mtplan->plans)
{
temp = grafter_try_replace_recurse(pstmt, lfirst(lc));
newlist = lappend(newlist, temp);
}
mtplan->plans = newlist;
}
break;
case T_Append:
{
Append *aplan = (Append *) newnode;
foreach (lc, aplan->appendplans)
{
temp = grafter_try_replace_recurse(pstmt, lfirst(lc));
newlist = lappend(newlist, temp);
}
aplan->appendplans = newlist;
}
break;
case T_MergeAppend:
{
MergeAppend *maplan = (MergeAppend *) newnode;
foreach (lc, maplan->mergeplans)
{
temp = grafter_try_replace_recurse(pstmt, lfirst(lc));
newlist = lappend(newlist, temp);
}
maplan->mergeplans = newlist;
}
break;
case T_BitmapAnd:
{
BitmapAnd *baplan = (BitmapAnd *) newnode;
foreach (lc, baplan->bitmapplans)
{
temp = grafter_try_replace_recurse(pstmt, lfirst(lc));
newlist = lappend(newlist, temp);
}
baplan->bitmapplans = newlist;
}
break;
case T_BitmapOr:
{
BitmapOr *boplan = (BitmapOr *) newnode;
foreach (lc, boplan->bitmapplans)
{
temp = grafter_try_replace_recurse(pstmt, lfirst(lc));
newlist = lappend(newlist, temp);
}
boplan->bitmapplans = newlist;
}
break;
default:
/* nothing to do, keep existgin one */
break;
}
/* also walk down left and right child plan sub-tree, if any */
newnode->lefttree
= grafter_try_replace_recurse(pstmt, newnode->lefttree);
newnode->righttree
= grafter_try_replace_recurse(pstmt, newnode->righttree);
return newnode;
}
static PlannedStmt *
pgstrom_grafter_entrypoint(Query *parse,
int cursorOptions,
ParamListInfo boundParams)
{
PlannedStmt *result;
if (planner_hook_next)
result = planner_hook_next(parse, cursorOptions, boundParams);
else
result = standard_planner(parse, cursorOptions, boundParams);
if (pgstrom_enabled())
{
List *sub_plans = NIL;
ListCell *cell;
result->planTree = grafter_try_replace_recurse(result,
result->planTree);
foreach (cell, result->subplans)
{
Plan *old_plan = lfirst(cell);
Plan *new_plan;
new_plan = grafter_try_replace_recurse(result, old_plan);
sub_plans = lappend(sub_plans, new_plan);
}
result->subplans = sub_plans;
}
return result;
}
void
pgstrom_init_grafter(void)
{
/* hook registration */
planner_hook_next = planner_hook;
planner_hook = pgstrom_grafter_entrypoint;
}