@@ -33,6 +33,8 @@ static void my_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry
33
33
static PlannedStmt * my_planner_hook (Query * parse ,int cursorOptions ,ParamListInfo boundParams );
34
34
35
35
static void append_child_relation (PlannerInfo * root ,RelOptInfo * rel ,Index rti ,RangeTblEntry * rte ,int childOID );
36
+ static void set_pathkeys (PlannerInfo * root ,RelOptInfo * childrel ,Path * path );
37
+ static void disable_inheritance (Query * parse );
36
38
37
39
static List * walk_expr_tree (Expr * expr ,const PartRelationInfo * prel );
38
40
static int make_hash (const PartRelationInfo * prel ,int value );
@@ -93,35 +95,53 @@ PlannedStmt *
93
95
my_planner_hook (Query * parse ,int cursorOptions ,ParamListInfo boundParams )
94
96
{
95
97
PlannedStmt * result ;
98
+
99
+ if (initialization_needed )
100
+ init ();
101
+
102
+ disable_inheritance (parse );
103
+ result = standard_planner (parse ,cursorOptions ,boundParams );
104
+ return result ;
105
+ }
106
+
107
+ /*
108
+ *
109
+ */
110
+ static void
111
+ disable_inheritance (Query * parse )
112
+ {
96
113
RangeTblEntry * rte ;
97
114
ListCell * lc ;
98
115
PartRelationInfo * prel ;
99
116
100
- if (initialization_needed )
101
- init () ;
117
+ if (parse -> commandType != CMD_SELECT )
118
+ return ;
102
119
103
- /* Disable inheritance for relations covered by pathman (only for SELECT for now) */
104
- if (parse -> commandType == CMD_SELECT )
120
+ foreach (lc ,parse -> rtable )
105
121
{
106
- foreach (lc ,parse -> rtable )
122
+ rte = (RangeTblEntry * )lfirst (lc );
123
+ switch (rte -> rtekind )
107
124
{
108
- rte = (RangeTblEntry * )lfirst (lc );
109
- if (rte -> inh )
110
- {
111
- /* look up this relation in pathman relations */
112
- prel = (PartRelationInfo * )
113
- hash_search (relations , (const void * )& rte -> relid ,HASH_FIND ,0 );
114
- if (prel != NULL )
115
- rte -> inh = false;
116
- }
125
+ case RTE_RELATION :
126
+ if (rte -> inh )
127
+ {
128
+ /* look up this relation in pathman relations */
129
+ prel = (PartRelationInfo * )
130
+ hash_search (relations , (const void * )& rte -> relid ,HASH_FIND ,0 );
131
+ if (prel != NULL )
132
+ rte -> inh = false;
133
+ }
134
+ break ;
135
+ case RTE_SUBQUERY :
136
+ /* recursively disable inheritance for subqueries */
137
+ disable_inheritance (rte -> subquery );
138
+ break ;
139
+ default :
140
+ break ;
117
141
}
118
142
}
119
-
120
- result = standard_planner (parse ,cursorOptions ,boundParams );
121
- return result ;
122
143
}
123
144
124
-
125
145
static void
126
146
my_shmem_startup (void )
127
147
{
@@ -163,7 +183,6 @@ my_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
163
183
children = lappend_int (children ,prel -> children [i ]);
164
184
165
185
/* Run over restrictions and collect children partitions */
166
- ereport (LOG , (errmsg ("Checking restrictions" )));
167
186
foreach (lc ,rel -> baserestrictinfo )
168
187
{
169
188
RestrictInfo * rinfo = (RestrictInfo * )lfirst (lc );
@@ -176,14 +195,7 @@ my_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
176
195
}
177
196
}
178
197
179
- // if (children == NIL)
180
- // {
181
- // ereport(LOG, (errmsg("Restrictions empty. Copy children from partrel")));
182
- // // children = get_children_oids(partrel);
183
- // // children = list_copy(partrel->children);
184
-
185
- // }
186
-
198
+ /* expand simple_rte_array and simple_rel_array */
187
199
if (list_length (children )> 0 )
188
200
{
189
201
RelOptInfo * * new_rel_array ;
@@ -212,65 +224,16 @@ my_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
212
224
root -> simple_rte_array = new_rte_array ;
213
225
/* TODO: free old arrays */
214
226
}
215
- else
216
- {
217
- ereport (LOG , (errmsg ("Children count is 0" )));
218
- }
219
-
220
- ereport (LOG , (errmsg ("Appending children" )));
221
- // Добавляем самого себя
222
- // append_child_relation(root, rel, rti, rte, partrel->oid);
223
- // {
224
- // AppendRelInfo *appinfo;
225
- // appinfo = makeNode(AppendRelInfo);
226
- // appinfo->parent_relid = rti;
227
- // appinfo->child_relid = rti;
228
- // appinfo->parent_reloid = rte->relid;
229
- // root->append_rel_list = lappend(root->append_rel_list, appinfo);
230
- // }
231
- // root->hasInheritedTarget = true;
232
227
233
228
foreach (lc ,children )
234
229
{
235
230
childOID = (Oid )lfirst_int (lc );
236
231
append_child_relation (root ,rel ,rti ,rte ,childOID );
237
- // root->simple_rel_array_size += 1;
238
232
}
239
233
240
234
/* TODO: clear old path list */
241
235
rel -> pathlist = NIL ;
242
- // if (root->parse->commandType == CMD_SELECT)
243
236
set_append_rel_pathlist (root ,rel ,rti ,rte );
244
- // else
245
- // {
246
- // set_plain_rel_pathlist(root, rel, rte);
247
- // /* Set plin pathlist for each child relation */
248
- // intparentRTindex = rti;
249
- // ListCell *l;
250
- // foreach(l, root->append_rel_list)
251
- // {
252
- // AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
253
- // intchildRTindex;
254
- // RangeTblEntry *childRTE;
255
- // RelOptInfo *childrel;
256
-
257
- // /* append_rel_list contains all append rels; ignore others */
258
- // if (appinfo->parent_relid != parentRTindex || appinfo->parent_relid == rti)
259
- // continue;
260
-
261
- // /* Re-locate the child RTE and RelOptInfo */
262
- // childRTindex = appinfo->child_relid;
263
- // // childRTE = root->simple_rte_array[childRTindex];
264
- // // childrel = root->simple_rel_array[childRTindex];
265
- // root->simple_rel_array[childRTindex] = NULL;
266
-
267
- // /*
268
- // * Compute the child's access paths.
269
- // */
270
- // // set_plain_rel_pathlist(root, childrel, childRTE);
271
- // // set_cheapest(childrel);
272
- // }
273
- // }
274
237
}
275
238
276
239
/* Invoke original hook if needed */
@@ -700,6 +663,7 @@ static void
700
663
set_plain_rel_pathlist (PlannerInfo * root ,RelOptInfo * rel ,RangeTblEntry * rte )
701
664
{
702
665
Relids required_outer ;
666
+ Path * path ;
703
667
704
668
/*
705
669
* We don't support pushing join clauses into the quals of a seqscan, but
@@ -709,7 +673,9 @@ set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
709
673
required_outer = rel -> lateral_relids ;
710
674
711
675
/* Consider sequential scan */
712
- add_path (rel ,create_seqscan_path (root ,rel ,required_outer ,0 ));
676
+ path = create_seqscan_path (root ,rel ,required_outer ,0 );
677
+ add_path (rel ,path );
678
+ set_pathkeys (root ,rel ,path );
713
679
714
680
/* Consider index scans */
715
681
create_index_paths (root ,rel );
@@ -794,6 +760,19 @@ set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
794
760
795
761
}
796
762
763
+ void
764
+ set_pathkeys (PlannerInfo * root ,RelOptInfo * childrel ,Path * path )
765
+ {
766
+ ListCell * lc ;
767
+ PathKey * pathkey ;
768
+
769
+ foreach (lc ,root -> sort_pathkeys )
770
+ {
771
+ pathkey = (PathKey * )lfirst (lc );
772
+ path -> pathkeys = lappend (path -> pathkeys ,pathkey );
773
+ }
774
+ }
775
+
797
776
static List *
798
777
accumulate_append_subpath (List * subpaths ,Path * path )
799
778
{