@@ -85,6 +85,25 @@ static CustomExecMethodscrossmatch_exec_methods;
8585)
8686
8787
88+ static inline int64
89+ get_index_size (Oid idx )
90+ {
91+ Datum size = DirectFunctionCall2 (pg_relation_size ,
92+ ObjectIdGetDatum (idx ),
93+ PointerGetDatum (cstring_to_text ("main" )));
94+ return DatumGetInt64 (size );
95+ }
96+
97+ static inline Oid
98+ get_dist_func ()
99+ {
100+ text * dist_func_name =
101+ cstring_to_text ("public.dist(public.spoint, public.spoint)" );
102+
103+ return DatumGetObjectId (DirectFunctionCall1 (to_regprocedure ,
104+ PointerGetDatum (dist_func_name )));
105+ }
106+
88107static float8
89108cstring_to_float8 (char * str )
90109{
@@ -166,10 +185,7 @@ pick_suitable_index(Oid relation, AttrNumber column)
166185
167186if (pg_ind -> indkey .values [i ]== column )
168187{
169- cur_index_size = DatumGetInt64 (
170- DirectFunctionCall2 (pg_relation_size ,
171- ObjectIdGetDatum (relation ),
172- PointerGetDatum (cstring_to_text ("main" ))));
188+ cur_index_size = get_index_size (pg_ind -> indexrelid );
173189
174190if (found_index == InvalidOid || cur_index_size < found_index_size )
175191{
@@ -263,6 +279,9 @@ create_crossmatch_path(PlannerInfo *root,
263279Oid outer_idx ;
264280Oid inner_idx ;
265281
282+ if (outer_rel == inner_rel )
283+ return ;
284+
266285if ((outer_idx = pick_suitable_index (outer_rel ,outer_spoint ))== InvalidOid ||
267286(inner_idx = pick_suitable_index (inner_rel ,inner_spoint ))== InvalidOid )
268287{
@@ -348,10 +367,17 @@ join_pathlist_hook(PlannerInfo *root,
348367JoinPathExtraData * extra )
349368{
350369ListCell * restr ;
351- text * dist_func_name = cstring_to_text ("dist(spoint,spoint)" );
352370Oid dist_func ;
353371Relids required_relids = NULL ;
354372
373+ if (set_join_pathlist_next )
374+ set_join_pathlist_next (root ,joinrel ,outerrel ,
375+ innerrel ,jointype ,extra );
376+
377+ /* Get oid of the dist(spoint, spoint) function */
378+ if ((dist_func = get_dist_func ())== InvalidOid )
379+ return ;
380+
355381if (outerrel -> reloptkind == RELOPT_BASEREL &&
356382innerrel -> reloptkind == RELOPT_BASEREL )
357383{
@@ -360,20 +386,6 @@ join_pathlist_hook(PlannerInfo *root,
360386}
361387else return ;/* one of relations can't have index */
362388
363- dist_func = DatumGetObjectId (DirectFunctionCall1 (to_regprocedure ,
364- PointerGetDatum (dist_func_name )));
365-
366- if (dist_func == InvalidOid )
367- return ;
368-
369- if (set_join_pathlist_next )
370- set_join_pathlist_next (root ,
371- joinrel ,
372- outerrel ,
373- innerrel ,
374- jointype ,
375- extra );
376-
377389foreach (restr ,extra -> restrictlist )
378390{
379391RestrictInfo * restrInfo = (RestrictInfo * )lfirst (restr );
@@ -433,7 +445,8 @@ create_crossmatch_plan(PlannerInfo *root,
433445cscan -> scan .plan .targetlist = tlist ;
434446cscan -> scan .plan .qual = joinclauses ;
435447cscan -> scan .scanrelid = 0 ;
436- cscan -> custom_scan_tlist = tlist ;/* output of this node */
448+ cscan -> custom_scan_tlist = tlist ;/* tlist of the 'virtual' join rel
449+ we'll have to build and scan */
437450
438451cscan -> flags = best_path -> flags ;
439452cscan -> methods = & crossmatch_plan_methods ;
@@ -465,9 +478,7 @@ crossmatch_create_scan_state(CustomScan *node)
465478scan_state -> css .flags = node -> flags ;
466479scan_state -> css .methods = & crossmatch_exec_methods ;
467480
468- /* TODO: check if this assignment is redundant */
469- scan_state -> css .ss .ps .ps_TupFromTlist = false;
470-
481+ /* Save scan tlist for join relation */
471482scan_state -> scan_tlist = node -> custom_scan_tlist ;
472483
473484scan_state -> outer_idx = linitial_oid (linitial (node -> custom_private ));
@@ -519,6 +530,7 @@ crossmatch_exec(CustomScanState *node)
519530
520531for (;;)
521532{
533+ /* Fetch next tid pair */
522534if (!node -> ss .ps .ps_TupFromTlist )
523535{
524536Datum * values = scan_state -> values ;
@@ -541,9 +553,9 @@ crossmatch_exec(CustomScanState *node)
541553if (scan_state -> scan_tlist != NIL )
542554{
543555TupleDesc tupdesc = node -> ss .ss_ScanTupleSlot -> tts_tupleDescriptor ;
544- int col_index = 0 ;
545556bool htup_outer_ready = false;
546557bool htup_inner_ready = false;
558+ int col_index = 0 ;
547559ListCell * l ;
548560
549561htup_outer .t_self = p_tids [0 ];
@@ -645,7 +657,7 @@ crossmatch_end(CustomScanState *node)
645657static void
646658crossmatch_rescan (CustomScanState * node )
647659{
648-
660+ /* NOTE: nothing to do here? */
649661}
650662
651663static void