2121#include "storage/ipc.h"
2222#include "catalog/pg_operator.h"
2323#include "catalog/pg_type.h"
24+ #include "foreign/fdwapi.h"
2425
2526PG_MODULE_MAGIC ;
2627
@@ -118,6 +119,7 @@ _PG_fini(void)
118119shmem_startup_hook = shmem_startup_hook_original ;
119120}
120121
122+ /* TODO: rename and write a descritption */
121123PlannedStmt *
122124my_planner_hook (Query * parse ,int cursorOptions ,ParamListInfo boundParams )
123125{
@@ -129,6 +131,9 @@ my_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams)
129131inheritance_disabled = false;
130132disable_inheritance (parse );
131133result = standard_planner (parse ,cursorOptions ,boundParams );
134+
135+ /* TODO: invoke original hook */
136+
132137return result ;
133138}
134139
@@ -318,18 +323,26 @@ append_child_relation(PlannerInfo *root, RelOptInfo *rel, Index rti,
318323AppendRelInfo * appinfo ;
319324Node * node ;
320325ListCell * lc ,* lc2 ;
326+ Relation newrelation ;
321327
322- /* Create RangeTblEntry for child relation */
328+ newrelation = heap_open (childOid ,NoLock );
329+
330+ /*
331+ * Create RangeTblEntry for child relation.
332+ * This code partially based on expand_inherited_rtentry() function.
333+ */
323334childrte = copyObject (rte );
324335childrte -> relid = childOid ;
336+ childrte -> relkind = newrelation -> rd_rel -> relkind ;
325337childrte -> inh = false;
326338childrte -> requiredPerms = 0 ;
327339root -> parse -> rtable = lappend (root -> parse -> rtable ,childrte );
328340childRTindex = list_length (root -> parse -> rtable );
329341root -> simple_rte_array [childRTindex ]= childrte ;
330342
331343/* Create RelOptInfo */
332- childrel = build_simple_rel (root ,childRTindex ,RELOPT_BASEREL );
344+ childrel = build_simple_rel (root ,childRTindex ,RELOPT_OTHER_MEMBER_REL );
345+ // childrel = build_simple_rel(root, childRTindex, RELOPT_BASEREL);
333346
334347/* copy targetlist */
335348childrel -> reltargetlist = NIL ;
@@ -380,8 +393,10 @@ append_child_relation(PlannerInfo *root, RelOptInfo *rel, Index rti,
380393root -> append_rel_list = lappend (root -> append_rel_list ,appinfo );
381394root -> total_table_pages += (double )childrel -> pages ;
382395
383- ereport (LOG ,
384- (errmsg ("Relation %u appended" ,childOid )));
396+ heap_close (newrelation ,NoLock );
397+
398+ // ereport(LOG,
399+ // (errmsg("Relation %u appended", childOid)));
385400}
386401
387402/* Convert wrapper into expression for given index */
@@ -392,7 +407,7 @@ wrapper_make_expression(WrapperNode *wrap, int index, bool *alwaysTrue)
392407
393408* alwaysTrue = false;
394409/*
395- * TODO: use faster algorithm using knowledgethan we enumerate indexes
410+ * TODO: use faster algorithm using knowledgethat we enumerate indexes
396411 * sequntially.
397412 */
398413found = irange_list_find (wrap -> rangeset ,index ,& lossy );
@@ -988,6 +1003,34 @@ set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
9881003create_tidscan_paths (root ,rel );
9891004}
9901005
1006+ /*
1007+ * set_foreign_size
1008+ *Set size estimates for a foreign table RTE
1009+ */
1010+ static void
1011+ set_foreign_size (PlannerInfo * root ,RelOptInfo * rel ,RangeTblEntry * rte )
1012+ {
1013+ /* Mark rel with estimated output rows, width, etc */
1014+ set_foreign_size_estimates (root ,rel );
1015+
1016+ /* Let FDW adjust the size estimates, if it can */
1017+ rel -> fdwroutine -> GetForeignRelSize (root ,rel ,rte -> relid );
1018+
1019+ /* ... but do not let it set the rows estimate to zero */
1020+ rel -> rows = clamp_row_est (rel -> rows );
1021+ }
1022+
1023+ /*
1024+ * set_foreign_pathlist
1025+ *Build access paths for a foreign table RTE
1026+ */
1027+ static void
1028+ set_foreign_pathlist (PlannerInfo * root ,RelOptInfo * rel ,RangeTblEntry * rte )
1029+ {
1030+ /* Call the FDW's GetForeignPaths function to generate path(s) */
1031+ rel -> fdwroutine -> GetForeignPaths (root ,rel ,rte -> relid );
1032+ }
1033+
9911034/*
9921035 * set_append_rel_pathlist
9931036 * Build access paths for an "append relation"
@@ -1027,8 +1070,15 @@ set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
10271070/*
10281071 * Compute the child's access paths.
10291072 */
1030- // set_rel_pathlist(root, childrel, childRTindex, childRTE);
1031- set_plain_rel_pathlist (root ,childrel ,childRTE );
1073+ if (childRTE -> relkind == RELKIND_FOREIGN_TABLE )
1074+ {
1075+ set_foreign_size (root ,childrel ,childRTE );
1076+ set_foreign_pathlist (root ,childrel ,childRTE );
1077+ }
1078+ else
1079+ {
1080+ set_plain_rel_pathlist (root ,childrel ,childRTE );
1081+ }
10321082set_cheapest (childrel );
10331083
10341084/*