@@ -85,6 +85,25 @@ static CustomExecMethods crossmatch_exec_methods;
85
85
)
86
86
87
87
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
+
88
107
static float8
89
108
cstring_to_float8 (char * str )
90
109
{
@@ -166,10 +185,7 @@ pick_suitable_index(Oid relation, AttrNumber column)
166
185
167
186
if (pg_ind -> indkey .values [i ] == column )
168
187
{
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 );
173
189
174
190
if (found_index == InvalidOid || cur_index_size < found_index_size )
175
191
{
@@ -263,6 +279,9 @@ create_crossmatch_path(PlannerInfo *root,
263
279
Oid outer_idx ;
264
280
Oid inner_idx ;
265
281
282
+ if (outer_rel == inner_rel )
283
+ return ;
284
+
266
285
if ((outer_idx = pick_suitable_index (outer_rel , outer_spoint )) == InvalidOid ||
267
286
(inner_idx = pick_suitable_index (inner_rel , inner_spoint )) == InvalidOid )
268
287
{
@@ -348,10 +367,17 @@ join_pathlist_hook(PlannerInfo *root,
348
367
JoinPathExtraData * extra )
349
368
{
350
369
ListCell * restr ;
351
- text * dist_func_name = cstring_to_text ("dist(spoint,spoint)" );
352
370
Oid dist_func ;
353
371
Relids required_relids = NULL ;
354
372
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
+
355
381
if (outerrel -> reloptkind == RELOPT_BASEREL &&
356
382
innerrel -> reloptkind == RELOPT_BASEREL )
357
383
{
@@ -360,20 +386,6 @@ join_pathlist_hook(PlannerInfo *root,
360
386
}
361
387
else return ; /* one of relations can't have index */
362
388
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
-
377
389
foreach (restr , extra -> restrictlist )
378
390
{
379
391
RestrictInfo * restrInfo = (RestrictInfo * ) lfirst (restr );
@@ -433,7 +445,8 @@ create_crossmatch_plan(PlannerInfo *root,
433
445
cscan -> scan .plan .targetlist = tlist ;
434
446
cscan -> scan .plan .qual = joinclauses ;
435
447
cscan -> 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 */
437
450
438
451
cscan -> flags = best_path -> flags ;
439
452
cscan -> methods = & crossmatch_plan_methods ;
@@ -465,9 +478,7 @@ crossmatch_create_scan_state(CustomScan *node)
465
478
scan_state -> css .flags = node -> flags ;
466
479
scan_state -> css .methods = & crossmatch_exec_methods ;
467
480
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 */
471
482
scan_state -> scan_tlist = node -> custom_scan_tlist ;
472
483
473
484
scan_state -> outer_idx = linitial_oid (linitial (node -> custom_private ));
@@ -519,6 +530,7 @@ crossmatch_exec(CustomScanState *node)
519
530
520
531
for (;;)
521
532
{
533
+ /* Fetch next tid pair */
522
534
if (!node -> ss .ps .ps_TupFromTlist )
523
535
{
524
536
Datum * values = scan_state -> values ;
@@ -541,9 +553,9 @@ crossmatch_exec(CustomScanState *node)
541
553
if (scan_state -> scan_tlist != NIL )
542
554
{
543
555
TupleDesc tupdesc = node -> ss .ss_ScanTupleSlot -> tts_tupleDescriptor ;
544
- int col_index = 0 ;
545
556
bool htup_outer_ready = false;
546
557
bool htup_inner_ready = false;
558
+ int col_index = 0 ;
547
559
ListCell * l ;
548
560
549
561
htup_outer .t_self = p_tids [0 ];
@@ -645,7 +657,7 @@ crossmatch_end(CustomScanState *node)
645
657
static void
646
658
crossmatch_rescan (CustomScanState * node )
647
659
{
648
-
660
+ /* NOTE: nothing to do here? */
649
661
}
650
662
651
663
static void
0 commit comments