55#include "nodes/primnodes.h"
66#include "optimizer/paths.h"
77#include "optimizer/pathnode.h"
8+ #include "optimizer/planner.h"
89#include "utils/hsearch.h"
910#include "utils/tqual.h"
1011#include "utils/rel.h"
@@ -72,6 +73,8 @@ void _PG_init(void);
7273void _PG_fini (void );
7374static void my_shmem_startup (void );
7475static void my_hook (PlannerInfo * root ,RelOptInfo * rel ,Index rti ,RangeTblEntry * rte );
76+ static PlannedStmt * my_planner_hook (Query * parse ,int cursorOptions ,ParamListInfo boundParams );
77+
7578static void append_child_relation (PlannerInfo * root ,RelOptInfo * rel ,Index rti ,RangeTblEntry * rte ,int childOID );
7679static void init (void );
7780static void create_part_relations_hashtable (void );
@@ -85,6 +88,7 @@ static List *handle_opexpr(const OpExpr *expr, const PartRelationInfo *prel);
8588static List * handle_boolexpr (const BoolExpr * expr ,const PartRelationInfo * prel );
8689static List * handle_arrexpr (const ScalarArrayOpExpr * expr ,const PartRelationInfo * prel );
8790
91+ static void set_plain_rel_pathlist (PlannerInfo * root ,RelOptInfo * rel ,RangeTblEntry * rte );
8892static void set_append_rel_pathlist (PlannerInfo * root ,RelOptInfo * rel ,Index rti ,RangeTblEntry * rte );
8993static List * accumulate_append_subpath (List * subpaths ,Path * path );
9094
@@ -102,6 +106,10 @@ _PG_init(void)
102106set_rel_pathlist_hook = my_hook ;
103107shmem_startup_hook_original = shmem_startup_hook ;
104108shmem_startup_hook = my_shmem_startup ;
109+
110+ planner_hook = my_planner_hook ;
111+ /* TEMP */
112+ // get_relation_info_hook = my_get_relation_info;
105113}
106114
107115void
@@ -113,6 +121,38 @@ _PG_fini(void)
113121hash_destroy (hash_restrictions );
114122}
115123
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+
116156/*
117157 * Initialize hashtables
118158 */
@@ -141,28 +181,31 @@ my_shmem_startup(void)
141181void
142182my_hook (PlannerInfo * root ,RelOptInfo * rel ,Index rti ,RangeTblEntry * rte )
143183{
144- PartRelationInfo * partrel = NULL ;
184+ PartRelationInfo * prel = NULL ;
145185
146- if (initialization_needed )
147- init ();
186+ /* This works on for SELECT queries */
187+ if (root -> parse -> commandType != CMD_SELECT )
188+ return ;
148189
149190/* Lookup partitioning information for parent relation */
150- partrel = (PartRelationInfo * )
191+ prel = (PartRelationInfo * )
151192hash_search (relations , (const void * )& rte -> relid ,HASH_FIND ,0 );
152193
153- if (partrel != NULL )
194+ if (prel != NULL )
154195{
155196List * children = NIL ;
156197ListCell * lc ;
157198int childOID = -1 ;
158199int i ;
159200
201+ rte -> inh = true;
202+
160203/* Run over restrictions and collect children partitions */
161204ereport (LOG , (errmsg ("Checking restrictions" )));
162205foreach (lc ,rel -> baserestrictinfo )
163206{
164207RestrictInfo * rinfo = (RestrictInfo * )lfirst (lc );
165- List * ret = walk_expr_tree (rinfo -> clause ,partrel );
208+ List * ret = walk_expr_tree (rinfo -> clause ,prel );
166209children = list_concat_unique_int (children ,ret );
167210list_free (ret );
168211}
@@ -172,8 +215,8 @@ my_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
172215ereport (LOG , (errmsg ("Restrictions empty. Copy children from partrel" )));
173216// children = get_children_oids(partrel);
174217// 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 ]);
177220}
178221
179222if (length (children )> 0 )
@@ -210,6 +253,18 @@ my_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
210253}
211254
212255ereport (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+
213268foreach (lc ,children )
214269{
215270childOID = (Oid )lfirst_int (lc );
@@ -219,7 +274,38 @@ my_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
219274
220275/* TODO: clear old path list */
221276rel -> pathlist = NIL ;
277+ // if (root->parse->commandType == CMD_SELECT)
222278set_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+ // }
223309}
224310
225311/* Invoke original hook if needed */