5
5
#include "nodes/primnodes.h"
6
6
#include "optimizer/paths.h"
7
7
#include "optimizer/pathnode.h"
8
+ #include "optimizer/planner.h"
8
9
#include "utils/hsearch.h"
9
10
#include "utils/tqual.h"
10
11
#include "utils/rel.h"
@@ -72,6 +73,8 @@ void _PG_init(void);
72
73
void _PG_fini (void );
73
74
static void my_shmem_startup (void );
74
75
static void my_hook (PlannerInfo * root ,RelOptInfo * rel ,Index rti ,RangeTblEntry * rte );
76
+ static PlannedStmt * my_planner_hook (Query * parse ,int cursorOptions ,ParamListInfo boundParams );
77
+
75
78
static void append_child_relation (PlannerInfo * root ,RelOptInfo * rel ,Index rti ,RangeTblEntry * rte ,int childOID );
76
79
static void init (void );
77
80
static void create_part_relations_hashtable (void );
@@ -85,6 +88,7 @@ static List *handle_opexpr(const OpExpr *expr, const PartRelationInfo *prel);
85
88
static List * handle_boolexpr (const BoolExpr * expr ,const PartRelationInfo * prel );
86
89
static List * handle_arrexpr (const ScalarArrayOpExpr * expr ,const PartRelationInfo * prel );
87
90
91
+ static void set_plain_rel_pathlist (PlannerInfo * root ,RelOptInfo * rel ,RangeTblEntry * rte );
88
92
static void set_append_rel_pathlist (PlannerInfo * root ,RelOptInfo * rel ,Index rti ,RangeTblEntry * rte );
89
93
static List * accumulate_append_subpath (List * subpaths ,Path * path );
90
94
@@ -102,6 +106,10 @@ _PG_init(void)
102
106
set_rel_pathlist_hook = my_hook ;
103
107
shmem_startup_hook_original = shmem_startup_hook ;
104
108
shmem_startup_hook = my_shmem_startup ;
109
+
110
+ planner_hook = my_planner_hook ;
111
+ /* TEMP */
112
+ // get_relation_info_hook = my_get_relation_info;
105
113
}
106
114
107
115
void
@@ -113,6 +121,38 @@ _PG_fini(void)
113
121
hash_destroy (hash_restrictions );
114
122
}
115
123
124
+ PlannedStmt *
125
+ my_planner_hook (Query * parse ,int cursorOptions ,ParamListInfo boundParams )
126
+ {
127
+ PlannedStmt * result ;
128
+ RangeTblEntry * rte ;
129
+ ListCell * lc ;
130
+ PartRelationInfo * prel ;
131
+
132
+ if (initialization_needed )
133
+ init ();
134
+
135
+ /* Disable inheritance for relations covered by pathman (only for SELECT for now) */
136
+ if (parse -> commandType == CMD_SELECT )
137
+ {
138
+ foreach (lc ,parse -> rtable )
139
+ {
140
+ rte = (RangeTblEntry * )lfirst (lc );
141
+ if (rte -> inh )
142
+ {
143
+ /* look up this relation in pathman relations */
144
+ prel = (PartRelationInfo * )
145
+ hash_search (relations , (const void * )& rte -> relid ,HASH_FIND ,0 );
146
+ if (prel != NULL )
147
+ rte -> inh = false;
148
+ }
149
+ }
150
+ }
151
+
152
+ result = standard_planner (parse ,cursorOptions ,boundParams );
153
+ return result ;
154
+ }
155
+
116
156
/*
117
157
* Initialize hashtables
118
158
*/
@@ -141,28 +181,31 @@ my_shmem_startup(void)
141
181
void
142
182
my_hook (PlannerInfo * root ,RelOptInfo * rel ,Index rti ,RangeTblEntry * rte )
143
183
{
144
- PartRelationInfo * partrel = NULL ;
184
+ PartRelationInfo * prel = NULL ;
145
185
146
- if (initialization_needed )
147
- init ();
186
+ /* This works on for SELECT queries */
187
+ if (root -> parse -> commandType != CMD_SELECT )
188
+ return ;
148
189
149
190
/* Lookup partitioning information for parent relation */
150
- partrel = (PartRelationInfo * )
191
+ prel = (PartRelationInfo * )
151
192
hash_search (relations , (const void * )& rte -> relid ,HASH_FIND ,0 );
152
193
153
- if (partrel != NULL )
194
+ if (prel != NULL )
154
195
{
155
196
List * children = NIL ;
156
197
ListCell * lc ;
157
198
int childOID = -1 ;
158
199
int i ;
159
200
201
+ rte -> inh = true;
202
+
160
203
/* Run over restrictions and collect children partitions */
161
204
ereport (LOG , (errmsg ("Checking restrictions" )));
162
205
foreach (lc ,rel -> baserestrictinfo )
163
206
{
164
207
RestrictInfo * rinfo = (RestrictInfo * )lfirst (lc );
165
- List * ret = walk_expr_tree (rinfo -> clause ,partrel );
208
+ List * ret = walk_expr_tree (rinfo -> clause ,prel );
166
209
children = list_concat_unique_int (children ,ret );
167
210
list_free (ret );
168
211
}
@@ -172,8 +215,8 @@ my_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
172
215
ereport (LOG , (errmsg ("Restrictions empty. Copy children from partrel" )));
173
216
// children = get_children_oids(partrel);
174
217
// children = list_copy(partrel->children);
175
- for (i = 0 ;i < partrel -> children_count ;i ++ )
176
- children = lappend_int (children ,partrel -> children [i ]);
218
+ for (i = 0 ;i < prel -> children_count ;i ++ )
219
+ children = lappend_int (children ,prel -> children [i ]);
177
220
}
178
221
179
222
if (length (children )> 0 )
@@ -210,6 +253,18 @@ my_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
210
253
}
211
254
212
255
ereport (LOG , (errmsg ("Appending children" )));
256
+ // Добавляем самого себя
257
+ // append_child_relation(root, rel, rti, rte, partrel->oid);
258
+ // {
259
+ // AppendRelInfo *appinfo;
260
+ // appinfo = makeNode(AppendRelInfo);
261
+ // appinfo->parent_relid = rti;
262
+ // appinfo->child_relid = rti;
263
+ // appinfo->parent_reloid = rte->relid;
264
+ // root->append_rel_list = lappend(root->append_rel_list, appinfo);
265
+ // }
266
+ // root->hasInheritedTarget = true;
267
+
213
268
foreach (lc ,children )
214
269
{
215
270
childOID = (Oid )lfirst_int (lc );
@@ -219,7 +274,38 @@ my_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
219
274
220
275
/* TODO: clear old path list */
221
276
rel -> pathlist = NIL ;
277
+ // if (root->parse->commandType == CMD_SELECT)
222
278
set_append_rel_pathlist (root ,rel ,rti ,rte );
279
+ // else
280
+ // {
281
+ // set_plain_rel_pathlist(root, rel, rte);
282
+ // /* Set plin pathlist for each child relation */
283
+ // intparentRTindex = rti;
284
+ // ListCell *l;
285
+ // foreach(l, root->append_rel_list)
286
+ // {
287
+ // AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
288
+ // intchildRTindex;
289
+ // RangeTblEntry *childRTE;
290
+ // RelOptInfo *childrel;
291
+
292
+ // /* append_rel_list contains all append rels; ignore others */
293
+ // if (appinfo->parent_relid != parentRTindex || appinfo->parent_relid == rti)
294
+ // continue;
295
+
296
+ // /* Re-locate the child RTE and RelOptInfo */
297
+ // childRTindex = appinfo->child_relid;
298
+ // // childRTE = root->simple_rte_array[childRTindex];
299
+ // // childrel = root->simple_rel_array[childRTindex];
300
+ // root->simple_rel_array[childRTindex] = NULL;
301
+
302
+ // /*
303
+ // * Compute the child's access paths.
304
+ // */
305
+ // // set_plain_rel_pathlist(root, childrel, childRTE);
306
+ // // set_cheapest(childrel);
307
+ // }
308
+ // }
223
309
}
224
310
225
311
/* Invoke original hook if needed */