21
21
#include "storage/ipc.h"
22
22
#include "catalog/pg_operator.h"
23
23
#include "catalog/pg_type.h"
24
+ #include "foreign/fdwapi.h"
24
25
25
26
PG_MODULE_MAGIC ;
26
27
@@ -118,6 +119,7 @@ _PG_fini(void)
118
119
shmem_startup_hook = shmem_startup_hook_original ;
119
120
}
120
121
122
+ /* TODO: rename and write a descritption */
121
123
PlannedStmt *
122
124
my_planner_hook (Query * parse , int cursorOptions , ParamListInfo boundParams )
123
125
{
@@ -129,6 +131,9 @@ my_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams)
129
131
inheritance_disabled = false;
130
132
disable_inheritance (parse );
131
133
result = standard_planner (parse , cursorOptions , boundParams );
134
+
135
+ /* TODO: invoke original hook */
136
+
132
137
return result ;
133
138
}
134
139
@@ -318,18 +323,26 @@ append_child_relation(PlannerInfo *root, RelOptInfo *rel, Index rti,
318
323
AppendRelInfo * appinfo ;
319
324
Node * node ;
320
325
ListCell * lc , * lc2 ;
326
+ Relation newrelation ;
321
327
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
+ */
323
334
childrte = copyObject (rte );
324
335
childrte -> relid = childOid ;
336
+ childrte -> relkind = newrelation -> rd_rel -> relkind ;
325
337
childrte -> inh = false;
326
338
childrte -> requiredPerms = 0 ;
327
339
root -> parse -> rtable = lappend (root -> parse -> rtable , childrte );
328
340
childRTindex = list_length (root -> parse -> rtable );
329
341
root -> simple_rte_array [childRTindex ] = childrte ;
330
342
331
343
/* 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);
333
346
334
347
/* copy targetlist */
335
348
childrel -> reltargetlist = NIL ;
@@ -380,8 +393,10 @@ append_child_relation(PlannerInfo *root, RelOptInfo *rel, Index rti,
380
393
root -> append_rel_list = lappend (root -> append_rel_list , appinfo );
381
394
root -> total_table_pages += (double ) childrel -> pages ;
382
395
383
- ereport (LOG ,
384
- (errmsg ("Relation %u appended" , childOid )));
396
+ heap_close (newrelation , NoLock );
397
+
398
+ // ereport(LOG,
399
+ // (errmsg("Relation %u appended", childOid)));
385
400
}
386
401
387
402
/* Convert wrapper into expression for given index */
@@ -392,7 +407,7 @@ wrapper_make_expression(WrapperNode *wrap, int index, bool *alwaysTrue)
392
407
393
408
* alwaysTrue = false;
394
409
/*
395
- * TODO: use faster algorithm using knowledge than we enumerate indexes
410
+ * TODO: use faster algorithm using knowledge that we enumerate indexes
396
411
* sequntially.
397
412
*/
398
413
found = irange_list_find (wrap -> rangeset , index , & lossy );
@@ -988,6 +1003,34 @@ set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
988
1003
create_tidscan_paths (root , rel );
989
1004
}
990
1005
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
+
991
1034
/*
992
1035
* set_append_rel_pathlist
993
1036
* Build access paths for an "append relation"
@@ -1027,8 +1070,15 @@ set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
1027
1070
/*
1028
1071
* Compute the child's access paths.
1029
1072
*/
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
+ }
1032
1082
set_cheapest (childrel );
1033
1083
1034
1084
/*
0 commit comments